1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 1997-2005 5 * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Kenneth Almquist. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifndef lint 36 #if 0 37 static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95"; 38 #endif 39 #endif /* not lint */ 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 #include <sys/types.h> 44 #include <sys/time.h> 45 #include <sys/stat.h> 46 #include <dirent.h> 47 #include <errno.h> 48 #include <inttypes.h> 49 #include <limits.h> 50 #include <pwd.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <unistd.h> 55 #include <wchar.h> 56 #include <wctype.h> 57 58 /* 59 * Routines to expand arguments to commands. We have to deal with 60 * backquotes, shell variables, and file metacharacters. 61 */ 62 63 #include "shell.h" 64 #include "main.h" 65 #include "nodes.h" 66 #include "eval.h" 67 #include "expand.h" 68 #include "syntax.h" 69 #include "parser.h" 70 #include "jobs.h" 71 #include "options.h" 72 #include "var.h" 73 #include "input.h" 74 #include "output.h" 75 #include "memalloc.h" 76 #include "error.h" 77 #include "mystring.h" 78 #include "arith.h" 79 #include "show.h" 80 #include "builtins.h" 81 82 /* 83 * Structure specifying which parts of the string should be searched 84 * for IFS characters. 85 */ 86 87 struct ifsregion { 88 struct ifsregion *next; /* next region in list */ 89 int begoff; /* offset of start of region */ 90 int endoff; /* offset of end of region */ 91 int inquotes; /* search for nul bytes only */ 92 }; 93 94 95 static char *expdest; /* output of current string */ 96 static struct nodelist *argbackq; /* list of back quote expressions */ 97 static struct ifsregion ifsfirst; /* first struct in list of ifs regions */ 98 static struct ifsregion *ifslastp; /* last struct in list */ 99 static struct arglist exparg; /* holds expanded arg list */ 100 101 static void argstr(char *, int); 102 static char *exptilde(char *, int); 103 static void expbackq(union node *, int, int); 104 static int subevalvar(char *, char *, int, int, int, int, int); 105 static char *evalvar(char *, int); 106 static int varisset(char *, int); 107 static void varvalue(char *, int, int, int); 108 static void recordregion(int, int, int); 109 static void removerecordregions(int); 110 static void ifsbreakup(char *, struct arglist *); 111 static void expandmeta(struct strlist *, int); 112 static void expmeta(char *, char *); 113 static void addfname(char *); 114 static struct strlist *expsort(struct strlist *); 115 static struct strlist *msort(struct strlist *, int); 116 static char *cvtnum(int, char *); 117 static int collate_range_cmp(wchar_t, wchar_t); 118 119 static int 120 collate_range_cmp(wchar_t c1, wchar_t c2) 121 { 122 static wchar_t s1[2], s2[2]; 123 124 s1[0] = c1; 125 s2[0] = c2; 126 return (wcscoll(s1, s2)); 127 } 128 129 /* 130 * Expand shell variables and backquotes inside a here document. 131 * union node *arg the document 132 * int fd; where to write the expanded version 133 */ 134 135 void 136 expandhere(union node *arg, int fd) 137 { 138 expandarg(arg, (struct arglist *)NULL, 0); 139 xwrite(fd, stackblock(), expdest - stackblock()); 140 } 141 142 static char * 143 stputs_quotes(const char *data, const char *syntax, char *p) 144 { 145 while (*data) { 146 CHECKSTRSPACE(2, p); 147 if (syntax[(int)*data] == CCTL) 148 USTPUTC(CTLESC, p); 149 USTPUTC(*data++, p); 150 } 151 return (p); 152 } 153 #define STPUTS_QUOTES(data, syntax, p) p = stputs_quotes((data), syntax, p) 154 155 /* 156 * Perform expansions on an argument, placing the resulting list of arguments 157 * in arglist. Parameter expansion, command substitution and arithmetic 158 * expansion are always performed; additional expansions can be requested 159 * via flag (EXP_*). 160 * The result is left in the stack string. 161 * When arglist is NULL, perform here document expansion. 162 * 163 * Caution: this function uses global state and is not reentrant. 164 * However, a new invocation after an interrupted invocation is safe 165 * and will reset the global state for the new call. 166 */ 167 void 168 expandarg(union node *arg, struct arglist *arglist, int flag) 169 { 170 struct strlist *sp; 171 char *p; 172 173 argbackq = arg->narg.backquote; 174 STARTSTACKSTR(expdest); 175 ifsfirst.next = NULL; 176 ifslastp = NULL; 177 argstr(arg->narg.text, flag); 178 if (arglist == NULL) { 179 STACKSTRNUL(expdest); 180 return; /* here document expanded */ 181 } 182 STPUTC('\0', expdest); 183 p = grabstackstr(expdest); 184 exparg.lastp = &exparg.list; 185 /* 186 * TODO - EXP_REDIR 187 */ 188 if (flag & EXP_FULL) { 189 ifsbreakup(p, &exparg); 190 *exparg.lastp = NULL; 191 exparg.lastp = &exparg.list; 192 expandmeta(exparg.list, flag); 193 } else { 194 if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ 195 rmescapes(p); 196 sp = (struct strlist *)stalloc(sizeof (struct strlist)); 197 sp->text = p; 198 *exparg.lastp = sp; 199 exparg.lastp = &sp->next; 200 } 201 while (ifsfirst.next != NULL) { 202 struct ifsregion *ifsp; 203 INTOFF; 204 ifsp = ifsfirst.next->next; 205 ckfree(ifsfirst.next); 206 ifsfirst.next = ifsp; 207 INTON; 208 } 209 *exparg.lastp = NULL; 210 if (exparg.list) { 211 *arglist->lastp = exparg.list; 212 arglist->lastp = exparg.lastp; 213 } 214 } 215 216 217 218 /* 219 * Perform parameter expansion, command substitution and arithmetic 220 * expansion, and tilde expansion if requested via EXP_TILDE/EXP_VARTILDE. 221 * Processing ends at a CTLENDVAR character as well as '\0'. 222 * This is used to expand word in ${var+word} etc. 223 * If EXP_FULL, EXP_CASE or EXP_REDIR are set, keep and/or generate CTLESC 224 * characters to allow for further processing. 225 * If EXP_FULL is set, also preserve CTLQUOTEMARK characters. 226 */ 227 static void 228 argstr(char *p, int flag) 229 { 230 char c; 231 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */ 232 int firsteq = 1; 233 int split_lit; 234 int lit_quoted; 235 236 split_lit = flag & EXP_SPLIT_LIT; 237 lit_quoted = flag & EXP_LIT_QUOTED; 238 flag &= ~(EXP_SPLIT_LIT | EXP_LIT_QUOTED); 239 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE))) 240 p = exptilde(p, flag); 241 for (;;) { 242 CHECKSTRSPACE(2, expdest); 243 switch (c = *p++) { 244 case '\0': 245 case CTLENDVAR: 246 goto breakloop; 247 case CTLQUOTEMARK: 248 lit_quoted = 1; 249 /* "$@" syntax adherence hack */ 250 if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=') 251 break; 252 if ((flag & EXP_FULL) != 0) 253 USTPUTC(c, expdest); 254 break; 255 case CTLQUOTEEND: 256 lit_quoted = 0; 257 break; 258 case CTLESC: 259 if (quotes) 260 USTPUTC(c, expdest); 261 c = *p++; 262 USTPUTC(c, expdest); 263 if (split_lit && !lit_quoted) 264 recordregion(expdest - stackblock() - 265 (quotes ? 2 : 1), 266 expdest - stackblock(), 0); 267 break; 268 case CTLVAR: 269 p = evalvar(p, flag); 270 break; 271 case CTLBACKQ: 272 case CTLBACKQ|CTLQUOTE: 273 expbackq(argbackq->n, c & CTLQUOTE, flag); 274 argbackq = argbackq->next; 275 break; 276 case CTLENDARI: 277 expari(flag); 278 break; 279 case ':': 280 case '=': 281 /* 282 * sort of a hack - expand tildes in variable 283 * assignments (after the first '=' and after ':'s). 284 */ 285 USTPUTC(c, expdest); 286 if (split_lit && !lit_quoted) 287 recordregion(expdest - stackblock() - 1, 288 expdest - stackblock(), 0); 289 if (flag & EXP_VARTILDE && *p == '~' && 290 (c != '=' || firsteq)) { 291 if (c == '=') 292 firsteq = 0; 293 p = exptilde(p, flag); 294 } 295 break; 296 default: 297 USTPUTC(c, expdest); 298 if (split_lit && !lit_quoted) 299 recordregion(expdest - stackblock() - 1, 300 expdest - stackblock(), 0); 301 } 302 } 303 breakloop:; 304 } 305 306 /* 307 * Perform tilde expansion, placing the result in the stack string and 308 * returning the next position in the input string to process. 309 */ 310 static char * 311 exptilde(char *p, int flag) 312 { 313 char c, *startp = p; 314 struct passwd *pw; 315 char *home; 316 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); 317 318 while ((c = *p) != '\0') { 319 switch(c) { 320 case CTLESC: /* This means CTL* are always considered quoted. */ 321 case CTLVAR: 322 case CTLBACKQ: 323 case CTLBACKQ | CTLQUOTE: 324 case CTLARI: 325 case CTLENDARI: 326 case CTLQUOTEMARK: 327 return (startp); 328 case ':': 329 if (flag & EXP_VARTILDE) 330 goto done; 331 break; 332 case '/': 333 case CTLENDVAR: 334 goto done; 335 } 336 p++; 337 } 338 done: 339 *p = '\0'; 340 if (*(startp+1) == '\0') { 341 if ((home = lookupvar("HOME")) == NULL) 342 goto lose; 343 } else { 344 if ((pw = getpwnam(startp+1)) == NULL) 345 goto lose; 346 home = pw->pw_dir; 347 } 348 if (*home == '\0') 349 goto lose; 350 *p = c; 351 if (quotes) 352 STPUTS_QUOTES(home, SQSYNTAX, expdest); 353 else 354 STPUTS(home, expdest); 355 return (p); 356 lose: 357 *p = c; 358 return (startp); 359 } 360 361 362 static void 363 removerecordregions(int endoff) 364 { 365 if (ifslastp == NULL) 366 return; 367 368 if (ifsfirst.endoff > endoff) { 369 while (ifsfirst.next != NULL) { 370 struct ifsregion *ifsp; 371 INTOFF; 372 ifsp = ifsfirst.next->next; 373 ckfree(ifsfirst.next); 374 ifsfirst.next = ifsp; 375 INTON; 376 } 377 if (ifsfirst.begoff > endoff) 378 ifslastp = NULL; 379 else { 380 ifslastp = &ifsfirst; 381 ifsfirst.endoff = endoff; 382 } 383 return; 384 } 385 386 ifslastp = &ifsfirst; 387 while (ifslastp->next && ifslastp->next->begoff < endoff) 388 ifslastp=ifslastp->next; 389 while (ifslastp->next != NULL) { 390 struct ifsregion *ifsp; 391 INTOFF; 392 ifsp = ifslastp->next->next; 393 ckfree(ifslastp->next); 394 ifslastp->next = ifsp; 395 INTON; 396 } 397 if (ifslastp->endoff > endoff) 398 ifslastp->endoff = endoff; 399 } 400 401 /* 402 * Expand arithmetic expression. Backup to start of expression, 403 * evaluate, place result in (backed up) result, adjust string position. 404 */ 405 void 406 expari(int flag) 407 { 408 char *p, *q, *start; 409 arith_t result; 410 int begoff; 411 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); 412 int quoted; 413 414 /* 415 * This routine is slightly over-complicated for 416 * efficiency. First we make sure there is 417 * enough space for the result, which may be bigger 418 * than the expression. Next we 419 * scan backwards looking for the start of arithmetic. If the 420 * next previous character is a CTLESC character, then we 421 * have to rescan starting from the beginning since CTLESC 422 * characters have to be processed left to right. 423 */ 424 CHECKSTRSPACE(DIGITS(result) - 2, expdest); 425 USTPUTC('\0', expdest); 426 start = stackblock(); 427 p = expdest - 2; 428 while (p >= start && *p != CTLARI) 429 --p; 430 if (p < start || *p != CTLARI) 431 error("missing CTLARI (shouldn't happen)"); 432 if (p > start && *(p - 1) == CTLESC) 433 for (p = start; *p != CTLARI; p++) 434 if (*p == CTLESC) 435 p++; 436 437 if (p[1] == '"') 438 quoted=1; 439 else 440 quoted=0; 441 begoff = p - start; 442 removerecordregions(begoff); 443 if (quotes) 444 rmescapes(p+2); 445 q = grabstackstr(expdest); 446 result = arith(p+2); 447 ungrabstackstr(q, expdest); 448 fmtstr(p, DIGITS(result), ARITH_FORMAT_STR, result); 449 while (*p++) 450 ; 451 if (quoted == 0) 452 recordregion(begoff, p - 1 - start, 0); 453 result = expdest - p + 1; 454 STADJUST(-result, expdest); 455 } 456 457 458 /* 459 * Perform command substitution. 460 */ 461 static void 462 expbackq(union node *cmd, int quoted, int flag) 463 { 464 struct backcmd in; 465 int i; 466 char buf[128]; 467 char *p; 468 char *dest = expdest; 469 struct ifsregion saveifs, *savelastp; 470 struct nodelist *saveargbackq; 471 char lastc; 472 int startloc = dest - stackblock(); 473 char const *syntax = quoted? DQSYNTAX : BASESYNTAX; 474 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); 475 int nnl; 476 477 INTOFF; 478 saveifs = ifsfirst; 479 savelastp = ifslastp; 480 saveargbackq = argbackq; 481 p = grabstackstr(dest); 482 evalbackcmd(cmd, &in); 483 ungrabstackstr(p, dest); 484 ifsfirst = saveifs; 485 ifslastp = savelastp; 486 argbackq = saveargbackq; 487 488 p = in.buf; 489 lastc = '\0'; 490 nnl = 0; 491 /* Don't copy trailing newlines */ 492 for (;;) { 493 if (--in.nleft < 0) { 494 if (in.fd < 0) 495 break; 496 while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR); 497 TRACE(("expbackq: read returns %d\n", i)); 498 if (i <= 0) 499 break; 500 p = buf; 501 in.nleft = i - 1; 502 } 503 lastc = *p++; 504 if (lastc != '\0') { 505 if (lastc == '\n') { 506 nnl++; 507 } else { 508 CHECKSTRSPACE(nnl + 2, dest); 509 while (nnl > 0) { 510 nnl--; 511 USTPUTC('\n', dest); 512 } 513 if (quotes && syntax[(int)lastc] == CCTL) 514 USTPUTC(CTLESC, dest); 515 USTPUTC(lastc, dest); 516 } 517 } 518 } 519 520 if (in.fd >= 0) 521 close(in.fd); 522 if (in.buf) 523 ckfree(in.buf); 524 if (in.jp) 525 exitstatus = waitforjob(in.jp, (int *)NULL); 526 if (quoted == 0) 527 recordregion(startloc, dest - stackblock(), 0); 528 TRACE(("expbackq: size=%td: \"%.*s\"\n", 529 ((dest - stackblock()) - startloc), 530 (int)((dest - stackblock()) - startloc), 531 stackblock() + startloc)); 532 expdest = dest; 533 INTON; 534 } 535 536 537 538 static int 539 subevalvar(char *p, char *str, int strloc, int subtype, int startloc, 540 int varflags, int quotes) 541 { 542 char *startp; 543 char *loc = NULL; 544 char *q; 545 int c = 0; 546 struct nodelist *saveargbackq = argbackq; 547 int amount; 548 549 argstr(p, (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX || 550 subtype == VSTRIMRIGHT || subtype == VSTRIMRIGHTMAX ? 551 EXP_CASE : 0) | EXP_TILDE); 552 STACKSTRNUL(expdest); 553 argbackq = saveargbackq; 554 startp = stackblock() + startloc; 555 if (str == NULL) 556 str = stackblock() + strloc; 557 558 switch (subtype) { 559 case VSASSIGN: 560 setvar(str, startp, 0); 561 amount = startp - expdest; 562 STADJUST(amount, expdest); 563 varflags &= ~VSNUL; 564 return 1; 565 566 case VSQUESTION: 567 if (*p != CTLENDVAR) { 568 outfmt(out2, "%s\n", startp); 569 error((char *)NULL); 570 } 571 error("%.*s: parameter %snot set", (int)(p - str - 1), 572 str, (varflags & VSNUL) ? "null or " 573 : nullstr); 574 return 0; 575 576 case VSTRIMLEFT: 577 for (loc = startp; loc < str; loc++) { 578 c = *loc; 579 *loc = '\0'; 580 if (patmatch(str, startp, quotes)) { 581 *loc = c; 582 goto recordleft; 583 } 584 *loc = c; 585 if (quotes && *loc == CTLESC) 586 loc++; 587 } 588 return 0; 589 590 case VSTRIMLEFTMAX: 591 for (loc = str - 1; loc >= startp;) { 592 c = *loc; 593 *loc = '\0'; 594 if (patmatch(str, startp, quotes)) { 595 *loc = c; 596 goto recordleft; 597 } 598 *loc = c; 599 loc--; 600 if (quotes && loc > startp && *(loc - 1) == CTLESC) { 601 for (q = startp; q < loc; q++) 602 if (*q == CTLESC) 603 q++; 604 if (q > loc) 605 loc--; 606 } 607 } 608 return 0; 609 610 case VSTRIMRIGHT: 611 for (loc = str - 1; loc >= startp;) { 612 if (patmatch(str, loc, quotes)) { 613 amount = loc - expdest; 614 STADJUST(amount, expdest); 615 return 1; 616 } 617 loc--; 618 if (quotes && loc > startp && *(loc - 1) == CTLESC) { 619 for (q = startp; q < loc; q++) 620 if (*q == CTLESC) 621 q++; 622 if (q > loc) 623 loc--; 624 } 625 } 626 return 0; 627 628 case VSTRIMRIGHTMAX: 629 for (loc = startp; loc < str - 1; loc++) { 630 if (patmatch(str, loc, quotes)) { 631 amount = loc - expdest; 632 STADJUST(amount, expdest); 633 return 1; 634 } 635 if (quotes && *loc == CTLESC) 636 loc++; 637 } 638 return 0; 639 640 641 default: 642 abort(); 643 } 644 645 recordleft: 646 amount = ((str - 1) - (loc - startp)) - expdest; 647 STADJUST(amount, expdest); 648 while (loc != str - 1) 649 *startp++ = *loc++; 650 return 1; 651 } 652 653 654 /* 655 * Expand a variable, and return a pointer to the next character in the 656 * input string. 657 */ 658 659 static char * 660 evalvar(char *p, int flag) 661 { 662 int subtype; 663 int varflags; 664 char *var; 665 char *val; 666 int patloc; 667 int c; 668 int set; 669 int special; 670 int startloc; 671 int varlen; 672 int varlenb; 673 int easy; 674 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); 675 676 varflags = (unsigned char)*p++; 677 subtype = varflags & VSTYPE; 678 var = p; 679 special = 0; 680 if (! is_name(*p)) 681 special = 1; 682 p = strchr(p, '=') + 1; 683 again: /* jump here after setting a variable with ${var=text} */ 684 if (varflags & VSLINENO) { 685 set = 1; 686 special = 0; 687 val = var; 688 p[-1] = '\0'; /* temporarily overwrite '=' to have \0 689 terminated string */ 690 } else if (special) { 691 set = varisset(var, varflags & VSNUL); 692 val = NULL; 693 } else { 694 val = bltinlookup(var, 1); 695 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) { 696 val = NULL; 697 set = 0; 698 } else 699 set = 1; 700 } 701 varlen = 0; 702 startloc = expdest - stackblock(); 703 if (!set && uflag && *var != '@' && *var != '*') { 704 switch (subtype) { 705 case VSNORMAL: 706 case VSTRIMLEFT: 707 case VSTRIMLEFTMAX: 708 case VSTRIMRIGHT: 709 case VSTRIMRIGHTMAX: 710 case VSLENGTH: 711 error("%.*s: parameter not set", (int)(p - var - 1), 712 var); 713 } 714 } 715 if (set && subtype != VSPLUS) { 716 /* insert the value of the variable */ 717 if (special) { 718 varvalue(var, varflags & VSQUOTE, subtype, flag); 719 if (subtype == VSLENGTH) { 720 varlenb = expdest - stackblock() - startloc; 721 varlen = varlenb; 722 if (localeisutf8) { 723 val = stackblock() + startloc; 724 for (;val != expdest; val++) 725 if ((*val & 0xC0) == 0x80) 726 varlen--; 727 } 728 STADJUST(-varlenb, expdest); 729 } 730 } else { 731 char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX 732 : BASESYNTAX; 733 734 if (subtype == VSLENGTH) { 735 for (;*val; val++) 736 if (!localeisutf8 || 737 (*val & 0xC0) != 0x80) 738 varlen++; 739 } 740 else { 741 if (quotes) 742 STPUTS_QUOTES(val, syntax, expdest); 743 else 744 STPUTS(val, expdest); 745 746 } 747 } 748 } 749 750 if (subtype == VSPLUS) 751 set = ! set; 752 753 easy = ((varflags & VSQUOTE) == 0 || 754 (*var == '@' && shellparam.nparam != 1)); 755 756 757 switch (subtype) { 758 case VSLENGTH: 759 expdest = cvtnum(varlen, expdest); 760 goto record; 761 762 case VSNORMAL: 763 if (!easy) 764 break; 765 record: 766 recordregion(startloc, expdest - stackblock(), 767 varflags & VSQUOTE || (ifsset() && ifsval()[0] == '\0' && 768 (*var == '@' || *var == '*'))); 769 break; 770 771 case VSPLUS: 772 case VSMINUS: 773 if (!set) { 774 argstr(p, flag | (flag & EXP_FULL ? EXP_SPLIT_LIT : 0) | 775 (varflags & VSQUOTE ? EXP_LIT_QUOTED : 0)); 776 break; 777 } 778 if (easy) 779 goto record; 780 break; 781 782 case VSTRIMLEFT: 783 case VSTRIMLEFTMAX: 784 case VSTRIMRIGHT: 785 case VSTRIMRIGHTMAX: 786 if (!set) 787 break; 788 /* 789 * Terminate the string and start recording the pattern 790 * right after it 791 */ 792 STPUTC('\0', expdest); 793 patloc = expdest - stackblock(); 794 if (subevalvar(p, NULL, patloc, subtype, 795 startloc, varflags, quotes) == 0) { 796 int amount = (expdest - stackblock() - patloc) + 1; 797 STADJUST(-amount, expdest); 798 } 799 /* Remove any recorded regions beyond start of variable */ 800 removerecordregions(startloc); 801 goto record; 802 803 case VSASSIGN: 804 case VSQUESTION: 805 if (!set) { 806 if (subevalvar(p, var, 0, subtype, startloc, varflags, 807 quotes)) { 808 varflags &= ~VSNUL; 809 /* 810 * Remove any recorded regions beyond 811 * start of variable 812 */ 813 removerecordregions(startloc); 814 goto again; 815 } 816 break; 817 } 818 if (easy) 819 goto record; 820 break; 821 822 case VSERROR: 823 c = p - var - 1; 824 error("${%.*s%s}: Bad substitution", c, var, 825 (c > 0 && *p != CTLENDVAR) ? "..." : ""); 826 827 default: 828 abort(); 829 } 830 p[-1] = '='; /* recover overwritten '=' */ 831 832 if (subtype != VSNORMAL) { /* skip to end of alternative */ 833 int nesting = 1; 834 for (;;) { 835 if ((c = *p++) == CTLESC) 836 p++; 837 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { 838 if (set) 839 argbackq = argbackq->next; 840 } else if (c == CTLVAR) { 841 if ((*p++ & VSTYPE) != VSNORMAL) 842 nesting++; 843 } else if (c == CTLENDVAR) { 844 if (--nesting == 0) 845 break; 846 } 847 } 848 } 849 return p; 850 } 851 852 853 854 /* 855 * Test whether a specialized variable is set. 856 */ 857 858 static int 859 varisset(char *name, int nulok) 860 { 861 862 if (*name == '!') 863 return backgndpidset(); 864 else if (*name == '@' || *name == '*') { 865 if (*shellparam.p == NULL) 866 return 0; 867 868 if (nulok) { 869 char **av; 870 871 for (av = shellparam.p; *av; av++) 872 if (**av != '\0') 873 return 1; 874 return 0; 875 } 876 } else if (is_digit(*name)) { 877 char *ap; 878 int num = atoi(name); 879 880 if (num > shellparam.nparam) 881 return 0; 882 883 if (num == 0) 884 ap = arg0; 885 else 886 ap = shellparam.p[num - 1]; 887 888 if (nulok && (ap == NULL || *ap == '\0')) 889 return 0; 890 } 891 return 1; 892 } 893 894 static void 895 strtodest(const char *p, int flag, int subtype, int quoted) 896 { 897 if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) 898 STPUTS_QUOTES(p, quoted ? DQSYNTAX : BASESYNTAX, expdest); 899 else 900 STPUTS(p, expdest); 901 } 902 903 /* 904 * Add the value of a specialized variable to the stack string. 905 */ 906 907 static void 908 varvalue(char *name, int quoted, int subtype, int flag) 909 { 910 int num; 911 char *p; 912 int i; 913 char sep; 914 char **ap; 915 916 switch (*name) { 917 case '$': 918 num = rootpid; 919 goto numvar; 920 case '?': 921 num = oexitstatus; 922 goto numvar; 923 case '#': 924 num = shellparam.nparam; 925 goto numvar; 926 case '!': 927 num = backgndpidval(); 928 numvar: 929 expdest = cvtnum(num, expdest); 930 break; 931 case '-': 932 for (i = 0 ; i < NOPTS ; i++) { 933 if (optlist[i].val) 934 STPUTC(optlist[i].letter, expdest); 935 } 936 break; 937 case '@': 938 if (flag & EXP_FULL && quoted) { 939 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { 940 strtodest(p, flag, subtype, quoted); 941 if (*ap) 942 STPUTC('\0', expdest); 943 } 944 break; 945 } 946 /* FALLTHROUGH */ 947 case '*': 948 if (ifsset()) 949 sep = ifsval()[0]; 950 else 951 sep = ' '; 952 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { 953 strtodest(p, flag, subtype, quoted); 954 if (!*ap) 955 break; 956 if (sep || (flag & EXP_FULL && !quoted && **ap != '\0')) 957 STPUTC(sep, expdest); 958 } 959 break; 960 case '0': 961 p = arg0; 962 strtodest(p, flag, subtype, quoted); 963 break; 964 default: 965 if (is_digit(*name)) { 966 num = atoi(name); 967 if (num > 0 && num <= shellparam.nparam) { 968 p = shellparam.p[num - 1]; 969 strtodest(p, flag, subtype, quoted); 970 } 971 } 972 break; 973 } 974 } 975 976 977 978 /* 979 * Record the fact that we have to scan this region of the 980 * string for IFS characters. 981 */ 982 983 static void 984 recordregion(int start, int end, int inquotes) 985 { 986 struct ifsregion *ifsp; 987 988 if (ifslastp == NULL) { 989 ifsp = &ifsfirst; 990 } else { 991 if (ifslastp->endoff == start 992 && ifslastp->inquotes == inquotes) { 993 /* extend previous area */ 994 ifslastp->endoff = end; 995 return; 996 } 997 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion)); 998 ifslastp->next = ifsp; 999 } 1000 ifslastp = ifsp; 1001 ifslastp->next = NULL; 1002 ifslastp->begoff = start; 1003 ifslastp->endoff = end; 1004 ifslastp->inquotes = inquotes; 1005 } 1006 1007 1008 1009 /* 1010 * Break the argument string into pieces based upon IFS and add the 1011 * strings to the argument list. The regions of the string to be 1012 * searched for IFS characters have been stored by recordregion. 1013 * CTLESC characters are preserved but have little effect in this pass 1014 * other than escaping CTL* characters. In particular, they do not escape 1015 * IFS characters: that should be done with the ifsregion mechanism. 1016 * CTLQUOTEMARK characters are used to preserve empty quoted strings. 1017 * This pass treats them as a regular character, making the string non-empty. 1018 * Later, they are removed along with the other CTL* characters. 1019 */ 1020 static void 1021 ifsbreakup(char *string, struct arglist *arglist) 1022 { 1023 struct ifsregion *ifsp; 1024 struct strlist *sp; 1025 char *start; 1026 char *p; 1027 char *q; 1028 const char *ifs; 1029 const char *ifsspc; 1030 int had_param_ch = 0; 1031 1032 start = string; 1033 1034 if (ifslastp == NULL) { 1035 /* Return entire argument, IFS doesn't apply to any of it */ 1036 sp = (struct strlist *)stalloc(sizeof *sp); 1037 sp->text = start; 1038 *arglist->lastp = sp; 1039 arglist->lastp = &sp->next; 1040 return; 1041 } 1042 1043 ifs = ifsset() ? ifsval() : " \t\n"; 1044 1045 for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) { 1046 p = string + ifsp->begoff; 1047 while (p < string + ifsp->endoff) { 1048 q = p; 1049 if (*p == CTLESC) 1050 p++; 1051 if (ifsp->inquotes) { 1052 /* Only NULs (should be from "$@") end args */ 1053 had_param_ch = 1; 1054 if (*p != 0) { 1055 p++; 1056 continue; 1057 } 1058 ifsspc = NULL; 1059 } else { 1060 if (!strchr(ifs, *p)) { 1061 had_param_ch = 1; 1062 p++; 1063 continue; 1064 } 1065 ifsspc = strchr(" \t\n", *p); 1066 1067 /* Ignore IFS whitespace at start */ 1068 if (q == start && ifsspc != NULL) { 1069 p++; 1070 start = p; 1071 continue; 1072 } 1073 had_param_ch = 0; 1074 } 1075 1076 /* Save this argument... */ 1077 *q = '\0'; 1078 sp = (struct strlist *)stalloc(sizeof *sp); 1079 sp->text = start; 1080 *arglist->lastp = sp; 1081 arglist->lastp = &sp->next; 1082 p++; 1083 1084 if (ifsspc != NULL) { 1085 /* Ignore further trailing IFS whitespace */ 1086 for (; p < string + ifsp->endoff; p++) { 1087 q = p; 1088 if (*p == CTLESC) 1089 p++; 1090 if (strchr(ifs, *p) == NULL) { 1091 p = q; 1092 break; 1093 } 1094 if (strchr(" \t\n", *p) == NULL) { 1095 p++; 1096 break; 1097 } 1098 } 1099 } 1100 start = p; 1101 } 1102 } 1103 1104 /* 1105 * Save anything left as an argument. 1106 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as 1107 * generating 2 arguments, the second of which is empty. 1108 * Some recent clarification of the Posix spec say that it 1109 * should only generate one.... 1110 */ 1111 if (had_param_ch || *start != 0) { 1112 sp = (struct strlist *)stalloc(sizeof *sp); 1113 sp->text = start; 1114 *arglist->lastp = sp; 1115 arglist->lastp = &sp->next; 1116 } 1117 } 1118 1119 1120 static char expdir[PATH_MAX]; 1121 #define expdir_end (expdir + sizeof(expdir)) 1122 1123 /* 1124 * Perform pathname generation and remove control characters. 1125 * At this point, the only control characters should be CTLESC and CTLQUOTEMARK. 1126 * The results are stored in the list exparg. 1127 */ 1128 static void 1129 expandmeta(struct strlist *str, int flag __unused) 1130 { 1131 char *p; 1132 struct strlist **savelastp; 1133 struct strlist *sp; 1134 char c; 1135 /* TODO - EXP_REDIR */ 1136 1137 while (str) { 1138 if (fflag) 1139 goto nometa; 1140 p = str->text; 1141 for (;;) { /* fast check for meta chars */ 1142 if ((c = *p++) == '\0') 1143 goto nometa; 1144 if (c == '*' || c == '?' || c == '[') 1145 break; 1146 } 1147 savelastp = exparg.lastp; 1148 INTOFF; 1149 expmeta(expdir, str->text); 1150 INTON; 1151 if (exparg.lastp == savelastp) { 1152 /* 1153 * no matches 1154 */ 1155 nometa: 1156 *exparg.lastp = str; 1157 rmescapes(str->text); 1158 exparg.lastp = &str->next; 1159 } else { 1160 *exparg.lastp = NULL; 1161 *savelastp = sp = expsort(*savelastp); 1162 while (sp->next != NULL) 1163 sp = sp->next; 1164 exparg.lastp = &sp->next; 1165 } 1166 str = str->next; 1167 } 1168 } 1169 1170 1171 /* 1172 * Do metacharacter (i.e. *, ?, [...]) expansion. 1173 */ 1174 1175 static void 1176 expmeta(char *enddir, char *name) 1177 { 1178 char *p; 1179 char *q; 1180 char *start; 1181 char *endname; 1182 int metaflag; 1183 struct stat statb; 1184 DIR *dirp; 1185 struct dirent *dp; 1186 int atend; 1187 int matchdot; 1188 int esc; 1189 1190 metaflag = 0; 1191 start = name; 1192 for (p = name; esc = 0, *p; p += esc + 1) { 1193 if (*p == '*' || *p == '?') 1194 metaflag = 1; 1195 else if (*p == '[') { 1196 q = p + 1; 1197 if (*q == '!' || *q == '^') 1198 q++; 1199 for (;;) { 1200 while (*q == CTLQUOTEMARK) 1201 q++; 1202 if (*q == CTLESC) 1203 q++; 1204 if (*q == '/' || *q == '\0') 1205 break; 1206 if (*++q == ']') { 1207 metaflag = 1; 1208 break; 1209 } 1210 } 1211 } else if (*p == '\0') 1212 break; 1213 else if (*p == CTLQUOTEMARK) 1214 continue; 1215 else { 1216 if (*p == CTLESC) 1217 esc++; 1218 if (p[esc] == '/') { 1219 if (metaflag) 1220 break; 1221 start = p + esc + 1; 1222 } 1223 } 1224 } 1225 if (metaflag == 0) { /* we've reached the end of the file name */ 1226 if (enddir != expdir) 1227 metaflag++; 1228 for (p = name ; ; p++) { 1229 if (*p == CTLQUOTEMARK) 1230 continue; 1231 if (*p == CTLESC) 1232 p++; 1233 *enddir++ = *p; 1234 if (*p == '\0') 1235 break; 1236 if (enddir == expdir_end) 1237 return; 1238 } 1239 if (metaflag == 0 || lstat(expdir, &statb) >= 0) 1240 addfname(expdir); 1241 return; 1242 } 1243 endname = p; 1244 if (start != name) { 1245 p = name; 1246 while (p < start) { 1247 while (*p == CTLQUOTEMARK) 1248 p++; 1249 if (*p == CTLESC) 1250 p++; 1251 *enddir++ = *p++; 1252 if (enddir == expdir_end) 1253 return; 1254 } 1255 } 1256 if (enddir == expdir) { 1257 p = "."; 1258 } else if (enddir == expdir + 1 && *expdir == '/') { 1259 p = "/"; 1260 } else { 1261 p = expdir; 1262 enddir[-1] = '\0'; 1263 } 1264 if ((dirp = opendir(p)) == NULL) 1265 return; 1266 if (enddir != expdir) 1267 enddir[-1] = '/'; 1268 if (*endname == 0) { 1269 atend = 1; 1270 } else { 1271 atend = 0; 1272 *endname = '\0'; 1273 endname += esc + 1; 1274 } 1275 matchdot = 0; 1276 p = start; 1277 while (*p == CTLQUOTEMARK) 1278 p++; 1279 if (*p == CTLESC) 1280 p++; 1281 if (*p == '.') 1282 matchdot++; 1283 while (! int_pending() && (dp = readdir(dirp)) != NULL) { 1284 if (dp->d_name[0] == '.' && ! matchdot) 1285 continue; 1286 if (patmatch(start, dp->d_name, 0)) { 1287 if (enddir + dp->d_namlen + 1 > expdir_end) 1288 continue; 1289 memcpy(enddir, dp->d_name, dp->d_namlen + 1); 1290 if (atend) 1291 addfname(expdir); 1292 else { 1293 if (enddir + dp->d_namlen + 2 > expdir_end) 1294 continue; 1295 enddir[dp->d_namlen] = '/'; 1296 enddir[dp->d_namlen + 1] = '\0'; 1297 expmeta(enddir + dp->d_namlen + 1, endname); 1298 } 1299 } 1300 } 1301 closedir(dirp); 1302 if (! atend) 1303 endname[-esc - 1] = esc ? CTLESC : '/'; 1304 } 1305 1306 1307 /* 1308 * Add a file name to the list. 1309 */ 1310 1311 static void 1312 addfname(char *name) 1313 { 1314 char *p; 1315 struct strlist *sp; 1316 1317 p = stalloc(strlen(name) + 1); 1318 scopy(name, p); 1319 sp = (struct strlist *)stalloc(sizeof *sp); 1320 sp->text = p; 1321 *exparg.lastp = sp; 1322 exparg.lastp = &sp->next; 1323 } 1324 1325 1326 /* 1327 * Sort the results of file name expansion. It calculates the number of 1328 * strings to sort and then calls msort (short for merge sort) to do the 1329 * work. 1330 */ 1331 1332 static struct strlist * 1333 expsort(struct strlist *str) 1334 { 1335 int len; 1336 struct strlist *sp; 1337 1338 len = 0; 1339 for (sp = str ; sp ; sp = sp->next) 1340 len++; 1341 return msort(str, len); 1342 } 1343 1344 1345 static struct strlist * 1346 msort(struct strlist *list, int len) 1347 { 1348 struct strlist *p, *q = NULL; 1349 struct strlist **lpp; 1350 int half; 1351 int n; 1352 1353 if (len <= 1) 1354 return list; 1355 half = len >> 1; 1356 p = list; 1357 for (n = half ; --n >= 0 ; ) { 1358 q = p; 1359 p = p->next; 1360 } 1361 q->next = NULL; /* terminate first half of list */ 1362 q = msort(list, half); /* sort first half of list */ 1363 p = msort(p, len - half); /* sort second half */ 1364 lpp = &list; 1365 for (;;) { 1366 if (strcmp(p->text, q->text) < 0) { 1367 *lpp = p; 1368 lpp = &p->next; 1369 if ((p = *lpp) == NULL) { 1370 *lpp = q; 1371 break; 1372 } 1373 } else { 1374 *lpp = q; 1375 lpp = &q->next; 1376 if ((q = *lpp) == NULL) { 1377 *lpp = p; 1378 break; 1379 } 1380 } 1381 } 1382 return list; 1383 } 1384 1385 1386 1387 static wchar_t 1388 get_wc(const char **p) 1389 { 1390 wchar_t c; 1391 int chrlen; 1392 1393 chrlen = mbtowc(&c, *p, 4); 1394 if (chrlen == 0) 1395 return 0; 1396 else if (chrlen == -1) 1397 c = 0; 1398 else 1399 *p += chrlen; 1400 return c; 1401 } 1402 1403 1404 /* 1405 * See if a character matches a character class, starting at the first colon 1406 * of "[:class:]". 1407 * If a valid character class is recognized, a pointer to the next character 1408 * after the final closing bracket is stored into *end, otherwise a null 1409 * pointer is stored into *end. 1410 */ 1411 static int 1412 match_charclass(const char *p, wchar_t chr, const char **end) 1413 { 1414 char name[20]; 1415 const char *nameend; 1416 wctype_t cclass; 1417 1418 *end = NULL; 1419 p++; 1420 nameend = strstr(p, ":]"); 1421 if (nameend == NULL || nameend - p >= sizeof(name) || nameend == p) 1422 return 0; 1423 memcpy(name, p, nameend - p); 1424 name[nameend - p] = '\0'; 1425 *end = nameend + 2; 1426 cclass = wctype(name); 1427 /* An unknown class matches nothing but is valid nevertheless. */ 1428 if (cclass == 0) 1429 return 0; 1430 return iswctype(chr, cclass); 1431 } 1432 1433 1434 /* 1435 * Returns true if the pattern matches the string. 1436 */ 1437 1438 int 1439 patmatch(const char *pattern, const char *string, int squoted) 1440 { 1441 const char *p, *q, *end; 1442 char c; 1443 wchar_t wc, wc2; 1444 1445 p = pattern; 1446 q = string; 1447 for (;;) { 1448 switch (c = *p++) { 1449 case '\0': 1450 goto breakloop; 1451 case CTLESC: 1452 if (squoted && *q == CTLESC) 1453 q++; 1454 if (*q++ != *p++) 1455 return 0; 1456 break; 1457 case CTLQUOTEMARK: 1458 continue; 1459 case '?': 1460 if (squoted && *q == CTLESC) 1461 q++; 1462 if (localeisutf8) 1463 wc = get_wc(&q); 1464 else 1465 wc = (unsigned char)*q++; 1466 if (wc == '\0') 1467 return 0; 1468 break; 1469 case '*': 1470 c = *p; 1471 while (c == CTLQUOTEMARK || c == '*') 1472 c = *++p; 1473 if (c != CTLESC && c != CTLQUOTEMARK && 1474 c != '?' && c != '*' && c != '[') { 1475 while (*q != c) { 1476 if (squoted && *q == CTLESC && 1477 q[1] == c) 1478 break; 1479 if (*q == '\0') 1480 return 0; 1481 if (squoted && *q == CTLESC) 1482 q++; 1483 q++; 1484 } 1485 } 1486 do { 1487 if (patmatch(p, q, squoted)) 1488 return 1; 1489 if (squoted && *q == CTLESC) 1490 q++; 1491 } while (*q++ != '\0'); 1492 return 0; 1493 case '[': { 1494 const char *endp; 1495 int invert, found; 1496 wchar_t chr; 1497 1498 endp = p; 1499 if (*endp == '!' || *endp == '^') 1500 endp++; 1501 for (;;) { 1502 while (*endp == CTLQUOTEMARK) 1503 endp++; 1504 if (*endp == '\0') 1505 goto dft; /* no matching ] */ 1506 if (*endp == CTLESC) 1507 endp++; 1508 if (*++endp == ']') 1509 break; 1510 } 1511 invert = 0; 1512 if (*p == '!' || *p == '^') { 1513 invert++; 1514 p++; 1515 } 1516 found = 0; 1517 if (squoted && *q == CTLESC) 1518 q++; 1519 if (localeisutf8) 1520 chr = get_wc(&q); 1521 else 1522 chr = (unsigned char)*q++; 1523 if (chr == '\0') 1524 return 0; 1525 c = *p++; 1526 do { 1527 if (c == CTLQUOTEMARK) 1528 continue; 1529 if (c == '[' && *p == ':') { 1530 found |= match_charclass(p, chr, &end); 1531 if (end != NULL) 1532 p = end; 1533 } 1534 if (c == CTLESC) 1535 c = *p++; 1536 if (localeisutf8 && c & 0x80) { 1537 p--; 1538 wc = get_wc(&p); 1539 if (wc == 0) /* bad utf-8 */ 1540 return 0; 1541 } else 1542 wc = (unsigned char)c; 1543 if (*p == '-' && p[1] != ']') { 1544 p++; 1545 while (*p == CTLQUOTEMARK) 1546 p++; 1547 if (*p == CTLESC) 1548 p++; 1549 if (localeisutf8) { 1550 wc2 = get_wc(&p); 1551 if (wc2 == 0) /* bad utf-8 */ 1552 return 0; 1553 } else 1554 wc2 = (unsigned char)*p++; 1555 if ( collate_range_cmp(chr, wc) >= 0 1556 && collate_range_cmp(chr, wc2) <= 0 1557 ) 1558 found = 1; 1559 } else { 1560 if (chr == wc) 1561 found = 1; 1562 } 1563 } while ((c = *p++) != ']'); 1564 if (found == invert) 1565 return 0; 1566 break; 1567 } 1568 dft: default: 1569 if (squoted && *q == CTLESC) 1570 q++; 1571 if (*q++ != c) 1572 return 0; 1573 break; 1574 } 1575 } 1576 breakloop: 1577 if (*q != '\0') 1578 return 0; 1579 return 1; 1580 } 1581 1582 1583 1584 /* 1585 * Remove any CTLESC and CTLQUOTEMARK characters from a string. 1586 */ 1587 1588 void 1589 rmescapes(char *str) 1590 { 1591 char *p, *q; 1592 1593 p = str; 1594 while (*p != CTLESC && *p != CTLQUOTEMARK && *p != CTLQUOTEEND) { 1595 if (*p++ == '\0') 1596 return; 1597 } 1598 q = p; 1599 while (*p) { 1600 if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) { 1601 p++; 1602 continue; 1603 } 1604 if (*p == CTLESC) 1605 p++; 1606 *q++ = *p++; 1607 } 1608 *q = '\0'; 1609 } 1610 1611 1612 1613 /* 1614 * See if a pattern matches in a case statement. 1615 */ 1616 1617 int 1618 casematch(union node *pattern, const char *val) 1619 { 1620 struct stackmark smark; 1621 int result; 1622 char *p; 1623 1624 setstackmark(&smark); 1625 argbackq = pattern->narg.backquote; 1626 STARTSTACKSTR(expdest); 1627 ifslastp = NULL; 1628 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE); 1629 STPUTC('\0', expdest); 1630 p = grabstackstr(expdest); 1631 result = patmatch(p, val, 0); 1632 popstackmark(&smark); 1633 return result; 1634 } 1635 1636 /* 1637 * Our own itoa(). 1638 */ 1639 1640 static char * 1641 cvtnum(int num, char *buf) 1642 { 1643 char temp[32]; 1644 int neg = num < 0; 1645 char *p = temp + 31; 1646 1647 temp[31] = '\0'; 1648 1649 do { 1650 *--p = num % 10 + '0'; 1651 } while ((num /= 10) != 0); 1652 1653 if (neg) 1654 *--p = '-'; 1655 1656 STPUTS(p, buf); 1657 return buf; 1658 } 1659 1660 /* 1661 * Do most of the work for wordexp(3). 1662 */ 1663 1664 int 1665 wordexpcmd(int argc, char **argv) 1666 { 1667 size_t len; 1668 int i; 1669 1670 out1fmt("%08x", argc - 1); 1671 for (i = 1, len = 0; i < argc; i++) 1672 len += strlen(argv[i]); 1673 out1fmt("%08x", (int)len); 1674 for (i = 1; i < argc; i++) 1675 outbin(argv[i], strlen(argv[i]) + 1, out1); 1676 return (0); 1677 } 1678