Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20080420020220

Diff

Diff from 1.94 to:

Annotations

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

Annotated File View

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