# 2.11BSD#431 - /usr/src/bin/csh/sh.misc.c

##### Functions

    any               X
    blkcat            X
    blkcpy            X
    blkend            X
    blkfree           X
    blklen            X
    blkpr             X
    blkspl            X
    calloc            X
    closem            X
    copy              X
    copyblk           X
    dcopy             X
    dmove             X
    donefds           X
    lastchr           X
    lshift            X
    nomem             X
    number            X
    onlyread          X
    prefix            X
    renum             X
    saveblk           X
    savestr           X
    strend            X
    strip             X
    strspl            X
    udvar             X
    xfree             X

       1: /*
       2:  * Copyright (c) 1980 Regents of the University of California.
       3:  * All rights reserved.  The Berkeley Software License Agreement
       4:  * specifies the terms and conditions for redistribution.
       5:  */
       6:
       7: #if !defined(lint) && defined(DOSCCS)
       8: static char *sccsid = "@(#)sh.misc.c	5.3 (Berkeley) 3/29/86";
       9: #endif
      10:
      11: #include "sh.h"
      12:
      13: /*
      14:  * C Shell
      15:  */
      16:
      17: any(c, s)
      18:     register int c;
      19:     register char *s;
      20: {
      21:
      22:     while (*s)
      23:         if (*s++ == c)
      24:             return(1);
      25:     return(0);
      26: }
      27:
      28: onlyread(cp)
      29:     char *cp;
      30: {
      31:     extern char end[];
      32:
      33:     return (cp < end);
      34: }
      35:
      36: xfree(cp)
      37:     char *cp;
      38: {
      39:     extern char end[];
      40:
      41:     if (cp >= end && cp < (char *) &cp)
      42:         free(cp);
      43: }
      44:
      45: char *
      46: savestr(s)
      47:     register char *s;
      48: {
      49:     char *n;
      50:     register char *p;
      51:
      52:     if (s == 0)
      53:         s = "";
      54:     for (p = s; *p++;)
      55:         ;
      56:     n = p = xalloc((unsigned) (p - s));
      57:     while (*p++ = *s++)
      58:         ;
      59:     return (n);
      60: }
      61:
      62: char *
      63: calloc(i, j)
      64:     register unsigned i;
      65:     unsigned j;
      66: {
      67:     char *cp;
      68:
      69:     i *= j;
      70:     cp = xalloc(i);
      71:     bzero(cp, (int)i);
      72:     return (cp);
      73: }
      74:
      75: nomem(i)
      76:     unsigned i;
      77: {
      78: #ifdef debug
      79:     static char *av[2] = {0, 0};
      80: #endif
      81:
      82:     child++;
      83: #ifndef debug
      84:     error("Out of memory");
      85: #ifdef lint
      86:     i = i;
      87: #endif
      88: #else
      89:     showall(av);
      90:     printf("i=%d: Out of memory\n", i);
      91:     chdir("/usr/bill/cshcore");
      92:     abort();
      93: #endif
      94:     return 0;       /* fool lint */
      95: }
      96:
      97: char **
      98: blkend(up)
      99:     register char **up;
     100: {
     101:
     102:     while (*up)
     103:         up++;
     104:     return (up);
     105: }
     106:
     107: blkpr(av)
     108:     register char **av;
     109: {
     110:
     111:     for (; *av; av++) {
     112:         printf("%s", *av);
     113:         if (av[1])
     114:             printf(" ");
     115:     }
     116: }
     117:
     118: blklen(av)
     119:     register char **av;
     120: {
     121:     register int i = 0;
     122:
     123:     while (*av++)
     124:         i++;
     125:     return (i);
     126: }
     127:
     128: char **
     129: blkcpy(oav, bv)
     130:     char **oav;
     131:     register char **bv;
     132: {
     133:     register char **av = oav;
     134:
     135:     while (*av++ = *bv++)
     136:         continue;
     137:     return (oav);
     138: }
     139:
     140: char **
     141: blkcat(up, vp)
     142:     char **up, **vp;
     143: {
     144:
     145:     (void) blkcpy(blkend(up), vp);
     146:     return (up);
     147: }
     148:
     149: blkfree(av0)
     150:     char **av0;
     151: {
     152:     register char **av = av0;
     153:
     154:     for (; *av; av++)
     155:         XFREE(*av)
     156:     XFREE((char *)av0)
     157: }
     158:
     159: char **
     160: saveblk(v)
     161:     register char **v;
     162: {
     163:     register char **newv =
     164:         (char **) calloc((unsigned) (blklen(v) + 1), sizeof (char **));
     165:     char **onewv = newv;
     166:
     167:     while (*v)
     168:         *newv++ = savestr(*v++);
     169:     return (onewv);
     170: }
     171:
     172: char *
     173: strspl(cp, dp)
     174:     char *cp, *dp;
     175: {
     176:     char *ep;
     177:     register char *p, *q;
     178:
     179:     for (p = cp; *p++;)
     180:         ;
     181:     for (q = dp; *q++;)
     182:         ;
     183:     ep = xalloc((unsigned) ((p - cp) + (q - dp) - 1));
     184:     for (p = ep, q = cp; *p++ = *q++;)
     185:         ;
     186:     for (p--, q = dp; *p++ = *q++;)
     187:         ;
     188:     return (ep);
     189: }
     190:
     191: char **
     192: blkspl(up, vp)
     193:     register char **up, **vp;
     194: {
     195:     register char **wp =
     196:         (char **) calloc((unsigned) (blklen(up) + blklen(vp) + 1),
     197:             sizeof (char **));
     198:
     199:     (void) blkcpy(wp, up);
     200:     return (blkcat(wp, vp));
     201: }
     202:
     203: lastchr(cp)
     204:     register char *cp;
     205: {
     206:
     207:     if (!*cp)
     208:         return (0);
     209:     while (cp[1])
     210:         cp++;
     211:     return (*cp);
     212: }
     213:
     214: /*
     215:  * This routine is called after an error to close up
     216:  * any units which may have been left open accidentally.
     217:  */
     218: closem()
     219: {
     220:     register int f;
     221:
     222:     for (f = 0; f < NOFILE; f++)
     223:         if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD &&
     224:             f != FSHTTY)
     225:             (void) close(f);
     226: }
     227:
     228: donefds()
     229: {
     230:
     231:     (void) close(0);
     232:     (void) close(1);
     233:     (void) close(2);
     234:     didfds = 0;
     235: }
     236:
     237: /*
     238:  * Move descriptor i to j.
     239:  * If j is -1 then we just want to get i to a safe place,
     240:  * i.e. to a unit > 2.  This also happens in dcopy.
     241:  */
     242: dmove(i, j)
     243:     register int i, j;
     244: {
     245:
     246:     if (i == j || i < 0)
     247:         return (i);
     248:     if (j >= 0) {
     249:         (void) dup2(i, j);
     250:         return (j);
     251:     }
     252:     j = dcopy(i, j);
     253:     if (j != i)
     254:         (void) close(i);
     255:     return (j);
     256: }
     257:
     258: dcopy(i, j)
     259:     register int i, j;
     260: {
     261:
     262:     if (i == j || i < 0 || j < 0 && i > 2)
     263:         return (i);
     264:     if (j >= 0) {
     265:         (void) dup2(i, j);
     266:         return (j);
     267:     }
     268:     (void) close(j);
     269:     return (renum(i, j));
     270: }
     271:
     272: renum(i, j)
     273:     register int i, j;
     274: {
     275:     register int k = dup(i);
     276:
     277:     if (k < 0)
     278:         return (-1);
     279:     if (j == -1 && k > 2)
     280:         return (k);
     281:     if (k != j) {
     282:         j = renum(k, j);
     283:         (void) close(k);
     284:         return (j);
     285:     }
     286:     return (k);
     287: }
     288:
     289: #ifndef copy
     290: copy(to, from, size)
     291:     register char *to, *from;
     292:     register int size;
     293: {
     294:
     295:     if (size)
     296:         do
     297:             *to++ = *from++;
     298:         while (--size != 0);
     299: }
     300: #endif
     301:
     302: /*
     303:  * Left shift a command argument list, discarding
     304:  * the first c arguments.  Used in "shift" commands
     305:  * as well as by commands like "repeat".
     306:  */
     307: lshift(v, c)
     308:     register char **v;
     309:     register int c;
     310: {
     311:     register char **u = v;
     312:
     313:     while (*u && --c >= 0)
     314:         xfree(*u++);
     315:     (void) blkcpy(v, u);
     316: }
     317:
     318: number(cp)
     319:     char *cp;
     320: {
     321:
     322:     if (*cp == '-') {
     323:         cp++;
     324:         if (!digit(*cp++))
     325:             return (0);
     326:     }
     327:     while (*cp && digit(*cp))
     328:         cp++;
     329:     return (*cp == 0);
     330: }
     331:
     332: char **
     333: copyblk(v)
     334:     register char **v;
     335: {
     336:     register char **nv =
     337:         (char **) calloc((unsigned) (blklen(v) + 1), sizeof (char **));
     338:
     339:     return (blkcpy(nv, v));
     340: }
     341:
     342: char *
     343: strend(cp)
     344:     register char *cp;
     345: {
     346:
     347:     while (*cp)
     348:         cp++;
     349:     return (cp);
     350: }
     351:
     352: char *
     353: strip(cp)
     354:     char *cp;
     355: {
     356:     register char *dp = cp;
     357:
     358:     while (*dp++ &= TRIM)
     359:         continue;
     360:     return (cp);
     361: }
     362:
     363: udvar(name)
     364:     char *name;
     365: {
     366:
     367:     setname(name);
     368:     bferr("Undefined variable");
     369: }
     370:
     371: prefix(sub, str)
     372:     register char *sub, *str;
     373: {
     374:
     375:     for (;;) {
     376:         if (*sub == 0)
     377:             return (1);
     378:         if (*str == 0)
     379:             return (0);
     380:         if (*sub++ != *str++)
     381:             return (0);
     382:     }
     383: }

