- 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;
+ }