home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2271 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  46.1 KB

  1. From: netnews@netcom.UUCP (USENET Administration)
  2. Newsgroups: alt.sources
  3. Subject: Public Domain Korn Shell - Part.06 of 7
  4. Message-ID: <18611@netcom.UUCP>
  5. Date: 12 Dec 90 11:38:51 GMT
  6.  
  7. #!/bin/sh
  8. # This is part 06 of ksh-pd
  9. # ============= src/eval.c ==============
  10. if test ! -d 'src'; then
  11.     echo 'x - creating directory src'
  12.     mkdir 'src'
  13. fi
  14. if test -f 'src/eval.c' -a X"$1" != X"-c"; then
  15.     echo 'x - skipping src/eval.c (File already exists)'
  16. else
  17. echo 'x - extracting src/eval.c (Text)'
  18. sed 's/^X//' << 'SHAR_EOF' > 'src/eval.c' &&
  19. X/*
  20. X * Expansion - quoting, separation, substitution, globbing
  21. X */
  22. X
  23. Xstatic char *RCSid = "$Header: /tmp/egisin/src/RCS/eval.c,v 3.3 88/12/17 20:59:47 egisin Exp $";
  24. X
  25. X#include <stddef.h>
  26. X#include <stdio.h>
  27. X#include <string.h>
  28. X#include <errno.h>
  29. X#include <setjmp.h>
  30. X#include <unistd.h>
  31. X#include <sys/types.h>
  32. X#include <dirent.h>
  33. X#include <pwd.h>
  34. X#include "sh.h"
  35. X#include "lex.h"
  36. X#include "tree.h"
  37. X#include "table.h"
  38. X#include "expand.h"
  39. X
  40. X/*
  41. X * string expansion
  42. X *
  43. X * first pass: quoting, IFS separation, ${} and $() substitution.
  44. X * second pass: filename expansion (*?[]~).
  45. X */
  46. X
  47. X/* expansion generator state */
  48. Xtypedef struct Expand {
  49. X    /* int  type; */    /* see expand() */
  50. X    char   *str;        /* string */
  51. X    union {
  52. X        char  **strv;    /* string[] */
  53. X        FILE   *file;    /* file */
  54. X    } u;            /* source */
  55. X    short    split;        /* split "$@"*/
  56. X} Expand;
  57. X
  58. X#define    XBASE    0        /* scanning original */
  59. X#define    XSUB    1        /* expanding ${} string */
  60. X#define    XARGSEP    2        /* ifs0 between "$@" */
  61. X#define    XARG    3        /* expanding $*, $@ */
  62. X#define    XCOM    4        /* expanding $() */
  63. X
  64. Xstatic    void    expand ARGS((char *, XPtrV *, int));
  65. Xstatic    int    comsub ARGS((Expand *, char *comm));
  66. Xstatic    int    varsub ARGS((Expand *, char *name, int stype));
  67. Xstatic    void    glob ARGS((char *cp, XPtrV *wp));
  68. Xstatic    void    globit ARGS((char *ds, char *dp, char *sp, XPtrV *wp, int check));
  69. Xstatic    char   *tilde ARGS((char *wp));
  70. Xstatic    char   *trimsub ARGS((char *str, char *pat, int how));
  71. X
  72. Xint    ifs0 = ' ';        /* todo: first char of $IFS */
  73. X
  74. X/* compile and expand word */
  75. Xchar *
  76. Xsubstitute(cp, f)
  77. X    char Const *cp;
  78. X    int f;
  79. X{
  80. X    struct source *s, *sold;
  81. X
  82. X    sold = source;
  83. X    s = pushs(SWSTR);
  84. X    s->str = (char *) cp;
  85. X    source = s;
  86. X    if (yylex(ONEWORD) != LWORD)
  87. X        errorf("eval:substitute error\n");
  88. X    source = sold;
  89. X    return evalstr(yylval.cp, f);
  90. X}
  91. X
  92. X/*
  93. X * expand arg-list
  94. X */
  95. Xchar **
  96. Xeval(ap, f)
  97. X    register char **ap;
  98. X{
  99. X    XPtrV w;
  100. X
  101. X    if (*ap == NULL)
  102. X        return ap;
  103. X    XPinit(w, 32);
  104. X    XPput(w, NULL);        /* space for shell name */
  105. X    while (*ap != NULL)
  106. X        expand(*ap++, &w, f);
  107. X    XPput(w, NULL);
  108. X    return (char **) XPclose(w) + 1;
  109. X}
  110. X
  111. X/*
  112. X * expand string
  113. X */
  114. Xchar *
  115. Xevalstr(cp, f)
  116. X    register char *cp;
  117. X    int f;
  118. X{
  119. X    XPtrV w;
  120. X
  121. X    XPinit(w, 1);
  122. X    expand(cp, &w, f);
  123. X    cp = (XPsize(w) == 0) ? "" : (char*) *XPptrv(w);
  124. X    XPfree(w);
  125. X    return cp;
  126. X}
  127. X
  128. X/* for nested substitution: ${var:=$var2} */
  129. Xtypedef struct SubType {
  130. X    short    type;        /* [=+-?%#] action after expanded word */
  131. X    short    base;        /* begin position of expanded word */
  132. X    char   *name;        /* name for ${var=word} */
  133. X} SubType;
  134. X
  135. Xstatic void
  136. Xexpand(cp, wp, f)
  137. X    char *cp;        /* input word */
  138. X    register XPtrV *wp;    /* output words */
  139. X    int f;            /* DO* flags */
  140. X{
  141. X    register int c;
  142. X    register int type = XBASE; /* expansion type */
  143. X    register int quote = 0;    /* quoted */
  144. X    XString ds;        /* destination string */
  145. X    register char *dp, *sp;    /* dest., source */
  146. X    int fdo, word, combase;    /* second pass flags; have word */
  147. X    Expand x;        /* expansion variables */
  148. X    SubType subtype [10];    /* substitution type stack */
  149. X    register SubType *st = subtype + 10;
  150. X
  151. X    if (cp == NULL)
  152. X        errorf("eval:expand(NULL)\n");
  153. X    if (flag[FNOGLOB])
  154. X        f &= ~ DOGLOB;
  155. X
  156. X    Xinit(ds, dp, 128);    /* init dest. string */
  157. X    type = XBASE;
  158. X    sp = cp;
  159. X    fdo = 0;
  160. X    word = !(f&DOBLANK);
  161. X
  162. X    while (1) {
  163. X        Xcheck(ds, dp);
  164. X
  165. X        switch (type) {
  166. X          case XBASE:    /* original prefixed string */
  167. X            c = *sp++;
  168. X            switch (c) {
  169. X              case EOS:
  170. X                c = 0;
  171. X                break;
  172. X              case CHAR:
  173. X                c = *sp++;
  174. X                break;
  175. X              case QCHAR:
  176. X                quote |= 2; /* temporary quote */
  177. X                c = *sp++;
  178. X                break;
  179. X              case OQUOTE:
  180. X                word = quote = 1;
  181. X                continue;
  182. X              case CQUOTE:
  183. X                quote = 0;
  184. X                continue;
  185. X              case COMSUB:
  186. X                type = comsub(&x, sp);
  187. X                sp = strchr(sp, 0) + 1;
  188. X                combase = Xsavepos(ds, dp);
  189. X                continue;
  190. X              case OSUBST: /* ${var{:}[=+-?]word} */
  191. X                cp = sp;         /* variable */
  192. X                sp = strchr(sp, 0) + 1;    /* skip variable */
  193. X                c = (*sp == CSUBST) ? 0 : *sp++;
  194. X                if ((c&0x7F) == '#' || (c&0x7F) == '%')
  195. X                    f |= DOPAT;
  196. X                type = varsub(&x, cp, c);
  197. X                if (type == XBASE) {    /* expand? */
  198. X                    if (st == subtype)
  199. X                        errorf("ridiculous ${} nesting\n");
  200. X                    --st;
  201. X                    st->type = c;
  202. X                    st->base = Xsavepos(ds, dp);
  203. X                    st->name = cp;
  204. X                } else
  205. X                    /* todo: nested OSUBST/CSUBST */
  206. X                    sp = wdscan(sp, CSUBST); /* skip word */
  207. X                continue;
  208. X              case CSUBST: /* only get here if expanding word */
  209. X                *dp = 0;
  210. X                if (f&DOGLOB)
  211. X                    f &= ~DOPAT;
  212. X                switch (st->type&0x7F) {
  213. X                  case '#':
  214. X                  case '%':
  215. X                    *dp = 0;
  216. X                    dp = Xrestpos(ds, dp, st->base);
  217. X                    x.str = trimsub(x.str, dp, st->type);
  218. X                    type = XSUB;
  219. X                    continue;
  220. X                  case '=':
  221. X#if 0
  222. X                    if ((x.u.vp->flag&RDONLY))
  223. X                        errorf("cannot set readonly %s\n", cp);
  224. X#endif
  225. X                    setstr(global(st->name), Xrestpos(ds, dp, st->base));
  226. X                    break;
  227. X                  case '?':
  228. X                    if (dp == Xrestpos(ds, dp, st->base))
  229. X                        errorf("missing value for %s\n", cp);
  230. X                    else
  231. X                        errorf("%s\n", Xrestpos(ds, dp, st->base));
  232. X                }
  233. X                st++;
  234. X                type = XBASE;
  235. X                continue;
  236. X            }
  237. X            break;
  238. X
  239. X          case XSUB:
  240. X            if ((c = *x.str++) == 0) {
  241. X                type = XBASE;
  242. X                continue;
  243. X            }
  244. X            break;
  245. X
  246. X          case XARGSEP:
  247. X            type = XARG;
  248. X            quote = 1;
  249. X          case XARG:
  250. X            if ((c = *x.str++) == 0) {
  251. X                if ((x.str = *x.u.strv++) == NULL) {
  252. X                    type = XBASE;
  253. X                    continue;
  254. X                } else if (quote && x.split) {
  255. X                    /* terminate word for "$@" */
  256. X                    type = XARGSEP;
  257. X                    quote = 0;
  258. X                }
  259. X                c = ifs0;
  260. X            }
  261. X            break;
  262. X
  263. X          case XCOM:
  264. X            c = getc(x.u.file);
  265. X            if (quote) {
  266. X                if (c == EOF) {
  267. X                    cp = Xrestpos(ds, sp, combase);
  268. X                    for (dp--; dp >= cp && *dp == '\n'; dp--)
  269. X                        ;
  270. X                    dp++;
  271. X                    fclose(x.u.file);
  272. X                    if (x.split)
  273. X                        waitlast();
  274. X                    type = XBASE;
  275. X                    continue;
  276. X                }
  277. X            } else {    /* this part is probably redundant */
  278. X                if (c == EOF || c == '\n') {
  279. X                    while ((c = getc(x.u.file)) == '\n')
  280. X                        ;
  281. X                    if (c == EOF) {
  282. X                        fclose(x.u.file);
  283. X                        if (x.split)
  284. X                            waitlast();
  285. X                        type = XBASE;
  286. X                        continue;
  287. X                    }
  288. X                    ungetc(c, x.u.file);
  289. X                    c = ifs0;
  290. X                }
  291. X            }
  292. X            break;
  293. X        }
  294. X
  295. X        /* check for end of word or IFS separation */
  296. X        if (c == 0 || !quote && (f&DOBLANK) && ctype(c, C_IFS)) {
  297. X            if (word) {
  298. X                *dp++ = 0;
  299. X                cp = Xclose(ds, dp);
  300. X                if (fdo&DOTILDE)
  301. X                    cp = tilde(cp);
  302. X                if (fdo&DOGLOB)
  303. X                    glob(cp, wp);
  304. X                else
  305. X                    {XPput(*wp, cp);}
  306. X                fdo = word = 0;
  307. X                if (c != 0)
  308. X                    Xinit(ds, dp, 128);
  309. X            } else
  310. X                ; /* ignore IFS */
  311. X            if (c == 0)
  312. X                return;
  313. X        } else {
  314. X            /* mark any special second pass chars */
  315. X            if (!quote)
  316. X                switch (c) {
  317. X                  case '*':
  318. X                  case '?':
  319. X                  case '[':
  320. X                    if (f&(DOPAT|DOGLOB)) {
  321. X                        fdo |= (f&DOGLOB);
  322. X                        *dp++ = MAGIC;
  323. X                    }
  324. X                    break;
  325. X                  case '~':
  326. X                    if ((f&DOTILDE) && dp == Xstring(ds, dp) ||
  327. X                        !(f&DOBLANK) && 
  328. X                        (dp[-1] == '=' || dp[-1] == ':')) {
  329. X                        fdo |= DOTILDE;
  330. X                        *dp++ = MAGIC;
  331. X                    }
  332. X                    break;
  333. X                }
  334. X            else
  335. X                quote &= ~2; /* undo temporary */
  336. X
  337. X            word = 1;
  338. X            *dp++ = c; /* save output char */
  339. X        }
  340. X    }
  341. X}
  342. X
  343. X/*
  344. X * Prepare to generate the string returned by ${} substitution.
  345. X */
  346. Xstatic int
  347. Xvarsub(xp, sp, stype)
  348. X    register Expand *xp;
  349. X    register char *sp;
  350. X    int stype;
  351. X{
  352. X    register int c;
  353. X    int type;
  354. X
  355. X    /* ${#var}, string length or argc */
  356. X    if (sp[0] == '#' && (c = sp[1]) != 0) {
  357. X        c = (c == '*' || c == '@') ? e.loc->argc :
  358. X            strlen(strval(global(sp+1)));
  359. X        xp->str = strsave(ulton((unsigned long)c, 10), ATEMP);
  360. X        return XSUB;
  361. X    }
  362. X
  363. X    c = sp[0];
  364. X    if (c == '*' || c == '@') {
  365. X        if (e.loc->argc == 0) {
  366. X            xp->str = null;
  367. X            type = XSUB;
  368. X        } else {
  369. X            xp->u.strv = e.loc->argv + 1;
  370. X            xp->str = *xp->u.strv++;
  371. X            xp->split = c == '@'; /* $@ */
  372. X            type = XARG;
  373. X        }
  374. X    } else {
  375. X        xp->str = strval(global(sp));
  376. X        type = XSUB;
  377. X    }
  378. X
  379. X    c = stype&0x7F;
  380. X    /* test the compiler's code generator */
  381. X    if (c == '%' || c == '#' ||
  382. X        (((stype&0x80) ? *xp->str=='\0' : xp->str==null) ? /* undef? */
  383. X         c == '=' || c == '-' || c == '?' : c == '+'))
  384. X        type = XBASE;    /* expand word instead of variable value */
  385. X    if (type != XBASE && flag[FNOUNSET] && xp->str == null)
  386. X        errorf("%s: unset variable\n", sp);
  387. X    return type;
  388. X}
  389. X
  390. X/*
  391. X * Run the command in $(...) and read its output.
  392. X */
  393. Xstatic int
  394. Xcomsub(xp, cp)
  395. X    register Expand *xp;
  396. X    char *cp;
  397. X{
  398. X    Source *s;
  399. X    register struct op *t;
  400. X    FILE *fi;
  401. X
  402. X    s = pushs(SSTRING);
  403. X    s->str = cp;
  404. X    t = compile(s);
  405. X
  406. X    if (t != NULL && t->type == TCOM && /* $(<file) */
  407. X        *t->args == NULL && *t->vars == NULL && t->ioact != NULL) {
  408. X        register struct ioword *io = *t->ioact;
  409. X
  410. X        if ((io->flag&IOTYPE) != IOREAD)
  411. X            errorf("funny $() command\n");
  412. X        fi = fopen(evalstr(io->name, DOTILDE), "r");
  413. X        if (fi != NULL)
  414. X            fileno(fi) = savefd(fileno(fi));
  415. X        xp->split = 0;    /* no waitlast() */
  416. X    } else {
  417. X        int ofd1, pv[2];
  418. X        openpipe(pv);
  419. X        fi = fdopen(pv[0], "r");
  420. X        ofd1 = savefd(1);
  421. X        dup2(pv[1], 1);
  422. X        close(pv[1]);
  423. X#if 0
  424. X        exchild(t, XXCOM|XPIPEO);
  425. X#else
  426. X        execute(t, XFORK|XXCOM|XPIPEO);
  427. X#endif
  428. X        dup2(ofd1, 1);
  429. X        xp->split = 1;    /* waitlast() */
  430. X    }    
  431. X
  432. X    if (fi == NULL)
  433. X        errorf("cannot open $() input\n");
  434. X    setvbuf(fi, (char *)NULL, _IOFBF, BUFSIZ);
  435. X    xp->u.file = fi;
  436. X    return XCOM;
  437. X}
  438. X
  439. X/*
  440. X * perform #pattern and %pattern substitution in ${}
  441. X */
  442. X
  443. Xstatic char *
  444. Xtrimsub(str, pat, how)
  445. X    register char *str;
  446. X    char *pat;
  447. X    int how;
  448. X{
  449. X    register char *end = strchr(str, 0);
  450. X    register char *p, c;
  451. X
  452. X    switch (how) {
  453. X      case '#':        /* shortest at begin */
  454. X        for (p = str; p <= end; p++) {
  455. X            c = *p; *p = '\0';
  456. X            if (gmatch(str, pat)) {
  457. X                *p = c;
  458. X                return p;
  459. X            }
  460. X            *p = c;
  461. X        }
  462. X        break;
  463. X    case '#'|0x80:        /* longest match at begin */
  464. X        for (p = end; p >= str; p--) {
  465. X            c = *p; *p = '\0';
  466. X            if (gmatch(str, pat)) {
  467. X                *p = c;
  468. X                return p;
  469. X            }
  470. X            *p = c;
  471. X        }
  472. X        break;
  473. X      case '%':        /* shortest match at end */
  474. X        for (p = end; p >= str; p--) {
  475. X            if (gmatch(p, pat)) {
  476. X                *p = '\0';
  477. X                return str;
  478. X            }
  479. X        }
  480. X        break;
  481. X      case '%'|0x80:    /* longest match at end */
  482. X        for (p = str; p <= end; p++) {
  483. X            if (gmatch(p, pat)) {
  484. X                *p = '\0';
  485. X                return str;
  486. X            }
  487. X        }
  488. X        break;
  489. X    }
  490. X
  491. X    return str;        /* no match, return string */
  492. X}
  493. X
  494. X/*
  495. X * glob
  496. X * Name derived from V6's /etc/glob, the program that expanded filenames.
  497. X */
  498. X
  499. Xstatic    char   *debunk();
  500. X
  501. Xstatic void 
  502. Xglob(cp, wp)
  503. X    char *cp;
  504. X    register XPtrV *wp;
  505. X{
  506. X    char path [PATH];
  507. X    register char *sp = cp;
  508. X    int oldsize;
  509. X
  510. X    oldsize = XPsize(*wp);
  511. X    globit(path, path, sp, wp, 0);
  512. X
  513. X    if (XPsize(*wp) == oldsize)
  514. X        {XPput(*wp, debunk(cp));}
  515. X    else
  516. X        qsortp(XPptrv(*wp) + oldsize, (size_t)(XPsize(*wp) - oldsize), xstrcmp);
  517. X}
  518. X
  519. Xstatic void
  520. Xglobit(ds, dp, sp, wp, check)
  521. X    char *ds;        /* dest path */
  522. X    char *dp;        /* dest end */
  523. X    char *sp;        /* source path */
  524. X    register XPtrV *wp;    /* output list */
  525. X    int check;        /* check dest existence */
  526. X{
  527. X    register char *np;    /* next source component */
  528. X    register char *tsp, *tdp;
  529. X
  530. X    if (sp == NULL) {    /* end of source path */
  531. X        if (check && eaccess(ds, 0) < 0)
  532. X            return;
  533. X        XPput(*wp, strsave(ds, ATEMP));
  534. X        return;
  535. X    }
  536. X
  537. X    if (dp > ds)
  538. X        *dp++ = '/';
  539. X    while (*sp == '/')
  540. X        *dp++ = *sp++;
  541. X    np = strchr(sp, '/');
  542. X    if (np != NULL)
  543. X        *np++ = 0;
  544. X
  545. X    *dp = 0;
  546. X    if (strchr(sp, MAGIC) == NULL) { /* contains no pattern? */
  547. X        tdp = dp; tsp = sp;
  548. X        while ((*tdp++ = *tsp++) != 0)
  549. X            ;
  550. X        --tdp;
  551. X        globit(ds, tdp, np, wp, check);
  552. X    } else {
  553. X        DIR *dirp;
  554. X        struct dirent *d;
  555. X
  556. X        /* ToDo:
  557. X         * should not attemp to open() special files: /dev/ttyd0/*
  558. X         * opendir should do this check, but Doug Gwyn's does not.
  559. X         */
  560. X        dirp = opendir((*ds == 0) ? "." : ds);
  561. X        if (dirp == NULL)
  562. X            goto Nodir;
  563. X        while ((d = readdir(dirp)) != NULL) {
  564. X            tsp = d->d_name;
  565. X            if (tsp[0] == '.' &&
  566. X                (tsp[1] == 0 || tsp[1] == '.' && tsp[2] == 0))
  567. X                continue; /* always ignore . and .. */
  568. X            if (*tsp == '.' && *sp != '.' || !gmatch(tsp, sp))
  569. X                continue;
  570. X
  571. X            tdp = dp;
  572. X            while ((*tdp++ = *tsp++) != 0)
  573. X                ;
  574. X            --tdp;
  575. X            globit(ds, tdp, np, wp, np != NULL);
  576. X        }
  577. X        closedir(dirp);
  578. X      Nodir:;
  579. X    }
  580. X
  581. X    if (np != NULL)
  582. X        *--np = '/';
  583. X}
  584. X
  585. X/* remove MAGIC from string */
  586. Xstatic char *
  587. Xdebunk(cp)
  588. X    char *cp;
  589. X{
  590. X    register char *dp, *sp;
  591. X
  592. X    for (dp = sp = cp; *sp != 0; sp++)
  593. X        if (*sp != MAGIC)
  594. X            *dp++ = *sp;
  595. X    *dp = 0;
  596. X    return cp;
  597. X}
  598. X
  599. X/*
  600. X * tilde expansion
  601. X *
  602. X * based on a version by Arnold Robbins
  603. X */
  604. X
  605. Xstatic char *homedir();
  606. X
  607. Xstatic char *
  608. Xtilde(acp)
  609. X    char *acp;
  610. X{
  611. X    register int c;
  612. X    char path [PATH+1];
  613. X    register char *cp = acp, *wp = path, *dp;
  614. X    char userid [16+1];
  615. X
  616. X  Again:
  617. X    while (1) {
  618. X        if ((c = *cp++) == 0) {
  619. X            *wp = 0;
  620. X            afree((Void*)acp, ATEMP);
  621. X            return strsave(path, ATEMP);
  622. X        } else if (c == MAGIC && *cp == '~')
  623. X            break;
  624. X        else
  625. X            *wp++ = c;
  626. X    }
  627. X
  628. X    dp = NULL;    /* no output substitution */
  629. X    if (cp[1] == 0 || cp[1] == '/' || cp[1] == ':') /* ~ or ~/ */
  630. X        dp = strval(global("HOME")), cp += 1;
  631. X    else if (cp[1] == '+' && (cp[2] == '/' || cp[2] == ':' || cp[2] == 0))
  632. X        dp = strval(global("PWD")), cp += 2;
  633. X    else if (cp[1] == '-' && (cp[2] == '/' || cp[2] == ':' || cp[2] == 0))
  634. X        dp = strval(global("OLDPWD")), cp += 2;
  635. X    else if (letter(cp[1])) {
  636. X        char *save = cp;
  637. X        for (dp = userid, cp++; letnum(*cp) && dp < userid+16; )
  638. X            *dp++ = *cp++;
  639. X        *dp = 0;
  640. X        dp = homedir(userid);
  641. X        if (dp == NULL)
  642. X            cp = save;
  643. X    }
  644. X    /* substitute */
  645. X    if (dp != NULL)
  646. X        while (*dp != 0)
  647. X            *wp++ = *dp++;
  648. X    goto Again;
  649. X}
  650. X
  651. X/*
  652. X * map userid to user's home directory.
  653. X * todo: implement a cache with the "homedirs" table.
  654. X * note that 4.3's getpw adds more than 6K to the shell,
  655. X * and the YP version probably adds much more.
  656. X * we might consider our own version of getpwnam() to keep the size down.
  657. X */
  658. X
  659. Xstatic char *
  660. Xhomedir(name)
  661. X    char *name;
  662. X{
  663. X    register struct tbl *ap;
  664. X    register struct passwd *pw;
  665. X
  666. X    ap = tsearch(&homedirs, name, hash(name));
  667. X    if ((ap != NULL && (ap->flag&ISSET)))
  668. X        return ap->val.s;
  669. X    pw = getpwnam(name);
  670. X    if (pw == NULL)
  671. X        return NULL;
  672. X    return pw->pw_dir;
  673. X}
  674. X
  675. SHAR_EOF
  676. true || echo 'restore of src/eval.c failed'
  677. fi
  678. # ============= src/expr.c ==============
  679. if test -f 'src/expr.c' -a X"$1" != X"-c"; then
  680.     echo 'x - skipping src/expr.c (File already exists)'
  681. else
  682. echo 'x - extracting src/expr.c (Text)'
  683. sed 's/^X//' << 'SHAR_EOF' > 'src/expr.c' &&
  684. X/*
  685. X * Korn expression evaluation
  686. X */
  687. X
  688. Xstatic char *RCSid = "$Header: /tmp/egisin/src/RCS/expr.c,v 3.1 88/11/03 09:15:55 egisin Exp $";
  689. X
  690. X#include <stddef.h>
  691. X#include <errno.h>
  692. X#include <setjmp.h>
  693. X#include "sh.h"
  694. X#include "table.h"
  695. X
  696. X#define    ef    else if        /* fashion statement */
  697. X
  698. X#define    VAR    0x01
  699. X#define    LIT    0x02
  700. X#define    LEQ    0x03
  701. X#define    LNE    0x04
  702. X#define    LLE    0x05
  703. X#define    LGE    0x06
  704. X
  705. Xstatic void token();        /* read next token */
  706. Xstatic Const char *expression;    /* expression being evaluated */
  707. Xstatic Const char *tokp;    /* lexical position */
  708. Xstatic int tok;            /* token from token() */
  709. Xstatic struct tbl *val;        /* value from token() */
  710. X
  711. Xstatic struct tbl *tempvar(), *intvar();
  712. Xstatic struct tbl *asn(), *e6(), *e5(), *e3(), *e2(), *e0();
  713. X
  714. X/*
  715. X * parse and evalute expression
  716. X */
  717. Xvoid
  718. Xevalerr(err)
  719. X    char *err;
  720. X{
  721. X    errorf("%s: %s\n", expression, err);
  722. X}
  723. X
  724. Xlong
  725. Xevaluate(expr)
  726. X    Const char *expr;
  727. X{
  728. X    struct tbl *v;
  729. X
  730. X    expression = tokp = expr;
  731. X    token();
  732. X    v = intvar(asn());
  733. X    if (!(tok == 0))
  734. X        evalerr("bad expression");
  735. X    return v->val.i;
  736. X}
  737. X
  738. Xstatic struct tbl *
  739. Xasn()
  740. X{
  741. X    register struct tbl *vl, *vr;
  742. X
  743. X    vr = vl = e6();
  744. X    if ((tok == '=')) {
  745. X        Area * olastarea = lastarea;
  746. X        token();
  747. X        if ((vl->flag&RDONLY)) /* assign to rvalue */
  748. X            evalerr("bad assignment");
  749. X        vr = intvar(asn());
  750. X        lastarea = olastarea;
  751. X        setint(vl, vr->val.i);
  752. X        if ((vl->flag&INTEGER) && vl->type == 0) /* default base? */
  753. X            vl->type = vr->type;
  754. X    }
  755. X    return vr;
  756. X}
  757. X
  758. Xstatic struct tbl *
  759. Xe6()
  760. X{
  761. X    register struct tbl *vl, *vr;
  762. X
  763. X    vl = e5();
  764. X    while ((tok == LEQ) || (tok == LNE)) {
  765. X        int op = tok;
  766. X        token();
  767. X        vl = intvar(vl);
  768. X        vr = intvar(e5());
  769. X        vl->val.i = vl->val.i == vr->val.i;
  770. X        if (op == LNE)
  771. X            vl->val.i = ! vl->val.i;
  772. X    }
  773. X    return vl;
  774. X}
  775. X
  776. Xstatic struct tbl *
  777. Xe5()
  778. X{
  779. X    register struct tbl *vl, *vr;
  780. X
  781. X    vl = e3();
  782. X    while ((tok == LLE) || (tok == '<') || (tok == '>') || (tok == LGE)) {
  783. X        int op = tok;
  784. X        token();
  785. X        vl = intvar(vl);
  786. X        vr = intvar(e3());
  787. X        if (op == LLE)
  788. X            vl->val.i = vl->val.i <= vr->val.i;
  789. X        ef (op == '<')
  790. X            vl->val.i = vl->val.i < vr->val.i;
  791. X        ef (op == LGE)
  792. X            vl->val.i = vl->val.i >= vr->val.i;
  793. X        ef (op == '>')
  794. X            vl->val.i = vl->val.i > vr->val.i;
  795. X    }
  796. X    return vl;
  797. X}
  798. X
  799. Xstatic struct tbl *
  800. Xe3()
  801. X{
  802. X    register struct tbl *vl, *vr;
  803. X
  804. X    vl = e2();
  805. X    while ((tok == '+') || (tok == '-')) {
  806. X        int op = tok;
  807. X        token();
  808. X        vl = intvar(vl);
  809. X        vr = intvar(e2());
  810. X        if (op == '+')
  811. X            vl->val.i += vr->val.i;
  812. X        ef (op == '-')
  813. X            vl->val.i -= vr->val.i;
  814. X    }
  815. X    return vl;
  816. X}
  817. X
  818. Xstatic struct tbl *
  819. Xe2()
  820. X{
  821. X    register struct tbl *vl, *vr;
  822. X
  823. X    vl = e0();
  824. X    while ((tok == '*') || (tok == '/') || (tok == '%')) {
  825. X        int op = tok;
  826. X        token();
  827. X        vl = intvar(vl);
  828. X        vr = intvar(e0());
  829. X        if (op != '*' && vr->val.i == 0)
  830. X            evalerr("zero divisor");
  831. X        if (op == '*')
  832. X            vl->val.i *= vr->val.i;
  833. X        ef (op == '/')
  834. X            vl->val.i /= vr->val.i;
  835. X        ef (op == '%')
  836. X            vl->val.i %= vr->val.i;
  837. X    }
  838. X    return vl;
  839. X}
  840. X
  841. Xstatic struct tbl *
  842. Xe0()
  843. X{
  844. X    register struct tbl *v;
  845. X
  846. X    if ((tok == '!') || (tok == '-')) {
  847. X        int op = tok;
  848. X        token();
  849. X        v = intvar(e0());
  850. X        if (op == '!')
  851. X            v->val.i = !v->val.i;
  852. X        ef (op == '-')
  853. X            v->val.i = -v->val.i;
  854. X    } else
  855. X    if ((tok == '(')) {
  856. X        token();
  857. X        v = asn();
  858. X        if (!(tok == ')'))
  859. X            evalerr("missing )");
  860. X        token();
  861. X    } else
  862. X    if ((tok == VAR) || (tok == LIT)) {
  863. X        v = val;
  864. X        token();
  865. X    } else
  866. X        evalerr("bad expression");
  867. X    return v;
  868. X}
  869. X
  870. Xstatic void
  871. Xtoken()
  872. X{
  873. X    register char *cp = (char *) tokp;
  874. X    register int c, c2;
  875. X
  876. X    /* skip white space */
  877. X    do c = *cp++;    while (c != '\0' && (c == ' ' || c == '\t'));
  878. X    tokp = cp-1;
  879. X
  880. X    if (letter(c)) {
  881. X        for (; letnum(c); c = *cp++)
  882. X            ;
  883. X        c = *--cp;
  884. X        *cp = 0;
  885. X        val = global(tokp);
  886. X        *cp = c;
  887. X        tok = VAR;
  888. X    } else
  889. X    if (digit(c)) {
  890. X        for (; letnum(c) || c == '#'; c = *cp++)
  891. X            ;
  892. X        c = *--cp;
  893. X        *cp = 0;
  894. X        val = tempvar();
  895. X        setstr(val, tokp);
  896. X        val->flag |= RDONLY;
  897. X        *cp = c;
  898. X        tok = LIT;
  899. X    } else {
  900. X        c2 = *cp++;
  901. X        if (c == '=' && c2 == '=')
  902. X            c = LEQ;
  903. X        ef (c == '!' && c2 == '=')
  904. X            c = LNE;
  905. X        ef (c == '<' && c2 == '=')
  906. X                c = LLE;
  907. X        ef (c == '>' && c2 == '=')
  908. X                c = LGE;
  909. X        else
  910. X            cp--;
  911. X        tok = c;
  912. X    }
  913. X    tokp = cp;
  914. X}
  915. X
  916. Xstatic struct tbl *
  917. Xtempvar()
  918. X{
  919. X    register struct tbl *vp;
  920. X
  921. X    vp = alloc(sizeof(struct tbl), ATEMP);
  922. X    lastarea = ATEMP;
  923. X    vp->flag = ISSET|INTEGER;
  924. X    vp->type = 0;
  925. X    vp->name[0] = '\0';
  926. X    return vp;
  927. X}
  928. X
  929. X/* cast (string) variable to temporary integer variable */
  930. Xstatic struct tbl *
  931. Xintvar(vp)
  932. X    register struct tbl *vp;
  933. X{
  934. X    register struct tbl *vq;
  935. X
  936. X    vq = tempvar();
  937. X    vq->type = 10;
  938. X    return strint(vq, vp);
  939. X}
  940. X
  941. SHAR_EOF
  942. true || echo 'restore of src/expr.c failed'
  943. fi
  944. # ============= ReadMe ==============
  945. if test -f 'ReadMe' -a X"$1" != X"-c"; then
  946.     echo 'x - skipping ReadMe (File already exists)'
  947. else
  948. echo 'x - extracting ReadMe (Text)'
  949. sed 's/^X//' << 'SHAR_EOF' > 'ReadMe' &&
  950. X    Notes on the Standard C / POSIX P1003.1 package
  951. X
  952. XThis package contains header files and library routines
  953. Xto provide a standard C (ANSI) and POSIX enviroment for portable programs.
  954. XThis allows most OS dependcies to be removed from an application,
  955. Xmaking it much more readable, and isolating them in a small,
  956. Xstandardized library. It has the disadvantage that it only
  957. Xworks with fairly stock UNIX versions, but a different approach
  958. Xwill be used for other systems.
  959. X
  960. XThis package supplements the existing libraries and header files
  961. Xof a AT&T-derived Unix system (System V and BSD in particular).
  962. XIt also provides function prototypes when the compiler supports them,
  963. Ximproving compile-time error checking and improving portability on
  964. Xmachines where sizeof(int) == sizeof(size_t) == sizeof(void*) does not hold.
  965. X
  966. XA different approach will be used for the DOS, Atari St, Minix,
  967. Xand possibly V7: a complete replacement standard C library will
  968. Xbe provided as a separate package. This would not be practical
  969. Xwith BSD or NFS systems because of the conflicts with the host
  970. Xstdio and the replacement stdio in libc's getpwent().
  971. X
  972. XContents:
  973. Xstdc/*:        The standard C header files and library.
  974. Xposix/*:    The POSIX header files and library.
  975. Xh/*, h/sys/*:    Links to the header files in stdc/ and posix/.
  976. Xlibstdc.a:    The standard C library.
  977. Xlibposix.a:    The POSIX emulation library.
  978. X
  979. XTo create the header files and libraries, perform the following
  980. Xthree steps in the stdc and posix directories:
  981. X    One of _BSD, _SYSV, or _V7 should be defined in the Makefile.
  982. X    Do "rm stdc/stdio.h", it gets created by make.
  983. X    Do "make link" first to set up the links from *.h to ../h.
  984. X    Do "make" to create the library.
  985. XCompile applications with -I$STD/h, link them with -L$STD ... -lstdc -lposix.
  986. X
  987. X    Notes on the standard C package
  988. XThe files <locale.h>, <assert.h>, and <math.h> don't exist yet.
  989. XMany standard C functions are not implemented yet.
  990. XThese include strtol, strtoul, atexit, tempfile(?), etc.
  991. XThe string routines are by Henry Spencer.
  992. X
  993. XKnown portability problems include:
  994. Xsize_t or ptrdiff_t in <stddef.h> may need to be long.
  995. XThe method of creating <stdio.h> from /usr/include/stdio.h and stdio.h_std
  996. Xmay not work on some versions of UNIX.
  997. XAlmost definitely not on Xenix, maybe not on merged BSD-SysV systems.
  998. XThis package contains a correct version of setvbuf() which
  999. Xdepends on the contents of FILE in the host stdio.h.
  1000. XThis will not work if FILE is neither stock System V or BSD.
  1001. XYou can safely "#if 0" out the body of setvbuf for PD ksh,
  1002. Xsetvbuf is used to decrease the size of buffers to speed up forks.
  1003. X
  1004. X    Notes on the POSIX package
  1005. XOnly headers and function required by the PD KornShell are implemented.
  1006. XI do not intend to develop this into a full POSIX emulation package.
  1007. XYou should install Doug Gwyn's <dirent> package if you do not have
  1008. X<dirent.h> nor <sys/dir>. If you do have <dirent.h>, unlink h/dirent.h
  1009. X(this should be configured automatically by the makefile).
  1010. X
  1011. SHAR_EOF
  1012. true || echo 'restore of ReadMe failed'
  1013. fi
  1014. # ============= posix/Makefile ==============
  1015. if test ! -d 'posix'; then
  1016.     echo 'x - creating directory posix'
  1017.     mkdir 'posix'
  1018. fi
  1019. if test -f 'posix/Makefile' -a X"$1" != X"-c"; then
  1020.     echo 'x - skipping posix/Makefile (File already exists)'
  1021. else
  1022. echo 'x - extracting posix/Makefile (Text)'
  1023. sed 's/^X//' << 'SHAR_EOF' > 'posix/Makefile' &&
  1024. X# POSIX P1003.1 compatability
  1025. X# does not requires SVID/P1003.2-compatible "make"
  1026. X
  1027. X# $Header: Makefile,v 1.1 88/03/29 18:28:38 egisin Locked $
  1028. X
  1029. XSYSTEM=BSD
  1030. XCC = gcc -ansi -O -W 
  1031. XLN = ln
  1032. XPRINT = lpr -p -Plp26_3018
  1033. X
  1034. XCFLAGS = -I../h -D_$(SYSTEM) 
  1035. X
  1036. XMISC =    Makefile 
  1037. XHDRS =    wait.h times.h unistd.h fcntl.h dirent.h 
  1038. XSRCS =    unistd.c fcntl.c times.c 
  1039. XOBJS =    unistd.o fcntl.o times.o 
  1040. X#OBJS =    $(SRCS:.c=.o)
  1041. XLIB =    libposix.a
  1042. XINCL =    ../h
  1043. X
  1044. Xall:    $(LIB)
  1045. X
  1046. Xlink:    $(HDRS)
  1047. X    [ -d $(INCL) ] || mkdir $(INCL)
  1048. X    [ -d $(INCL)/sys ] || mkdir $(INCL)/sys
  1049. X    $(LN) wait.h time.h times.h $(INCL)/sys
  1050. X    $(LN) unistd.h $(INCL)
  1051. X    if [ ! -r /usr/include/unistd.h ]; then $(LN) dirent.h $(INCL); fi
  1052. X
  1053. X$(LIB)(%.o): %.o
  1054. X
  1055. X%: RCS/%,v
  1056. X    co %@
  1057. X
  1058. X$(LIB):    $(OBJS)
  1059. X    ar r $@ $?
  1060. X    -ranlib $@
  1061. X
  1062. X#$(LIB): lib.a($OBJS)
  1063. X#    ar rv $@ $?
  1064. X#    -ranlib $@
  1065. X#    rm -f $?
  1066. X
  1067. Xstd_p.tar: $(MISC) $(SRCS)
  1068. X    tar cf std_p.tar $(MISC) $(HDRS) $(SRCS)
  1069. X
  1070. Xprint: $(MISC) $(HDRS) $(SRCS)
  1071. X    $(PRINT) $(MISC) $(HDRS) $(SRCS)
  1072. X
  1073. XIndex: $(SRCS)
  1074. X    ctags -x $(HDRS) $(SRCS) >Index
  1075. X
  1076. Xci:
  1077. X    ci -l $(MISC) $(HDRS) $(SRCS)
  1078. X
  1079. Xfcntl.o: fcntl.h 
  1080. X
  1081. Xtimes.o: times.h 
  1082. X
  1083. SHAR_EOF
  1084. true || echo 'restore of posix/Makefile failed'
  1085. fi
  1086. # ============= posix/dirent.C ==============
  1087. if test -f 'posix/dirent.C' -a X"$1" != X"-c"; then
  1088.     echo 'x - skipping posix/dirent.C (File already exists)'
  1089. else
  1090. echo 'x - extracting posix/dirent.C (Text)'
  1091. sed 's/^X//' << 'SHAR_EOF' > 'posix/dirent.C' &&
  1092. X/*
  1093. X * simple implementation of directory(3) routines for V7 and Minix.
  1094. X * completly untested. not designed to be efficient.
  1095. X * missing telldir and seekdir.
  1096. X */
  1097. X
  1098. X#include <sys/types.h>
  1099. X#include <dirent.h>
  1100. X
  1101. Xchar    *malloc();
  1102. X
  1103. X#define    DIRSIZ    14
  1104. Xstruct    direct_v7
  1105. X{
  1106. X    unsigned short    d_ino;
  1107. X    char    d_name[DIRSIZ];
  1108. X};
  1109. X
  1110. XDIR *opendir(filename)
  1111. X    char *filename;
  1112. X{
  1113. X    DIR *dirp;
  1114. X
  1115. X    dirp = (DIR *) malloc(sizeof(DIR));
  1116. X    if (dirp == NULL)
  1117. X        return NULL;
  1118. X    dirp->fd = open(filename, 0);
  1119. X    if (dirp->fd < 0) {
  1120. X        free((char *) dirp);
  1121. X        return NULL;
  1122. X    }
  1123. X    return dirp;
  1124. X}
  1125. X
  1126. Xstruct dirent *readdir(dirp)
  1127. X    register DIR *dirp;
  1128. X{
  1129. X    static    struct direct_v7 ent;
  1130. X
  1131. X    while (read(dirp->fd, (char *)&ent, (int)sizeof(ent)) == sizeof(ent))
  1132. X        if (ent.d_ino != 0)
  1133. X            goto found;
  1134. X    return (struct dirent *) NULL;
  1135. X found:
  1136. X    dirp->ent.d_ino = ent.d_ino;
  1137. X    strncpy(dirp->ent.d_name, ent.d_name, DIRSIZ);
  1138. X    return &dirp->ent;
  1139. X}
  1140. X
  1141. Xvoid rewinddir(dirp)
  1142. X    DIR *dirp;
  1143. X{
  1144. X    lseek(dirp->fd, 0L, 0);
  1145. X}
  1146. X
  1147. Xclosedir(dirp)
  1148. X    DIR *dirp;
  1149. X{
  1150. X    close(dirp->fd);
  1151. X    dirp->fd = -1;
  1152. X    free((char *) dirp);
  1153. X    return 0;
  1154. X}
  1155. SHAR_EOF
  1156. true || echo 'restore of posix/dirent.C failed'
  1157. fi
  1158. # ============= posix/dirent.H ==============
  1159. if test -f 'posix/dirent.H' -a X"$1" != X"-c"; then
  1160.     echo 'x - skipping posix/dirent.H (File already exists)'
  1161. else
  1162. echo 'x - extracting posix/dirent.H (Text)'
  1163. sed 's/^X//' << 'SHAR_EOF' > 'posix/dirent.H' &&
  1164. X/*
  1165. X    <sys/dirent.h> -- file system independent directory entry (SVR3)
  1166. X
  1167. X    last edit:    25-Apr-1987    D A Gwyn
  1168. X    last hack:    14-Aug-1987    Eric Gisin
  1169. X
  1170. X    prerequisite:    <sys/types.h>
  1171. X*/
  1172. X
  1173. X#define    MAXNAMLEN    16        /* maximum filename length */
  1174. X
  1175. Xstruct dirent                /* data from getdents()/readdir() */
  1176. X    {
  1177. X    long        d_ino;        /* inode number of entry */
  1178. X    off_t        d_off;        /* offset of disk directory entry */
  1179. X    unsigned short    d_reclen;    /* length of this record */
  1180. X    char    d_name[MAXNAMLEN];    /* name of file */
  1181. X};
  1182. X
  1183. X#ifndef NAME_MAX
  1184. X#define    NAME_MAX    (MAXNAMLEN - 1)    /* DAG -- added for POSIX */
  1185. X#endif
  1186. X
  1187. Xtypedef struct {            /* returned by opendir() */
  1188. X    int    fd;
  1189. X    struct dirent ent;
  1190. X} DIR;
  1191. X
  1192. Xextern DIR        *opendir();
  1193. Xextern struct dirent    *readdir();
  1194. Xextern off_t        telldir();
  1195. Xextern void        seekdir();
  1196. Xextern void        rewinddir();
  1197. Xextern int        closedir();
  1198. X
  1199. X#ifndef NULL
  1200. X#define    NULL    0            /* DAG -- added for convenience */
  1201. X#endif
  1202. SHAR_EOF
  1203. true || echo 'restore of posix/dirent.H failed'
  1204. fi
  1205. # ============= posix/dirent.h ==============
  1206. if test -f 'posix/dirent.h' -a X"$1" != X"-c"; then
  1207.     echo 'x - skipping posix/dirent.h (File already exists)'
  1208. else
  1209. echo 'x - extracting posix/dirent.h (Text)'
  1210. sed 's/^X//' << 'SHAR_EOF' > 'posix/dirent.h' &&
  1211. X/* <dirent.h> based on BSD <sys/dir.h> */
  1212. X
  1213. X#include <sys/dir.h>
  1214. X#define dirent direct
  1215. X
  1216. SHAR_EOF
  1217. true || echo 'restore of posix/dirent.h failed'
  1218. fi
  1219. # ============= posix/fcntl.c ==============
  1220. if test -f 'posix/fcntl.c' -a X"$1" != X"-c"; then
  1221.     echo 'x - skipping posix/fcntl.c (File already exists)'
  1222. else
  1223. echo 'x - extracting posix/fcntl.c (Text)'
  1224. sed 's/^X//' << 'SHAR_EOF' > 'posix/fcntl.c' &&
  1225. X/* fcntl emulation */
  1226. X
  1227. X#include <errno.h>
  1228. X#include <sys/types.h>
  1229. X#include <unistd.h>
  1230. X#include <fcntl.h>
  1231. X
  1232. X#if _V7
  1233. X
  1234. X#include <sgtty.h>
  1235. X
  1236. Xint
  1237. Xfcntl(fd, cmd, arg)
  1238. X    int fd, cmd, arg;
  1239. X{
  1240. X    switch (cmd) {
  1241. X      case F_SETFD:        /* set fd flags */
  1242. X        ioctl(fd, (arg&FD_CLEXEC) ? FIOCLEX : FIONCLEX, (char *)NULL);
  1243. X        break;
  1244. X      case F_DUPFD:        /* dup fd */
  1245. X        /* this one is fun. find an unused fd >= arg and dup2 */
  1246. X        break;
  1247. X    }
  1248. X    return 0;
  1249. X}
  1250. X
  1251. X#endif
  1252. X
  1253. SHAR_EOF
  1254. true || echo 'restore of posix/fcntl.c failed'
  1255. fi
  1256. # ============= posix/fcntl.h ==============
  1257. if test -f 'posix/fcntl.h' -a X"$1" != X"-c"; then
  1258.     echo 'x - skipping posix/fcntl.h (File already exists)'
  1259. else
  1260. echo 'x - extracting posix/fcntl.h (Text)'
  1261. sed 's/^X//' << 'SHAR_EOF' > 'posix/fcntl.h' &&
  1262. X/* P1003.1 fcntl/open definitions */
  1263. X/* Based on a version by Terrence W. Holm */
  1264. X
  1265. X/*  for fcntl(2)  */
  1266. X
  1267. X#define    F_DUPFD        0
  1268. X#define    F_GETFD        1
  1269. X#define    F_SETFD        2
  1270. X#define    F_GETFL        3
  1271. X#define    F_SETFL        4
  1272. X
  1273. X#define    FD_CLEXEC    1        /* fcntl F_SETFD close on exec mode */
  1274. X
  1275. X/*  for open(2)  */
  1276. X
  1277. X#define    O_RDONLY    0
  1278. X#define    O_WRONLY    1
  1279. X#define    O_RDWR        2
  1280. X
  1281. X#if _BSD
  1282. X#undef    O_RDONLY
  1283. X#undef    O_WRONLY
  1284. X#undef    O_RDWR
  1285. X#include "/usr/include/fcntl.h"
  1286. X#endif
  1287. X
  1288. SHAR_EOF
  1289. true || echo 'restore of posix/fcntl.h failed'
  1290. fi
  1291. # ============= posix/io.h ==============
  1292. if test -f 'posix/io.h' -a X"$1" != X"-c"; then
  1293.     echo 'x - skipping posix/io.h (File already exists)'
  1294. else
  1295. echo 'x - extracting posix/io.h (Text)'
  1296. sed 's/^X//' << 'SHAR_EOF' > 'posix/io.h' &&
  1297. X/* POSIX IO functions */
  1298. X
  1299. X/* include <unistd.h> to get this */
  1300. X
  1301. X#if ! _IO_H
  1302. X#define    _IO_H    1
  1303. X
  1304. X#include <unistd.h>
  1305. X
  1306. X#if _ST                /* dLibs hack */
  1307. X#define    unlink    remove
  1308. X#endif
  1309. X
  1310. Xstruct stat;            /* create global incompletely-typed structure */
  1311. X
  1312. Xint chdir ARGS ((const char *path));
  1313. Xint umask ARGS ((int mode));
  1314. X
  1315. Xint open ARGS ((const char *path, int flags, ... /*mode*/));
  1316. Xint creat ARGS ((const char *path, int mode));
  1317. Xint pipe ARGS ((int pv[2]));
  1318. Xint close ARGS ((int fd));
  1319. X
  1320. Xint fcntl ARGS ((int fd, int cmd, int arg));
  1321. Xint dup ARGS ((int fd));
  1322. Xint dup2 ARGS ((int ofd, int nfd));
  1323. X
  1324. Xint link ARGS ((const char *opath, const char *npath));
  1325. Xint unlink ARGS ((const char *path));
  1326. Xint rename ARGS ((const char *opath, const char *npath));
  1327. Xint mkdir ARGS ((const char *path, int mode));
  1328. X
  1329. Xlong lseek ARGS ((int fd, long off, int how));
  1330. Xint read ARGS ((int fd, char *buf, unsigned len));
  1331. Xint write ARGS ((int fd, char *buf, unsigned len));
  1332. X
  1333. Xint access ARGS ((const char *path, int mode));
  1334. Xint stat ARGS ((const char *path, struct stat *sp));
  1335. Xint fstat ARGS ((int fd, struct stat *sp));
  1336. X
  1337. Xint chmod ARGS ((const char *path, int mode));
  1338. Xint chown ARGS ((const char *path, int uid));
  1339. Xint chgrp ARGS ((const char *path, int gid));
  1340. Xint utime ARGS ((const char *path, long tv[2]));
  1341. X
  1342. X#if _BSD || _V7
  1343. Xint ioctl ARGS ((int fd, int cmd, void *argp)); /* BSD is "uns long cmd" */
  1344. X#endif
  1345. X
  1346. X#endif
  1347. SHAR_EOF
  1348. true || echo 'restore of posix/io.h failed'
  1349. fi
  1350. # ============= posix/times.c ==============
  1351. if test -f 'posix/times.c' -a X"$1" != X"-c"; then
  1352.     echo 'x - skipping posix/times.c (File already exists)'
  1353. else
  1354. echo 'x - extracting posix/times.c (Text)'
  1355. sed 's/^X//' << 'SHAR_EOF' > 'posix/times.c' &&
  1356. X/* P1003.1 times emulation */
  1357. X
  1358. X#include <sys/times.h>
  1359. X
  1360. X#if _BSD
  1361. X
  1362. X#include <sys/time.h>
  1363. X#include <sys/resource.h>
  1364. X
  1365. Xstatic    long base_tv_sec = 0;
  1366. X
  1367. Xclock_t
  1368. Xtimes(tmsp)
  1369. X    register struct tms *tmsp;
  1370. X{
  1371. X    struct timeval tv;
  1372. X    struct rusage ru;
  1373. X
  1374. X    getrusage(RUSAGE_SELF, &ru);
  1375. X    tmsp->tms_utime = ru.ru_utime.tv_sec*CLK_TCK
  1376. X        + (long)ru.ru_utime.tv_usec*CLK_TCK/1000000;
  1377. X    tmsp->tms_stime = ru.ru_stime.tv_sec*CLK_TCK
  1378. X        + (long)ru.ru_stime.tv_usec*CLK_TCK/1000000;
  1379. X    getrusage(RUSAGE_CHILDREN, &ru);
  1380. X    tmsp->tms_cutime = ru.ru_utime.tv_sec*CLK_TCK
  1381. X        + (long)ru.ru_utime.tv_usec*CLK_TCK/1000000;
  1382. X    tmsp->tms_cstime = ru.ru_stime.tv_sec*CLK_TCK
  1383. X        + (long)ru.ru_stime.tv_usec*CLK_TCK/1000000;
  1384. X
  1385. X    gettimeofday(&tv, (struct timezone *)NULL);
  1386. X    if (base_tv_sec == 0)
  1387. X        base_tv_sec = tv.tv_sec;
  1388. X    tv.tv_sec -= base_tv_sec; /*  prevent clock_t overflow */
  1389. X    return tv.tv_sec*CLK_TCK + (long)tv.tv_usec*CLK_TCK/1000000;
  1390. X}
  1391. X
  1392. X#endif
  1393. X
  1394. X#if _V7
  1395. X
  1396. Xclock_t
  1397. Xtimes(tmsp)
  1398. X    struct tms *tmsp;
  1399. X{
  1400. X    struct timeb tb;
  1401. X
  1402. X#undef times            /* access real times() */
  1403. X    times(tmsp);
  1404. X#define times times_
  1405. X    ftime(&tb);
  1406. X    return tb.time*CLK_TCK + (long)tb.millitm*CLK_TCK/1000;
  1407. X}
  1408. X
  1409. X#endif
  1410. X
  1411. SHAR_EOF
  1412. true || echo 'restore of posix/times.c failed'
  1413. fi
  1414. # ============= posix/unistd.c ==============
  1415. if test -f 'posix/unistd.c' -a X"$1" != X"-c"; then
  1416.     echo 'x - skipping posix/unistd.c (File already exists)'
  1417. else
  1418. echo 'x - extracting posix/unistd.c (Text)'
  1419. sed 's/^X//' << 'SHAR_EOF' > 'posix/unistd.c' &&
  1420. X/* misc. POSIX emulation */
  1421. X
  1422. X/* $Header$ */
  1423. X
  1424. X#include <string.h>
  1425. X#include <errno.h>
  1426. X#include <sys/types.h>
  1427. X#include <unistd.h>
  1428. X
  1429. X#if _V7 || _BSD
  1430. X
  1431. Xchar *
  1432. Xgetcwd(buf, len)
  1433. X    char *buf;
  1434. X    size_t len;
  1435. X{
  1436. X    char cwd [1024];
  1437. X    extern char *getwd();
  1438. X    if (getwd(cwd) == NULL)
  1439. X        return NULL;
  1440. X    if (strlen(cwd)+1 >= len) {
  1441. X        errno = ERANGE;
  1442. X        return NULL;
  1443. X    }
  1444. X    return strcpy(buf, cwd);
  1445. X}
  1446. X
  1447. X#endif
  1448. X
  1449. X#if _V7
  1450. X
  1451. Xlong
  1452. Xulimit(cmd, limit)
  1453. X    int cmd;
  1454. X    long limit;
  1455. X{
  1456. X    return 0;
  1457. X}
  1458. X
  1459. X#endif
  1460. X
  1461. SHAR_EOF
  1462. true || echo 'restore of posix/unistd.c failed'
  1463. fi
  1464. # ============= posix/unistd.h ==============
  1465. if test -f 'posix/unistd.h' -a X"$1" != X"-c"; then
  1466.     echo 'x - skipping posix/unistd.h (File already exists)'
  1467. else
  1468. echo 'x - extracting posix/unistd.h (Text)'
  1469. sed 's/^X//' << 'SHAR_EOF' > 'posix/unistd.h' &&
  1470. X/* unistd.h: misc. P1003.1 definitions */
  1471. X/* Based on a version by Terrence W. Holm */
  1472. X
  1473. X#if ! _UNISTD_H
  1474. X#define    _UNISTD_H 1
  1475. X
  1476. X#include <stddef.h>
  1477. X
  1478. X/* doesn't really belong here, but the library function need it */
  1479. X/* todo: use _ARGS, _Void, _Const */
  1480. X#if __STDC__
  1481. X#define    ARGS(args)    args
  1482. X#define Void    void
  1483. X#define    Const    const
  1484. X#else
  1485. X#define    ARGS(args)    ()
  1486. X#define    Void    char
  1487. X#define    Const
  1488. X#endif
  1489. X
  1490. X#include <io.h>            /* POSIX IO functions */
  1491. X
  1492. X/*  for access(2)  */
  1493. X
  1494. X#define    R_OK    4
  1495. X#define    W_OK    2
  1496. X#define    X_OK    1
  1497. X#define    F_OK    0
  1498. X
  1499. X/*  for lockf(2)  */
  1500. X
  1501. X#define    F_ULOCK    0
  1502. X#define    F_LOCK    1
  1503. X#define    F_TLOCK    2
  1504. X#define    F_TEST    3
  1505. X
  1506. X/*  for lseek(2)  */
  1507. X
  1508. X#define    SEEK_SET    0
  1509. X#define    SEEK_CUR    1
  1510. X#define    SEEK_END    2
  1511. X
  1512. X#define    IN_PATH    "/usr/include"
  1513. X
  1514. Xchar   *getcwd ARGS ((char *buf, size_t len));
  1515. X
  1516. X#endif
  1517. X
  1518. SHAR_EOF
  1519. true || echo 'restore of posix/unistd.h failed'
  1520. fi
  1521. # ============= posix/time.h ==============
  1522. if test -f 'posix/time.h' -a X"$1" != X"-c"; then
  1523.     echo 'x - skipping posix/time.h (File already exists)'
  1524. else
  1525. echo 'x - extracting posix/time.h (Text)'
  1526. sed 's/^X//' << 'SHAR_EOF' > 'posix/time.h' &&
  1527. X/*
  1528. X * Replacement for BSD <sys/time.h>
  1529. X * because Ultrix screws it up.
  1530. X */
  1531. X
  1532. Xstruct timeval {
  1533. X    long tv_sec;        /* time_t */
  1534. X    long tv_usec;        /* microsex */
  1535. X};
  1536. X
  1537. Xstruct timezone {
  1538. X    int tz_minuteswest;    /* of Greenwinch */
  1539. X    int tz_dsttime;        /* type of dst correction to apply */
  1540. X};
  1541. SHAR_EOF
  1542. true || echo 'restore of posix/time.h failed'
  1543. fi
  1544. # ============= posix/times.h ==============
  1545. if test -f 'posix/times.h' -a X"$1" != X"-c"; then
  1546.     echo 'x - skipping posix/times.h (File already exists)'
  1547. else
  1548. echo 'x - extracting posix/times.h (Text)'
  1549. sed 's/^X//' << 'SHAR_EOF' > 'posix/times.h' &&
  1550. X/*
  1551. X * sys/times.h: POSIX times()
  1552. X */
  1553. X
  1554. X#if ! _TIMES_H
  1555. X#define    _TIMES_H 1
  1556. X
  1557. X#include <time.h>        /* defines CLK_TCK */
  1558. X
  1559. X#if __STDC__
  1560. X#define    ARGS(args)    args
  1561. X#else
  1562. X#define    ARGS(args)    ()
  1563. X#endif
  1564. X
  1565. Xstruct tms {
  1566. X    clock_t    tms_utime, tms_stime;
  1567. X    clock_t    tms_cutime, tms_cstime;
  1568. X};
  1569. X
  1570. X#if _V7
  1571. X#define times times_
  1572. X#endif
  1573. X
  1574. Xclock_t    times ARGS((struct tms *tmsp));
  1575. X
  1576. X#endif
  1577. X
  1578. SHAR_EOF
  1579. true || echo 'restore of posix/times.h failed'
  1580. fi
  1581. # ============= posix/wait.h ==============
  1582. if test -f 'posix/wait.h' -a X"$1" != X"-c"; then
  1583.     echo 'x - skipping posix/wait.h (File already exists)'
  1584. else
  1585. echo 'x - extracting posix/wait.h (Text)'
  1586. sed 's/^X//' << 'SHAR_EOF' > 'posix/wait.h' &&
  1587. X/*
  1588. X * POSIX <sys/wait.h>
  1589. X */
  1590. X
  1591. X#if __STDC__
  1592. X#define    ARGS(args)    args
  1593. X#else
  1594. X#define    ARGS(args)    ()
  1595. X#endif
  1596. X
  1597. X#if 1
  1598. Xtypedef int pid_t;        /* belong in sys/types.h */
  1599. X#endif
  1600. X
  1601. X/* waitpid options */
  1602. X#define WNOHANG        1    /* don't hang in wait */
  1603. X#define WUNTRACED    2    /* tell about stopped, untraced children */
  1604. X
  1605. X#define    WSTOPPED    0x7F    /* process is stopped */
  1606. X
  1607. X#define WIFSTOPPED(x)    (((x)&0xFF) == 0x7F)
  1608. X#define WIFSIGNALED(x)    (((x)&0xFF) != 0x7F && ((x)&0x7F) != 0)
  1609. X#define WIFEXITED(x)    (((x)&0xFF) != 0x7F && ((x)&0x7F) == 0)
  1610. X#define    WIFCORED(x)    (!!((x)&0x80)) /* non-standard */
  1611. X#define    WEXITSTATUS(x)    ((x)>>8&0xFF)
  1612. X#define    WTERMSIG(x)    ((x)&0x7F)
  1613. X#define    WSTOPSIG(x)    ((x)>>8&0xFF)
  1614. X
  1615. Xpid_t wait ARGS((int *statp));
  1616. X#if _BSD
  1617. Xpid_t wait3 ARGS((int *statp, int options, Void *));
  1618. X/* todo: does not emulate pid argument */
  1619. X#define    waitpid(pid, sp, opts)    wait3(sp, opts, (Void*)NULL)
  1620. X#else
  1621. Xpid_t waitpid ARGS((pid_t pid, int *statp, int options));
  1622. X#endif
  1623. X
  1624. SHAR_EOF
  1625. true || echo 'restore of posix/wait.h failed'
  1626. fi
  1627. # ============= posix/fixincludes ==============
  1628. if test -f 'posix/fixincludes' -a X"$1" != X"-c"; then
  1629.     echo 'x - skipping posix/fixincludes (File already exists)'
  1630. else
  1631. echo 'x - extracting posix/fixincludes (Text)'
  1632. sed 's/^X//' << 'SHAR_EOF' > 'posix/fixincludes' &&
  1633. X# Install modified versions of certain ANSI-incompatible system header files
  1634. X# which are fixed to work correctly with ANSI C
  1635. X# and placed in a directory that GNU C will search.
  1636. X# This works properly on a Sun in system version 3.4;
  1637. X# for other versions, you had better check.
  1638. X
  1639. Xmkdir /usr/local/lib/gcc-include
  1640. Xmkdir /usr/local/lib/gcc-include/sys
  1641. Xcp /usr/include/sys/ioctl.h /usr/local/lib/gcc-include/sys/ioctl.h
  1642. Xchmod +w /usr/local/lib/gcc-include/sys/ioctl.h
  1643. Xex /usr/local/lib/gcc-include/sys/ioctl.h <<\EOF
  1644. Xg/_IO/s/(\(.\),/('\1',/
  1645. Xg/#define._IO/s/'x'/x/g
  1646. Xwq
  1647. XEOF
  1648. X
  1649. Xcp /usr/include/sys/ttychars.h /usr/local/lib/gcc-include/sys/ttychars.h
  1650. Xchmod +w /usr/local/lib/gcc-include/sys/ttychars.h
  1651. Xex /usr/local/lib/gcc-include/sys/ttychars.h <<\EOF
  1652. Xg/CTRL/s/(\(.\))/('\1')/
  1653. Xg/#define.CTRL/s/'c'/c/g
  1654. Xwq
  1655. XEOF
  1656. SHAR_EOF
  1657. true || echo 'restore of posix/fixincludes failed'
  1658. fi
  1659. # ============= stdc/Makefile ==============
  1660. if test ! -d 'stdc'; then
  1661.     echo 'x - creating directory stdc'
  1662.     mkdir 'stdc'
  1663. fi
  1664. if test -f 'stdc/Makefile' -a X"$1" != X"-c"; then
  1665.     echo 'x - skipping stdc/Makefile (File already exists)'
  1666. else
  1667. echo 'x - extracting stdc/Makefile (Text)'
  1668. sed 's/^X//' << 'SHAR_EOF' > 'stdc/Makefile' &&
  1669. X# Standard C (ANSI) compatabilaty
  1670. X# does not requires SVID/P1003.2-compatible "make"
  1671. X
  1672. X# $Header: Makefile,v 1.1 88/03/29 18:28:38 egisin Locked $
  1673. X
  1674. XSYSTEM=BSD
  1675. XCC = gcc -ansi -O -W 
  1676. XLN = ln
  1677. XPRINT = lpr -p -Plp26_3018
  1678. X
  1679. XCFLAGS = -I../h -D_$(SYSTEM)
  1680. X
  1681. XMISC =    Makefile stdio.h_std
  1682. XHDRS =    limits.h stddef.h stdlib.h string.h time.h stdarg.h 
  1683. XSRCS =    clock.c stdio.c setvbuf.c vprintf.c fprintf.c sprintf.c \
  1684. X    strstr.c \
  1685. X    memmove.c memcpy.c memset.c memcmp.c memchr.c 
  1686. XOBJS =    clock.o stdio.o setvbuf.o vprintf.o fprintf.o sprintf.o \
  1687. X    strstr.o \
  1688. X    memmove.o memcpy.o memset.o memcmp.o memchr.o 
  1689. X#OBJS =    $(SRCS:.c=.o)
  1690. XLIB =    libstdc.a
  1691. XINCL =    ../h
  1692. X
  1693. Xall:    $(LIB)
  1694. X
  1695. Xlink:    $(HDRS) stdio.h 
  1696. X    [ -d $(INCL) ] || mkdir $(INCL)
  1697. X    [ -d $(INCL)/sys ] || mkdir $(INCL)/sys
  1698. X    $(LN) types.h $(INCL)/sys
  1699. X    $(LN) limits.h stddef.h stdlib.h stdio.h string.h time.h stdarg.h $(INCL)
  1700. X
  1701. X$(LIB)(%.o): %.o
  1702. X
  1703. X%: RCS/%,v
  1704. X    co %@
  1705. X
  1706. X$(LIB):    $(OBJS)
  1707. X    ar r $@ $?
  1708. X    -ranlib $@
  1709. X
  1710. X#$(LIB): lib.a($OBJS)
  1711. X#    ar rv $@ $?
  1712. X#    -ranlib $@
  1713. X#    rm -f $?
  1714. X
  1715. Xstdio.h: stdio.h_std /usr/include/stdio.h 
  1716. X    sed </usr/include/stdio.h >stdio.hacked \
  1717. X        -e '/^# *include/ d' -e '/char..sprintf/ d' -e '/# *define.NULL./ d'
  1718. X    sed <stdio.h_std >stdio.h -e '/%%%/ r stdio.hacked'
  1719. X    rm stdio.hacked
  1720. X
  1721. Xstd_c.tar: $(MISC) $(HDRS) $(SRCS)
  1722. X    tar cf std_c.tar $(MISC) $(HDRS) $(SRCS)
  1723. X
  1724. Xprint: $(MISC) $(HDRS) $(SRCS)
  1725. X    $(PRINT) $(MISC) $(HDRS) $(SRCS)
  1726. X
  1727. Xstring.h: stddef.h
  1728. X
  1729. Xstdlib.h: stddef.h
  1730. X
  1731. Xstdio.h: stddef.h 
  1732. X
  1733. Xtime.h: stddef.h
  1734. X
  1735. Xstdio.o: stdio.h
  1736. X
  1737. Xsetvbuf.o: stdlib.h stdio.h
  1738. X
  1739. Xfprintf.o: stdarg.h stdio.h
  1740. X
  1741. Xsprintf.o: stdarg.h stdio.h
  1742. X
  1743. Xvprintf.o: stdarg.h stdio.h
  1744. X
  1745. Xstrstr.o: string.h 
  1746. X
  1747. SHAR_EOF
  1748. true || echo 'restore of stdc/Makefile failed'
  1749. fi
  1750. # ============= stdc/vprintf.c ==============
  1751. if test -f 'stdc/vprintf.c' -a X"$1" != X"-c"; then
  1752.     echo 'x - skipping stdc/vprintf.c (File already exists)'
  1753. else
  1754. echo 'x - extracting stdc/vprintf.c (Text)'
  1755. sed 's/^X//' << 'SHAR_EOF' > 'stdc/vprintf.c' &&
  1756. X#if __STDC__
  1757. X#include <stdarg.h>
  1758. X#else
  1759. X#include <varargs.h>
  1760. X#endif
  1761. X#include <stdio.h>
  1762. X
  1763. X#define    BUF    40        /* buffer for int -> string conversion */
  1764. X
  1765. Xint
  1766. X#if __STDC__
  1767. Xvprintf(Const char *fmt, va_list va) {
  1768. X#else
  1769. Xvprintf(fmt, va) char *fmt; va_list va; {
  1770. X#endif
  1771. X    return vfprintf(stdout, fmt, va);
  1772. X}
  1773. X
  1774. Xint
  1775. X#if __STDC__
  1776. Xvfprintf(register FILE *f, register Const char *fmt, register va_list va) {
  1777. X#else
  1778. Xvfprintf(f, fmt, va) register FILE *f; register char *fmt; register va_list va; {
  1779. X#endif
  1780. X    register int c;
  1781. X    int pos = 0;            /* todo: implement */
  1782. X
  1783. X    while ((c = *fmt++))
  1784. X        if (c == '%') {
  1785. X        long n;
  1786. X        register unsigned long u;
  1787. X        char buf [BUF+1];
  1788. X        register char *p = buf + BUF;
  1789. X        register enum {
  1790. X            FF_ALT = 0x01, /* #, alternate format */
  1791. X            FF_SHORT = 0x02, /* h, short arg */
  1792. X            FF_LONG = 0x04,    /* l, long arg */
  1793. X            FF_ZERO = 0x08,    /* 0, zero fill */
  1794. X            FF_LEFT = 0x10,    /* -, left adjust */
  1795. X            FF_PREC = 0x20,    /* .*, precision */
  1796. X            FF_NEG = 0x40,    /* signed arg */
  1797. X            FF_PUTS = 0x80,    /* fputs(p, f) */
  1798. X            FF_DEFAULT = 0
  1799. X        } flags = FF_DEFAULT;
  1800. X        int sign = '-';    /* sign: [ +-] */
  1801. X        int width = 0, prec = 0; /* width, precision */
  1802. X
  1803. X        *p = 0;
  1804. X
  1805. X        /* scan flag characters */
  1806. X        for (c = *fmt++; ; c = *fmt++) switch (c) {
  1807. X          case '0':
  1808. X            flags |= FF_ZERO;
  1809. X            break;
  1810. X
  1811. X          case '#':        /* alternate format */
  1812. X            flags |= FF_ALT;
  1813. X            break;
  1814. X
  1815. X          case ' ':        /* blank sign */
  1816. X            sign = ' ';
  1817. X            break;
  1818. X          case '+':        /* +/- sign */
  1819. X            sign = '+';
  1820. X            break;
  1821. X
  1822. X          case '-':        /* left just. */
  1823. X            flags |= FF_LEFT;
  1824. X            break;
  1825. X
  1826. X          default:
  1827. X            goto Frogs;
  1828. X        }
  1829. X      Frogs:
  1830. X
  1831. X        /* scan width */
  1832. X          if (c == '*') {        /* width from arg list */
  1833. X            width = va_arg(va, int);
  1834. X            c = *fmt++;
  1835. X        } else
  1836. X            while ('0' <= c && c <= '9') {
  1837. X                width = width*10 + (c-'0');
  1838. X                c = *fmt++;
  1839. X            }
  1840. X
  1841. X        if (c == '.') {        /* scan precision */
  1842. X            flags |= FF_PREC;
  1843. X            c = *fmt++;
  1844. X            if (c == '*') {    /* precision from arg list */
  1845. X                prec = va_arg(va, int);
  1846. X                c = *fmt++;
  1847. X            } else
  1848. X                while ('0' <= c && c <= '9') {
  1849. X                    prec = prec*10 + (c-'0');
  1850. X                    c = *fmt++;
  1851. X                }
  1852. X        }
  1853. X
  1854. X        /* length modifiers */
  1855. X        if (c == 'h') {
  1856. X            flags |= FF_SHORT;
  1857. X            c = *fmt++;
  1858. X        } else if (c == 'l') {
  1859. X            flags |= FF_LONG;
  1860. X            c = *fmt++;
  1861. X        }
  1862. X
  1863. X        /* do conversion */
  1864. X        switch (c) {
  1865. X          case '%':        /* %% -> % */
  1866. X            putc(c, f);
  1867. X            pos ++;
  1868. X            break;
  1869. X
  1870. X          case 'p':        /* pointer */
  1871. X            *--p = '}';
  1872. X            u = (unsigned long) va_arg(va, Void*);
  1873. X            do {
  1874. X                *--p = "0123456789ABCDEF"[u%16];
  1875. X                u /= 16;
  1876. X            } while (u != 0);
  1877. X            *--p = '{';
  1878. X            flags |= FF_PUTS;
  1879. X            break;
  1880. X
  1881. X          case 'n':        /* save position */
  1882. X            *va_arg(va, int*) = pos;
  1883. X            break;
  1884. X
  1885. X          case 'c':        /* character */
  1886. X            u = (flags&FF_SHORT) ? va_arg(va, unsigned short)
  1887. X              : (flags&&FF_LONG) ? va_arg(va, unsigned long)
  1888. X              : va_arg(va, unsigned int);
  1889. X            *--p = u;
  1890. X            flags |= FF_PUTS;
  1891. X            break;
  1892. X
  1893. X          case 's':        /* string */
  1894. X            p = va_arg(va, char *);
  1895. X            if ((flags&FF_PREC) && strlen(p) > prec) {
  1896. X                pos += prec;
  1897. X                while (--prec >= 0)
  1898. X                    putc(*p++, f);
  1899. X                break;
  1900. X            }
  1901. X            flags |= FF_PUTS;
  1902. X            break;
  1903. X
  1904. X          case 'i': case 'd': case 'u': /* decimal */
  1905. X            if (c != 'u') {    /* signed */
  1906. X                n = (flags&FF_SHORT) ? va_arg(va, short)
  1907. X                  : (flags&&FF_LONG) ? va_arg(va, long)
  1908. X                  : va_arg(va, int);
  1909. X                if (n < 0)
  1910. X                    flags |= FF_NEG;
  1911. X                u = (n < 0) ? -n : n;
  1912. X            } else
  1913. X                u = (flags&FF_SHORT) ? va_arg(va, unsigned short)
  1914. X                  : (flags&&FF_LONG) ? va_arg(va, unsigned long)
  1915. X                  : va_arg(va, unsigned int);
  1916. X            do {
  1917. X                *--p = '0' + u%10;
  1918. X                u /= 10;
  1919. X            } while (u != 0);
  1920. X            prec -= buf+BUF - p;
  1921. X            while (--prec >= 0)
  1922. X                *--p = '0';
  1923. X            if (flags&FF_NEG)
  1924. X                *--p = '-';
  1925. X            else
  1926. X                if (sign != '-')
  1927. X                    *--p = (sign == '+') ? '+' : ' ';
  1928. X            flags |= FF_PUTS;
  1929. X            break;
  1930. X
  1931. X          case 'x': case 'X':    /* hex, Hex */
  1932. X            u = (flags&FF_SHORT) ? va_arg(va, unsigned short)
  1933. X              : (flags&&FF_LONG) ? va_arg(va, unsigned long)
  1934. X              : va_arg(va, unsigned int);
  1935. X            do {
  1936. X                *--p = "0123456789ABCDEF"[u%16];
  1937. X                u /= 16;
  1938. X            } while (u != 0);
  1939. X            prec -= buf+BUF - p;
  1940. X            while (--prec >= 0)
  1941. X                *--p = '0';
  1942. X            if (flags&&FF_ALT)
  1943. X                *--p = 'x', *--p = '0';
  1944. X            flags |= FF_PUTS;
  1945. X            break;
  1946. X
  1947. X          case 'o':        /* octal */
  1948. X            u = (flags&FF_SHORT) ? va_arg(va, unsigned short)
  1949. X              : (flags&&FF_LONG) ? va_arg(va, unsigned long)
  1950. X              : va_arg(va, unsigned int);
  1951. X            do {
  1952. X                *--p = '0' + u%8;
  1953. X                u /= 8;
  1954. X            } while (u != 0);
  1955. X            prec -= buf+BUF - p;
  1956. X            while (--prec >= 0)
  1957. X                *--p = '0';
  1958. X            if (flags&&FF_ALT && *p != '0')
  1959. X                *--p = '0';
  1960. X            flags |= FF_PUTS;
  1961. X            break;
  1962. X
  1963. X          default:        /* todo: error */
  1964. X            putc('%', f);
  1965. X            putc(c, f);
  1966. X            pos += 2;
  1967. X            break;
  1968. X        }
  1969. X
  1970. X        /* copy adjusted string "p" to output */
  1971. X        if (flags&FF_PUTS) {
  1972. X            int len = strlen(p);
  1973. X            int pad = width - len;
  1974. X            if (!(flags&FF_LEFT))
  1975. X                while (--pad >= 0)
  1976. X                    putc(' ', f);
  1977. X            while (*p)
  1978. X                putc(*p++, f);
  1979. X            if ((flags&FF_LEFT))
  1980. X                while (--pad >= 0)
  1981. X                    putc(' ', f);
  1982. X            pos += (len < width) ? width : len;
  1983. X        }
  1984. X        } else {            /* ordinary character */
  1985. X        putc(c, f);
  1986. X        pos ++;
  1987. X        }
  1988. X    return pos;
  1989. X}
  1990. X
  1991. SHAR_EOF
  1992. true || echo 'restore of stdc/vprintf.c failed'
  1993. fi
  1994. # ============= stdc/stddef.h ==============
  1995. if test -f 'stdc/stddef.h' -a X"$1" != X"-c"; then
  1996.     echo 'x - skipping stdc/stddef.h (File already exists)'
  1997. else
  1998. echo 'x - extracting stdc/stddef.h (Text)'
  1999. sed 's/^X//' << 'SHAR_EOF' > 'stdc/stddef.h' &&
  2000. X/* ANSI common definitions */
  2001. X
  2002. X/* $Header$ */
  2003. X
  2004. X#ifndef NULL
  2005. X#if __STDC__
  2006. X#define    NULL    (void*)0
  2007. X#else
  2008. X#define    NULL    0
  2009. X#endif
  2010. X#endif
  2011. X
  2012. X#if ! _STDDEF_H
  2013. X#define    _STDDEF_H 1
  2014. X
  2015. X/* doesn't really belong here, but the library function need it */
  2016. X#if __STDC__
  2017. X#define    ARGS(args)    args
  2018. X#define Void    void
  2019. X#define    Const    const
  2020. X#else
  2021. X#define    ARGS(args)    ()
  2022. X#define    Void    char
  2023. X#define    Const
  2024. X#endif
  2025. X
  2026. Xtypedef unsigned size_t;        /* may need long */
  2027. Xtypedef int ptrdiff_t;
  2028. X
  2029. X#define    offsetof(type,id) ((size_t)&((type*)NULL)->id)
  2030. X
  2031. Xextern    int errno;        /* really belongs in <errno.h> */
  2032. X
  2033. X#endif
  2034. X
  2035. SHAR_EOF
  2036. true || echo 'restore of stdc/stddef.h failed'
  2037. fi
  2038. # ============= stdc/stdio.c ==============
  2039. if test -f 'stdc/stdio.c' -a X"$1" != X"-c"; then
  2040.     echo 'x - skipping stdc/stdio.c (File already exists)'
  2041. else
  2042. echo 'x - extracting stdc/stdio.c (Text)'
  2043. sed 's/^X//' << 'SHAR_EOF' > 'stdc/stdio.c' &&
  2044. X/*
  2045. X * Emulation of misc. ANSI C stdio functions
  2046. X */
  2047. X
  2048. X/* $Header */
  2049. X
  2050. X#include <stdio.h>
  2051. X
  2052. X#if 1
  2053. Xint
  2054. Xremove(name)
  2055. X    Const char *name;
  2056. X{
  2057. X    return unlink(name);
  2058. X}
  2059. X#endif
  2060. X
  2061. X#if _V7
  2062. Xint
  2063. Xrename(oname, name)
  2064. X    Const char *oname, *nname;
  2065. X{
  2066. X    return link(oname, nname) == 0 && unlink(oname) == 0 ? 0 : -1;
  2067. X}
  2068. X#endif
  2069. X
  2070. SHAR_EOF
  2071. true || echo 'restore of stdc/stdio.c failed'
  2072. fi
  2073. true || echo 'restore of stdc/stdio.h failed'
  2074. echo End of part 6, continue with part 7
  2075. exit 0
  2076.