#### Defined functions

any defined in line 17; used 27 times

- in /usr/src/bin/csh/sh.dol.c line 433, 489, 650, 667
- in /usr/src/bin/csh/sh.exec.c line 47
- in /usr/src/bin/csh/sh.exec2.c line 25, 213
- in /usr/src/bin/csh/sh.exp.c line 252, 425
- in /usr/src/bin/csh/sh.glob.c line 73
- in /usr/src/bin/csh/sh.lex.c line 335, 402, 479, 570, 768, 876, 927, 935
- in /usr/src/bin/csh/sh.parse.c line 46, 176, 490, 549, 566
- in /usr/src/bin/csh/sh.sem.c line 73
- in /usr/src/bin/csh/sh.set.c line 174-179(2)
- in /usr/src/bin/csh/sh.time.c line 55

blkcat defined in line 140; used 2 times

- in line 200
- in /usr/src/bin/csh/sh.h line 397

blkcpy defined in line 128; used 7 times

- in line 145, 199, 315, 339
- in /usr/src/bin/csh/sh.h line 398
- in /usr/src/bin/csh/sh.lex.c line 1207, 1276

blkend defined in line 97; used 2 times

- in line 145
- in /usr/src/bin/csh/sh.h line 399

blkfree defined in line 149; used 21 times

