# 2.11BSD#431 - /usr/src/bin/cc.c

##### Functions

    callsys           X
    dexit             X
    error             X
    getsuf            X
    idexit            X
    main              X
    nodup             X
    savestr           X
    setsuf            X
    strspl            X

##### Variables

    Mflag             X
    as                X
    av                X
    c2                X
    ccom              X
    ccom1             X
    cflag             X
    chpass            X
    clist             X
    cpp               X
    crt0              X
    debug             X
    eflag             X
    exfail            X
    exflag            X
    ld                X
    llist             X
    na                X
    nc                X
    nl                X
    np                X
    npassname         X
    nxo               X
    oflag             X
    outfile           X
    pflag             X
    plist             X
    proflag           X
    saveleft          X
    savetab           X
    sflag             X
    tmp0              X
    tmp1              X
    tmp2              X
    tmp3              X
    tmp4              X
    tmp5              X
    wflag             X

##### Macros

    NSAVETAB          X
    cunlink           X

       1: static  char sccsid[] = "@(#)cc.c 4.13 9/18/85";
       2: /*
       3:  * cc - front end for C compiler
       4:  */
       5: #include <sys/param.h>
       6: #include <stdio.h>
       7: #include <ctype.h>
       8: #include <signal.h>
       9: #include <sys/dir.h>
      10:
      11: char    *cpp = "/lib/cpp";
      12: char    *ccom = "/lib/c0";
      13: char    *ccom1 = "/lib/c1";
      14: char    *c2 = "/lib/c2";
      15: char    *as = "/bin/as";
      16: char    *ld = "/bin/ld";
      17: char    *crt0 = "/lib/crt0.o";
      18:
      19: char    tmp0[30];       /* big enough for /tmp/ctm%05.5d */
      20: char    *tmp1, *tmp2, *tmp3, *tmp4, *tmp5;
      21: char    *outfile;
      22: char    *savestr(), *strspl(), *setsuf();
      23: int idexit();
      24: char    **av, **clist, **llist, **plist;
      25: int cflag, eflag, oflag, pflag, sflag, wflag, exflag, proflag;
      26: int Mflag, debug;
      27: int exfail;
      28: char    *chpass;
      29: char    *npassname;
      30:
      31: int nc, nl, np, nxo, na;
      32:
      33: #define cunlink(s)  if (s) unlink(s)
      34:
      35: main(argc, argv)
      36:     char **argv;
      37: {
      38:     char *t;
      39:     char *assource;
      40:     int i, j, c;
      41:
      42:     /* ld currently adds upto 5 args; 10 is room to spare */
      43:     av = (char **)calloc(argc+10, sizeof (char **));
      44:     clist = (char **)calloc(argc, sizeof (char **));
      45:     llist = (char **)calloc(argc, sizeof (char **));
      46:     plist = (char **)calloc(argc, sizeof (char **));
      47:     for (i = 1; i < argc; i++) {
      48:         if (*argv[i] == '-') switch (argv[i][1]) {
      49:
      50:         case 'S':
      51:             sflag++;
      52:             cflag++;
      53:             continue;
      54:         case 'o':
      55:             if (++i < argc) {
      56:                 outfile = argv[i];
      57:                 switch (getsuf(outfile)) {
      58:
      59:                 case 'c':
      60:                     error("-o would overwrite %s",
      61:                         outfile);
      62:                     exit(8);
      63:                 }
      64:             }
      65:             continue;
      66:         case 'O':
      67:             oflag++;
      68:             continue;
      69:         case 'p':
      70:             proflag++;
      71:             crt0 = "/lib/mcrt0.o";
      72:             if (argv[i][2] == 'g')
      73:                 crt0 = "/usr/lib/gcrt0.o";
      74:             continue;
      75:         case 'w':
      76:             wflag++;
      77:             continue;
      78:         case 'E':
      79:             exflag++;
      80:         case 'P':
      81:             pflag++;
      82:             if (argv[i][1]=='P')
      83:                 fprintf(stderr,
      84:     "cc: warning: -P option obsolete; you should use -E instead\n");
      85:             plist[np++] = argv[i];
      86:         case 'c':
      87:             cflag++;
      88:             continue;
      89:         case 'M':
      90:             exflag++;
      91:             pflag++;
      92:             Mflag++;
      93:             /* and fall through */
      94:         case 'D':
      95:         case 'I':
      96:         case 'U':
      97:         case 'C':
      98:             plist[np++] = argv[i];
      99:             continue;
     100:         case 'L':
     101:             llist[nl++] = argv[i];
     102:             continue;
     103:         case 't':
     104:             if (chpass)
     105:                 error("-t overwrites earlier option", 0);
     106:             chpass = argv[i]+2;
     107:             if (chpass[0]==0)
     108:                 chpass = "012p";
     109:             continue;
     110:         case 'B':
     111:             if (npassname)
     112:                 error("-B overwrites earlier option", 0);
     113:             npassname = argv[i]+2;
     114:             if (npassname[0]==0)
     115:                 npassname = "/usr/c/o";
     116:             continue;
     117:         case 'd':
     118:             if (argv[i][2] == '\0') {
     119:                 debug++;
     120:                 continue;
     121:             }
     122:             continue;
     123:         }
     124:         t = argv[i];
     125:         c = getsuf(t);
     126:         if (c=='c' || c=='s' || exflag) {
     127:             clist[nc++] = t;
     128:             t = setsuf(t, 'o');
     129:         }
     130:         if (nodup(llist, t)) {
     131:             llist[nl++] = t;
     132:             if (getsuf(t)=='o')
     133:                 nxo++;
     134:         }
     135:     }
     136:     if (npassname && chpass ==0)
     137:         chpass = "012p";
     138:     if (chpass && npassname==0)
     139:         npassname = "/usr/new";
     140:     if (chpass)
     141:     for (t=chpass; *t; t++) {
     142:         switch (*t) {
     143:
     144:         case '0':
     145:             ccom = strspl(npassname, "c0");
     146:             continue;
     147:         case '1':
     148:             ccom1 = strspl(npassname, "c1");
     149:             continue;
     150:         case '2':
     151:             c2 = strspl(npassname, "c2");
     152:             continue;
     153:         case 'p':
     154:             cpp = strspl(npassname, "cpp");
     155:             continue;
     156:         }
     157:     }
     158:     if (nc==0)
     159:         goto nocom;
     160:     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
     161:         signal(SIGINT, idexit);
     162:     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
     163:         signal(SIGTERM, idexit);
     164:     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
     165:         signal(SIGHUP, idexit);
     166:     if (pflag==0)
     167:         sprintf(tmp0, "/tmp/ctm%05.5d", getpid());
     168:     tmp1 = strspl(tmp0, "1");
     169:     tmp2 = strspl(tmp0, "2");
     170:     tmp3 = strspl(tmp0, "3");
     171:     if (pflag==0)
     172:         tmp4 = strspl(tmp0, "4");
     173:     if (oflag)
     174:         tmp5 = strspl(tmp0, "5");
     175:     for (i=0; i<nc; i++) {
     176:         if (nc > 1 && !Mflag) {
     177:             printf("%s:\n", clist[i]);
     178:             fflush(stdout);
     179:         }
     180:         if (!Mflag && getsuf(clist[i]) == 's') {
     181:             assource = clist[i];
     182:             goto assemble;
     183:         } else
     184:             assource = tmp3;
     185:         if (pflag)
     186:             tmp4 = setsuf(clist[i], 'i');
     187:         av[0] = "cpp"; av[1] = clist[i];
     188:         na = 2;
     189:         if (!exflag)
     190:             av[na++] = tmp4;
     191:         for (j = 0; j < np; j++)
     192:             av[na++] = plist[j];
     193:         av[na++] = 0;
     194:         if (callsys(cpp, av)) {
     195:             exfail++;
     196:             eflag++;
     197:         }
     198:         if (pflag || exfail) {
     199:             cflag++;
     200:             continue;
     201:         }
     202:         if (sflag) {
     203:             if (nc==1 && outfile)
     204:                 tmp3 = outfile;
     205:             else
     206:                 tmp3 = setsuf(clist[i], 's');
     207:             assource = tmp3;
     208:         }
     209:         av[0] = "c0";
     210:         av[1] = tmp4; av[2] = tmp1; av[3] = tmp2; na = 4;
     211:         if (proflag)
     212:             av[na++] = "-P";
     213:         if (wflag)
     214:             av[na++] = "-w";
     215:         av[na] = 0;
     216:         if (callsys(ccom, av)) {
     217:             cflag++;
     218:             eflag++;
     219:             continue;
     220:         }
     221:         av[0] = "c1"; av[1] = tmp1; av[2] = tmp2;
     222:         av[3] = oflag ? tmp5 : tmp3;
     223:         av[4] = 0;
     224:         if (callsys(ccom1, av)) {
     225:             cflag++;
     226:             eflag++;
     227:             continue;
     228:         }
     229:         if (oflag) {
     230:             av[0] = "c2"; av[1] = tmp5; av[2] = tmp3; av[3] = 0;
     231:             if (callsys(c2, av)) {
     232:                 unlink(tmp3);
     233:                 tmp3 = assource = tmp5;
     234:             } else
     235:                 unlink(tmp5);
     236:         }
     237:         if (sflag)
     238:             continue;
     239:     assemble:
     240:         cunlink(tmp1); cunlink(tmp2); cunlink(tmp4);
     241:         av[0] = "as"; av[1] = "-V";
     242:         av[2] = "-u"; av[3] = "-o";
     243:         if (cflag && nc==1 && outfile)
     244:             av[4] = outfile;
     245:         else
     246:             av[4] = setsuf(clist[i], 'o');
     247:         av[5] = assource;
     248:         av[6] = 0;
     249:         if (callsys(as, av) > 1) {
     250:             cflag++;
     251:             eflag++;
     252:             continue;
     253:         }
     254:     }
     255: nocom:
     256:     if (cflag==0 && nl!=0) {
     257:         i = 0;
     258:         av[0] = "ld"; av[1] = "-X"; av[2] = crt0; na = 3;
     259:         if (outfile) {
     260:             av[na++] = "-o";
     261:             av[na++] = outfile;
     262:         }
     263:         while (i < nl)
     264:             av[na++] = llist[i++];
     265:         if (proflag)
     266:             av[na++] = "-lc_p";
     267:         else
     268:             av[na++] = "-lc";
     269:         av[na++] = 0;
     270:         eflag |= callsys(ld, av);
     271:         if (nc==1 && nxo==1 && eflag==0)
     272:             unlink(setsuf(clist[0], 'o'));
     273:     }
     274:     dexit();
     275: }
     276:
     277: idexit()
     278: {
     279:
     280:     eflag = 100;
     281:     dexit();
     282: }
     283:
     284: dexit()
     285: {
     286:
     287:     if (!pflag) {
     288:         cunlink(tmp1);
     289:         cunlink(tmp2);
     290:         if (sflag==0)
     291:             cunlink(tmp3);
     292:         cunlink(tmp4);
     293:         cunlink(tmp5);
     294:     }
     295:     exit(eflag);
     296: }
     297:
     298: error(s, x)
     299:     char *s, *x;
     300: {
     301:     FILE *diag = exflag ? stderr : stdout;
     302:
     303:     fprintf(diag, "cc: ");
     304:     fprintf(diag, s, x);
     305:     putc('\n', diag);
     306:     exfail++;
     307:     cflag++;
     308:     eflag++;
     309: }
     310:
     311: getsuf(as)
     312: char as[];
     313: {
     314:     register int c;
     315:     register char *s;
     316:     register int t;
     317:
     318:     s = as;
     319:     c = 0;
     320:     while (t = *s++)
     321:         if (t=='/')
     322:             c = 0;
     323:         else
     324:             c++;
     325:     s -= 3;
     326:     if (c <= MAXNAMLEN && c > 2 && *s++ == '.')
     327:         return (*s);
     328:     return (0);
     329: }
     330:
     331: char *
     332: setsuf(as, ch)
     333:     char *as;
     334: {
     335:     register char *s, *s1;
     336:
     337:     s = s1 = savestr(as);
     338:     while (*s)
     339:         if (*s++ == '/')
     340:             s1 = s;
     341:     s[-1] = ch;
     342:     return (s1);
     343: }
     344:
     345: callsys(f, v)
     346:     char *f, **v;
     347: {
     348:     int t, status;
     349:     char **cpp;
     350:
     351:     if (debug) {
     352:         fprintf(stderr, "%s:", f);
     353:         for (cpp = v; *cpp != 0; cpp++)
     354:             fprintf(stderr, " %s", *cpp);
     355:         fprintf(stderr, "\n");
     356:     }
     357:     t = vfork();
     358:     if (t == -1) {
     359:         printf("No more processes\n");
     360:         return (100);
     361:     }
     362:     if (t == 0) {
     363:         execv(f, v);
     364:         printf("Can't find %s\n", f);
     365:         fflush(stdout);
     366:         _exit(100);
     367:     }
     368:     while (t != wait(&status))
     369:         ;
     370:     if ((t=(status&0377)) != 0 && t!=14) {
     371:         if (t!=2) {
     372:             printf("Fatal error in %s\n", f);
     373:             eflag = 8;
     374:         }
     375:         dexit();
     376:     }
     377:     return ((status>>8) & 0377);
     378: }
     379:
     380: nodup(l, os)
     381:     char **l, *os;
     382: {
     383:     register char *t, *s;
     384:     register int c;
     385:
     386:     s = os;
     387:     if (getsuf(s) != 'o')
     388:         return (1);
     389:     while (t = *l++) {
     390:         while (c = *s++)
     391:             if (c != *t++)
     392:                 break;
     393:         if (*t==0 && c==0)
     394:             return (0);
     395:         s = os;
     396:     }
     397:     return (1);
     398: }
     399:
     400: #define NSAVETAB    1024
     401: char    *savetab;
     402: int saveleft;
     403:
     404: char *
     405: savestr(cp)
     406:     register char *cp;
     407: {
     408:     register int len;
     409:
     410:     len = strlen(cp) + 1;
     411:     if (len > saveleft) {
     412:         saveleft = NSAVETAB;
     413:         if (len > saveleft)
     414:             saveleft = len;
     415:         savetab = (char *)malloc(saveleft);
     416:         if (savetab == 0) {
     417:             fprintf(stderr, "ran out of memory (savestr)\n");
     418:             exit(1);
     419:         }
     420:     }
     421:     strncpy(savetab, cp, len);
     422:     cp = savetab;
     423:     savetab += len;
     424:     saveleft -= len;
     425:     return (cp);
     426: }
     427:
     428: char *
     429: strspl(left, right)
     430:     char *left, *right;
     431: {
     432:     char buf[BUFSIZ];
     433:
     434:     strcpy(buf, left);
     435:     strcat(buf, right);
     436:     return (savestr(buf));
     437: }

