2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)expand.c 8.5 (Berkeley) 5/15/95
37 * $FreeBSD: src/bin/sh/expand.c,v 1.31.2.5 2003/01/17 07:44:01 tjr Exp $
38 * $DragonFly: src/bin/sh/expand.c,v 1.8 2007/01/07 08:26:55 pavalos Exp $
41 #include <sys/types.h>
54 * Routines to expand arguments to commands. We have to deal with
55 * backquotes, shell variables, and file metacharacters.
77 * Structure specifying which parts of the string should be searched
82 struct ifsregion *next; /* next region in list */
83 int begoff; /* offset of start of region */
84 int endoff; /* offset of end of region */
85 int nulonly; /* search for nul bytes only */
89 STATIC char *expdest; /* output of current string */
90 STATIC struct nodelist *argbackq; /* list of back quote expressions */
91 STATIC struct ifsregion ifsfirst; /* first struct in list of ifs regions */
92 STATIC struct ifsregion *ifslastp; /* last struct in list */
93 STATIC struct arglist exparg; /* holds expanded arg list */
95 STATIC void argstr(char *, int);
96 STATIC char *exptilde(char *, int);
97 STATIC void expbackq(union node *, int, int);
98 STATIC int subevalvar(char *, char *, int, int, int, int);
99 STATIC char *evalvar(char *, int);
100 STATIC int varisset(char *, int);
101 STATIC void varvalue(char *, int, int);
102 STATIC void recordregion(int, int, int);
103 STATIC void removerecordregions(int);
104 STATIC void ifsbreakup(char *, struct arglist *);
105 STATIC void expandmeta(struct strlist *, int);
106 STATIC void expmeta(char *, char *);
107 STATIC void addfname(char *);
108 STATIC struct strlist *expsort(struct strlist *);
109 STATIC struct strlist *msort(struct strlist *, int);
110 STATIC int pmatch(char *, char *, int);
111 STATIC char *cvtnum(int, char *);
112 STATIC int collate_range_cmp(int, int);
115 collate_range_cmp (int c1, int c2)
117 static char s1[2], s2[2];
126 if ((ret = strcoll(s1, s2)) != 0)
131 extern int oexitstatus;
134 * Expand shell variables and backquotes inside a here document.
135 * union node *arg the document
136 * int fd; where to write the expanded version
140 expandhere(union node *arg, int fd)
143 expandarg(arg, (struct arglist *)NULL, 0);
144 xwrite(fd, stackblock(), expdest - stackblock());
149 * Perform variable substitution and command substitution on an argument,
150 * placing the resulting list of arguments in arglist. If EXP_FULL is true,
151 * perform splitting and file name expansion. When arglist is NULL, perform
152 * here document expansion.
156 expandarg(union node *arg, struct arglist *arglist, int flag)
161 argbackq = arg->narg.backquote;
162 STARTSTACKSTR(expdest);
163 ifsfirst.next = NULL;
165 argstr(arg->narg.text, flag);
166 if (arglist == NULL) {
167 return; /* here document expanded */
169 STPUTC('\0', expdest);
170 p = grabstackstr(expdest);
171 exparg.lastp = &exparg.list;
175 if (flag & EXP_FULL) {
176 ifsbreakup(p, &exparg);
177 *exparg.lastp = NULL;
178 exparg.lastp = &exparg.list;
179 expandmeta(exparg.list, flag);
181 if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
183 sp = (struct strlist *)stalloc(sizeof (struct strlist));
186 exparg.lastp = &sp->next;
188 while (ifsfirst.next != NULL) {
189 struct ifsregion *ifsp;
191 ifsp = ifsfirst.next->next;
192 ckfree(ifsfirst.next);
193 ifsfirst.next = ifsp;
196 *exparg.lastp = NULL;
198 *arglist->lastp = exparg.list;
199 arglist->lastp = exparg.lastp;
206 * Perform variable and command substitution. If EXP_FULL is set, output CTLESC
207 * characters to allow for further processing. Otherwise treat
208 * $@ like $* since no splitting will be performed.
212 argstr(char *p, int flag)
215 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */
218 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
219 p = exptilde(p, flag);
223 case CTLENDVAR: /* ??? */
226 /* "$@" syntax adherence hack */
227 if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
229 if ((flag & EXP_FULL) != 0)
239 p = evalvar(p, flag);
242 case CTLBACKQ|CTLQUOTE:
243 expbackq(argbackq->n, c & CTLQUOTE, flag);
244 argbackq = argbackq->next;
252 * sort of a hack - expand tildes in variable
253 * assignments (after the first '=' and after ':'s).
256 if (flag & EXP_VARTILDE && *p == '~') {
263 p = exptilde(p, flag);
274 exptilde(char *p, int flag)
279 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
281 while ((c = *p) != '\0') {
288 if (flag & EXP_VARTILDE)
298 if (*(startp+1) == '\0') {
299 if ((home = lookupvar("HOME")) == NULL)
302 if ((pw = getpwnam(startp+1)) == NULL)
309 while ((c = *home++) != '\0') {
310 if (quotes && SQSYNTAX[(int)c] == CCTL)
311 STPUTC(CTLESC, expdest);
322 removerecordregions(int endoff)
324 if (ifslastp == NULL)
327 if (ifsfirst.endoff > endoff) {
328 while (ifsfirst.next != NULL) {
329 struct ifsregion *ifsp;
331 ifsp = ifsfirst.next->next;
332 ckfree(ifsfirst.next);
333 ifsfirst.next = ifsp;
336 if (ifsfirst.begoff > endoff)
339 ifslastp = &ifsfirst;
340 ifsfirst.endoff = endoff;
345 ifslastp = &ifsfirst;
346 while (ifslastp->next && ifslastp->next->begoff < endoff)
347 ifslastp=ifslastp->next;
348 while (ifslastp->next != NULL) {
349 struct ifsregion *ifsp;
351 ifsp = ifslastp->next->next;
352 ckfree(ifslastp->next);
353 ifslastp->next = ifsp;
356 if (ifslastp->endoff > endoff)
357 ifslastp->endoff = endoff;
361 * Expand arithmetic expression. Backup to start of expression,
362 * evaluate, place result in (backed up) result, adjust string position.
370 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
375 * This routine is slightly over-complicated for
376 * efficiency. First we make sure there is
377 * enough space for the result, which may be bigger
378 * than the expression if we add exponentiation. Next we
379 * scan backwards looking for the start of arithmetic. If the
380 * next previous character is a CTLESC character, then we
381 * have to rescan starting from the beginning since CTLESC
382 * characters have to be processed left to right.
384 #if INT_MAX / 1000000000 >= 10 || INT_MIN / 1000000000 <= -10
385 #error "integers with more than 10 digits are not supported"
387 CHECKSTRSPACE(12 - 2, expdest);
388 USTPUTC('\0', expdest);
389 start = stackblock();
391 while (p >= start && *p != CTLARI)
393 if (p < start || *p != CTLARI)
394 error("missing CTLARI (shouldn't happen)");
395 if (p > start && *(p - 1) == CTLESC)
396 for (p = start; *p != CTLARI; p++)
405 removerecordregions(begoff);
409 fmtstr(p, 12, "%d", result);
413 recordregion(begoff, p - 1 - start, 0);
414 result = expdest - p + 1;
415 STADJUST(-result, expdest);
420 * Expand stuff in backwards quotes.
424 expbackq(union node *cmd, int quoted, int flag)
430 char *dest = expdest;
431 struct ifsregion saveifs, *savelastp;
432 struct nodelist *saveargbackq;
434 int startloc = dest - stackblock();
435 char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
437 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
441 savelastp = ifslastp;
442 saveargbackq = argbackq;
445 p = grabstackstr(dest);
446 evalbackcmd(cmd, &in);
447 ungrabstackstr(p, dest);
449 ifslastp = savelastp;
450 argbackq = saveargbackq;
456 if (--in.nleft < 0) {
459 while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
460 TRACE(("expbackq: read returns %d\n", i));
468 if (quotes && syntax[(int)lastc] == CCTL)
469 STPUTC(CTLESC, dest);
474 /* Eat all trailing newlines */
475 for ( ; (dest - stackblock()) > startloc && *(dest-1) == '\n'; )
483 exitstatus = waitforjob(in.jp, (int *)NULL);
485 recordregion(startloc, dest - stackblock(), 0);
486 TRACE(("evalbackq: size=%d: \"%.*s\"\n",
487 (dest - stackblock()) - startloc,
488 (dest - stackblock()) - startloc,
489 stackblock() + startloc));
497 subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
504 int saveherefd = herefd;
505 struct nodelist *saveargbackq = argbackq;
510 STACKSTRNUL(expdest);
512 argbackq = saveargbackq;
513 startp = stackblock() + startloc;
515 str = stackblock() + strloc;
519 setvar(str, startp, 0);
520 amount = startp - expdest;
521 STADJUST(amount, expdest);
528 if (*p != CTLENDVAR) {
529 outfmt(&errout, "%s\n", startp);
532 error("%.*s: parameter %snot set", p - str - 1,
533 str, (varflags & VSNUL) ? "null or "
538 for (loc = startp; loc < str; loc++) {
541 if (patmatch(str, startp, varflags & VSQUOTE)) {
546 if ((varflags & VSQUOTE) && *loc == CTLESC)
552 for (loc = str - 1; loc >= startp;) {
555 if (patmatch(str, startp, varflags & VSQUOTE)) {
561 if ((varflags & VSQUOTE) && loc > startp &&
562 *(loc - 1) == CTLESC) {
563 for (q = startp; q < loc; q++)
573 for (loc = str - 1; loc >= startp;) {
574 if (patmatch(str, loc, varflags & VSQUOTE)) {
575 amount = loc - expdest;
576 STADJUST(amount, expdest);
580 if ((varflags & VSQUOTE) && loc > startp &&
581 *(loc - 1) == CTLESC) {
582 for (q = startp; q < loc; q++)
592 for (loc = startp; loc < str - 1; loc++) {
593 if (patmatch(str, loc, varflags & VSQUOTE)) {
594 amount = loc - expdest;
595 STADJUST(amount, expdest);
598 if ((varflags & VSQUOTE) && *loc == CTLESC)
609 amount = ((str - 1) - (loc - startp)) - expdest;
610 STADJUST(amount, expdest);
611 while (loc != str - 1)
618 * Expand a variable, and return a pointer to the next character in the
623 evalvar(char *p, int flag)
636 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
639 subtype = varflags & VSTYPE;
644 p = strchr(p, '=') + 1;
645 again: /* jump here after setting a variable with ${var=text} */
647 set = varisset(var, varflags & VSNUL);
650 val = bltinlookup(var, 1);
651 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
658 startloc = expdest - stackblock();
667 error("%.*s: parameter not set", p - var - 1, var);
670 if (set && subtype != VSPLUS) {
671 /* insert the value of the variable */
673 varvalue(var, varflags & VSQUOTE, flag & EXP_FULL);
674 if (subtype == VSLENGTH) {
675 varlen = expdest - stackblock() - startloc;
676 STADJUST(-varlen, expdest);
679 char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
682 if (subtype == VSLENGTH) {
689 syntax[(int)*val] == CCTL)
690 STPUTC(CTLESC, expdest);
691 STPUTC(*val++, expdest);
698 if (subtype == VSPLUS)
701 easy = ((varflags & VSQUOTE) == 0 ||
702 (*var == '@' && shellparam.nparam != 1));
707 expdest = cvtnum(varlen, expdest);
714 recordregion(startloc, expdest - stackblock(),
735 * Terminate the string and start recording the pattern
738 STPUTC('\0', expdest);
739 patloc = expdest - stackblock();
740 if (subevalvar(p, NULL, patloc, subtype,
741 startloc, varflags) == 0) {
742 int amount = (expdest - stackblock() - patloc) + 1;
743 STADJUST(-amount, expdest);
745 /* Remove any recorded regions beyond start of variable */
746 removerecordregions(startloc);
752 if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
755 * Remove any recorded regions beyond
758 removerecordregions(startloc);
771 if (subtype != VSNORMAL) { /* skip to end of alternative */
774 if ((c = *p++) == CTLESC)
776 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
778 argbackq = argbackq->next;
779 } else if (c == CTLVAR) {
780 if ((*p++ & VSTYPE) != VSNORMAL)
782 } else if (c == CTLENDVAR) {
794 * Test whether a specialized variable is set.
798 varisset(char *name, int nulok)
802 return backgndpid != -1;
803 else if (*name == '@' || *name == '*') {
804 if (*shellparam.p == NULL)
810 for (av = shellparam.p; *av; av++)
815 } else if (is_digit(*name)) {
817 int num = atoi(name);
819 if (num > shellparam.nparam)
825 ap = shellparam.p[num - 1];
827 if (nulok && (ap == NULL || *ap == '\0'))
836 * Add the value of a specialized variable to the stack string.
840 varvalue(char *name, int quoted, int allow_split)
849 #define STRTODEST(p) \
852 syntax = quoted? DQSYNTAX : BASESYNTAX; \
854 if (syntax[(int)*p] == CCTL) \
855 STPUTC(CTLESC, expdest); \
856 STPUTC(*p++, expdest); \
860 STPUTC(*p++, expdest); \
872 num = shellparam.nparam;
877 expdest = cvtnum(num, expdest);
880 for (i = 0 ; i < NOPTS ; i++) {
882 STPUTC(optlist[i].letter, expdest);
886 if (allow_split && quoted) {
887 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
890 STPUTC('\0', expdest);
900 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
903 STPUTC(sep, expdest);
911 if (is_digit(*name)) {
913 if (num > 0 && num <= shellparam.nparam) {
914 p = shellparam.p[num - 1];
925 * Record the the fact that we have to scan this region of the
926 * string for IFS characters.
930 recordregion(int start, int end, int nulonly)
932 struct ifsregion *ifsp;
934 if (ifslastp == NULL) {
937 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
938 ifslastp->next = ifsp;
941 ifslastp->next = NULL;
942 ifslastp->begoff = start;
943 ifslastp->endoff = end;
944 ifslastp->nulonly = nulonly;
950 * Break the argument string into pieces based upon IFS and add the
951 * strings to the argument list. The regions of the string to be
952 * searched for IFS characters have been stored by recordregion.
955 ifsbreakup(char *string, struct arglist *arglist)
957 struct ifsregion *ifsp;
970 if (ifslastp != NULL) {
973 p = string + ifsp->begoff;
974 nulonly = ifsp->nulonly;
975 ifs = nulonly ? nullstr :
976 ( ifsset() ? ifsval() : " \t\n" );
978 while (p < string + ifsp->endoff) {
982 if (strchr(ifs, *p)) {
984 ifsspc = (strchr(" \t\n", *p) != NULL);
985 /* Ignore IFS whitespace at start */
986 if (q == start && ifsspc) {
992 sp = (struct strlist *)stalloc(sizeof *sp);
994 *arglist->lastp = sp;
995 arglist->lastp = &sp->next;
999 if (p >= string + ifsp->endoff) {
1005 if (strchr(ifs, *p) == NULL ) {
1008 } else if (strchr(" \t\n",*p) == NULL) {
1024 } while ((ifsp = ifsp->next) != NULL);
1025 if (*start || (!ifsspc && start > string &&
1027 sp = (struct strlist *)stalloc(sizeof *sp);
1029 *arglist->lastp = sp;
1030 arglist->lastp = &sp->next;
1033 sp = (struct strlist *)stalloc(sizeof *sp);
1035 *arglist->lastp = sp;
1036 arglist->lastp = &sp->next;
1043 * Expand shell metacharacters. At this point, the only control characters
1044 * should be escapes. The results are stored in the list exparg.
1047 STATIC char *expdir;
1051 expandmeta(struct strlist *str, int flag __unused)
1054 struct strlist **savelastp;
1057 /* TODO - EXP_REDIR */
1063 for (;;) { /* fast check for meta chars */
1064 if ((c = *p++) == '\0')
1066 if (c == '*' || c == '?' || c == '[' || c == '!')
1069 savelastp = exparg.lastp;
1071 if (expdir == NULL) {
1072 int i = strlen(str->text);
1073 expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */
1076 expmeta(expdir, str->text);
1080 if (exparg.lastp == savelastp) {
1085 *exparg.lastp = str;
1086 rmescapes(str->text);
1087 exparg.lastp = &str->next;
1089 *exparg.lastp = NULL;
1090 *savelastp = sp = expsort(*savelastp);
1091 while (sp->next != NULL)
1093 exparg.lastp = &sp->next;
1101 * Do metacharacter (i.e. *, ?, [...]) expansion.
1105 expmeta(char *enddir, char *name)
1120 for (p = name ; ; p++) {
1121 if (*p == '*' || *p == '?')
1123 else if (*p == '[') {
1125 if (*q == '!' || *q == '^')
1128 while (*q == CTLQUOTEMARK)
1132 if (*q == '/' || *q == '\0')
1139 } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) {
1141 } else if (*p == '\0')
1143 else if (*p == CTLQUOTEMARK)
1145 else if (*p == CTLESC)
1153 if (metaflag == 0) { /* we've reached the end of the file name */
1154 if (enddir != expdir)
1156 for (p = name ; ; p++) {
1157 if (*p == CTLQUOTEMARK)
1165 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1170 if (start != name) {
1173 while (*p == CTLQUOTEMARK)
1180 if (enddir == expdir) {
1182 } else if (enddir == expdir + 1 && *expdir == '/') {
1188 if ((dirp = opendir(q)) == NULL)
1190 if (enddir != expdir)
1192 if (*endname == 0) {
1200 while (*p == CTLQUOTEMARK)
1206 while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1207 if (dp->d_name[0] == '.' && ! matchdot)
1209 if (patmatch(start, dp->d_name, 0)) {
1211 scopy(dp->d_name, enddir);
1215 for (t = enddir, q = dp->d_name;
1216 (*t++ = *q++) != '\0';)
1219 expmeta(t, endname);
1230 * Add a file name to the list.
1234 addfname(char *name)
1239 p = stalloc(strlen(name) + 1);
1241 sp = (struct strlist *)stalloc(sizeof *sp);
1244 exparg.lastp = &sp->next;
1249 * Sort the results of file name expansion. It calculates the number of
1250 * strings to sort and then calls msort (short for merge sort) to do the
1254 STATIC struct strlist *
1255 expsort(struct strlist *str)
1261 for (sp = str ; sp ; sp = sp->next)
1263 return msort(str, len);
1267 STATIC struct strlist *
1268 msort(struct strlist *list, int len)
1270 struct strlist *p, *q = NULL;
1271 struct strlist **lpp;
1279 for (n = half ; --n >= 0 ; ) {
1283 q->next = NULL; /* terminate first half of list */
1284 q = msort(list, half); /* sort first half of list */
1285 p = msort(p, len - half); /* sort second half */
1288 if (strcmp(p->text, q->text) < 0) {
1291 if ((p = *lpp) == NULL) {
1298 if ((q = *lpp) == NULL) {
1310 * Returns true if the pattern matches the string.
1314 patmatch(char *pattern, char *string, int squoted)
1317 if (pattern[0] == '!' && pattern[1] == '!')
1318 return 1 - pmatch(pattern + 2, string);
1321 return pmatch(pattern, string, squoted);
1326 pmatch(char *pattern, char *string, int squoted)
1338 if (squoted && *q == CTLESC)
1346 if (squoted && *q == CTLESC)
1353 while (c == CTLQUOTEMARK || c == '*')
1355 if (c != CTLESC && c != CTLQUOTEMARK &&
1356 c != '?' && c != '*' && c != '[') {
1358 if (squoted && *q == CTLESC &&
1363 if (squoted && *q == CTLESC)
1369 if (pmatch(p, q, squoted))
1371 if (squoted && *q == CTLESC)
1373 } while (*q++ != '\0');
1381 if (*endp == '!' || *endp == '^')
1384 while (*endp == CTLQUOTEMARK)
1387 goto dft; /* no matching ] */
1388 if (*endp == CTLESC)
1394 if (*p == '!' || *p == '^') {
1400 if (squoted && chr == CTLESC)
1406 if (c == CTLQUOTEMARK)
1410 if (*p == '-' && p[1] != ']') {
1412 while (*p == CTLQUOTEMARK)
1416 if ( collate_range_cmp(chr, c) >= 0
1417 && collate_range_cmp(chr, *p) <= 0
1425 } while ((c = *p++) != ']');
1426 if (found == invert)
1431 if (squoted && *q == CTLESC)
1447 * Remove any CTLESC characters from a string.
1451 rmescapes(char *str)
1456 while (*p != CTLESC && *p != CTLQUOTEMARK) {
1462 if (*p == CTLQUOTEMARK) {
1476 * See if a pattern matches in a case statement.
1480 casematch(union node *pattern, char *val)
1482 struct stackmark smark;
1486 setstackmark(&smark);
1487 argbackq = pattern->narg.backquote;
1488 STARTSTACKSTR(expdest);
1490 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
1491 STPUTC('\0', expdest);
1492 p = grabstackstr(expdest);
1493 result = patmatch(p, val, 0);
1494 popstackmark(&smark);
1503 cvtnum(int num, char *buf)
1507 char *p = temp + 31;
1512 *--p = num % 10 + '0';
1513 } while ((num /= 10) != 0);
1524 * Do most of the work for wordexp(3).
1528 wordexpcmd(int argc, char **argv)
1533 out1fmt("%08x", argc - 1);
1534 for (i = 1, len = 0; i < argc; i++)
1535 len += strlen(argv[i]);
1536 out1fmt("%08x", (int)len);
1537 for (i = 1; i < argc; i++) {