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.6 2005/07/03 14:03:24 corecode Exp $
41 #include <sys/types.h>
53 * Routines to expand arguments to commands. We have to deal with
54 * backquotes, shell variables, and file metacharacters.
76 * Structure specifying which parts of the string should be searched
81 struct ifsregion *next; /* next region in list */
82 int begoff; /* offset of start of region */
83 int endoff; /* offset of end of region */
84 int nulonly; /* search for nul bytes only */
88 STATIC char *expdest; /* output of current string */
89 STATIC struct nodelist *argbackq; /* list of back quote expressions */
90 STATIC struct ifsregion ifsfirst; /* first struct in list of ifs regions */
91 STATIC struct ifsregion *ifslastp; /* last struct in list */
92 STATIC struct arglist exparg; /* holds expanded arg list */
94 STATIC void argstr(char *, int);
95 STATIC char *exptilde(char *, int);
96 STATIC void expbackq(union node *, int, int);
97 STATIC int subevalvar(char *, char *, int, int, int, int);
98 STATIC char *evalvar(char *, int);
99 STATIC int varisset(char *, int);
100 STATIC void varvalue(char *, int, int);
101 STATIC void recordregion(int, int, int);
102 STATIC void removerecordregions(int);
103 STATIC void ifsbreakup(char *, struct arglist *);
104 STATIC void expandmeta(struct strlist *, int);
105 STATIC void expmeta(char *, char *);
106 STATIC void addfname(char *);
107 STATIC struct strlist *expsort(struct strlist *);
108 STATIC struct strlist *msort(struct strlist *, int);
109 STATIC int pmatch(char *, char *, int);
110 STATIC char *cvtnum(int, char *);
111 STATIC int collate_range_cmp(int, int);
114 collate_range_cmp (int c1, int c2)
116 static char s1[2], s2[2];
125 if ((ret = strcoll(s1, s2)) != 0)
131 * Expand shell variables and backquotes inside a here document.
132 * union node *arg the document
133 * int fd; where to write the expanded version
137 expandhere(union node *arg, int fd)
140 expandarg(arg, (struct arglist *)NULL, 0);
141 xwrite(fd, stackblock(), expdest - stackblock());
146 * Perform variable substitution and command substitution on an argument,
147 * placing the resulting list of arguments in arglist. If EXP_FULL is true,
148 * perform splitting and file name expansion. When arglist is NULL, perform
149 * here document expansion.
153 expandarg(union node *arg, struct arglist *arglist, int flag)
158 argbackq = arg->narg.backquote;
159 STARTSTACKSTR(expdest);
160 ifsfirst.next = NULL;
162 argstr(arg->narg.text, flag);
163 if (arglist == NULL) {
164 return; /* here document expanded */
166 STPUTC('\0', expdest);
167 p = grabstackstr(expdest);
168 exparg.lastp = &exparg.list;
172 if (flag & EXP_FULL) {
173 ifsbreakup(p, &exparg);
174 *exparg.lastp = NULL;
175 exparg.lastp = &exparg.list;
176 expandmeta(exparg.list, flag);
178 if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
180 sp = (struct strlist *)stalloc(sizeof (struct strlist));
183 exparg.lastp = &sp->next;
185 while (ifsfirst.next != NULL) {
186 struct ifsregion *ifsp;
188 ifsp = ifsfirst.next->next;
189 ckfree(ifsfirst.next);
190 ifsfirst.next = ifsp;
193 *exparg.lastp = NULL;
195 *arglist->lastp = exparg.list;
196 arglist->lastp = exparg.lastp;
203 * Perform variable and command substitution. If EXP_FULL is set, output CTLESC
204 * characters to allow for further processing. Otherwise treat
205 * $@ like $* since no splitting will be performed.
209 argstr(char *p, int flag)
212 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */
215 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
216 p = exptilde(p, flag);
220 case CTLENDVAR: /* ??? */
223 /* "$@" syntax adherence hack */
224 if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
226 if ((flag & EXP_FULL) != 0)
236 p = evalvar(p, flag);
239 case CTLBACKQ|CTLQUOTE:
240 expbackq(argbackq->n, c & CTLQUOTE, flag);
241 argbackq = argbackq->next;
249 * sort of a hack - expand tildes in variable
250 * assignments (after the first '=' and after ':'s).
253 if (flag & EXP_VARTILDE && *p == '~') {
260 p = exptilde(p, flag);
271 exptilde(char *p, int flag)
276 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
278 while ((c = *p) != '\0') {
285 if (flag & EXP_VARTILDE)
295 if (*(startp+1) == '\0') {
296 if ((home = lookupvar("HOME")) == NULL)
299 if ((pw = getpwnam(startp+1)) == NULL)
306 while ((c = *home++) != '\0') {
307 if (quotes && SQSYNTAX[(int)c] == CCTL)
308 STPUTC(CTLESC, expdest);
319 removerecordregions(int endoff)
321 if (ifslastp == NULL)
324 if (ifsfirst.endoff > endoff) {
325 while (ifsfirst.next != NULL) {
326 struct ifsregion *ifsp;
328 ifsp = ifsfirst.next->next;
329 ckfree(ifsfirst.next);
330 ifsfirst.next = ifsp;
333 if (ifsfirst.begoff > endoff)
336 ifslastp = &ifsfirst;
337 ifsfirst.endoff = endoff;
342 ifslastp = &ifsfirst;
343 while (ifslastp->next && ifslastp->next->begoff < endoff)
344 ifslastp=ifslastp->next;
345 while (ifslastp->next != NULL) {
346 struct ifsregion *ifsp;
348 ifsp = ifslastp->next->next;
349 ckfree(ifslastp->next);
350 ifslastp->next = ifsp;
353 if (ifslastp->endoff > endoff)
354 ifslastp->endoff = endoff;
358 * Expand arithmetic expression. Backup to start of expression,
359 * evaluate, place result in (backed up) result, adjust string position.
367 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
372 * This routine is slightly over-complicated for
373 * efficiency. First we make sure there is
374 * enough space for the result, which may be bigger
375 * than the expression if we add exponentiation. Next we
376 * scan backwards looking for the start of arithmetic. If the
377 * next previous character is a CTLESC character, then we
378 * have to rescan starting from the beginning since CTLESC
379 * characters have to be processed left to right.
381 #if INT_MAX / 1000000000 >= 10 || INT_MIN / 1000000000 <= -10
382 #error "integers with more than 10 digits are not supported"
384 CHECKSTRSPACE(12 - 2, expdest);
385 USTPUTC('\0', expdest);
386 start = stackblock();
388 while (p >= start && *p != CTLARI)
390 if (p < start || *p != CTLARI)
391 error("missing CTLARI (shouldn't happen)");
392 if (p > start && *(p - 1) == CTLESC)
393 for (p = start; *p != CTLARI; p++)
402 removerecordregions(begoff);
406 fmtstr(p, 12, "%d", result);
410 recordregion(begoff, p - 1 - start, 0);
411 result = expdest - p + 1;
412 STADJUST(-result, expdest);
417 * Expand stuff in backwards quotes.
421 expbackq(union node *cmd, int quoted, int flag)
427 char *dest = expdest;
428 struct ifsregion saveifs, *savelastp;
429 struct nodelist *saveargbackq;
431 int startloc = dest - stackblock();
432 char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
434 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
438 savelastp = ifslastp;
439 saveargbackq = argbackq;
442 p = grabstackstr(dest);
443 evalbackcmd(cmd, &in);
444 ungrabstackstr(p, dest);
446 ifslastp = savelastp;
447 argbackq = saveargbackq;
453 if (--in.nleft < 0) {
456 while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
457 TRACE(("expbackq: read returns %d\n", i));
465 if (quotes && syntax[(int)lastc] == CCTL)
466 STPUTC(CTLESC, dest);
471 /* Eat all trailing newlines */
472 for ( ; (dest - stackblock()) > startloc && *(dest-1) == '\n'; )
480 exitstatus = waitforjob(in.jp, (int *)NULL);
482 recordregion(startloc, dest - stackblock(), 0);
483 TRACE(("evalbackq: size=%d: \"%.*s\"\n",
484 (dest - stackblock()) - startloc,
485 (dest - stackblock()) - startloc,
486 stackblock() + startloc));
494 subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
501 int saveherefd = herefd;
502 struct nodelist *saveargbackq = argbackq;
507 STACKSTRNUL(expdest);
509 argbackq = saveargbackq;
510 startp = stackblock() + startloc;
512 str = stackblock() + strloc;
516 setvar(str, startp, 0);
517 amount = startp - expdest;
518 STADJUST(amount, expdest);
525 if (*p != CTLENDVAR) {
526 outfmt(&errout, "%s\n", startp);
529 error("%.*s: parameter %snot set", p - str - 1,
530 str, (varflags & VSNUL) ? "null or "
535 for (loc = startp; loc < str; loc++) {
538 if (patmatch(str, startp, varflags & VSQUOTE)) {
543 if ((varflags & VSQUOTE) && *loc == CTLESC)
549 for (loc = str - 1; loc >= startp;) {
552 if (patmatch(str, startp, varflags & VSQUOTE)) {
558 if ((varflags & VSQUOTE) && loc > startp &&
559 *(loc - 1) == CTLESC) {
560 for (q = startp; q < loc; q++)
570 for (loc = str - 1; loc >= startp;) {
571 if (patmatch(str, loc, varflags & VSQUOTE)) {
572 amount = loc - expdest;
573 STADJUST(amount, expdest);
577 if ((varflags & VSQUOTE) && loc > startp &&
578 *(loc - 1) == CTLESC) {
579 for (q = startp; q < loc; q++)
589 for (loc = startp; loc < str - 1; loc++) {
590 if (patmatch(str, loc, varflags & VSQUOTE)) {
591 amount = loc - expdest;
592 STADJUST(amount, expdest);
595 if ((varflags & VSQUOTE) && *loc == CTLESC)
606 amount = ((str - 1) - (loc - startp)) - expdest;
607 STADJUST(amount, expdest);
608 while (loc != str - 1)
615 * Expand a variable, and return a pointer to the next character in the
620 evalvar(char *p, int flag)
633 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
636 subtype = varflags & VSTYPE;
641 p = strchr(p, '=') + 1;
642 again: /* jump here after setting a variable with ${var=text} */
644 set = varisset(var, varflags & VSNUL);
647 val = bltinlookup(var, 1);
648 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
655 startloc = expdest - stackblock();
664 error("%.*s: parameter not set", p - var - 1, var);
667 if (set && subtype != VSPLUS) {
668 /* insert the value of the variable */
670 varvalue(var, varflags & VSQUOTE, flag & EXP_FULL);
671 if (subtype == VSLENGTH) {
672 varlen = expdest - stackblock() - startloc;
673 STADJUST(-varlen, expdest);
676 char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
679 if (subtype == VSLENGTH) {
686 syntax[(int)*val] == CCTL)
687 STPUTC(CTLESC, expdest);
688 STPUTC(*val++, expdest);
695 if (subtype == VSPLUS)
698 easy = ((varflags & VSQUOTE) == 0 ||
699 (*var == '@' && shellparam.nparam != 1));
704 expdest = cvtnum(varlen, expdest);
711 recordregion(startloc, expdest - stackblock(),
732 * Terminate the string and start recording the pattern
735 STPUTC('\0', expdest);
736 patloc = expdest - stackblock();
737 if (subevalvar(p, NULL, patloc, subtype,
738 startloc, varflags) == 0) {
739 int amount = (expdest - stackblock() - patloc) + 1;
740 STADJUST(-amount, expdest);
742 /* Remove any recorded regions beyond start of variable */
743 removerecordregions(startloc);
749 if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
752 * Remove any recorded regions beyond
755 removerecordregions(startloc);
768 if (subtype != VSNORMAL) { /* skip to end of alternative */
771 if ((c = *p++) == CTLESC)
773 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
775 argbackq = argbackq->next;
776 } else if (c == CTLVAR) {
777 if ((*p++ & VSTYPE) != VSNORMAL)
779 } else if (c == CTLENDVAR) {
791 * Test whether a specialized variable is set.
795 varisset(char *name, int nulok)
799 return backgndpid != -1;
800 else if (*name == '@' || *name == '*') {
801 if (*shellparam.p == NULL)
807 for (av = shellparam.p; *av; av++)
812 } else if (is_digit(*name)) {
814 int num = atoi(name);
816 if (num > shellparam.nparam)
822 ap = shellparam.p[num - 1];
824 if (nulok && (ap == NULL || *ap == '\0'))
833 * Add the value of a specialized variable to the stack string.
837 varvalue(char *name, int quoted, int allow_split)
842 extern int oexitstatus;
847 #define STRTODEST(p) \
850 syntax = quoted? DQSYNTAX : BASESYNTAX; \
852 if (syntax[(int)*p] == CCTL) \
853 STPUTC(CTLESC, expdest); \
854 STPUTC(*p++, expdest); \
858 STPUTC(*p++, expdest); \
870 num = shellparam.nparam;
875 expdest = cvtnum(num, expdest);
878 for (i = 0 ; i < NOPTS ; i++) {
880 STPUTC(optlist[i].letter, expdest);
884 if (allow_split && quoted) {
885 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
888 STPUTC('\0', expdest);
898 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
901 STPUTC(sep, expdest);
909 if (is_digit(*name)) {
911 if (num > 0 && num <= shellparam.nparam) {
912 p = shellparam.p[num - 1];
923 * Record the the fact that we have to scan this region of the
924 * string for IFS characters.
928 recordregion(int start, int end, int nulonly)
930 struct ifsregion *ifsp;
932 if (ifslastp == NULL) {
935 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
936 ifslastp->next = ifsp;
939 ifslastp->next = NULL;
940 ifslastp->begoff = start;
941 ifslastp->endoff = end;
942 ifslastp->nulonly = nulonly;
948 * Break the argument string into pieces based upon IFS and add the
949 * strings to the argument list. The regions of the string to be
950 * searched for IFS characters have been stored by recordregion.
953 ifsbreakup(char *string, struct arglist *arglist)
955 struct ifsregion *ifsp;
968 if (ifslastp != NULL) {
971 p = string + ifsp->begoff;
972 nulonly = ifsp->nulonly;
973 ifs = nulonly ? nullstr :
974 ( ifsset() ? ifsval() : " \t\n" );
976 while (p < string + ifsp->endoff) {
980 if (strchr(ifs, *p)) {
982 ifsspc = (strchr(" \t\n", *p) != NULL);
983 /* Ignore IFS whitespace at start */
984 if (q == start && ifsspc) {
990 sp = (struct strlist *)stalloc(sizeof *sp);
992 *arglist->lastp = sp;
993 arglist->lastp = &sp->next;
997 if (p >= string + ifsp->endoff) {
1003 if (strchr(ifs, *p) == NULL ) {
1006 } else if (strchr(" \t\n",*p) == NULL) {
1022 } while ((ifsp = ifsp->next) != NULL);
1023 if (*start || (!ifsspc && start > string &&
1025 sp = (struct strlist *)stalloc(sizeof *sp);
1027 *arglist->lastp = sp;
1028 arglist->lastp = &sp->next;
1031 sp = (struct strlist *)stalloc(sizeof *sp);
1033 *arglist->lastp = sp;
1034 arglist->lastp = &sp->next;
1041 * Expand shell metacharacters. At this point, the only control characters
1042 * should be escapes. The results are stored in the list exparg.
1045 STATIC char *expdir;
1049 expandmeta(struct strlist *str, int flag __unused)
1052 struct strlist **savelastp;
1055 /* TODO - EXP_REDIR */
1061 for (;;) { /* fast check for meta chars */
1062 if ((c = *p++) == '\0')
1064 if (c == '*' || c == '?' || c == '[' || c == '!')
1067 savelastp = exparg.lastp;
1069 if (expdir == NULL) {
1070 int i = strlen(str->text);
1071 expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */
1074 expmeta(expdir, str->text);
1078 if (exparg.lastp == savelastp) {
1083 *exparg.lastp = str;
1084 rmescapes(str->text);
1085 exparg.lastp = &str->next;
1087 *exparg.lastp = NULL;
1088 *savelastp = sp = expsort(*savelastp);
1089 while (sp->next != NULL)
1091 exparg.lastp = &sp->next;
1099 * Do metacharacter (i.e. *, ?, [...]) expansion.
1103 expmeta(char *enddir, char *name)
1118 for (p = name ; ; p++) {
1119 if (*p == '*' || *p == '?')
1121 else if (*p == '[') {
1123 if (*q == '!' || *q == '^')
1126 while (*q == CTLQUOTEMARK)
1130 if (*q == '/' || *q == '\0')
1137 } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) {
1139 } else if (*p == '\0')
1141 else if (*p == CTLQUOTEMARK)
1143 else if (*p == CTLESC)
1151 if (metaflag == 0) { /* we've reached the end of the file name */
1152 if (enddir != expdir)
1154 for (p = name ; ; p++) {
1155 if (*p == CTLQUOTEMARK)
1163 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1168 if (start != name) {
1171 while (*p == CTLQUOTEMARK)
1178 if (enddir == expdir) {
1180 } else if (enddir == expdir + 1 && *expdir == '/') {
1186 if ((dirp = opendir(q)) == NULL)
1188 if (enddir != expdir)
1190 if (*endname == 0) {
1198 while (*p == CTLQUOTEMARK)
1204 while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1205 if (dp->d_name[0] == '.' && ! matchdot)
1207 if (patmatch(start, dp->d_name, 0)) {
1209 scopy(dp->d_name, enddir);
1213 for (t = enddir, q = dp->d_name;
1214 (*t++ = *q++) != '\0';)
1217 expmeta(t, endname);
1228 * Add a file name to the list.
1232 addfname(char *name)
1237 p = stalloc(strlen(name) + 1);
1239 sp = (struct strlist *)stalloc(sizeof *sp);
1242 exparg.lastp = &sp->next;
1247 * Sort the results of file name expansion. It calculates the number of
1248 * strings to sort and then calls msort (short for merge sort) to do the
1252 STATIC struct strlist *
1253 expsort(struct strlist *str)
1259 for (sp = str ; sp ; sp = sp->next)
1261 return msort(str, len);
1265 STATIC struct strlist *
1266 msort(struct strlist *list, int len)
1268 struct strlist *p, *q = NULL;
1269 struct strlist **lpp;
1277 for (n = half ; --n >= 0 ; ) {
1281 q->next = NULL; /* terminate first half of list */
1282 q = msort(list, half); /* sort first half of list */
1283 p = msort(p, len - half); /* sort second half */
1286 if (strcmp(p->text, q->text) < 0) {
1289 if ((p = *lpp) == NULL) {
1296 if ((q = *lpp) == NULL) {
1308 * Returns true if the pattern matches the string.
1312 patmatch(char *pattern, char *string, int squoted)
1315 if (pattern[0] == '!' && pattern[1] == '!')
1316 return 1 - pmatch(pattern + 2, string);
1319 return pmatch(pattern, string, squoted);
1324 pmatch(char *pattern, char *string, int squoted)
1336 if (squoted && *q == CTLESC)
1344 if (squoted && *q == CTLESC)
1351 while (c == CTLQUOTEMARK || c == '*')
1353 if (c != CTLESC && c != CTLQUOTEMARK &&
1354 c != '?' && c != '*' && c != '[') {
1356 if (squoted && *q == CTLESC &&
1361 if (squoted && *q == CTLESC)
1367 if (pmatch(p, q, squoted))
1369 if (squoted && *q == CTLESC)
1371 } while (*q++ != '\0');
1379 if (*endp == '!' || *endp == '^')
1382 while (*endp == CTLQUOTEMARK)
1385 goto dft; /* no matching ] */
1386 if (*endp == CTLESC)
1392 if (*p == '!' || *p == '^') {
1398 if (squoted && chr == CTLESC)
1404 if (c == CTLQUOTEMARK)
1408 if (*p == '-' && p[1] != ']') {
1410 while (*p == CTLQUOTEMARK)
1414 if ( collate_range_cmp(chr, c) >= 0
1415 && collate_range_cmp(chr, *p) <= 0
1423 } while ((c = *p++) != ']');
1424 if (found == invert)
1429 if (squoted && *q == CTLESC)
1445 * Remove any CTLESC characters from a string.
1449 rmescapes(char *str)
1454 while (*p != CTLESC && *p != CTLQUOTEMARK) {
1460 if (*p == CTLQUOTEMARK) {
1474 * See if a pattern matches in a case statement.
1478 casematch(union node *pattern, char *val)
1480 struct stackmark smark;
1484 setstackmark(&smark);
1485 argbackq = pattern->narg.backquote;
1486 STARTSTACKSTR(expdest);
1488 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
1489 STPUTC('\0', expdest);
1490 p = grabstackstr(expdest);
1491 result = patmatch(p, val, 0);
1492 popstackmark(&smark);
1501 cvtnum(int num, char *buf)
1505 char *p = temp + 31;
1510 *--p = num % 10 + '0';
1511 } while ((num /= 10) != 0);