#### Defined functions

callsys defined in line 345; used 6 times

- in line 194, 216, 224, 231, 249, 270

dexit defined in line 284; used 3 times

- in line 274, 281, 375

error defined in line 298; used 3 times

- in line 60, 105, 112

getsuf defined in line 311; used 5 times

- in line 57, 125, 132, 180, 387

idexit defined in line 277; used 4 times

- in line 23, 161-165(3)

main defined in line 35; never used
nodup defined in line 380; used 1 times

- in line 130

savestr defined in line 404; used 3 times

- in line 22, 337, 436

setsuf defined in line 331; used 6 times

- in line 22, 128, 186, 206, 246, 272

strspl defined in line 428; used 10 times

- in line 22, 145-154(4), 168-174(5)

#### Defined variables

Mflag defined in line 26; used 3 times

- in line 92, 176-180(2)

as defined in line 15; used 7 times

- in line 249, 311-312(2), 318, 332-337(3)

av defined in line 24; used 45 times

- in line 43, 187-194(6), 209-224(14), 230-231(5), 241-249(9), 258-270(10)

c2 defined in line 14; used 2 times

- in line 151, 231

ccom defined in line 12; used 2 times

- in line 145, 216

ccom1 defined in line 13; used 2 times

- in line 148, 224

cflag defined in line 25; used 9 times

