Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20080409193319

Diff

Diff from 1.83 to:

Annotations

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

Annotated File View

ragge
1.83
1 /*      $Id: cc.c,v 1.83 2008/04/09 19:33:19 ragge 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];
ragge
1.83
120 char    *idirafter;
gmcgarry
1.78
121 int     nm;
ragge
1.63
122 int     Cflag;
ragge
1.43
123 int     dflag;
ragge
1.3
124 int     pflag;
125 int     sflag;
126 int     cflag;
127 int     eflag;
128 int     gflag;
ragge
1.5
129 int     vflag;
ragge
1.7
130 int     tflag;
ragge
1.8
131 int     Eflag;
ragge
1.4
132 int     Oflag;
ragge
1.58
133 int     kflag;  /* generate PIC/pic code */
134 #define F_PIC   1
135 #define F_pic   2
ragge
1.51
136 int     Mflag;  /* dependencies only */
ragge
1.52
137 int     pgflag;
ragge
1.1
138 int     exfail;
ragge
1.10
139 int     Xflag;
gmcgarry
1.82
140 int     Werror;
ragge
1.14
141 int     nostartfilesBstatic;
ragge
1.52
142 int     nostdincnostdlib;
ragge
1.43
143 int     onlyas;
ragge
1.52
144 int     pthreads;
ragge
1.14
145
gmcgarry
1.79
146 char    *passp = LIBEXECDIR "/" PREPROCESSOR;
147 char    *pass0 = LIBEXECDIR "/" COMPILER;
148 char    *as = ASSEMBLER;
149 char    *ld = LINKER;
ragge
1.47
150 char    *Bflag;
ragge
1.25
151 char *cppadd[] = CPPADD;
152 char *dynlinker[] = DYNLINKER;
153 char *crt0file = CRT0FILE;
gmcgarry
1.81
154 char *crt0file_profile = CRT0FILE_PROFILE;
ragge
1.25
155 char *startfiles[] = STARTFILES;
156 char *endfiles[] = ENDFILES;
157 char *cppmdadd[] = CPPMDADD;
ragge
1.50
158 #ifdef LIBCLIBS
159 char *libclibs[] = LIBCLIBS;
160 #else
161 char *libclibs[] = { "-lc"NULL };
162 #endif
gmcgarry
1.81
163 #ifdef LIBCLIBS_PROFILE
164 char *libclibs_profile[] = LIBCLIBS_PROFILE;
165 #else
166 char *libclibs_profile[] = { "-lc_p"NULL };
167 #endif
ragge
1.48
168 #ifndef STARTLABEL
169 #define STARTLABEL "__start"
170 #endif
ragge
1.1
171
ragge
1.4
172 int
pj
1.26
173 main(int argcchar *argv[])
174 {
ragge
1.15
175         char *t, *u;
ragge
1.1
176         char *assource;
ragge
1.3
177         char **pv, *ptemp[MAXOPT], **pvt;
ragge
1.36
178         int ncnlijcnxona;
ragge
1.3
179
ragge
1.36
180         i = nc = nl = nxo = 0;
ragge
1.3
181         pv = ptemp;
182         while(++i < argc) {
ragge
1.66
183                 if (argv[i][0] == '-') {
184                         switch (argv[i][1]) {
185                         default:
186                                 goto passa;
187 #ifdef notyet
188         /* must add library options first (-L/-l/...) */
189                                 error("unrecognized option `-%c'"argv[i][1]);
190                                 break;
191 #endif
ragge
1.22
192
ragge
1.66
193                         case 'B'/* other search paths for binaries */
194                                 Bflag = &argv[i][2];
195                                 break;
ragge
1.22
196
ragge
1.66
197                         case 'X':
198                                 Xflag++;
199                                 break;
200                         case 'W'/* Ignore (most of) W-flags */
gmcgarry
1.82
201                                 if (strncmp(argv[i], "-Werror"7) == 0) {
202                                         Werror = 1;
203                                 } else if (strncmp(argv[i], "-Wl,"4) == 0) {
ragge
1.66
204                                         /* options to the linker */
205                                         t = &argv[i][4];
206                                         while ((u = strchr(t','))) {
207                                                 *u++ = 0;
208                                                 llist[nl++] = t;
209                                                 t = u;
210                                         }
ragge
1.15
211                                         llist[nl++] = t;
ragge
1.83
212                                 } else if (strncmp(argv[i], "-Wp,"4) == 0) {
213                                         /* preprocessor */
214                                         if (!strncmp(argv[i], "-Wp,-C"6))
215                                                 Cflag++;
ragge
1.15
216                                 }
ragge
1.66
217                                 break;
ragge
1.15
218
ragge
1.66
219                         case 'f'/* GCC compatibility flags */
220                                 if (strcmp(argv[i], "-fPIC") == 0)
221                                         kflag = F_PIC;
222                                 if (strcmp(argv[i], "-fpic") == 0)
223                                         kflag = F_pic;
224                                 /* silently ignore the rest */
225                                 break;
ragge
1.14
226
ragge
1.66
227                         case 'g'/* create debug output */
228                                 gflag++;
229                                 break;
ragge
1.25
230
ragge
1.66
231                         case 'i':
232                                 if (strcmp(argv[i], "-isystem") == 0) {
233                                         *pv++ = "-S";
234                                         *pv++ = argv[++i];
235                                 } else if (strcmp(argv[i], "-include") == 0) {
236                                         *pv++ = "-i";
237                                         *pv++ = argv[++i];
ragge
1.83
238                                 } else if (strcmp(argv[i], "-idirafter") == 0) {
239                                         idirafter = argv[++i];
ragge
1.66
240                                 } else
241                                         goto passa;
242                                 break;
ragge
1.20
243
ragge
1.66
244                         case 'k'/* generate PIC code */
245                                 kflag = F_pic;
246                                 break;
ragge
1.49
247
gmcgarry
1.78
248                         case 'm'/* target-dependent options */
249                                 mlist[nm++] = argv[i];
250                                 break;
251
ragge
1.66
252                         case 'n'/* handle -n flags */
253                                 if (strcmp(argv[i], "-nostdinc") == 0)
254                                         nostdinc++;
255                                 else if (strcmp(argv[i], "-nostdlib") == 0) {
256                                         nostdlib++;
257                                         nostartfiles++;
258                                 } else if (strcmp(argv[i], "-nostartfiles") == 0)
259                                         nostartfiles = 1;
260                                 else
261                                         goto passa;
262                                 break;
ragge
1.14
263
ragge
1.66
264                         case 'p':
265                                 if (strcmp(argv[i], "-pg") == 0)
266                                         pgflag++;
267                                 else if (strcmp(argv[i], "-pthread") == 0)
268                                         pthreads++;
gmcgarry
1.81
269                                 else if (strcmp(argv[i], "-pipe") == 0) {
270                                 } else
ragge
1.66
271                                         errorx(1"unknown option %s"argv[i]);
272                                 break;
ragge
1.52
273
ragge
1.66
274                         case 'x':
ragge
1.83
275                                 t = &argv[i][2];
276                                 if (*t == 0)
277                                         t = argv[++i];
278                                 if (strcmp(t"c") == 0)
279                                         ; /* default */
280 #ifdef notyet
281                                 else if (strcmp(t"c++")
282                                         cxxflag++;
283 #endif
284                                 else
285                                         xlist[xnum++] = argv[i];
ragge
1.66
286                                 break;
287                         case 't':
288                                 tflag++;
289                                 break;
290                         case 'S':
291                                 sflag++;
292                                 cflag++;
293                                 break;
294                         case 'o':
295                                 if (outfile)
296                                         errorx(8"too many -o");
297                                 outfile = argv[++i];
298                                 break;
299                         case 'O':
300                                 Oflag++;
301                                 break;
302                         case 'E':
303                                 Eflag++;
304                                 break;
305                         case 'P':
306                                 pflag++;
307                                 *pv++ = argv[i];
308                         case 'c':
309                                 cflag++;
310                                 break;
ragge
1.3
311
ragge
1.24
312 #if 0
ragge
1.66
313                         case '2':
314                                 if(argv[i][2] == '\0')
315                                         pref = "/lib/crt2.o";
316                                 else {
317                                         pref = "/lib/crt20.o";
318                                 }
319                                 break;
ragge
1.24
320 #endif
ragge
1.66
321                         case 'C':
322                                 Cflag = 1;
323                                 break;
324                         case 'D':
325                         case 'I':
326                         case 'U':
327                                 *pv++ = argv[i];
328                                 if (argv[i][2] == 0)
329                                         *pv++ = argv[++i];
330                                 if (pv >= ptemp+MAXOPT) {
331                                         error("Too many DIU options");
332                                         --pv;
ragge
1.3
333                                 }
ragge
1.66
334                                 break;
ragge
1.3
335
ragge
1.66
336                         case 'M':
337                                 Mflag++;
338                                 break;
ragge
1.51
339
ragge
1.66
340                         case 'd':
341                                 dflag++;
ragge
1.67
342                                 strlcpy(alistargv[i], sizeof (alist));
ragge
1.66
343                                 break;
344                         case 'v':
345                                 printf("%s\n"VERSSTR);
346                                 vflag++;
347                                 break;
ragge
1.14
348
ragge
1.66
349                         case 's':
350                                 if (strcmp(argv[i], "-static") == 0)
351                                         Bstatic = 1;
352                                 else
353                                         goto passa;
354                                 break;
355                         }
ragge
1.3
356                 } else {
357                 passa:
358                         t = argv[i];
ragge
1.11
359                         if (*argv[i] == '-' && argv[i][1] == 'L')
360                                 ;
ragge
1.59
361                         else if((c=getsuf(t))=='c' || c=='S' || c=='i' ||
ragge
1.51
362                             c=='s'|| Eflag) {
ragge
1.3
363                                 clist[nc++] = t;
ragge
1.66
364                                 if (nc>=MAXFIL) {
ragge
1.36
365                                         error("Too many source files");
ragge
1.3
366                                         exit(1);
ragge
1.66
367                                 }
ragge
1.3
368                                 t = setsuf(t'o');
369                         }
stefan
1.76
370
371                         /* Check for duplicate .o files. */
372                         for (j = getsuf(t) == 'o' ? 0 : nlj < nlj++) {
373                                 if (strcmp(llist[j], t) == 0)
374                                         break;
375                         }
376                         if (j == nl) {
ragge
1.3
377                                 llist[nl++] = t;
ragge
1.66
378                                 if (nl >= MAXLIB) {
ragge
1.36
379                                         error("Too many object/library files");
ragge
1.3
380                                         exit(1);
ragge
1.66
381                                 }
ragge
1.3
382                                 if (getsuf(t)=='o')
383                                         nxo++;
ragge
1.1
384                         }
385                 }
386         }
ragge
1.18
387         /* Sanity checking */
388         if (nc == 0 && nl == 0)
ragge
1.36
389                 errorx(8"no input files");
ragge
1.61
390         if (outfile && (cflag || sflag || Eflag) && nc > 1)
391                 errorx(8"-o given with -c || -E || -S and more than one file");
ragge
1.19
392         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
ragge
1.36
393                 errorx(8"output file will be clobbered");
ragge
1.4
394         if (gflagOflag = 0;
ragge
1.24
395 #if 0
396         if (proflag)
ragge
1.3
397                 pref = "/lib/mcrt0.o";
ragge
1.24
398 #endif
ragge
1.3
399         if(nc==0)
400                 goto nocom;
401         if (pflag==0) {
stefan
1.74
402                 if (!sflag)
403                         tmp3 = gettmp();
pj
1.30
404                 tmp4 = gettmp();
ragge
1.1
405         }
ragge
1.3
406         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
ragge
1.1
407                 signal(SIGINTidexit);
ragge
1.3
408         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
ragge
1.1
409                 signal(SIGTERMidexit);
ragge
1.3
410         pvt = pv;
ragge
1.1
411         for (i=0i<nci++) {
ragge
1.18
412                 /*
413                  * C preprocessor
414                  */
ragge
1.37
415                 if (nc>1 && !Eflag)
ragge
1.1
416                         printf("%s:\n"clist[i]);
ragge
1.43
417                 onlyas = 0;
pj
1.32
418                 assource = tmp3;
ragge
1.59
419                 if (getsuf(clist[i])=='i') {
420                         if(Eflag)
421                                 continue;
422                         goto com;
423                 } else if (getsuf(clist[i])=='s') {
ragge
1.1
424                         assource = clist[i];
ragge
1.43
425                         goto assemble;
stefan
1.73
426                 } else if (getsuf(clist[i])=='S')
ragge
1.43
427                         onlyas = 1;
ragge
1.1
428                 if (pflag)
429                         tmp4 = setsuf(clist[i], 'i');
ragge
1.5
430                 na = 0;
431                 av[na++] = "cpp";
ragge
1.60
432                 if (vflag)
433                         av[na++] = "-v";
ragge
1.20
434                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
ragge
1.40
435                 av[na++] = "-D__PCC_MINOR__=" MKS(PCC_MINOR);
436                 av[na++] = "-D__PCC_MINORMINOR__=" MKS(PCC_MINORMINOR);
ragge
1.52
437                 if (getsuf(clist[i])=='S')
438                         av[na++] = "-D__ASSEMBLER__";
439                 if (pthreads)
440                         av[na++] = "-D_PTHREADS";
ragge
1.63
441                 if (Cflag)
442                         av[na++] = "-C";
ragge
1.51
443                 if (Mflag)
444                         av[na++] = "-M";
ragge
1.57
445                 if (dflag)
446                         av[na++] = alist;
ragge
1.5
447                 for (j = 0cppadd[j]; j++)
448                         av[na++] = cppadd[j];
ragge
1.24
449                 for (j = 0cppmdadd[j]; j++)
450                         av[na++] = cppmdadd[j];
ragge
1.7
451                 if (tflag)
ragge
1.20
452                         av[na++] = "-t";
ragge
1.7
453                 for(pv=ptemppv <pvtpv++)
454                         av[na++] = *pv;
ragge
1.52
455                 if (!nostdinc)
456                         av[na++] = "-S"av[na++] = STDINC;
ragge
1.83
457                 if (idirafter) {
458                         av[na++] = "-I";
459                         av[na++] = idirafter;
460                 }
ragge
1.5
461                 av[na++] = clist[i];
ragge
1.51
462                 if (!Eflag && !Mflag)
ragge
1.20
463                         av[na++] = tmp4;
ragge
1.61
464                 if (Eflag && outfile)
465                          av[na++] = outfile;
ragge
1.3
466                 av[na++]=0;
467                 if (callsys(passpav))
468                         {exfail++; eflag++;}
ragge
1.51
469                 if (Eflag || Mflag)
ragge
1.37
470                         continue;
stefan
1.73
471                 if (onlyas) {
472                         assource = tmp4;
ragge
1.7
473                         goto assemble;
stefan
1.73
474                 }
ragge
1.18
475
476                 /*
477                  * C compiler
478                  */
ragge
1.59
479         com:
ragge
1.25
480                 na = 0;
481                 av[na++]= "ccom";
ragge
1.60
482                 if (vflag)
483                         av[na++] = "-v";
gmcgarry
1.81
484                 if (pgflag)
485                         av[na++] = "-p";
ragge
1.25
486                 if (gflag)
487                         av[na++] = "-g";
gmcgarry
1.82
488 #ifdef MACHOABI
489                 /* darwin always wants PIC compilation */
490                 av[na++] = "-k";
491 #else
ragge
1.49
492                 if (kflag)
493                         av[na++] = "-k";
gmcgarry
1.82
494 #endif
495                 if (Werror)
496                         av[na++] = "-Werror";
ragge
1.44
497                 if (Oflag) {
498                         av[na++] = "-xtemps";
ragge
1.64
499                         av[na++] = "-xdeljumps";
ragge
1.44
500                 }
ragge
1.45
501                 for (j = 0j < xnumj++)
502                         av[na++] = xlist[j];
gmcgarry
1.78
503                 for (j = 0j < nmj++)
504                         av[na++] = mlist[j];
ragge
1.68
505                 if (getsuf(clist[i])=='i')
506                         av[na++] = clist[i];
507                 else
508                         av[na++] = tmp4/* created by cpp */
ragge
1.3
509                 if (pflag || exfail)
510                         {
ragge
1.1
511                         cflag++;
512                         continue;
ragge
1.3
513                         }
ragge
1.16
514                 if(sflag) {
515                         if (outfile)
stefan
1.77
516                                 tmp3 = outfile;
ragge
1.16
517                         else
stefan
1.77
518                                 tmp3 = setsuf(clist[i], 's');
ragge
1.16
519                 }
ragge
1.25
520                 av[na++] = tmp3;
521 #if 0
ragge
1.3
522                 if (proflag) {
523                         av[3] = "-XP";
524                         av[4] = 0;
525                 } else
526                         av[3] = 0;
ragge
1.25
527 #endif
528                 av[na++] = NULL;
ragge
1.3
529                 if (callsys(pass0av)) {
ragge
1.1
530                         cflag++;
531                         eflag++;
532                         continue;
533                 }
534                 if (sflag)
535                         continue;
ragge
1.18
536
537                 /*
538                  * Assembler
539                  */
ragge
1.1
540         assemble:
ragge
1.49
541                 na = 0;
gmcgarry
1.79
542                 av[na++] = as;
ragge
1.60
543                 if (vflag)
544                         av[na++] = "-v";
ragge
1.49
545                 if (kflag)
546                         av[na++] = "-k";
547                 av[na++] = "-o";
ragge
1.18
548                 if (outfile && cflag)
ragge
1.49
549                         av[na++] = outfile;
ragge
1.18
550                 else
ragge
1.49
551                         av[na++] = setsuf(clist[i], 'o');
stefan
1.73
552                 av[na++] = assource;
ragge
1.49
553                 if (dflag)
554                         av[na++] = alist;
555                 av[na++] = 0;
gmcgarry
1.79
556                 if (callsys(asav)) {
ragge
1.1
557                         cflag++;
558                         eflag++;
ragge
1.7
559                         cunlink(tmp4);
ragge
1.1
560                         continue;
561                 }
ragge
1.7
562                 cunlink(tmp4);
ragge
1.1
563         }
ragge
1.18
564
ragge
1.51
565         if (Eflag || Mflag)
ragge
1.37
566                 dexit(eflag);
567
ragge
1.18
568         /*
569          * Linker
570          */
ragge
1.1
571 nocom:
572         if (cflag==0 && nl!=0) {
ragge
1.6
573                 j = 0;
gmcgarry
1.79
574                 av[j++] = ld;
ragge
1.60
575                 if (vflag)
576                         av[j++] = "-v";
ragge
1.6
577                 av[j++] = "-X";
578                 av[j++] = "-d";
579                 av[j++] = "-e";
ragge
1.48
580                 av[j++] = STARTLABEL;
ragge
1.14
581                 if (Bstatic == 0) { /* Dynamic linkage */
ragge
1.24
582                         for (i = 0dynlinker[i]; i++)
583                                 av[j++] = dynlinker[i];
ragge
1.54
584                 } else
585                         av[j++] = "-Bstatic";
ragge
1.1
586                 if (outfile) {
ragge
1.3
587                         av[j++] = "-o";
588                         av[j++] = outfile;
589                 }
ragge
1.14
590                 if (!nostartfiles) {
gmcgarry
1.81
591                         av[j++] = pgflag ? crt0file_profile : crt0file;
ragge
1.24
592                         for (i = 0startfiles[i]; i++)
593                                 av[j++] = startfiles[i];
ragge
1.14
594                 }
ragge
1.24
595                 i = 0;
ragge
1.14
596                 while(i<nl) {
ragge
1.3
597                         av[j++] = llist[i++];
ragge
1.14
598                         if (j >= MAXAV)
ragge
1.36
599                                 error("Too many ld options");
ragge
1.14
600                 }
gmcgarry
1.82
601 #ifndef MACHOABI
602                 /* darwin assembler doesn't want -g */
ragge
1.65
603                 if (gflag)
604                         av[j++] = "-g";
gmcgarry
1.82
605 #endif
ragge
1.11
606 #if 0
ragge
1.3
607                 if (gflag)
608                         av[j++] = "-lg";
ragge
1.11
609 #endif
ragge
1.52
610                 if (pthreads)
611                         av[j++] = "-lpthread";
gmcgarry
1.81
612                 if (!nostdlib) {
613                         if (pgflag) {
614                                 for (i = 0libclibs_profile[i]; i++)
615                                         av[j++] = libclibs_profile[i];
616                         } else {
617                                 for (i = 0libclibs[i]; i++)
618                                         av[j++] = libclibs[i];
619                         }
620                 }
ragge
1.14
621                 if (!nostartfiles) {
ragge
1.24
622                         for (i = 0endfiles[i]; i++)
623                                 av[j++] = endfiles[i];
ragge
1.14
624                 }
ragge
1.3
625                 av[j++] = 0;
gmcgarry
1.79
626                 eflag |= callsys(ldav);
ragge
1.1
627                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
628                         cunlink(setsuf(clist[0], 'o'));
ragge
1.46
629                 else if (nc > 0 && eflag == 0) {
630                         /* remove .o files XXX ugly */
631                         for (i = 0i < nci++)
632                                 cunlink(setsuf(clist[i], 'o'));
633                 }
ragge
1.1
634         }
ragge
1.36
635         dexit(eflag);
ragge
1.4
636         return 0;
ragge
1.1
637 }
638
ragge
1.36
639 /*
640  * exit and cleanup after interrupt.
641  */
ragge
1.4
642 void
643 idexit(int arg)
ragge
1.1
644 {
ragge
1.36
645         dexit(100);
ragge
1.1
646 }
647
ragge
1.36
648 /*
649  * exit and cleanup.
650  */
ragge
1.4
651 void
ragge
1.36
652 dexit(int eval)
ragge
1.1
653 {
ragge
1.10
654         if (!pflag && !Xflag) {
ragge
1.1
655                 if (sflag==0)
656                         cunlink(tmp3);
657                 cunlink(tmp4);
658         }
ragge
1.70
659         if (eval == 100)
660                 _exit(eval);
ragge
1.36
661         exit(eval);
ragge
1.1
662 }
663
ragge
1.36
664 static void
665 ccerror(char *sva_list ap)
ragge
1.1
666 {
ragge
1.36
667         vfprintf(Eflag ? stderr : stdoutsap);
ragge
1.8
668         putc('\n'Eflagstderr : stdout);
ragge
1.1
669         exfail++;
670         cflag++;
671         eflag++;
672 }
673
ragge
1.36
674 /*
675  * complain a bit.
676  */
677 void
678 error(char *s, ...)
679 {
680         va_list ap;
681
682         va_start(aps);
683         ccerror(sap);
684         va_end(ap);
685 }
686
687 /*
688  * complain a bit and then exit.
689  */
ragge
1.18
690 void
ragge
1.36
691 errorx(int evalchar *s, ...)
ragge
1.18
692 {
ragge
1.36
693         va_list ap;
694
695         va_start(aps);
696         ccerror(sap);
697         va_end(ap);
698         dexit(eval);
ragge
1.18
699 }
ragge
1.3
700
ragge
1.4
701 int
stefan
1.72
702 getsuf(char *as)
ragge
1.1
703 {
704         register char *s;
705
ragge
1.4
706         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
707                 return s[1];
ragge
1.3
708         return(0);
ragge
1.1
709 }
710
ragge
1.36
711 /*
712  * Get basename of string s and change its suffix to ch.
713  */
ragge
1.1
714 char *
ragge
1.36
715 setsuf(char *schar ch)
ragge
1.1
716 {
ragge
1.36
717         s = copy(basename(s));
pj
1.31
718         s[strlen(s) - 1] = ch;
ragge
1.36
719         return(s);
ragge
1.1
720 }
721
ragge
1.4
722 int
ragge
1.56
723 callsys(char f[], char *v[])
724 {
stefan
1.75
725         int tstatus = 0;
726         pid_t p;
ragge
1.4
727         char *s;
ragge
1.5
728
729         if (vflag) {
ragge
1.41
730                 fprintf(stderr"%s "f);
731                 for (t = 1v[t]; t++)
ragge
1.6
732                         fprintf(stderr"%s "v[t]);
733                 fprintf(stderr"\n");
ragge
1.5
734         }
ragge
1.1
735
stefan
1.75
736         if ((p = fork()) == 0) {
ragge
1.47
737                 if (Bflag) {
ragge
1.56
738                         size_t len = strlen(Bflag) + 8;
ragge
1.53
739                         char *a = malloc(len);
stefan
1.71
740                         if (a == NULL) {
741                                 error("callsys: malloc failed");
742                                 exit(1);
743                         }
ragge
1.47
744                         if ((s = strrchr(f'/'))) {
ragge
1.53
745                                 strlcpy(aBflaglen);
746                                 strlcat(aslen);
ragge
1.47
747                                 execv(av);
748                         }
749                 }
gmcgarry
1.79
750                 execvp(fv);
ragge
1.4
751                 if ((s = strrchr(f'/')))
752                         execvp(s+1v);
stefan
1.75
753                 fprintf(stderr"Can't find %s\n"f);
754                 _exit(100);
755         } else {
756                 if (p == -1) {
ragge
1.3
757                         printf("Try again\n");
758                         return(100);
759                 }
stefan
1.75
760         }
761         while (waitpid(p, &status0) == -1 && errno == EINTR)
762                 ;
763         if (WIFEXITED(status))
764                 return (WEXITSTATUS(status));
765         if (WIFSIGNALED(status))
ragge
1.36
766                 dexit(eflag);
stefan
1.75
767         errorx(8"Fatal error in %s"f);
ragge
1.1
768 }
769
ragge
1.3
770 char *
pj
1.28
771 copy(char *as)
ragge
1.3
772 {
pj
1.28
773         char *p;
774
ragge
1.36
775         if ((p = strdup(as)) == NULL)
776                 errorx(8"no space for file names");
ragge
1.3
777
pj
1.28
778         return p;
ragge
1.3
779 }
780
ragge
1.4
781 int
stefan
1.72
782 cunlink(char *f)
ragge
1.1
783 {
ragge
1.13
784         if (f==0 || Xflag)
ragge
1.3
785                 return(0);
786         return(unlink(f));
ragge
1.1
787 }
pj
1.30
788
789 char *
stefan
1.69
790 gettmp(void)
pj
1.30
791 {
stefan
1.69
792         char *sfn = copy("/tmp/ctm.XXXXXX");
pj
1.30
793         int fd = -1;
794
795         if ((fd = mkstemp(sfn)) == -1) {
796                 fprintf(stderr"%s: %s\n"sfnstrerror(errno));
797                 exit(8);
798         }
799         close(fd);
800
801         return sfn;
802 }
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-22 14:15 +0200