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.7 2006/09/28 22:29:44 pavalos 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)
130 extern int oexitstatus;
133 * Expand shell variables and backquotes inside a here document.
134 * union node *arg the document
135 * int fd; where to write the expanded version
139 expandhere(union node *arg, int fd)
142 expandarg(arg, (struct arglist *)NULL, 0);
143 xwrite(fd, stackblock(), expdest - stackblock());
148 * Perform variable substitution and command substitution on an argument,
149 * placing the resulting list of arguments in arglist. If EXP_FULL is true,
150 * perform splitting and file name expansion. When arglist is NULL, perform
151 * here document expansion.
155 expandarg(union node *arg, struct arglist *arglist, int flag)
160 argbackq = arg->narg.backquote;
161 STARTSTACKSTR(expdest);
162 ifsfirst.next = NULL;
164 argstr(arg->narg.text, flag);
165 if (arglist == NULL) {
166 return; /* here document expanded */
168 STPUTC('\0', expdest);
169 p = grabstackstr(expdest);
170 exparg.lastp = &exparg.list;
174 if (flag & EXP_FULL) {
175 ifsbreakup(p, &exparg);
176 *exparg.lastp = NULL;
177 exparg.lastp = &exparg.list;
178 expandmeta(exparg.list, flag);
180 if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
182 sp = (struct strlist *)stalloc(sizeof (struct strlist));
185 exparg.lastp = &sp->next;
187 while (ifsfirst.next != NULL) {
188 struct ifsregion *ifsp;
190 ifsp = ifsfirst.next->next;
191 ckfree(ifsfirst.next);
192 ifsfirst.next = ifsp;
195 *exparg.lastp = NULL;
197 *arglist->lastp = exparg.list;
198 arglist->lastp = exparg.lastp;
205 * Perform variable and command substitution. If EXP_FULL is set, output CTLESC
206 * characters to allow for further processing. Otherwise treat
207 * $@ like $* since no splitting will be performed.
211 argstr(char *p, int flag)
214 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */
217 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
218 p = exptilde(p, flag);
222 case CTLENDVAR: /* ??? */
225 /* "$@" syntax adherence hack */
226 if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
228 if ((flag & EXP_FULL) != 0)
238 p = evalvar(p, flag);
241 case CTLBACKQ|CTLQUOTE:
242 expbackq(argbackq->n, c & CTLQUOTE, flag);
243 argbackq = argbackq->next;
251 * sort of a hack - expand tildes in variable
252 * assignments (after the first '=' and after ':'s).
255 if (flag & EXP_VARTILDE && *p == '~') {
262 p = exptilde(p, flag);
273 exptilde(char *p, int flag)
278 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
280 while ((c = *p) != '\0') {
287 if (flag & EXP_VARTILDE)
297 if (*(startp+1) == '\0') {
298 if ((home = lookupvar("HOME")) == NULL)
301 if ((pw = getpwnam(startp+1)) == NULL)
308 while ((c = *home++) != '\0') {
309 if (quotes && SQSYNTAX[(int)c] == CCTL)
310 STPUTC(CTLESC, expdest);
321 removerecordregions(int endoff)
323 if (ifslastp == NULL)
326 if (ifsfirst.endoff > endoff) {
327 while (ifsfirst.next != NULL) {
328 struct ifsregion *ifsp;
330 ifsp = ifsfirst.next->next;
331 ckfree(ifsfirst.next);
332 ifsfirst.next = ifsp;
335 if (ifsfirst.begoff > endoff)
338 ifslastp = &ifsfirst;
339 ifsfirst.endoff = endoff;
344 ifslastp = &ifsfirst;
345 while (ifslastp->next && ifslastp->next->begoff < endoff)
346 ifslastp=ifslastp->next;
347 while (ifslastp->next != NULL) {
348 struct ifsregion *ifsp;
350 ifsp = ifslastp->next->next;
351 ckfree(ifslastp->next);
352 ifslastp->next = ifsp;
355 if (ifslastp->endoff > endoff)
356 ifslastp->endoff = endoff;
360 * Expand arithmetic expression. Backup to start of expression,
361 * evaluate, place result in (backed up) result, adjust string position.
369 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
374 * This routine is slightly over-complicated for
375 * efficiency. First we make sure there is
376 * enough space for the result, which may be bigger
377 * than the expression if we add exponentiation. Next we
378 * scan backwards looking for the start of arithmetic. If the
379 * next previous character is a CTLESC character, then we
380 * have to rescan starting from the beginning since CTLESC
381 * characters have to be processed left to right.
383 #if INT_MAX / 1000000000 >= 10 || INT_MIN / 1000000000 <= -10
384 #error "integers with more than 10 digits are not supported"
386 CHECKSTRSPACE(12 - 2, expdest);
387 USTPUTC('\0', expdest);
388 start = stackblock();
390 while (p >= start && *p != CTLARI)
392 if (p < start || *p != CTLARI)
393 error("missing CTLARI (shouldn't happen)");
394 if (p > start && *(p - 1) == CTLESC)
395 for (p = start; *p != CTLARI; p++)
404 removerecordregions(begoff);
408 fmtstr(p, 12, "%d", result);
412 recordregion(begoff, p - 1 - start, 0);
413 result = expdest - p + 1;
414 STADJUST(-result, expdest);
419 * Expand stuff in backwards quotes.
423 expbackq(union node *cmd, int quoted, int flag)
429 char *dest = expdest;
430 struct ifsregion saveifs, *savelastp;
431 struct nodelist *saveargbackq;
433 int startloc = dest - stackblock();
434 char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
436 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
440 savelastp = ifslastp;
441 saveargbackq = argbackq;
444 p = grabstackstr(dest);
445 evalbackcmd(cmd, &in);
446 ungrabstackstr(p, dest);
448 ifslastp = savelastp;
449 argbackq = saveargbackq;
455 if (--in.nleft < 0) {
458 while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
459 TRACE(("expbackq: read returns %d\n", i));
467 if (quotes && syntax[(int)lastc] == CCTL)
468 STPUTC(CTLESC, dest);
473 /* Eat all trailing newlines */
474 for ( ; (dest - stackblock()) > startloc && *(dest-1) == '\n'; )
482 exitstatus = waitforjob(in.jp, (int *)NULL);
484 recordregion(startloc, dest - stackblock(), 0);
485 TRACE(("evalbackq: size=%d: \"%.*s\"\n",
486 (dest - stackblock()) - startloc,
487 (dest - stackblock()) - startloc,
488 stackblock() + startloc));
496 subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
503 int saveherefd = herefd;
504 struct nodelist *saveargbackq = argbackq;
509 STACKSTRNUL(expdest);
511 argbackq = saveargbackq;
512 startp = stackblock() + startloc;
514 str = stackblock() + strloc;
518 setvar(str, startp, 0);
519 amount = startp - expdest;
520 STADJUST(amount, expdest);
527 if (*p != CTLENDVAR) {
528 outfmt(&errout, "%s\n", startp);
531 error("%.*s: parameter %snot set", p - str - 1,
532 str, (varflags & VSNUL) ? "null or "
537 for (loc = startp; loc < str; loc++) {
540 if (patmatch(str, startp, varflags & VSQUOTE)) {
545 if ((varflags & VSQUOTE) && *loc == CTLESC)
551 for (loc = str - 1; loc >= startp;) {
554 if (patmatch(str, startp, varflags & VSQUOTE)) {
560 if ((varflags & VSQUOTE) && loc > startp &&
561 *(loc - 1) == CTLESC) {
562 for (q = startp; q < loc; q++)
572 for (loc = str - 1; loc >= startp;) {
573 if (patmatch(str, loc, varflags & VSQUOTE)) {
574 amount = loc - expdest;
575 STADJUST(amount, expdest);
579 if ((varflags & VSQUOTE) && loc > startp &&
580 *(loc - 1) == CTLESC) {
581 for (q = startp; q < loc; q++)
591 for (loc = startp; loc < str - 1; loc++) {
592 if (patmatch(str, loc, varflags & VSQUOTE)) {
593 amount = loc - expdest;
594 STADJUST(amount, expdest);
597 if ((varflags & VSQUOTE) && *loc == CTLESC)
608 amount = ((str - 1) - (loc - startp)) - expdest;
609 STADJUST(amount, expdest);
610 while (loc != str - 1)
617 * Expand a variable, and return a pointer to the next character in the
622 evalvar(char *p, int flag)
635 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
638 subtype = varflags & VSTYPE;
643 p = strchr(p, '=') + 1;
644 again: /* jump here after setting a variable with ${var=text} */
646 set = varisset(var, varflags & VSNUL);
649 val = bltinlookup(var, 1);
650 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
657 startloc = expdest - stackblock();
666 error("%.*s: parameter not set", p - var - 1, var);
669 if (set && subtype != VSPLUS) {
670 /* insert the value of the variable */
672 varvalue(var, varflags & VSQUOTE, flag & EXP_FULL);
673 if (subtype == VSLENGTH) {
674 varlen = expdest - stackblock() - startloc;
675 STADJUST(-varlen, expdest);
678 char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
681 if (subtype == VSLENGTH) {
688 syntax[(int)*val] == CCTL)
689 STPUTC(CTLESC, expdest);
690 STPUTC(*val++, expdest);
697 if (subtype == VSPLUS)
700 easy = ((varflags & VSQUOTE) == 0 ||
701 (*var == '@' && shellparam.nparam != 1));
706 expdest = cvtnum(varlen, expdest);
713 recordregion(startloc, expdest - stackblock(),
734 * Terminate the string and start recording the pattern
737 STPUTC('\0', expdest);
738 patloc = expdest - stackblock();
739 if (subevalvar(p, NULL, patloc, subtype,
740 startloc, varflags) == 0) {
741 int amount = (expdest - stackblock() - patloc) + 1;
742 STADJUST(-amount, expdest);
744 /* Remove any recorded regions beyond start of variable */
745 removerecordregions(startloc);
751 if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
754 * Remove any recorded regions beyond
757 removerecordregions(startloc);
770 if (subtype != VSNORMAL) { /* skip to end of alternative */
773 if ((c = *p++) == CTLESC)
775 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
777 argbackq = argbackq->next;
778 } else if (c == CTLVAR) {
779 if ((*p++ & VSTYPE) != VSNORMAL)
781 } else if (c == CTLENDVAR) {
793 * Test whether a specialized variable is set.
797 varisset(char *name, int nulok)
801 return backgndpid != -1;
802 else if (*name == '@' || *name == '*') {
803 if (*shellparam.p == NULL)
809 for (av = shellparam.p; *av; av++)
814 } else if (is_digit(*name)) {
816 int num = atoi(name);
818 if (num > shellparam.nparam)
824 ap = shellparam.p[num - 1];
826 if (nulok && (ap == NULL || *ap == '\0'))
835 * Add the value of a specialized variable to the stack string.
839 varvalue(char *name, int quoted, int allow_split)
848 #define STRTODEST(p) \
851 syntax = quoted? DQSYNTAX : BASESYNTAX; \
853 if (syntax[(int)*p] == CCTL) \
854 STPUTC(CTLESC, expdest); \
855 STPUTC(*p++, expdest); \
859 STPUTC(*p++, expdest); \
871 num = shellparam.nparam;
876 expdest = cvtnum(num, expdest);
879 for (i = 0 ; i < NOPTS ; i++) {
881 STPUTC(optlist[i].letter, expdest);
885 if (allow_split && quoted) {
886 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
889 STPUTC('\0', expdest);
899 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
902 STPUTC(sep, expdest);
910 if (is_digit(*name)) {
912 if (num > 0 && num <= shellparam.nparam) {
913 p = shellparam.p[num - 1];
924 * Record the the fact that we have to scan this region of the
925 * string for IFS characters.
929 recordregion(int start, int end, int nulonly)
931 struct ifsregion *ifsp;
933 if (ifslastp == NULL) {
936 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
937 ifslastp->next = ifsp;
940 ifslastp->next = NULL;
941 ifslastp->begoff = start;
942 ifslastp->endoff = end;
943 ifslastp->nulonly = nulonly;
949 * Break the argument string into pieces based upon IFS and add the
950 * strings to the argument list. The regions of the string to be
951 * searched for IFS characters have been stored by recordregion.
954 ifsbreakup(char *string, struct arglist *arglist)
956 struct ifsregion *ifsp;
969 if (ifslastp != NULL) {
972 p = string + ifsp->begoff;
973 nulonly = ifsp->nulonly;
974 ifs = nulonly ? nullstr :
975 ( ifsset() ? ifsval() : " \t\n" );
977 while (p < string + ifsp->endoff) {
981 if (strchr(ifs, *p)) {
983 ifsspc = (strchr(" \t\n", *p) != NULL);
984 /* Ignore IFS whitespace at start */
985 if (q == start && ifsspc) {
991 sp = (struct strlist *)stalloc(sizeof *sp);
993 *arglist->lastp = sp;
994 arglist->lastp = &sp->next;
998 if (p >= string + ifsp->endoff) {
1004 if (strchr(ifs, *p) == NULL ) {
1007 } else if (strchr(" \t\n",*p) == NULL) {
1023 } while ((ifsp = ifsp->next) != NULL);
1024 if (*start || (!ifsspc && start > string &&
1026 sp = (struct strlist *)stalloc(sizeof *sp);
1028 *arglist->lastp = sp;
1029 arglist->lastp = &sp->next;
1032 sp = (struct strlist *)stalloc(sizeof *sp);
1034 *arglist->lastp = sp;
1035 arglist->lastp = &sp->next;
1042 * Expand shell metacharacters. At this point, the only control characters
1043 * should be escapes. The results are stored in the list exparg.
1046 STATIC char *expdir;
1050 expandmeta(struct strlist *str, int flag __unused)
1053 struct strlist **savelastp;
1056 /* TODO - EXP_REDIR */
1062 for (;;) { /* fast check for meta chars */
1063 if ((c = *p++) == '\0')
1065 if (c == '*' || c == '?' || c == '[' || c == '!')
1068 savelastp = exparg.lastp;
1070 if (expdir == NULL) {
1071 int i = strlen(str->text);
1072 expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */
1075 expmeta(expdir, str->text);
1079 if (exparg.lastp == savelastp) {
1084 *exparg.lastp = str;
1085 rmescapes(str->text);
1086 exparg.lastp = &str->next;
1088 *exparg.lastp = NULL;
1089 *savelastp = sp = expsort(*savelastp);
1090 while (sp->next != NULL)
1092 exparg.lastp = &sp->next;
1100 * Do metacharacter (i.e. *, ?, [...]) expansion.
1104 expmeta(char *enddir, char *name)
1119 for (p = name ; ; p++) {
1120 if (*p == '*' || *p == '?')
1122 else if (*p == '[') {
1124 if (*q == '!' || *q == '^')
1127 while (*q == CTLQUOTEMARK)
1131 if (*q == '/' || *q == '\0')
1138 } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) {
1140 } else if (*p == '\0')
1142 else if (*p == CTLQUOTEMARK)
1144 else if (*p == CTLESC)
1152 if (metaflag == 0) { /* we've reached the end of the file name */
1153 if (enddir != expdir)
1155 for (p = name ; ; p++) {
1156 if (*p == CTLQUOTEMARK)
1164 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1169 if (start != name) {
1172 while (*p == CTLQUOTEMARK)
1179 if (enddir == expdir) {
1181 } else if (enddir == expdir + 1 && *expdir == '/') {
1187 if ((dirp = opendir(q)) == NULL)
1189 if (enddir != expdir)
1191 if (*endname == 0) {
1199 while (*p == CTLQUOTEMARK)
1205 while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1206 if (dp->d_name[0] == '.' && ! matchdot)
1208 if (patmatch(start, dp->d_name, 0)) {
1210 scopy(dp->d_name, enddir);
1214 for (t = enddir, q = dp->d_name;
1215 (*t++ = *q++) != '\0';)
1218 expmeta(t, endname);
1229 * Add a file name to the list.
1233 addfname(char *name)
1238 p = stalloc(strlen(name) + 1);
1240 sp = (struct strlist *)stalloc(sizeof *sp);
1243 exparg.lastp = &sp->next;
1248 * Sort the results of file name expansion. It calculates the number of
1249 * strings to sort and then calls msort (short for merge sort) to do the
1253 STATIC struct strlist *
1254 expsort(struct strlist *str)
1260 for (sp = str ; sp ; sp = sp->next)
1262 return msort(str, len);
1266 STATIC struct strlist *
1267 msort(struct strlist *list, int len)
1269 struct strlist *p, *q = NULL;
1270 struct strlist **lpp;
1278 for (n = half ; --n >= 0 ; ) {
1282 q->next = NULL; /* terminate first half of list */
1283 q = msort(list, half); /* sort first half of list */
1284 p = msort(p, len - half); /* sort second half */
1287 if (strcmp(p->text, q->text) < 0) {
1290 if ((p = *lpp) == NULL) {
1297 if ((q = *lpp) == NULL) {
1309 * Returns true if the pattern matches the string.
1313 patmatch(char *pattern, char *string, int squoted)
1316 if (pattern[0] == '!' && pattern[1] == '!')
1317 return 1 - pmatch(pattern + 2, string);
1320 return pmatch(pattern, string, squoted);
1325 pmatch(char *pattern, char *string, int squoted)
1337 if (squoted && *q == CTLESC)
1345 if (squoted && *q == CTLESC)
1352 while (c == CTLQUOTEMARK || c == '*')
1354 if (c != CTLESC && c != CTLQUOTEMARK &&
1355 c != '?' && c != '*' && c != '[') {
1357 if (squoted && *q == CTLESC &&
1362 if (squoted && *q == CTLESC)
1368 if (pmatch(p, q, squoted))
1370 if (squoted && *q == CTLESC)
1372 } while (*q++ != '\0');
1380 if (*endp == '!' || *endp == '^')
1383 while (*endp == CTLQUOTEMARK)
1386 goto dft; /* no matching ] */
1387 if (*endp == CTLESC)
1393 if (*p == '!' || *p == '^') {
1399 if (squoted && chr == CTLESC)
1405 if (c == CTLQUOTEMARK)
1409 if (*p == '-' && p[1] != ']') {
1411 while (*p == CTLQUOTEMARK)
1415 if ( collate_range_cmp(chr, c) >= 0
1416 && collate_range_cmp(chr, *p) <= 0
1424 } while ((c = *p++) != ']');
1425 if (found == invert)
1430 if (squoted && *q == CTLESC)
1446 * Remove any CTLESC characters from a string.
1450 rmescapes(char *str)
1455 while (*p != CTLESC && *p != CTLQUOTEMARK) {
1461 if (*p == CTLQUOTEMARK) {
1475 * See if a pattern matches in a case statement.
1479 casematch(union node *pattern, char *val)
1481 struct stackmark smark;
1485 setstackmark(&smark);
1486 argbackq = pattern->narg.backquote;
1487 STARTSTACKSTR(expdest);
1489 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
1490 STPUTC('\0', expdest);
1491 p = grabstackstr(expdest);
1492 result = patmatch(p, val, 0);
1493 popstackmark(&smark);
1502 cvtnum(int num, char *buf)
1506 char *p = temp + 31;
1511 *--p = num % 10 + '0';
1512 } while ((num /= 10) != 0);