sh: allow glob after directory

This commit is contained in:
K. Lange 2018-10-05 20:29:48 +09:00
parent ebf6e3f746
commit e943b521ee

View File

@ -1044,11 +1044,31 @@ _done:
int has_before = !!strlen(before);
int has_after = !!strlen(after);
if (!has_before || !strchr(before,'/')) {
if (1) {
/* read current directory, add all */
DIR * dirp = opendir(".");
DIR * dirp;
char * prepend = "";
if (has_before) {
char * dir = strrchr(before,'/');
if (!dir) {
dirp = opendir(".");
} else if (dir == before) {
dirp = opendir("/");
prepend = before;
before++;
} else {
*dir = '\0';
dirp = opendir(before);
prepend = before;
before = dir+1;
}
} else {
dirp = opendir(".");
}
int before_i = i;
if (dirp) {
struct dirent * ent = readdir(dirp);
while (ent != NULL) {
if (ent->d_name[0] != '.') {
@ -1059,6 +1079,7 @@ _done:
if (has_before) {
if (strstr(s,before) != s) {
free(s);
goto _nope;
}
t = &s[strlen(before)];
@ -1066,21 +1087,27 @@ _done:
if (has_after) {
if (strlen(t) >= strlen(after)) {
if (!strcmp(after,&t[strlen(t)-strlen(after)])) {
argv[i] = s;
char * out = malloc(strlen(s) + 2 + strlen(prepend));
sprintf(out,"%s%s%s", prepend, !!*prepend ? "/" : "", s);
argv[i] = out;
i++;
argcs[cmdi]++;
}
}
} else {
argv[i] = s;
char * out = malloc(strlen(s) + 2 + strlen(prepend));
sprintf(out,"%s%s%s", prepend, !!*prepend ? "/" : "", s);
argv[i] = out;
i++;
argcs[cmdi]++;
}
free(s);
}
_nope:
ent = readdir(dirp);
}
closedir(dirp);
}
if (before_i == i) {
/* no matches */
@ -1092,12 +1119,6 @@ _nope:
} else {
free(c);
}
} else {
/* directory globs not supported */
glob[0] = '*';
argv[i] = c;
i++;
argcs[cmdi]++;
}
} else {
argv[i] = c;