Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20080413163540

Diff

Diff from 1.84 to:

Annotations

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

Annotated File View

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