- in line 52, 87, 199, 217, 225, 243, 250, 256, 307

chpass defined in line 28; used 9 times

- in line 104-108(4), 136-141(5)

clist defined in line 24; used 10 times

- in line 44, 127, 177-187(5), 206, 246, 272

cpp defined in line 11; used 7 times

- in line 154, 194, 349-354(5)

crt0 defined in line 17; used 3 times

- in line 71-73(2), 258

debug defined in line 26; used 2 times

- in line 119, 351

eflag defined in line 25; used 10 times

- in line 196, 218, 226, 251, 270-271(2), 280, 295, 308, 373

exfail defined in line 27; used 3 times

- in line 195-198(2), 306

exflag defined in line 25; used 5 times

- in line 79, 90, 126, 189, 301

ld defined in line 16; used 1 times

- in line 270

llist defined in line 24; used 5 times

- in line 45, 101, 130-131(2), 264

na defined in line 31; used 15 times

- in line 188-193(4), 210-215(4), 258-269(7)

nc defined in line 31; used 7 times

- in line 127, 158, 175-176(2), 203, 243, 271

nl defined in line 31; used 4 times

- in line 101, 131, 256, 263

np defined in line 31; used 3 times

- in line 85, 98, 191

npassname defined in line 29; used 11 times

- in line 111-115(4), 136-139(3), 145-154(4)

