X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fmisc%2Fwordexp.c;h=d123cf757daad53740a4855800eabb440e731ad4;hb=239c15565fe174a77cdd0a8226863a1da1880857;hp=b0d6846fc69376ed2901eaa583eee25576a9b737;hpb=d251c2645c86e89704545337b955ff8b3e835290;p=musl diff --git a/src/misc/wordexp.c b/src/misc/wordexp.c index b0d6846f..d123cf75 100644 --- a/src/misc/wordexp.c +++ b/src/misc/wordexp.c @@ -5,6 +5,17 @@ #include #include #include +#include +#include +#include +#include +#include "pthread_impl.h" + +static void reap(pid_t pid) +{ + int status; + while (waitpid(pid, &status, 0) < 0 && errno == EINTR); +} static char *getword(FILE *f) { @@ -12,17 +23,20 @@ static char *getword(FILE *f) return getdelim(&s, (size_t [1]){0}, 0, f) < 0 ? 0 : s; } -int wordexp(const char *s, wordexp_t *we, int flags) +static int do_wordexp(const char *s, wordexp_t *we, int flags) { - size_t i, l, len; + size_t i, l; int sq=0, dq=0; size_t np=0; - char *cmd, *w, **tmp; + char *w, **tmp; char *redir = (flags & WRDE_SHOWERR) ? "" : "2>/dev/null"; - int err = 0, status; + int err = 0; FILE *f; size_t wc = 0; char **wv = 0; + int p[2]; + pid_t pid; + sigset_t set; if (flags & WRDE_REUSE) wordfree(we); @@ -57,6 +71,7 @@ int wordexp(const char *s, wordexp_t *we, int flags) if (!(sq|dq|np)) return WRDE_BADCHAR; break; case '$': + if (sq) break; if (s[i+1]=='(' && s[i+2]=='(') { i += 2; np += 2; @@ -75,21 +90,48 @@ int wordexp(const char *s, wordexp_t *we, int flags) i = wc; if (flags & WRDE_DOOFFS) { if (we->we_offs > SIZE_MAX/sizeof(void *)/4) - return WRDE_NOSPACE; + goto nospace; i += we->we_offs; + } else { + we->we_offs = 0; } - len = 50 + strlen(s); - cmd = malloc(len); - if (!cmd) return WRDE_NOSPACE; - snprintf(cmd, len, "printf %%s\\\\0 %s %s", s, redir); -printf("{%s}\n", cmd); - f = popen(cmd, "r"); - free(cmd); - if (!f) return WRDE_NOSPACE; + if (pipe2(p, O_CLOEXEC) < 0) goto nospace; + __block_all_sigs(&set); + pid = fork(); + __restore_sigs(&set); + if (pid < 0) { + close(p[0]); + close(p[1]); + goto nospace; + } + if (!pid) { + if (p[1] == 1) fcntl(1, F_SETFD, 0); + else dup2(p[1], 1); + execl("/bin/sh", "sh", "-c", + "eval \"printf %s\\\\\\\\0 x $1 $2\"", + "sh", s, redir, (char *)0); + _exit(1); + } + close(p[1]); + + f = fdopen(p[0], "r"); + if (!f) { + close(p[0]); + kill(pid, SIGKILL); + reap(pid); + goto nospace; + } l = wv ? i+1 : 0; + free(getword(f)); + if (feof(f)) { + fclose(f); + reap(pid); + return WRDE_SYNTAX; + } + while ((w = getword(f))) { if (i+1 >= l) { l += l/2+10; @@ -102,19 +144,36 @@ printf("{%s}\n", cmd); } if (!feof(f)) err = WRDE_NOSPACE; - status = pclose(f); - if (WEXITSTATUS(status)) { - if (!(flags & WRDE_APPEND)) { - free(wv); - return WRDE_SYNTAX; - } else if (wv==we->we_wordv) { - return WRDE_SYNTAX; - } - } + fclose(f); + reap(pid); + + if (!wv) wv = calloc(i+1, sizeof *wv); we->we_wordv = wv; - we->we_wordc = i - we->we_offs; + we->we_wordc = i; + + if (flags & WRDE_DOOFFS) { + if (wv) for (i=we->we_offs; i; i--) + we->we_wordv[i-1] = 0; + we->we_wordc -= we->we_offs; + } return err; + +nospace: + if (!(flags & WRDE_APPEND)) { + we->we_wordc = 0; + we->we_wordv = 0; + } + return WRDE_NOSPACE; +} + +int wordexp(const char *restrict s, wordexp_t *restrict we, int flags) +{ + int r, cs; + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); + r = do_wordexp(s, we, flags); + pthread_setcancelstate(cs, 0); + return r; } void wordfree(wordexp_t *we)