if (dir[0] == '/' && dir[1] == '\0' && cp[0] == '/' && cp[1] == '/') { cp++; ftpdslashbug = 1; } tcp = strdup(cp);
if (ftpdslashbug && (cp[dirlen] != '/')) ftpdslashbug = 0; tcp = strdup(cp + dirlen + ftpdslashbug);
complete_remote(word, list)	char *word;	int list;{	static StringList *dirlist;	static char	 lastdir[MAXPATHLEN];	static int	 ftpdslashbug;	StringList	*words;	char		 dir[MAXPATHLEN];	char		*file, *cp;	int		 i, offset;	unsigned char	 rv;	char *dummyargv[] = { "complete", dir, NULL };	offset = 0;	if ((file = strrchr(word, '/')) == NULL) {		strcpy(dir, ".");		file = word;	} else {		if (file == word)			strcpy(dir, "/");		else {			offset = file - word;			strncpy(dir, word, offset);			dir[offset] = '\0';			offset++;		}		file++;	}	if (dirchange || strcmp(dir, lastdir) != 0) {	/* dir not cached */		if (dirlist != NULL)			sl_free(dirlist, 1);		dirlist = sl_init();		ftpdslashbug = 0;		mflag = 1;		while ((cp = remglob(dummyargv, 0)) != NULL) {			char *tcp;			if (!mflag)				continue;			if (*cp == '\0') {				mflag = 0;				continue;			}			/*			 * Work around ftpd(1) bug, which puts a // instead			 * of / in front of each filename returned by "NLST /".			 * Without this, remote completes of / look ugly.			 */			if (dir[0] == '/' && dir[1] == '\0' &&			    cp[0] == '/' && cp[1] == '/') {				cp++;				ftpdslashbug = 1;			}			tcp = strdup(cp);			if (tcp == NULL)				errx(1, "Can't allocate memory for remote dir");			sl_add(dirlist, tcp);		}		strcpy(lastdir, dir);		dirchange = 0;	}	words = sl_init();	for (i = 0; i < dirlist->sl_cur; i++) {		cp = dirlist->sl_str[i];		if (strlen(word) > strlen(cp))			continue;		if (strncmp(word, cp, strlen(word)) == 0)			sl_add(words, cp + offset + ftpdslashbug);	}	rv = complete_ambiguous(file, list, words);	sl_free(words, 0);	return rv;}