Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20080416023204

Diff

Diff from 1.87 to:

Annotations

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

Annotated File View

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