Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20080417111438

Diff

Diff from 1.89 to:

Annotations

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

Annotated File View

gmcgarry
1.89
1 /*      $Id: cc.c,v 1.89 2008/04/17 11:14:38 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':
ragge
1.88
283                                 if (strcmp(argv[i], "-pg") == 0 ||
284                                     strcmp(argv[i], "-p") == 0)
ragge
1.66
285                                         pgflag++;
286                                 else if (strcmp(argv[i], "-pthread") == 0)
287                                         pthreads++;
gmcgarry
1.81
288                                 else if (strcmp(argv[i], "-pipe") == 0) {
289                                 } else
ragge
1.66
290                                         errorx(1"unknown option %s"argv[i]);
291                                 break;
ragge
1.52
292
ragge
1.66
293                         case 'x':
ragge
1.83
294                                 t = &argv[i][2];
295                                 if (*t == 0)
296                                         t = argv[++i];
297                                 if (strcmp(t"c") == 0)
298                                         ; /* default */
299 #ifdef notyet
300                                 else if (strcmp(t"c++")
301                                         cxxflag++;
302 #endif
303                                 else
304                                         xlist[xnum++] = argv[i];
ragge
1.66
305                                 break;
306                         case 't':
307                                 tflag++;
308                                 break;
309                         case 'S':
310                                 sflag++;
311                                 cflag++;
312                                 break;
313                         case 'o':
314                                 if (outfile)
315                                         errorx(8"too many -o");
316                                 outfile = argv[++i];
317                                 break;
318                         case 'O':
ragge
1.85
319                                 if (argv[i][2] != '0')
320                                         Oflag++;
ragge
1.66
321                                 break;
322                         case 'E':
323                                 Eflag++;
324                                 break;
325                         case 'P':
326                                 pflag++;
327                                 *pv++ = argv[i];
328                         case 'c':
329                                 cflag++;
330                                 break;
ragge
1.3
331
ragge
1.24
332 #if 0
ragge
1.66
333                         case '2':
334                                 if(argv[i][2] == '\0')
335                                         pref = "/lib/crt2.o";
336                                 else {
337                                         pref = "/lib/crt20.o";
338                                 }
339                                 break;
ragge
1.24
340 #endif
ragge
1.66
341                         case 'C':
342                                 Cflag = 1;
343                                 break;
344                         case 'D':
345                         case 'I':
346                         case 'U':
347                                 *pv++ = argv[i];
348                                 if (argv[i][2] == 0)
349                                         *pv++ = argv[++i];
350                                 if (pv >= ptemp+MAXOPT) {
351                                         error("Too many DIU options");
352                                         --pv;
ragge
1.3
353                                 }
ragge
1.66
354                                 break;
ragge
1.3
355
ragge
1.66
356                         case 'M':
357                                 Mflag++;
358                                 break;
ragge
1.51
359
ragge
1.66
360                         case 'd':
361                                 dflag++;
ragge
1.67
362                                 strlcpy(alistargv[i], sizeof (alist));
ragge
1.66
363                                 break;
364                         case 'v':
365                                 printf("%s\n"VERSSTR);
366                                 vflag++;
367                                 break;
ragge
1.14
368
ragge
1.66
369                         case 's':
370                                 if (strcmp(argv[i], "-static") == 0)
371                                         Bstatic = 1;
ragge
1.84
372                                 else if (strcmp(argv[i], "-shared") == 0) {
373                                         shared = 1;
374                                         nostdlib = 1;
375                                 } else
ragge
1.66
376                                         goto passa;
377                                 break;
378                         }
ragge
1.3
379                 } else {
380                 passa:
381                         t = argv[i];
ragge
1.11
382                         if (*argv[i] == '-' && argv[i][1] == 'L')
383                                 ;
ragge
1.59
384                         else if((c=getsuf(t))=='c' || c=='S' || c=='i' ||
ragge
1.51
385                             c=='s'|| Eflag) {
ragge
1.3
386                                 clist[nc++] = t;
ragge
1.66
387                                 if (nc>=MAXFIL) {
ragge
1.36
388                                         error("Too many source files");
ragge
1.3
389                                         exit(1);
ragge
1.66
390                                 }
ragge
1.3
391                                 t = setsuf(t'o');
392                         }
stefan
1.76
393
394                         /* Check for duplicate .o files. */
395                         for (j = getsuf(t) == 'o' ? 0 : nlj < nlj++) {
396                                 if (strcmp(llist[j], t) == 0)
397                                         break;
398                         }
399                         if (j == nl) {
ragge
1.3
400                                 llist[nl++] = t;
ragge
1.66
401                                 if (nl >= MAXLIB) {
ragge
1.36
402                                         error("Too many object/library files");
ragge
1.3
403                                         exit(1);
ragge
1.66
404                                 }
ragge
1.3
405                                 if (getsuf(t)=='o')
406                                         nxo++;
ragge
1.1
407                         }
408                 }
409         }
ragge
1.18
410         /* Sanity checking */
411         if (nc == 0 && nl == 0)
ragge
1.36
412                 errorx(8"no input files");
ragge
1.61
413         if (outfile && (cflag || sflag || Eflag) && nc > 1)
414                 errorx(8"-o given with -c || -E || -S and more than one file");
ragge
1.19
415         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
ragge
1.36
416                 errorx(8"output file will be clobbered");
ragge
1.4
417         if (gflagOflag = 0;
ragge
1.24
418 #if 0
419         if (proflag)
ragge
1.3
420                 pref = "/lib/mcrt0.o";
ragge
1.24
421 #endif
ragge
1.3
422         if(nc==0)
423                 goto nocom;
424         if (pflag==0) {
stefan
1.74
425                 if (!sflag)
426                         tmp3 = gettmp();
pj
1.30
427                 tmp4 = gettmp();
ragge
1.1
428         }
ragge
1.3
429         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
ragge
1.1
430                 signal(SIGINTidexit);
ragge
1.3
431         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
ragge
1.1
432                 signal(SIGTERMidexit);
ragge
1.3
433         pvt = pv;
ragge
1.1
434         for (i=0i<nci++) {
ragge
1.18
435                 /*
436                  * C preprocessor
437                  */
ragge
1.37
438                 if (nc>1 && !Eflag)
ragge
1.1
439                         printf("%s:\n"clist[i]);
ragge
1.43
440                 onlyas = 0;
pj
1.32
441                 assource = tmp3;
ragge
1.59
442                 if (getsuf(clist[i])=='i') {
443                         if(Eflag)
444                                 continue;
445                         goto com;
446                 } else if (getsuf(clist[i])=='s') {
ragge
1.1
447                         assource = clist[i];
ragge
1.43
448                         goto assemble;
stefan
1.73
449                 } else if (getsuf(clist[i])=='S')
ragge
1.43
450                         onlyas = 1;
ragge
1.1
451                 if (pflag)
452                         tmp4 = setsuf(clist[i], 'i');
ragge
1.5
453                 na = 0;
454                 av[na++] = "cpp";
ragge
1.60
455                 if (vflag)
456                         av[na++] = "-v";
ragge
1.20
457                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
ragge
1.40
458                 av[na++] = "-D__PCC_MINOR__=" MKS(PCC_MINOR);
459                 av[na++] = "-D__PCC_MINORMINOR__=" MKS(PCC_MINORMINOR);
ragge
1.52
460                 if (getsuf(clist[i])=='S')
461                         av[na++] = "-D__ASSEMBLER__";
462                 if (pthreads)
463                         av[na++] = "-D_PTHREADS";
ragge
1.63
464                 if (Cflag)
465                         av[na++] = "-C";
ragge
1.51
466                 if (Mflag)
467                         av[na++] = "-M";
ragge
1.57
468                 if (dflag)
469                         av[na++] = alist;
ragge
1.5
470                 for (j = 0cppadd[j]; j++)
471                         av[na++] = cppadd[j];
ragge
1.24
472                 for (j = 0cppmdadd[j]; j++)
473                         av[na++] = cppmdadd[j];
ragge
1.7
474                 if (tflag)
ragge
1.20
475                         av[na++] = "-t";
ragge
1.7
476                 for(pv=ptemppv <pvtpv++)
477                         av[na++] = *pv;
ragge
1.52
478                 if (!nostdinc)
479                         av[na++] = "-S"av[na++] = STDINC;
ragge
1.83
480                 if (idirafter) {
481                         av[na++] = "-I";
482                         av[na++] = idirafter;
483                 }
ragge
1.5
484                 av[na++] = clist[i];
ragge
1.51
485                 if (!Eflag && !Mflag)
ragge
1.20
486                         av[na++] = tmp4;
ragge
1.61
487                 if (Eflag && outfile)
488                          av[na++] = outfile;
ragge
1.3
489                 av[na++]=0;
490                 if (callsys(passpav))
491                         {exfail++; eflag++;}
ragge
1.51
492                 if (Eflag || Mflag)
ragge
1.37
493                         continue;
stefan
1.73
494                 if (onlyas) {
495                         assource = tmp4;
ragge
1.7
496                         goto assemble;
stefan
1.73
497                 }
ragge
1.18
498
499                 /*
500                  * C compiler
501                  */
ragge
1.59
502         com:
ragge
1.25
503                 na = 0;
504                 av[na++]= "ccom";
ragge
1.60
505                 if (vflag)
506                         av[na++] = "-v";
gmcgarry
1.81
507                 if (pgflag)
508                         av[na++] = "-p";
ragge
1.25
509                 if (gflag)
510                         av[na++] = "-g";
gmcgarry
1.82
511 #ifdef MACHOABI
512                 /* darwin always wants PIC compilation */
513                 av[na++] = "-k";
514 #else
ragge
1.49
515                 if (kflag)
516                         av[na++] = "-k";
gmcgarry
1.82
517 #endif
518                 if (Werror)
519                         av[na++] = "-Werror";
ragge
1.44
520                 if (Oflag) {
521                         av[na++] = "-xtemps";
ragge
1.64
522                         av[na++] = "-xdeljumps";
ragge
1.44
523                 }
ragge
1.45
524                 for (j = 0j < xnumj++)
525                         av[na++] = xlist[j];
gmcgarry
1.78
526                 for (j = 0j < nmj++)
527                         av[na++] = mlist[j];
ragge
1.68
528                 if (getsuf(clist[i])=='i')
529                         av[na++] = clist[i];
530                 else
531                         av[na++] = tmp4/* created by cpp */
ragge
1.3
532                 if (pflag || exfail)
533                         {
ragge
1.1
534                         cflag++;
535                         continue;
ragge
1.3
536                         }
ragge
1.16
537                 if(sflag) {
538                         if (outfile)
stefan
1.77
539                                 tmp3 = outfile;
ragge
1.16
540                         else
stefan
1.77
541                                 tmp3 = setsuf(clist[i], 's');
ragge
1.16
542                 }
ragge
1.25
543                 av[na++] = tmp3;
544 #if 0
ragge
1.3
545                 if (proflag) {
546                         av[3] = "-XP";
547                         av[4] = 0;
548                 } else
549                         av[3] = 0;
ragge
1.25
550 #endif
551                 av[na++] = NULL;
ragge
1.3
552                 if (callsys(pass0av)) {
ragge
1.1
553                         cflag++;
554                         eflag++;
555                         continue;
556                 }
557                 if (sflag)
558                         continue;
ragge
1.18
559
560                 /*
561                  * Assembler
562                  */
ragge
1.1
563         assemble:
ragge
1.49
564                 na = 0;
gmcgarry
1.79
565                 av[na++] = as;
ragge
1.60
566                 if (vflag)
567                         av[na++] = "-v";
ragge
1.49
568                 if (kflag)
569                         av[na++] = "-k";
570                 av[na++] = "-o";
ragge
1.18
571                 if (outfile && cflag)
ragge
1.49
572                         av[na++] = outfile;
ragge
1.18
573                 else
ragge
1.49
574                         av[na++] = setsuf(clist[i], 'o');
stefan
1.73
575                 av[na++] = assource;
ragge
1.49
576                 if (dflag)
577                         av[na++] = alist;
578                 av[na++] = 0;
gmcgarry
1.79
579                 if (callsys(asav)) {
ragge
1.1
580                         cflag++;
581                         eflag++;
ragge
1.7
582                         cunlink(tmp4);
ragge
1.1
583                         continue;
584                 }
ragge
1.7
585                 cunlink(tmp4);
ragge
1.1
586         }
ragge
1.18
587
ragge
1.51
588         if (Eflag || Mflag)
ragge
1.37
589                 dexit(eflag);
590
ragge
1.18
591         /*
592          * Linker
593          */
ragge
1.1
594 nocom:
595         if (cflag==0 && nl!=0) {
ragge
1.6
596                 j = 0;
gmcgarry
1.79
597                 av[j++] = ld;
ragge
1.60
598                 if (vflag)
599                         av[j++] = "-v";
ragge
1.6
600                 av[j++] = "-X";
ragge
1.84
601                 if (shared) {
602                         av[j++] = "-shared";
603                 } else {
604                         av[j++] = "-d";
gmcgarry
1.89
605 #ifndef os_win32
ragge
1.84
606                         av[j++] = "-e";
607                         av[j++] = STARTLABEL;
gmcgarry
1.89
608 #endif
ragge
1.84
609                         if (Bstatic == 0) { /* Dynamic linkage */
610                                 for (i = 0dynlinker[i]; i++)
611                                         av[j++] = dynlinker[i];
612                         } else
613                                 av[j++] = "-Bstatic";
614                 }
ragge
1.1
615                 if (outfile) {
ragge
1.3
616                         av[j++] = "-o";
617                         av[j++] = outfile;
618                 }
gmcgarry
1.89
619 #ifndef os_win32
ragge
1.84
620                 if (shared) {
621 #ifdef STARTFILES_S
622                         for (i = 0startfiles_S[i]; i++)
623                                 av[j++] = startfiles_S[i];
624 #endif
625                 } else {
626                         if (!nostartfiles) {
627                                 av[j++] = pgflag ? crt0file_profile : crt0file;
628                                 for (i = 0startfiles[i]; i++)
629                                         av[j++] = startfiles[i];
630                         }
ragge
1.14
631                 }
gmcgarry
1.89
632 #endif
ragge
1.24
633                 i = 0;
ragge
1.14
634                 while(i<nl) {
ragge
1.3
635                         av[j++] = llist[i++];
ragge
1.14
636                         if (j >= MAXAV)
ragge
1.36
637                                 error("Too many ld options");
ragge
1.14
638                 }
gmcgarry
1.82
639 #ifndef MACHOABI
640                 /* darwin assembler doesn't want -g */
ragge
1.65
641                 if (gflag)
642                         av[j++] = "-g";
gmcgarry
1.82
643 #endif
ragge
1.11
644 #if 0
ragge
1.3
645                 if (gflag)
646                         av[j++] = "-lg";
ragge
1.11
647 #endif
ragge
1.52
648                 if (pthreads)
649                         av[j++] = "-lpthread";
gmcgarry
1.81
650                 if (!nostdlib) {
651                         if (pgflag) {
652                                 for (i = 0libclibs_profile[i]; i++)
653                                         av[j++] = libclibs_profile[i];
654                         } else {
655                                 for (i = 0libclibs[i]; i++)
656                                         av[j++] = libclibs[i];
657                         }
658                 }
ragge
1.84
659                 if (shared) {
660 #ifdef STARTFILES_S
661                         for (i = 0endfiles_S[i]; i++)
662                                 av[j++] = endfiles_S[i];
663 #endif
664                 } else {
665                         if (!nostartfiles) {
666                                 for (i = 0endfiles[i]; i++)
667                                         av[j++] = endfiles[i];
668                         }
ragge
1.14
669                 }
ragge
1.3
670                 av[j++] = 0;
gmcgarry
1.79
671                 eflag |= callsys(ldav);
ragge
1.1
672                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
673                         cunlink(setsuf(clist[0], 'o'));
ragge
1.46
674                 else if (nc > 0 && eflag == 0) {
675                         /* remove .o files XXX ugly */
676                         for (i = 0i < nci++)
677                                 cunlink(setsuf(clist[i], 'o'));
678                 }
ragge
1.1
679         }
ragge
1.36
680         dexit(eflag);
ragge
1.4
681         return 0;
ragge
1.1
682 }
683
ragge
1.36
684 /*
685  * exit and cleanup after interrupt.
686  */
ragge
1.4
687 void
688 idexit(int arg)
ragge
1.1
689 {
ragge
1.36
690         dexit(100);
ragge
1.1
691 }
692
ragge
1.36
693 /*
694  * exit and cleanup.
695  */
ragge
1.4
696 void
ragge
1.36
697 dexit(int eval)
ragge
1.1
698 {
ragge
1.10
699         if (!pflag && !Xflag) {
ragge
1.1
700                 if (sflag==0)
701                         cunlink(tmp3);
702                 cunlink(tmp4);
703         }
ragge
1.70
704         if (eval == 100)
705                 _exit(eval);
ragge
1.36
706         exit(eval);
ragge
1.1
707 }
708
ragge
1.36
709 static void
710 ccerror(char *sva_list ap)
ragge
1.1
711 {
ragge
1.36
712         vfprintf(Eflag ? stderr : stdoutsap);
ragge
1.8
713         putc('\n'Eflagstderr : stdout);
ragge
1.1
714         exfail++;
715         cflag++;
716         eflag++;
717 }
718
ragge
1.36
719 /*
720  * complain a bit.
721  */
722 void
723 error(char *s, ...)
724 {
725         va_list ap;
726
727         va_start(aps);
728         ccerror(sap);
729         va_end(ap);
730 }
731
732 /*
733  * complain a bit and then exit.
734  */
ragge
1.18
735 void
ragge
1.36
736 errorx(int evalchar *s, ...)
ragge
1.18
737 {
ragge
1.36
738         va_list ap;
739
740         va_start(aps);
741         ccerror(sap);
742         va_end(ap);
743         dexit(eval);
ragge
1.18
744 }
ragge
1.3
745
ragge
1.4
746 int
stefan
1.72
747 getsuf(char *as)
ragge
1.1
748 {
749         register char *s;
750
ragge
1.4
751         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
752                 return s[1];
ragge
1.3
753         return(0);
ragge
1.1
754 }
755
ragge
1.36
756 /*
757  * Get basename of string s and change its suffix to ch.
758  */
ragge
1.1
759 char *
ragge
1.36
760 setsuf(char *schar ch)
ragge
1.1
761 {
ragge
1.36
762         s = copy(basename(s));
pj
1.31
763         s[strlen(s) - 1] = ch;
ragge
1.36
764         return(s);
ragge
1.1
765 }
766
gmcgarry
1.89
767 #ifdef WIN32
ragge
1.4
768 int
gmcgarry
1.86
769 callsys(char *fchar *v[])
ragge
1.56
770 {
stefan
1.75
771         int tstatus = 0;
gmcgarry
1.89
772         char cmd[MAX_PATH];
gmcgarry
1.86
773         STARTUPINFO si;
774         PROCESS_INFORMATION pi;
775         DWORD exitCode;
776         BOOL ok;
777
gmcgarry
1.89
778         len = strlcpy(cmdfMAX_PATH);
779         for (t = 1v[t] && len < MAX_PATHt++) {
780                 len = strlcat(cmd" "MAX_PATH);
781                 len = strlcat(cmdv[t], MAX_PATH);
782         }
783
784         if (vflag)
785                 printf("%s\n"cmd);
786
gmcgarry
1.86
787         ZeroMemory(&sisizeof(STARTUPINFO));
788         si.cb = sizeof(STARTUPINFO);
gmcgarry
1.89
789         ZeroMemory(&pisizeof(PROCESS_INFORMATION));
790
791         ok = CreateProcess(NULL,  // the executable program
792                 cmd,   // the command line arguments
gmcgarry
1.86
793                 NULL,       // ignored
794                 NULL,       // ignored
795                 TRUE,       // inherit handles
gmcgarry
1.89
796                 HIGH_PRIORITY_CLASS,
gmcgarry
1.86
797                 NULL,       // ignored
798                 NULL,       // ignored
799                 &si,
800                 &pi);
801
802         if (!ok) {
803                 printf("Try Again\n");
804                 return 100;
805         }
806
gmcgarry
1.89
807         WaitForSingleObject(pi.hProcessINFINITE);
808         GetExitCodeProcess(pi.hProcess, &exitCode;
gmcgarry
1.86
809         return (exitCode != 0);
gmcgarry
1.89
810 }
811
812 #else
813
814 int
815 callsys(char *fchar *v[])
816 {
817         int tstatus = 0;
818         pid_t p;
819         char *s;
820
821         if (vflag) {
822                 fprintf(stderr"%s "f);
823                 for (t = 1v[t]; t++)
824                         fprintf(stderr"%s "v[t]);
825                 fprintf(stderr"\n");
gmcgarry
1.86
826         }
827
stefan
1.75
828         if ((p = fork()) == 0) {
ragge
1.47
829                 if (Bflag) {
ragge
1.56
830                         size_t len = strlen(Bflag) + 8;
ragge
1.53
831                         char *a = malloc(len);
stefan
1.71
832                         if (a == NULL) {
833                                 error("callsys: malloc failed");
834                                 exit(1);
835                         }
ragge
1.47
836                         if ((s = strrchr(f'/'))) {
ragge
1.53
837                                 strlcpy(aBflaglen);
838                                 strlcat(aslen);
ragge
1.47
839                                 execv(av);
840                         }
841                 }
gmcgarry
1.79
842                 execvp(fv);
ragge
1.4
843                 if ((s = strrchr(f'/')))
844                         execvp(s+1v);
stefan
1.75
845                 fprintf(stderr"Can't find %s\n"f);
846                 _exit(100);
847         } else {
848                 if (p == -1) {
ragge
1.3
849                         printf("Try again\n");
850                         return(100);
851                 }
stefan
1.75
852         }
853         while (waitpid(p, &status0) == -1 && errno == EINTR)
854                 ;
855         if (WIFEXITED(status))
856                 return (WEXITSTATUS(status));
857         if (WIFSIGNALED(status))
ragge
1.36
858                 dexit(eflag);
stefan
1.75
859         errorx(8"Fatal error in %s"f);
gmcgarry
1.89
860 }
gmcgarry
1.86
861 #endif
ragge
1.1
862
ragge