- in /usr/src/bin/csh/sh.c line 654-656(2)
- in /usr/src/bin/csh/sh.dol.c line 64, 89, 702
- in /usr/src/bin/csh/sh.err.c line 49-51(2)
- in /usr/src/bin/csh/sh.func.c line 704, 763, 1150
- in /usr/src/bin/csh/sh.glob.c line 54, 88, 573
- in /usr/src/bin/csh/sh.sem.c line 161-163(2), 323-325(2)
- in /usr/src/bin/csh/sh.set.c line 393-396(2), 413, 479

blklen defined in line 118; used 10 times

- in line 164, 196(2), 337
- in /usr/src/bin/csh/sh.c line 852
- in /usr/src/bin/csh/sh.dol.c line 410
- in /usr/src/bin/csh/sh.func.c line 70, 354
- in /usr/src/bin/csh/sh.set.c line 131, 682

blkpr defined in line 107; used 8 times

- in /usr/src/bin/csh/sh.exec.c line 250
- in /usr/src/bin/csh/sh.exec2.c line 143
- in /usr/src/bin/csh/sh.exp.c line 583, 593
- in /usr/src/bin/csh/sh.func.c line 147
- in /usr/src/bin/csh/sh.glob.c line 43, 51
- in /usr/src/bin/csh/sh.set.c line 687