nxo defined in line 31; used 2 times

- in line 133, 271

oflag defined in line 25; used 4 times

- in line 67, 173, 222, 229

outfile defined in line 21; used 9 times

- in line 56-61(3), 203-204(2), 243-244(2), 259-261(2)

pflag defined in line 25; used 7 times

- in line 81, 91, 166-171(2), 185, 198, 287

plist defined in line 24; used 4 times

- in line 46, 85, 98, 192

proflag defined in line 25; used 3 times

- in line 70, 211, 265

saveleft defined in line 402; used 6 times

- in line 411-415(5), 424

savetab defined in line 401; used 5 times

- in line 415-423(5)

sccsid defined in line 1; never used
sflag defined in line 25; used 4 times

- in line 51, 202, 237, 290

tmp0 defined in line 19; used 6 times

- in line 167-174(6)

tmp1 defined in line 20; used 5 times

- in line 168, 210, 221, 240, 288

tmp2 defined in line 20; used 5 times

- in line 169, 210, 221, 240, 289

tmp3 defined in line 20; used 10 times

- in line 170, 184, 204-207(3), 222, 230-233(3), 291

tmp4 defined in line 20; used 6 times

- in line 172, 186-190(2), 210, 240, 292

tmp5 defined in line 20; used 6 times

- in line 174, 222, 230-235(3), 293

wflag defined in line 25; used 2 times

- in line 76, 213

#### Defined macros

NSAVETAB defined in line 400; used 1 times

- in line 412

cunlink defined in line 33; used 8 times

- in line 240(3), 288-293(5)

Last modified: 1987-02-21 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 2518 |