Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20080318044712

Diff

Diff from 1.82 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/pcc/pcc/cc/cc/cc.c

Annotated File View

gmcgarry
1.82
1 /*      $Id: cc.c,v 1.82 2008/03/18 04:47:12 gmcgarry Exp $     */
ragge
1.3
2 /*
3  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code and documentation must retain the above
10  * copyright notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditionsand the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * All advertising materials mentioning features or use of this software
15  * must display the following acknowledgement:
16  *      This product includes software developed or owned by Caldera
17  *      International, Inc.
18  * Neither the name of Caldera International, Inc. nor the names of other
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
ragge
1.2
35
ragge
1.1
36 /*
ragge
1.3
37  * Front-end to the C compiler.
ragge
1.18
38  *
39  * Brief description of its syntax:
40  * - Files that end with .c are passed via cpp->ccom->as->ld
ragge
1.59
41  * - Files that end with .i are passed via ccom->as->ld
ragge
1.18
42  * - Files that end with .s are passed as->ld
43  * - Files that end with .o are passed directly to ld
44  * - Multiple files may be given on the command line.
45  * - Unrecognized options are all sent directly to ld.
46  * -c or -S cannot be combined with -o if multiple files are given.
47  *
48  * This file should be rewritten readable.
ragge
1.1
49  */
ragge
1.80
50 #include "config.h"
51
ragge
1.9
52 #include <sys/types.h>
ragge
1.36
53 #include <sys/wait.h>
ragge
1.2
54
ragge
1.1
55 #include <ctype.h>
stefan
1.72
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <libgen.h>
ragge
1.1
59 #include <signal.h>
stefan
1.72
60 #include <stdarg.h>
61 #include <stdio.h>
62 #include <stdlib.h>
ragge
1.4
63 #include <string.h>
64 #include <unistd.h>
ragge
1.24
65
66 #include "ccconfig.h"
ragge
1.3
67 /* C command */
ragge
1.1
68
ragge
1.20
69 #define MKS(x) _MKS(x)
70 #define _MKS(x) #x
71
72 /*
73  * Many specific definitions, should be declared elsewhere.
74  */
gmcgarry
1.79
75
76 #ifndef STDINC
77 #define STDINC          "/usr/include/"
78 #endif
79
80 #ifndef PREPROCESSOR
81 #define PREPROCESSOR    "cpp"
82 #endif
83
84 #ifndef COMPILER
85 #define COMPILER        "ccom";
86 #endif
87
88 #ifndef ASSEMBLER
89 #define ASSEMBLER       "as"
90 #endif
91
92 #ifndef LINKER
93 #define LINKER          "ld"
94 #endif
ragge
1.20
95
ragge
1.62
96 #define MAXFIL 10000
ragge
1.12
97 #define MAXLIB 10000
ragge
1.14
98 #define MAXAV  10000
ragge
1.3
99 #define MAXOPT 100
100 char    *tmp3;
101 char    *tmp4;
ragge
1.1
102 char    *outfile;
stefan
1.72
103 char *copy(char *),*setsuf(char *, char);
104 int getsuf(char *);
ragge
1.4
105 int main(intchar *[]);
ragge
1.36
106 void error(char *, ...);
stefan
1.72
107 void errorx(intchar *, ...);
ragge
1.4
108 int callsys(char [], char *[]);
109 int cunlink(char *);
stefan
1.72
110 void dexit(int);
ragge
1.4
111 void idexit(int);
pj
1.30
112 char *gettmp();
ragge
1.14
113 char    *av[MAXAV];
ragge
1.3
114 char    *clist[MAXFIL];
115 char    *llist[MAXLIB];
ragge
1.4
116 char    alist[20];
ragge
1.43
117 char    *xlist[100];
118 int     xnum;
gmcgarry
1.78
119 char    *mlist[100];
120 int     nm;
ragge
1.63
121 int     Cflag;
ragge
1.43
122 int     dflag;
ragge
1.3
123 int     pflag;
124 int     sflag;
125 int     cflag;
126 int     eflag;
127 int     gflag;
ragge
1.5
128 int     vflag;
ragge
1.7
129 int     tflag;
ragge
1.8
130 int     Eflag;
ragge
1.4
131 int     Oflag;
ragge
1.58
132 int     kflag;  /* generate PIC/pic code */
133 #define F_PIC   1
134 #define F_pic   2
ragge
1.51
135 int     Mflag;  /* dependencies only */
ragge
1.52
136 int     pgflag;
ragge
1.1
137 int     exfail;
ragge
1.10
138 int     Xflag;
gmcgarry
1.82
139 int     Werror;
ragge
1.14
140 int     nostartfilesBstatic;
ragge
1.52
141 int     nostdincnostdlib;
ragge
1.43
142 int     onlyas;
ragge
1.52
143 int     pthreads;
ragge
1.14
144
gmcgarry
1.79
145 char    *passp = LIBEXECDIR "/" PREPROCESSOR;
146 char    *pass0 = LIBEXECDIR "/" COMPILER;
147 char    *as = ASSEMBLER;
148 char    *ld = LINKER;
ragge
1.47
149 char    *Bflag;
ragge
1.25
150 char *cppadd[] = CPPADD;
151 char *dynlinker[] = DYNLINKER;
152 char *crt0file = CRT0FILE;
gmcgarry
1.81
153 char *crt0file_profile = CRT0FILE_PROFILE;
ragge
1.25
154 char *startfiles[] = STARTFILES;
155 char *endfiles[] = ENDFILES;
156 char *cppmdadd[] = CPPMDADD;
ragge
1.50
157 #ifdef LIBCLIBS
158 char *libclibs[] = LIBCLIBS;
159 #else
160 char *libclibs[] = { "-lc"NULL };
161 #endif
gmcgarry
1.81
162 #ifdef LIBCLIBS_PROFILE
163 char *libclibs_profile[] = LIBCLIBS_PROFILE;
164 #else
165 char *libclibs_profile[] = { "-lc_p"NULL };
166 #endif
ragge
1.48
167 #ifndef STARTLABEL
168 #define STARTLABEL "__start"
169 #endif
ragge
1.1
170
ragge
1.4
171 int
pj
1.26
172 main(int argcchar *argv[])
173 {
ragge
1.15
174         char *t, *u;
ragge
1.1
175         char *assource;
ragge
1.3
176         char **pv, *ptemp[MAXOPT], **pvt;
ragge
1.36
177         int ncnlijcnxona;
ragge
1.3
178
ragge
1.36
179         i = nc = nl = nxo = 0;
ragge
1.3
180         pv = ptemp;
181         while(++i < argc) {
ragge
1.66
182                 if (argv[i][0] == '-') {
183                         switch (argv[i][1]) {
184                         default:
185                                 goto passa;
186 #ifdef notyet
187         /* must add library options first (-L/-l/...) */
188                                 error("unrecognized option `-%c'"argv[i][1]);
189                                 break;
190 #endif
ragge
1.22
191
ragge
1.66
192                         case 'B'/* other search paths for binaries */
193                                 Bflag = &argv[i][2];
194                                 break;
ragge
1.22
195
ragge
1.66
196                         case 'X':
197                                 Xflag++;
198                                 break;
199                         case 'W'/* Ignore (most of) W-flags */
gmcgarry
1.82
200                                 if (strncmp(argv[i], "-Werror"7) == 0) {
201                                         Werror = 1;
202                                 } else if (strncmp(argv[i], "-Wl,"4) == 0) {
ragge
1.66
203                                         /* options to the linker */
204                                         t = &argv[i][4];
205                                         while ((u = strchr(t','))) {
206                                                 *u++ = 0;
207                                                 llist[nl++] = t;
208                                                 t = u;
209                                         }
ragge
1.15
210                                         llist[nl++] = t;
211                                 }
ragge
1.66
212                                 break;
ragge
1.15
213
ragge
1.66
214                         case 'f'/* GCC compatibility flags */
215                                 if (strcmp(argv[i], "-fPIC") == 0)
216                                         kflag = F_PIC;
217                                 if (strcmp(argv[i], "-fpic") == 0)
218                                         kflag = F_pic;
219                                 /* silently ignore the rest */
220                                 break;
ragge
1.14
221
ragge
1.66
222                         case 'g'/* create debug output */
223                                 gflag++;
224                                 break;
ragge
1.25
225
ragge
1.66
226                         case 'i':
227                                 if (strcmp(argv[i], "-isystem") == 0) {
228                                         *pv++ = "-S";
229                                         *pv++ = argv[++i];
230                                 } else if (strcmp(argv[i], "-include") == 0) {
231                                         *pv++ = "-i";
232                                         *pv++ = argv[++i];
233                                 } else
234                                         goto passa;
235                                 break;
ragge
1.20
236
ragge
1.66
237                         case 'k'/* generate PIC code */
238                                 kflag = F_pic;
239                                 break;
ragge
1.49
240
gmcgarry
1.78
241                         case 'm'/* target-dependent options */
242                                 mlist[nm++] = argv[i];
243                                 break;
244
ragge
1.66
245                         case 'n'/* handle -n flags */
246                                 if (strcmp(argv[i], "-nostdinc") == 0)
247                                         nostdinc++;
248                                 else if (strcmp(argv[i], "-nostdlib") == 0) {
249                                         nostdlib++;
250                                         nostartfiles++;
251                                 } else if (strcmp(argv[i], "-nostartfiles") == 0)
252                                         nostartfiles = 1;
253                                 else
254                                         goto passa;
255                                 break;
ragge
1.14
256
ragge
1.66
257                         case 'p':
258                                 if (strcmp(argv[i], "-pg") == 0)
259                                         pgflag++;
260                                 else if (strcmp(argv[i], "-pthread") == 0)
261                                         pthreads++;
gmcgarry
1.81
262                                 else if (strcmp(argv[i], "-pipe") == 0) {
263                                 } else
ragge
1.66
264                                         errorx(1"unknown option %s"argv[i]);
265                                 break;
ragge
1.52
266
ragge
1.66
267                         case 'x':
268                                 xlist[xnum++] = argv[i];
269                                 break;
270                         case 't':
271                                 tflag++;
272                                 break;
273                         case 'S':
274                                 sflag++;
275                                 cflag++;
276                                 break;
277                         case 'o':
278                                 if (outfile)
279                                         errorx(8"too many -o");
280                                 outfile = argv[++i];
281                                 break;
282                         case 'O':
283                                 Oflag++;
284                                 break;
285                         case 'E':
286                                 Eflag++;
287                                 break;
288                         case 'P':
289                                 pflag++;
290                                 *pv++ = argv[i];
291                         case 'c':
292                                 cflag++;
293                                 break;
ragge
1.3
294
ragge
1.24
295 #if 0
ragge
1.66
296                         case '2':
297                                 if(argv[i][2] == '\0')
298                                         pref = "/lib/crt2.o";
299                                 else {
300                                         pref = "/lib/crt20.o";
301                                 }
302                                 break;
ragge
1.24
303 #endif
ragge
1.66
304                         case 'C':
305                                 Cflag = 1;
306                                 break;
307                         case 'D':
308                         case 'I':
309                         case 'U':
310                                 *pv++ = argv[i];
311                                 if (argv[i][2] == 0)
312                                         *pv++ = argv[++i];
313                                 if (pv >= ptemp+MAXOPT) {
314                                         error("Too many DIU options");
315                                         --pv;
ragge
1.3
316                                 }
ragge
1.66
317                                 break;
ragge
1.3
318
ragge
1.66
319                         case 'M':
320                                 Mflag++;
321                                 break;
ragge
1.51
322
ragge
1.66
323                         case 'd':
324                                 dflag++;
ragge
1.67
325                                 strlcpy(alistargv[i], sizeof (alist));
ragge
1.66
326                                 break;
327                         case 'v':
328                                 printf("%s\n"VERSSTR);
329                                 vflag++;
330                                 break;
ragge
1.14
331
ragge
1.66
332                         case 's':
333                                 if (strcmp(argv[i], "-static") == 0)
334                                         Bstatic = 1;
335                                 else
336                                         goto passa;
337                                 break;
338                         }
ragge
1.3
339                 } else {
340                 passa:
341                         t = argv[i];
ragge
1.11
342                         if (*argv[i] == '-' && argv[i][1] == 'L')
343                                 ;
ragge
1.59
344                         else if((c=getsuf(t))=='c' || c=='S' || c=='i' ||
ragge
1.51
345                             c=='s'|| Eflag) {
ragge
1.3
346                                 clist[nc++] = t;
ragge
1.66
347                                 if (nc>=MAXFIL) {
ragge
1.36
348                                         error("Too many source files");
ragge
1.3
349                                         exit(1);
ragge
1.66
350                                 }
ragge
1.3
351                                 t = setsuf(t'o');
352                         }
stefan
1.76
353
354                         /* Check for duplicate .o files. */
355                         for (j = getsuf(t) == 'o' ? 0 : nlj < nlj++) {
356                                 if (strcmp(llist[j], t) == 0)
357                                         break;
358                         }
359                         if (j == nl) {
ragge
1.3
360                                 llist[nl++] = t;
ragge
1.66
361                                 if (nl >= MAXLIB) {
ragge
1.36
362                                         error("Too many object/library files");
ragge
1.3
363                                         exit(1);
ragge
1.66
364                                 }
ragge
1.3
365                                 if (getsuf(t)=='o')
366                                         nxo++;
ragge
1.1
367                         }
368                 }
369         }
ragge
1.18
370         /* Sanity checking */
371         if (nc == 0 && nl == 0)
ragge
1.36
372                 errorx(8"no input files");
ragge
1.61
373         if (outfile && (cflag || sflag || Eflag) && nc > 1)
374                 errorx(8"-o given with -c || -E || -S and more than one file");
ragge
1.19
375         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
ragge
1.36
376                 errorx(8"output file will be clobbered");
ragge
1.4
377         if (gflagOflag = 0;
ragge
1.24
378 #if 0
379         if (proflag)
ragge
1.3
380                 pref = "/lib/mcrt0.o";
ragge
1.24
381 #endif
ragge
1.3
382         if(nc==0)
383                 goto nocom;
384         if (pflag==0) {
stefan
1.74
385                 if (!sflag)
386                         tmp3 = gettmp();
pj
1.30
387                 tmp4 = gettmp();
ragge
1.1
388         }
ragge
1.3
389         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
ragge
1.1
390                 signal(SIGINTidexit);
ragge
1.3
391         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
ragge
1.1
392                 signal(SIGTERMidexit);
ragge
1.3
393         pvt = pv;
ragge
1.1
394         for (i=0i<nci++) {
ragge
1.18
395                 /*
396                  * C preprocessor
397                  */
ragge
1.37
398                 if (nc>1 && !Eflag)
ragge
1.1
399                         printf("%s:\n"clist[i]);
ragge
1.43
400                 onlyas = 0;
pj
1.32
401                 assource = tmp3;
ragge
1.59
402                 if (getsuf(clist[i])=='i') {
403                         if(Eflag)
404                                 continue;
405                         goto com;
406                 } else if (getsuf(clist[i])=='s') {
ragge
1.1
407                         assource = clist[i];
ragge
1.43
408                         goto assemble;
stefan
1.73
409                 } else if (getsuf(clist[i])=='S')
ragge
1.43
410                         onlyas = 1;
ragge
1.1
411                 if (pflag)
412                         tmp4 = setsuf(clist[i], 'i');
ragge
1.5
413                 na = 0;
414                 av[na++] = "cpp";
ragge
1.60
415                 if (vflag)
416                         av[na++] = "-v";
ragge
1.20
417                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
ragge
1.40
418                 av[na++] = "-D__PCC_MINOR__=" MKS(PCC_MINOR);
419                 av[na++] = "-D__PCC_MINORMINOR__=" MKS(PCC_MINORMINOR);
ragge
1.52
420                 if (getsuf(clist[i])=='S')
421                         av[na++] = "-D__ASSEMBLER__";
422                 if (pthreads)
423                         av[na++] = "-D_PTHREADS";
ragge
1.63
424                 if (Cflag)
425                         av[na++] = "-C";
ragge
1.51
426                 if (Mflag)
427                         av[na++] = "-M";
ragge
1.57
428                 if (dflag)
429                         av[na++] = alist;
ragge
1.5
430                 for (j = 0cppadd[j]; j++)
431                         av[na++] = cppadd[j];
ragge
1.24
432                 for (j = 0cppmdadd[j]; j++)
433                         av[na++] = cppmdadd[j];
ragge
1.7
434                 if (tflag)
ragge
1.20
435                         av[na++] = "-t";
ragge
1.7
436                 for(pv=ptemppv <pvtpv++)
437                         av[na++] = *pv;
ragge
1.52
438                 if (!nostdinc)
439                         av[na++] = "-S"av[na++] = STDINC;
ragge
1.5
440                 av[na++] = clist[i];
ragge
1.51
441                 if (!Eflag && !Mflag)
ragge
1.20
442                         av[na++] = tmp4;
ragge
1.61
443                 if (Eflag && outfile)
444                          av[na++] = outfile;
ragge
1.3
445                 av[na++]=0;
446                 if (callsys(passpav))
447                         {exfail++; eflag++;}
ragge
1.51
448                 if (Eflag || Mflag)
ragge
1.37
449                         continue;
stefan
1.73
450                 if (onlyas) {
451                         assource = tmp4;
ragge
1.7
452                         goto assemble;
stefan
1.73
453                 }
ragge
1.18
454
455                 /*
456                  * C compiler
457                  */
ragge
1.59
458         com:
ragge
1.25
459                 na = 0;
460                 av[na++]= "ccom";
ragge
1.60
461                 if (vflag)
462                         av[na++] = "-v";
gmcgarry
1.81
463                 if (pgflag)
464                         av[na++] = "-p";
ragge
1.25
465                 if (gflag)
466                         av[na++] = "-g";
gmcgarry
1.82
467 #ifdef MACHOABI
468                 /* darwin always wants PIC compilation */
469                 av[na++] = "-k";
470 #else
ragge
1.49
471                 if (kflag)
472                         av[na++] = "-k";
gmcgarry
1.82
473 #endif
474                 if (Werror)
475                         av[na++] = "-Werror";
ragge
1.44
476                 if (Oflag) {
477                         av[na++] = "-xtemps";
ragge
1.64
478                         av[na++] = "-xdeljumps";
ragge
1.44
479                 }
ragge
1.45
480                 for (j = 0j < xnumj++)
481                         av[na++] = xlist[j];
gmcgarry
1.78
482                 for (j = 0j < nmj++)
483                         av[na++] = mlist[j];
ragge
1.68
484                 if (getsuf(clist[i])=='i')
485                         av[na++] = clist[i];
486                 else
487                         av[na++] = tmp4/* created by cpp */
ragge
1.3
488                 if (pflag || exfail)
489                         {
ragge
1.1
490                         cflag++;
491                         continue;
ragge
1.3
492                         }
ragge
1.16
493                 if(sflag) {
494                         if (outfile)
stefan
1.77
495                                 tmp3 = outfile;
ragge
1.16
496                         else
stefan
1.77
497                                 tmp3 = setsuf(clist[i], 's');
ragge
1.16
498                 }
ragge
1.25
499                 av[na++] = tmp3;
500 #if 0
ragge
1.3
501                 if (proflag) {
502                         av[3] = "-XP";
503                         av[4] = 0;
504                 } else
505                         av[3] = 0;
ragge
1.25
506 #endif
507                 av[na++] = NULL;
ragge
1.3
508                 if (callsys(pass0av)) {
ragge
1.1
509                         cflag++;
510                         eflag++;
511                         continue;
512                 }
513                 if (sflag)
514                         continue;
ragge
1.18
515
516                 /*
517                  * Assembler
518                  */
ragge
1.1
519         assemble:
ragge
1.49
520                 na = 0;
gmcgarry
1.79
521                 av[na++] = as;
ragge
1.60
522                 if (vflag)
523                         av[na++] = "-v";
ragge
1.49
524                 if (kflag)
525                         av[na++] = "-k";
526                 av[na++] = "-o";
ragge
1.18
527                 if (outfile && cflag)
ragge
1.49
528                         av[na++] = outfile;
ragge
1.18
529                 else
ragge
1.49
530                         av[na++] = setsuf(clist[i], 'o');
stefan
1.73
531                 av[na++] = assource;
ragge
1.49
532                 if (dflag)
533                         av[na++] = alist;
534                 av[na++] = 0;
gmcgarry
1.79
535                 if (callsys(asav)) {
ragge
1.1
536                         cflag++;
537                         eflag++;
ragge
1.7
538                         cunlink(tmp4);
ragge
1.1
539                         continue;
540                 }
ragge
1.7
541                 cunlink(tmp4);
ragge
1.1
542         }
ragge
1.18
543
ragge
1.51
544         if (Eflag || Mflag)
ragge
1.37
545                 dexit(eflag);
546
ragge
1.18
547         /*
548          * Linker
549          */
ragge
1.1
550 nocom:
551         if (cflag==0 && nl!=0) {
ragge
1.6
552                 j = 0;
gmcgarry
1.79
553                 av[j++] = ld;
ragge
1.60
554                 if (vflag)
555                         av[j++] = "-v";
ragge
1.6
556                 av[j++] = "-X";
557                 av[j++] = "-d";
558                 av[j++] = "-e";
ragge
1.48
559                 av[j++] = STARTLABEL;
ragge
1.14
560                 if (Bstatic == 0) { /* Dynamic linkage */
ragge
1.24
561                         for (i = 0dynlinker[i]; i++)
562                                 av[j++] = dynlinker[i];
ragge
1.54
563                 } else
564                         av[j++] = "-Bstatic";
ragge
1.1
565                 if (outfile) {
ragge
1.3
566                         av[j++] = "-o";
567                         av[j++] = outfile;
568                 }
ragge
1.14
569                 if (!nostartfiles) {
gmcgarry
1.81
570                         av[j++] = pgflag ? crt0file_profile : crt0file;
ragge
1.24
571                         for (i = 0startfiles[i]; i++)
572                                 av[j++] = startfiles[i];
ragge
1.14
573                 }
ragge
1.24
574                 i = 0;
ragge
1.14
575                 while(i<nl) {
ragge
1.3
576                         av[j++] = llist[i++];
ragge
1.14
577                         if (j >= MAXAV)
ragge
1.36
578                                 error("Too many ld options");
ragge
1.14
579                 }
gmcgarry
1.82
580 #ifndef MACHOABI
581                 /* darwin assembler doesn't want -g */
ragge
1.65
582                 if (gflag)
583                         av[j++] = "-g";
gmcgarry
1.82
584 #endif
ragge
1.11
585 #if 0
ragge
1.3
586                 if (gflag)
587                         av[j++] = "-lg";
ragge
1.11
588 #endif
ragge
1.52
589                 if (pthreads)
590                         av[j++] = "-lpthread";
gmcgarry
1.81
591                 if (!nostdlib) {
592                         if (pgflag) {
593                                 for (i = 0libclibs_profile[i]; i++)
594                                         av[j++] = libclibs_profile[i];
595                         } else {
596                                 for (i = 0libclibs[i]; i++)
597                                         av[j++] = libclibs[i];
598                         }
599                 }
ragge
1.14
600                 if (!nostartfiles) {
ragge
1.24
601                         for (i = 0endfiles[i]; i++)
602                                 av[j++] = endfiles[i];
ragge
1.14
603                 }
ragge
1.3
604                 av[j++] = 0;
gmcgarry
1.79
605                 eflag |= callsys(ldav);
ragge
1.1
606                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
607                         cunlink(setsuf(clist[0], 'o'));
ragge
1.46
608                 else if (nc > 0 && eflag == 0) {
609                         /* remove .o files XXX ugly */
610                         for (i = 0i < nci++)
611                                 cunlink(setsuf(clist[i], 'o'));
612                 }
ragge
1.1
613         }
ragge
1.36
614         dexit(eflag);
ragge
1.4
615         return 0;
ragge
1.1
616 }
617
ragge
1.36
618 /*
619  * exit and cleanup after interrupt.
620  */
ragge
1.4
621 void
622 idexit(int arg)
ragge
1.1
623 {
ragge
1.36
624         dexit(100);
ragge
1.1
625 }
626
ragge
1.36
627 /*
628  * exit and cleanup.
629  */
ragge
1.4
630 void
ragge
1.36
631 dexit(int eval)
ragge
1.1
632 {
ragge
1.10
633         if (!pflag && !Xflag) {
ragge
1.1
634                 if (sflag==0)
635                         cunlink(tmp3);
636                 cunlink(tmp4);
637         }
ragge
1.70
638         if (eval == 100)
639                 _exit(eval);
ragge
1.36
640         exit(eval);
ragge
1.1
641 }
642
ragge
1.36
643 static void
644 ccerror(char *sva_list ap)
ragge
1.1
645 {
ragge
1.36
646         vfprintf(Eflag ? stderr : stdoutsap);
ragge
1.8
647         putc('\n'Eflagstderr : stdout);
ragge
1.1
648         exfail++;
649         cflag++;
650         eflag++;
651 }
652
ragge
1.36
653 /*
654  * complain a bit.
655  */
656 void
657 error(char *s, ...)
658 {
659         va_list ap;
660
661         va_start(aps);
662         ccerror(sap);
663         va_end(ap);
664 }
665
666 /*
667  * complain a bit and then exit.
668  */
ragge
1.18
669 void
ragge
1.36
670 errorx(int evalchar *s, ...)
ragge
1.18
671 {
ragge
1.36
672         va_list ap;
673
674         va_start(aps);
675         ccerror(sap);
676         va_end(ap);
677         dexit(eval);
ragge
1.18
678 }
ragge
1.3
679
ragge
1.4
680 int
stefan
1.72
681 getsuf(char *as)
ragge
1.1
682 {
683         register char *s;
684
ragge
1.4
685         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
686                 return s[1];
ragge
1.3
687         return(0);
ragge
1.1
688 }
689
ragge
1.36
690 /*
691  * Get basename of string s and change its suffix to ch.
692  */
ragge
1.1
693 char *
ragge
1.36
694 setsuf(char *schar ch)
ragge
1.1
695 {
ragge
1.36
696         s = copy(basename(s));
pj
1.31
697         s[strlen(s) - 1] = ch;
ragge
1.36
698         return(s);
ragge
1.1
699 }
700
ragge
1.4
701 int
ragge
1.56
702 callsys(char f[], char *v[])
703 {
stefan
1.75
704         int tstatus = 0;
705         pid_t p;
ragge
1.4
706         char *s;
ragge
1.5
707
708         if (vflag) {
ragge
1.41
709                 fprintf(stderr"%s "f);
710                 for (t = 1v[t]; t++)
ragge
1.6
711                         fprintf(stderr"%s "v[t]);
712                 fprintf(stderr"\n");
ragge
1.5
713         }
ragge
1.1
714
stefan
1.75
715         if ((p = fork()) == 0) {
ragge
1.47
716                 if (Bflag) {
ragge
1.56
717                         size_t len = strlen(Bflag) + 8;
ragge
1.53
718                         char *a = malloc(len);
stefan
1.71
719                         if (a == NULL) {
720                                 error("callsys: malloc failed");
721                                 exit(1);
722                         }
ragge
1.47
723                         if ((s = strrchr(f'/'))) {
ragge
1.53
724                                 strlcpy(aBflaglen);
725                                 strlcat(aslen);
ragge
1.47
726                                 execv(av);
727                         }
728                 }
gmcgarry
1.79
729                 execvp(fv);
ragge
1.4
730                 if ((s = strrchr(f'/')))
731                         execvp(s+1v);
stefan
1.75
732                 fprintf(stderr"Can't find %s\n"f);
733                 _exit(100);
734         } else {
735                 if (p == -1) {
ragge
1.3
736                         printf("Try again\n");
737                         return(100);
738                 }
stefan
1.75
739         }
740         while (waitpid(p, &status0) == -1 && errno == EINTR)
741                 ;
742         if (WIFEXITED(status))
743                 return (WEXITSTATUS(status));
744         if (WIFSIGNALED(status))
ragge
1.36
745                 dexit(eflag);
stefan
1.75
746         errorx(8"Fatal error in %s"f);
ragge
1.1
747 }
748
ragge
1.3
749 char *
pj
1.28
750 copy(char *as)
ragge
1.3
751 {
pj
1.28
752         char *p;
753
ragge
1.36
754         if ((p = strdup(as)) == NULL)
755                 errorx(8"no space for file names");
ragge
1.3
756
pj
1.28
757         return p;
ragge
1.3
758 }
759
ragge
1.4
760 int
stefan
1.72
761 cunlink(char *f)
ragge
1.1
762 {
ragge
1.13
763         if (f==0 || Xflag)
ragge
1.3
764                 return(0);
765         return(unlink(f));
ragge
1.1
766 }
pj
1.30
767
768 char *
stefan
1.69
769 gettmp(void)
pj
1.30
770 {
stefan
1.69
771         char *sfn = copy("/tmp/ctm.XXXXXX");
pj
1.30
772         int fd = -1;
773
774         if ((fd = mkstemp(sfn)) == -1) {
775                 fprintf(stderr"%s: %s\n"sfnstrerror(errno));
776                 exit(8);
777         }
778         close(fd);
779
780         return sfn;
781 }
FishEye: Open Source License registered to PCC.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-09-21 06:05 +0200