blkspl defined in line 191; used 5 times

- in /usr/src/bin/csh/sh.exec.c line 79, 207
- in /usr/src/bin/csh/sh.func.c line 823, 842
- in /usr/src/bin/csh/sh.h line 400

calloc defined in line 62; used 26 times

- in line 164, 196, 337
- in /usr/src/bin/csh/sh.c line 395
- in /usr/src/bin/csh/sh.dir.c line 44, 120, 237
- in /usr/src/bin/csh/sh.exp.c line 500
- in /usr/src/bin/csh/sh.file.c line 401
- in /usr/src/bin/csh/sh.func.c line 361, 396
- in /usr/src/bin/csh/sh.h line 401
- in /usr/src/bin/csh/sh.lex.c line 663, 1203, 1211, 1331-1333(2)
- in /usr/src/bin/csh/sh.parse.c line 230, 237, 280, 320, 360, 402, 503-504(2)
- in /usr/src/bin/csh/sh.proc.c line 347

closem defined in line 218; used 2 times

- in /usr/src/bin/csh/sh.c line 720, 904

copy defined in line 290; never used
copyblk defined in line 332; used 5 times

- in /usr/src/bin/csh/sh.dol.c line 107
- in /usr/src/bin/csh/sh.func.c line 1134
- in /usr/src/bin/csh/sh.glob.c line 57, 588
- in /usr/src/bin/csh/sh.h line 404

dcopy defined in line 258; used 10 times

- in line 252
- in /usr/src/bin/csh/sh.c line 303, 900-903(4)
- in /usr/src/bin/csh/sh.err.c line 101
- in /usr/src/bin/csh/sh.sem.c line 252-254(3)

dmove defined in line 242; used 7 times

- in /usr/src/bin/csh/sh.c line 207, 422, 821
- in /usr/src/bin/csh/sh.glob.c line 653-654(2)
- in /usr/src/bin/csh/sh.sem.c line 394-395(2)

donefds defined in line 228; used 4 times

- in /usr/src/bin/csh/sh.c line 464
- in /usr/src/bin/csh/sh.func.c line 240, 483
- in /usr/src/bin/csh/sh.sem.c line 314

lastchr defined in line 203; used 3 times

- in /usr/src/bin/csh/sh.func.c line 29, 587, 598

lshift defined in line 307; used 8 times

- in /usr/src/bin/csh/sh.exec.c line 237
- in /usr/src/bin/csh/sh.func.c line 238, 476
- in /usr/src/bin/csh/sh.sem.c line 77, 83, 91, 97
- in /usr/src/bin/csh/sh.set.c line 537

nomem defined in line 75; used 1 times

- in /usr/src/bin/csh/sh.h line 394

number defined in line 318; used 1 times

- in /usr/src/bin/csh/sh.c line 853

onlyread defined in line 28; used 1 times

- in /usr/src/bin/csh/sh.set.c line 377

prefix defined in line 371; used 6 times

- in /usr/src/bin/csh/sh.dir.c line 89, 145(2)
- in /usr/src/bin/csh/sh.func.c line 895
- in /usr/src/bin/csh/sh.proc.c line 951-954(2)

renum defined in line 272; used 2 times

- in line 269, 282

saveblk defined in line 159; used 4 times

