3 ms·
Well, FreeBSD does it by calling a sh builtin: /* * We are the child; make /bin/sh expand `words'. */ (void)__libc_sigprocmask(SIG_SETMASK,
by coldtea 8y ago
Well, FreeBSD does it by calling a sh builtin:
/*
* We are the child; make /bin/sh expand `words'.
*/
(void)__libc_sigprocmask(SIG_SETMASK,
&oldsigblock, NULL);
if ((pdes[1] != STDOUT_FILENO ?
_dup2(pdes[1], STDOUT_FILENO) :
_fcntl(pdes[1], F_SETFD, 0)) < 0)
_exit(1);
if (_fcntl(pdesw[0], F_SETFD, 0) < 0)
_exit(1);
execl(_PATH_BSHELL, "sh", flags & WRDE_UNDEF ? "-u" : "+u",
"-c", "IFS=$1;eval \"$2\";"
"freebsd_wordexp -f \"$3\" ${4:+\"$4\"}",
"",
ifs != NULL ? ifs : " \t\n",
flags & WRDE_SHOWERR ? "" : "exec 2>/dev/null",
wfdstr,
flags & WRDE_NOCMD ? "-p" : "",
(char *)NULL);
_exit(1);
}
- MichaelMoser123 8y agofunny that glob doesn't do that http://xr.anadoxin.org/source/xref/macos-10.14-mojave/Libc-1272.200.26/gen/FreeBSD/glob.c http://xr.anadoxin.org/source/xref/macos-10.14-mojave/Libc-1... however wordexp does.
- self_awareness 8y agoSeems Linux version also depends on forking to the shell: https://code.woboq.org/userspace/glibc/posix/wordexp.c.html https://code.woboq.org/userspace/glibc/posix/wordexp.c.html
- leni536 8y agoFor the sake of completeness musl libc also does this. https://git.musl-libc.org/cgit/musl/tree/src/misc/wordexp.c#n111 https://git.musl-libc.org/cgit/musl/tree/src/misc/wordexp.c#...
- e12e 8y agoI guess the lesson is one might not want to implement word expansion in a shell using libc... [ed: if one has ambitions to function as /bin/sh...]
- deleted 8y ago[deleted]
- fouronnes3 8y agoSlightly OT, you know how people hated on C++ ranges for readability? Honestly this messy soup of C is no better.
- giornogiovanna 8y agoWhat in particular upsets you about it? It's pretty readable to me, and unlike C++ ranges, is actually fairly terse.
- fouronnes3 8y agoCryptic variable names, weird indentation, ternary operator, string escaping, statements without braces.
- sjmulder 8y ago> weird indentation This is regular BSD style, used in lots of places. Tabs for indentation, 4 spaces for wrapped lines. > ternary operator Totally fine here IMO. Would get really verbose without. > without braces Again, regular BSD style and a matter of taste.
- taoistextremist 8y agoYeah, but I think they have a point about the variable names. Really hard to tell the significance of anything there.
- kazinator 8y agoLooks transparent to me. pdes must be "pipe descriptors". ifs probably refers to the IFS concept in the shell; it may have come from getenv("IFS"). wordexp is described as influenced by IFS, and this function may have to look at that variable even though it's calling an external program to pass it different arguments (which is a bit weird at first glance: the program seems dedicated to the purpose; why can't it handle that aspect internally). flags is probably coming from the wordexp flags argument; it's being tested using the documented bitmask constants in the API. Basically the coder here didn't just make up identifiers; they have connections to pervasive Unix concepts.
- w0utert 8y agoBut why? Looking at the wordexp synopsis it doesn't seem like a particularly advanced function. Why don't the various libc's just implement this function directly in C?
- anyfoo 8y agoBecause it's literally meant to perform shell style substitution, with all the myriad of expansions that the shell supports. Similarly to system(), which also passes its input to the shell, it's never safe to use with untrusted input. (The man-page says so.)
- rurban 8y agoThe macOS libc is the FreeBSD libc. FreeBSD is upstream. FreeBSD changed it recently to use /bin/sh instead of /usr/bin/perl, because it's a bit smaller and faster.