|
@@ -41,7 +41,7 @@ strpcmp(const void *va, const void *vb)
|
|
|
Completion*
|
|
|
complete(char *dir, char *s)
|
|
|
{
|
|
|
- long i, l, n, nmatch, len, nbytes;
|
|
|
+ long i, l, n, nfile, len, nbytes;
|
|
|
int fd, minlen;
|
|
|
Dir *dirp;
|
|
|
char **name, *p;
|
|
@@ -78,49 +78,51 @@ complete(char *dir, char *s)
|
|
|
|
|
|
/* find the matches */
|
|
|
len = strlen(s);
|
|
|
- nmatch = 0;
|
|
|
+ nfile = 0;
|
|
|
minlen = 1000000;
|
|
|
for(i=0; i<n; i++)
|
|
|
if(strncmp(s, dirp[i].name, len) == 0){
|
|
|
- name[nmatch] = dirp[i].name;
|
|
|
- mode[nmatch] = dirp[i].mode;
|
|
|
+ name[nfile] = dirp[i].name;
|
|
|
+ mode[nfile] = dirp[i].mode;
|
|
|
if(minlen > strlen(dirp[i].name))
|
|
|
minlen = strlen(dirp[i].name);
|
|
|
- nmatch++;
|
|
|
+ nfile++;
|
|
|
}
|
|
|
|
|
|
- if(nmatch > 0) {
|
|
|
+ if(nfile > 0) {
|
|
|
/* report interesting results */
|
|
|
/* trim length back to longest common initial string */
|
|
|
- for(i=1; i<nmatch; i++)
|
|
|
+ for(i=1; i<nfile; i++)
|
|
|
minlen = longestprefixlength(name[0], name[i], minlen);
|
|
|
|
|
|
/* build the answer */
|
|
|
- c->complete = (nmatch == 1);
|
|
|
+ c->complete = (nfile == 1);
|
|
|
c->advance = c->complete || (minlen > len);
|
|
|
c->string = (char*)(c+1);
|
|
|
memmove(c->string, name[0]+len, minlen-len);
|
|
|
if(c->complete)
|
|
|
c->string[minlen++ - len] = (mode[0]&DMDIR)? '/' : ' ';
|
|
|
c->string[minlen - len] = '\0';
|
|
|
+ c->nmatch = nfile;
|
|
|
} else {
|
|
|
/* no match, so return all possible strings */
|
|
|
for(i=0; i<n; i++){
|
|
|
name[i] = dirp[i].name;
|
|
|
mode[i] = dirp[i].mode;
|
|
|
}
|
|
|
- nmatch = n;
|
|
|
+ nfile = n;
|
|
|
+ c->nmatch = 0;
|
|
|
}
|
|
|
|
|
|
/* attach list of names */
|
|
|
- nbytes = nmatch * sizeof(char*);
|
|
|
- for(i=0; i<nmatch; i++)
|
|
|
+ nbytes = nfile * sizeof(char*);
|
|
|
+ for(i=0; i<nfile; i++)
|
|
|
nbytes += strlen(name[i]) + 1 + 1;
|
|
|
c->filename = malloc(nbytes);
|
|
|
if(c->filename == nil)
|
|
|
goto Return;
|
|
|
- p = (char*)(c->filename + nmatch);
|
|
|
- for(i=0; i<nmatch; i++){
|
|
|
+ p = (char*)(c->filename + nfile);
|
|
|
+ for(i=0; i<nfile; i++){
|
|
|
c->filename[i] = p;
|
|
|
strcpy(p, name[i]);
|
|
|
p += strlen(p);
|
|
@@ -128,7 +130,7 @@ complete(char *dir, char *s)
|
|
|
*p++ = '/';
|
|
|
*p++ = '\0';
|
|
|
}
|
|
|
- c->nfile = nmatch;
|
|
|
+ c->nfile = nfile;
|
|
|
qsort(c->filename, c->nfile, sizeof(c->filename[0]), strpcmp);
|
|
|
|
|
|
Return:
|