- in /usr/src/bin/csh/sh.c line 91
- in /usr/src/bin/csh/sh.func.c line 153
- in /usr/src/bin/csh/sh.h line 434
- in /usr/src/bin/csh/sh.set.c line 68

savestr defined in line 45; used 45 times

- in line 168
- in /usr/src/bin/csh/sh.c line 78, 84-86(2), 402
- in /usr/src/bin/csh/sh.dir.c line 45, 110, 160, 168, 470
- in /usr/src/bin/csh/sh.dol.c line 88
- in /usr/src/bin/csh/sh.exec.c line 222
- in /usr/src/bin/csh/sh.exp.c line 485, 506
- in /usr/src/bin/csh/sh.func.c line 104, 364, 462
- in /usr/src/bin/csh/sh.glob.c line 551, 602, 743
- in /usr/src/bin/csh/sh.h line 435
- in /usr/src/bin/csh/sh.lex.c line 133, 268, 671, 699, 742-744(2), 760, 769-776(3), 785-790(3)
- in /usr/src/bin/csh/sh.parse.c line 555, 573, 583
- in /usr/src/bin/csh/sh.proc.c line 361
- in /usr/src/bin/csh/sh.set.c line 73-75(2), 168, 227, 288, 377, 420

strend defined in line 342; used 5 times

- in /usr/src/bin/csh/sh.glob.c line 146
- in /usr/src/bin/csh/sh.h line 438
- in /usr/src/bin/csh/sh.lex.c line 729, 770, 781

strip defined in line 352; used 9 times

- in /usr/src/bin/csh/sh.exec2.c line 210
- in /usr/src/bin/csh/sh.func.c line 100, 145, 153, 348, 600
- in /usr/src/bin/csh/sh.glob.c line 541
- in /usr/src/bin/csh/sh.h line 440
- in /usr/src/bin/csh/sh.set.c line 531

strspl defined in line 172; used 14 times

- in /usr/src/bin/csh/sh.c line 97, 421
- in /usr/src/bin/csh/sh.err.c line 138
- in /usr/src/bin/csh/sh.exec.c line 107, 126
- in /usr/src/bin/csh/sh.exec2.c line 33, 225, 235-236(2)
- in /usr/src/bin/csh/sh.func.c line 815-822(3)
- in /usr/src/bin/csh/sh.h line 441
- in /usr/src/bin/csh/sh.parse.c line 128

udvar defined in line 363; used 4 times

- in /usr/src/bin/csh/sh.dol.c line 406
- in /usr/src/bin/csh/sh.set.c line 130, 466, 534

xfree defined in line 36; used 70 times

- in line 314
- in /usr/src/bin/csh/sh.c line 425, 522-523(2), 822
- in /usr/src/bin/csh/sh.dir.c line 159, 167-172(2), 198, 318(2), 440
- in /usr/src/bin/csh/sh.dol.c line 481, 520
- in /usr/src/bin/csh/sh.err.c line 63
- in /usr/src/bin/csh/sh.exec.c line 60, 134, 150-151(2), 209
- in /usr/src/bin/csh/sh.exec2.c line 45, 51, 59, 227-239(4), 247
- in /usr/src/bin/csh/sh.exp.c line 187-191(3), 233(2), 262(2), 297(2), 342(2), 364, 374, 450, 479
- in /usr/src/bin/csh/sh.func.c line 93, 287, 309, 603, 706-707(2), 790, 816-818(2), 824, 844-845(2)
- in /usr/src/bin/csh/sh.glob.c line 548, 674
- in /usr/src/bin/csh/sh.hist.c line 75
- in /usr/src/bin/csh/sh.lex.c line 1208, 1275
- in /usr/src/bin/csh/sh.proc.c line 172-176(2), 868
- in /usr/src/bin/csh/sh.sem.c line 157-159(3), 347, 363
- in /usr/src/bin/csh/sh.set.c line 119, 229

#### Defined variables

sccsid defined in line 8; never used

Last modified: 1991-08-31 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 1597 |