Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20080419131406

Diff

Diff from 1.91 to:

Annotations

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

Annotated File View

ragge
1.91
1 /*      $Id: cc.c,v 1.91 2008/04/19 13:14:06 ragge Exp $        */
ragge
1.3
2 /*
3  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code and documentation must retain the above
10  * copyright notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditionsand the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * All advertising materials mentioning features or use of this software
15  * must display the following acknowledgement:
16  *      This product includes software developed or owned by Caldera
17  *      International, Inc.
18  * Neither the name of Caldera International, Inc. nor the names of other
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
ragge
1.2
35
ragge
1.1
36 /*
ragge
1.3
37  * Front-end to the C compiler.
ragge
1.18
38  *
39  * Brief description of its syntax:
40  * - Files that end with .c are passed via cpp->ccom->as->ld
ragge
1.59
41  * - Files that end with .i are passed via ccom->as->ld
ragge
1.18
42  * - Files that end with .s are passed as->ld
43  * - Files that end with .o are passed directly to ld
44  * - Multiple files may be given on the command line.
45  * - Unrecognized options are all sent directly to ld.
46  * -c or -S cannot be combined with -o if multiple files are given.
47  *
48  * This file should be rewritten readable.
ragge
1.1
49  */
ragge
1.80
50 #include "config.h"
51
ragge
1.9
52 #include <sys/types.h>
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;
168 char *dynlinker[] = DYNLINKER;
169 char *crt0file = CRT0FILE;
gmcgarry
1.81
170 char *crt0file_profile = CRT0FILE_PROFILE;
ragge
1.25
171 char *startfiles[] = STARTFILES;
172 char *endfiles[] = ENDFILES;
ragge
1.84
173 #ifdef STARTFILES_S
174 char *startfiles_S[] = STARTFILES_S;
175 char *endfiles_S[] = ENDFILES_S;
176 #endif
ragge
1.25
177 char *cppmdadd[] = CPPMDADD;
ragge
1.50
178 #ifdef LIBCLIBS
179 char *libclibs[] = LIBCLIBS;
180 #else
181 char *libclibs[] = { "-lc"NULL };
182 #endif
gmcgarry
1.81
183 #ifdef LIBCLIBS_PROFILE
184 char *libclibs_profile[] = LIBCLIBS_PROFILE;
185 #else
186 char *libclibs_profile[] = { "-lc_p"NULL };
187 #endif
ragge
1.48
188 #ifndef STARTLABEL
189 #define STARTLABEL "__start"
190 #endif
ragge
1.1
191
ragge
1.4
192 int
pj
1.26
193 main(int argcchar *argv[])
194 {
ragge
1.15
195         char *t, *u;
ragge
1.1
196         char *assource;
ragge
1.3
197         char **pv, *ptemp[MAXOPT], **pvt;
ragge
1.36
198         int ncnlijcnxona;
ragge
1.3
199
ragge
1.36
200         i = nc = nl = nxo = 0;
ragge
1.3
201         pv = ptemp;
202         while(++i < argc) {
ragge
1.66
203                 if (argv[i][0] == '-') {
204                         switch (argv[i][1]) {
205                         default:
206                                 goto passa;
207 #ifdef notyet
208         /* must add library options first (-L/-l/...) */
209                                 error("unrecognized option `-%c'"argv[i][1]);
210                                 break;
211 #endif
ragge
1.22
212
ragge
1.66
213                         case 'B'/* other search paths for binaries */
214                                 Bflag = &argv[i][2];
215                                 break;
ragge
1.22
216
ragge
1.66
217                         case 'X':
218                                 Xflag++;
219                                 break;
220                         case 'W'/* Ignore (most of) W-flags */
gmcgarry
1.82
221                                 if (strncmp(argv[i], "-Werror"7) == 0) {
222                                         Werror = 1;
223                                 } else if (strncmp(argv[i], "-Wl,"4) == 0) {
ragge
1.66
224                                         /* options to the linker */
225                                         t = &argv[i][4];
226                                         while ((u = strchr(t','))) {
227                                                 *u++ = 0;
228                                                 llist[nl++] = t;
229                                                 t = u;
230                                         }
ragge
1.15
231                                         llist[nl++] = t;
ragge
1.83
232                                 } else if (strncmp(argv[i], "-Wp,"4) == 0) {
233                                         /* preprocessor */
234                                         if (!strncmp(argv[i], "-Wp,-C"6))
235                                                 Cflag++;
ragge
1.15
236                                 }
ragge
1.66
237                                 break;
ragge
1.15
238
ragge
1.66
239                         case 'f'/* GCC compatibility flags */
240                                 if (strcmp(argv[i], "-fPIC") == 0)
241                                         kflag = F_PIC;
242                                 if (strcmp(argv[i], "-fpic") == 0)
243                                         kflag = F_pic;
244                                 /* silently ignore the rest */
245                                 break;
ragge
1.14
246
ragge
1.66
247                         case 'g'/* create debug output */
248                                 gflag++;
249                                 break;
ragge
1.25
250
ragge
1.66
251                         case 'i':
252                                 if (strcmp(argv[i], "-isystem") == 0) {
253                                         *pv++ = "-S";
254                                         *pv++ = argv[++i];
255                                 } else if (strcmp(argv[i], "-include") == 0) {
256                                         *pv++ = "-i";
257                                         *pv++ = argv[++i];
ragge
1.83
258                                 } else if (strcmp(argv[i], "-idirafter") == 0) {
259                                         idirafter = argv[++i];
ragge
1.66
260                                 } else
261                                         goto passa;
262                                 break;
ragge
1.20
263
ragge
1.66
264                         case 'k'/* generate PIC code */
265                                 kflag = F_pic;
266                                 break;
ragge
1.49
267
gmcgarry
1.78
268                         case 'm'/* target-dependent options */
269                                 mlist[nm++] = argv[i];
270                                 break;
271
ragge
1.66
272                         case 'n'/* handle -n flags */
273                                 if (strcmp(argv[i], "-nostdinc") == 0)
274                                         nostdinc++;
275                                 else if (strcmp(argv[i], "-nostdlib") == 0) {
276                                         nostdlib++;
277                                         nostartfiles++;
278                                 } else if (strcmp(argv[i], "-nostartfiles") == 0)
279                                         nostartfiles = 1;
280                                 else
281                                         goto passa;
282                                 break;
ragge
1.14
283
ragge
1.66
284                         case 'p':
ragge
1.88
285                                 if (strcmp(argv[i], "-pg") == 0 ||
286                                     strcmp(argv[i], "-p") == 0)
ragge
1.66
287                                         pgflag++;
288                                 else if (strcmp(argv[i], "-pthread") == 0)
289                                         pthreads++;
gmcgarry
1.81
290                                 else if (strcmp(argv[i], "-pipe") == 0) {
291                                 } else
ragge
1.66
292                                         errorx(1"unknown option %s"argv[i]);
293                                 break;
ragge
1.52
294
ragge
1.66
295                         case 'x':
ragge
1.83
296                                 t = &argv[i][2];
297                                 if (*t == 0)
298                                         t = argv[++i];
299                                 if (strcmp(t"c") == 0)
ragge
1.91
300                                         xcflag = 1/* default */
ragge
1.83
301 #ifdef notyet
302                                 else if (strcmp(t"c++")
303                                         cxxflag++;
304 #endif
305                                 else
306                                         xlist[xnum++] = argv[i];
ragge
1.66
307                                 break;
308                         case 't':
309                                 tflag++;
310                                 break;
311                         case 'S':
312                                 sflag++;
313                                 cflag++;
314                                 break;
315                         case 'o':
316                                 if (outfile)
317                                         errorx(8"too many -o");
318                                 outfile = argv[++i];
319                                 break;
320                         case 'O':
ragge
1.85
321                                 if (argv[i][2] != '0')
322                                         Oflag++;
ragge
1.66
323                                 break;
324                         case 'E':
325                                 Eflag++;
326                                 break;
327                         case 'P':
328                                 pflag++;
329                                 *pv++ = argv[i];
330                         case 'c':
331                                 cflag++;
332                                 break;
ragge
1.3
333
ragge
1.24
334 #if 0
ragge
1.66
335                         case '2':
336                                 if(argv[i][2] == '\0')
337                                         pref = "/lib/crt2.o";
338                                 else {
339                                         pref = "/lib/crt20.o";
340                                 }
341                                 break;
ragge
1.24
342 #endif
ragge
1.66
343                         case 'C':
344                                 Cflag = 1;
345                                 break;
346                         case 'D':
347                         case 'I':
348                         case 'U':
349                                 *pv++ = argv[i];
350                                 if (argv[i][2] == 0)
351                                         *pv++ = argv[++i];
352                                 if (pv >= ptemp+MAXOPT) {
353                                         error("Too many DIU options");
354                                         --pv;
ragge
1.3
355                                 }
ragge
1.66
356                                 break;
ragge
1.3
357
ragge
1.66
358                         case 'M':
359                                 Mflag++;
360                                 break;
ragge
1.51
361
ragge
1.66
362                         case 'd':
363                                 dflag++;
ragge
1.67
364                                 strlcpy(alistargv[i], sizeof (alist));
ragge
1.66
365                                 break;
366                         case 'v':
367                                 printf("%s\n"VERSSTR);
368                                 vflag++;
369                                 break;
ragge
1.14
370
ragge
1.66
371                         case 's':
372                                 if (strcmp(argv[i], "-static") == 0)
373                                         Bstatic = 1;
ragge
1.84
374                                 else if (strcmp(argv[i], "-shared") == 0) {
375                                         shared = 1;
376                                         nostdlib = 1;
377                                 } else
ragge
1.66
378                                         goto passa;
379                                 break;
380                         }
ragge
1.3
381                 } else {
382                 passa:
383                         t = argv[i];
ragge
1.11
384                         if (*argv[i] == '-' && argv[i][1] == 'L')
385                                 ;
ragge
1.59
386                         else if((c=getsuf(t))=='c' || c=='S' || c=='i' ||
ragge
1.91
387                             c=='s'|| Eflag || xcflag) {
ragge
1.3
388                                 clist[nc++] = t;
ragge
1.66
389                                 if (nc>=MAXFIL) {
ragge
1.36
390                                         error("Too many source files");
ragge
1.3
391                                         exit(1);
ragge
1.66
392                                 }
ragge
1.3
393                                 t = setsuf(t'o');
394                         }
stefan
1.76
395
396                         /* Check for duplicate .o files. */
397                         for (j = getsuf(t) == 'o' ? 0 : nlj < nlj++) {
398                                 if (strcmp(llist[j], t) == 0)
399                                         break;
400                         }
401                         if (j == nl) {
ragge
1.3
402                                 llist[nl++] = t;
ragge
1.66
403                                 if (nl >= MAXLIB) {
ragge
1.36
404                                         error("Too many object/library files");
ragge
1.3
405                                         exit(1);
ragge
1.66
406                                 }
ragge
1.3
407                                 if (getsuf(t)=='o')
408                                         nxo++;
ragge
1.1
409                         }
410                 }
411         }
ragge
1.18
412         /* Sanity checking */
413         if (nc == 0 && nl == 0)
ragge
1.36
414                 errorx(8"no input files");
ragge
1.61
415         if (outfile && (cflag || sflag || Eflag) && nc > 1)
416                 errorx(8"-o given with -c || -E || -S and more than one file");
ragge
1.19
417         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
ragge
1.36
418                 errorx(8"output file will be clobbered");
ragge
1.4
419         if (gflagOflag = 0;
ragge
1.24
420 #if 0
421         if (proflag)
ragge
1.3
422                 pref = "/lib/mcrt0.o";
ragge
1.24
423 #endif
ragge
1.3
424         if(nc==0)
425                 goto nocom;
426         if (pflag==0) {
stefan
1.74
427                 if (!sflag)
428                         tmp3 = gettmp();
pj
1.30
429                 tmp4 = gettmp();
ragge
1.1
430         }
ragge
1.3
431         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
ragge
1.1
432                 signal(SIGINTidexit);
ragge
1.3
433         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
ragge
1.1
434                 signal(SIGTERMidexit);
ragge
1.3
435         pvt = pv;
ragge
1.1
436         for (i=0i<nci++) {
ragge
1.18
437                 /*
438                  * C preprocessor
439                  */
ragge
1.37
440                 if (nc>1 && !Eflag)
ragge
1.1
441                         printf("%s:\n"clist[i]);
ragge
1.43
442                 onlyas = 0;
pj
1.32
443                 assource = tmp3;
ragge
1.59
444                 if (getsuf(clist[i])=='i') {
445                         if(Eflag)
446                                 continue;
447                         goto com;
448                 } else if (getsuf(clist[i])=='s') {
ragge
1.1
449                         assource = clist[i];
ragge
1.43
450                         goto assemble;
stefan
1.73
451                 } else if (getsuf(clist[i])=='S')
ragge
1.43
452                         onlyas = 1;
ragge
1.1
453                 if (pflag)
454                         tmp4 = setsuf(clist[i], 'i');
ragge
1.5
455                 na = 0;
456                 av[na++] = "cpp";
ragge
1.60
457                 if (vflag)
458                         av[na++] = "-v";
ragge
1.20
459                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
ragge
1.40
460                 av[na++] = "-D__PCC_MINOR__=" MKS(PCC_MINOR);
461                 av[na++] = "-D__PCC_MINORMINOR__=" MKS(PCC_MINORMINOR);
ragge
1.52
462                 if (getsuf(clist[i])=='S')
463                         av[na++] = "-D__ASSEMBLER__";
464                 if (pthreads)
465                         av[na++] = "-D_PTHREADS";
ragge
1.63
466                 if (Cflag)
467                         av[na++] = "-C";
ragge
1.51
468                 if (Mflag)
469                         av[na++] = "-M";
ragge
1.57
470                 if (dflag)
471                         av[na++] = alist;
ragge
1.5
472                 for (j = 0cppadd[j]; j++)
473                         av[na++] = cppadd[j];
ragge
1.24
474                 for (j = 0cppmdadd[j]; j++)
475                         av[na++] = cppmdadd[j];
ragge
1.7
476                 if (tflag)
ragge
1.20
477                         av[na++] = "-t";
ragge
1.7
478                 for(pv=ptemppv <pvtpv++)
479                         av[na++] = *pv;
ragge
1.52
480                 if (!nostdinc)
481                         av[na++] = "-S"av[na++] = STDINC;
ragge
1.83
482                 if (idirafter) {
483                         av[na++] = "-I";
484                         av[na++] = idirafter;
485                 }
ragge
1.5
486                 av[na++] = clist[i];
ragge
1.51
487                 if (!Eflag && !Mflag)
ragge
1.20
488                         av[na++] = tmp4;
ragge
1.61
489                 if (Eflag && outfile)
490                          av[na++] = outfile;
ragge
1.3
491                 av[na++]=0;
492                 if (callsys(passpav))
493                         {exfail++; eflag++;}
ragge
1.51
494                 if (Eflag || Mflag)
ragge
1.37
495                         continue;
stefan
1.73
496                 if (onlyas) {
497                         assource = tmp4;
ragge
1.7
498                         goto assemble;
stefan
1.73
499                 }
ragge
1.18
500
501                 /*
502                  * C compiler
503                  */
ragge
1.59
504         com:
ragge
1.25
505                 na = 0;
506                 av[na++]= "ccom";
ragge
1.60
507                 if (vflag)
508                         av[na++] = "-v";
gmcgarry
1.81
509                 if (pgflag)
510                         av[na++] = "-p";
ragge
1.25
511                 if (gflag)
512                         av[na++] = "-g";
gmcgarry
1.82
513 #ifdef MACHOABI
514                 /* darwin always wants PIC compilation */
515                 av[na++] = "-k";
516 #else
ragge
1.49
517                 if (kflag)
518                         av[na++] = "-k";
gmcgarry
1.82
519 #endif
520                 if (Werror)
521                         av[na++] = "-Werror";
ragge
1.44
522                 if (Oflag) {
523                         av[na++] = "-xtemps";
ragge
1.64
524                         av[na++] = "-xdeljumps";
ragge
1.44
525                 }
ragge
1.45
526                 for (j = 0j < xnumj++)
527                         av[na++] = xlist[j];
gmcgarry
1.78
528                 for (j = 0j < nmj++)
529                         av[na++] = mlist[j];
ragge
1.68
530                 if (getsuf(clist[i])=='i')
531                         av[na++] = clist[i];
532                 else
533                         av[na++] = tmp4/* created by cpp */
ragge
1.3
534                 if (pflag || exfail)
535                         {
ragge
1.1
536                         cflag++;
537                         continue;
ragge
1.3
538                         }
ragge
1.16
539                 if(sflag) {
540                         if (outfile)
stefan
1.77
541                                 tmp3 = outfile;
ragge
1.16
542                         else
stefan
1.77
543                                 tmp3 = setsuf(clist[i], 's');
ragge
1.16
544                 }
ragge
1.25
545                 av[na++] = tmp3;
546 #if 0
ragge
1.3
547                 if (proflag) {
548                         av[3] = "-XP";
549                         av[4] = 0;
550                 } else
551                         av[3] = 0;
ragge
1.25
552 #endif
553                 av[na++] = NULL;
ragge
1.3
554                 if (callsys(pass0av)) {
ragge
1.1
555                         cflag++;
556                         eflag++;
557                         continue;
558                 }
559                 if (sflag)
560                         continue;
ragge
1.18
561
562                 /*
563                  * Assembler
564                  */
ragge
1.1
565         assemble:
ragge
1.49
566                 na = 0;
gmcgarry
1.79
567                 av[na++] = as;
ragge
1.60
568                 if (vflag)
569                         av[na++] = "-v";
ragge
1.49
570                 if (kflag)
571                         av[na++] = "-k";
572                 av[na++] = "-o";
ragge
1.18
573                 if (outfile && cflag)
ragge
1.49
574                         av[na++] = outfile;
ragge
1.18
575                 else
ragge
1.49
576                         av[na++] = setsuf(clist[i], 'o');
stefan
1.73
577                 av[na++] = assource;
ragge
1.49
578                 if (dflag)
579                         av[na++] = alist;
580                 av[na++] = 0;
gmcgarry
1.79
581                 if (callsys(asav)) {
ragge
1.1
582                         cflag++;
583                         eflag++;
ragge
1.7
584                         cunlink(tmp4);
ragge
1.1
585                         continue;
586                 }
ragge
1.7
587                 cunlink(tmp4);
ragge
1.1
588         }
ragge
1.18
589
ragge
1.51
590         if (Eflag || Mflag)
ragge
1.37
591                 dexit(eflag);
592
ragge
1.18
593         /*
594          * Linker
595          */
ragge
1.1
596 nocom:
597         if (cflag==0 && nl!=0) {
ragge
1.6
598                 j = 0;
gmcgarry
1.79
599                 av[j++] = ld;
ragge
1.60
600                 if (vflag)
601                         av[j++] = "-v";
ragge
1.6
602                 av[j++] = "-X";
ragge
1.84
603                 if (shared) {
604                         av[j++] = "-shared";
605                 } else {
606                         av[j++] = "-d";
gmcgarry
1.89
607 #ifndef os_win32
ragge
1.84
608                         av[j++] = "-e";
609                         av[j++] = STARTLABEL;
gmcgarry
1.89
610 #endif
ragge
1.84
611                         if (Bstatic == 0) { /* Dynamic linkage */
612                                 for (i = 0dynlinker[i]; i++)
613                                         av[j++] = dynlinker[i];
614                         } else
615                                 av[j++] = "-Bstatic";
616                 }
ragge
1.1
617                 if (outfile) {
ragge
1.3
618                         av[j++] = "-o";
619                         av[j++] = outfile;
620                 }
gmcgarry
1.89
621 #ifndef os_win32
ragge
1.84
622                 if (shared) {
623 #ifdef STARTFILES_S
624                         for (i = 0startfiles_S[i]; i++)
625                                 av[j++] = startfiles_S[i];
626 #endif
627                 } else {
628                         if (!nostartfiles) {
629                                 av[j++] = pgflag ? crt0file_profile : crt0file;
630                                 for (i = 0startfiles[i]; i++)
631                                         av[j++] = startfiles[i];
632                         }
ragge
1.14
633                 }
gmcgarry
1.89
634 #endif
ragge
1.24
635                 i = 0;
ragge
1.14
636                 while(i<nl) {
ragge
1.3
637                         av[j++] = llist[i++];
ragge
1.14
638                         if (j >= MAXAV)
ragge
1.36
639                                 error("Too many ld options");
ragge
1.14
640                 }
gmcgarry
1.82
641 #ifndef MACHOABI
642                 /* darwin assembler doesn't want -g */
ragge
1.65
643                 if (gflag)
644                         av[j++] = "-g";
gmcgarry
1.82
645 #endif
ragge
1.11
646 #if 0
ragge
1.3
647                 if (gflag)
648                         av[j++] = "-lg";
ragge
1.11
649 #endif
ragge
1.52
650                 if (pthreads)
651                         av[j++] = "-lpthread";
gmcgarry
1.81
652                 if (!nostdlib) {
653                         if (pgflag) {
654                                 for (i = 0libclibs_profile[i]; i++)
655                                         av[j++] = libclibs_profile[i];
656                         } else {
657                                 for (i = 0libclibs[i]; i++)
658                                         av[j++] = libclibs[i];
659                         }
660                 }
ragge
1.84
661                 if (shared) {
662 #ifdef STARTFILES_S
663                         for (i = 0endfiles_S[i]; i++)
664                                 av[j++] = endfiles_S[i];
665 #endif
666                 } else {
667                         if (!nostartfiles) {
668                                 for (i = 0endfiles[i]; i++)
669                                         av[j++] = endfiles[i];
670                         }
ragge
1.14
671                 }
ragge
1.3
672                 av[j++] = 0;
gmcgarry
1.79
673                 eflag |= callsys(ldav);
ragge
1.1
674                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
675                         cunlink(setsuf(clist[0], 'o'));
ragge
1.46
676                 else if (nc > 0 && eflag == 0) {
677                         /* remove .o files XXX ugly */
678                         for (i = 0i < nci++)
679                                 cunlink(setsuf(clist[i], 'o'));
680                 }
ragge
1.1
681         }
ragge
1.36
682         dexit(eflag);
ragge
1.4
683         return 0;
ragge
1.1
684 }
685
ragge
1.36
686 /*
687  * exit and cleanup after interrupt.
688  */
ragge
1.4
689 void
690 idexit(int arg)
ragge
1.1
691 {
ragge
1.36
692         dexit(100);
ragge
1.1
693 }
694
ragge
1.36
695 /*
696  * exit and cleanup.
697  */
ragge
1.4
698 void
ragge
1.36
699 dexit(int eval)
ragge
1.1
700 {
ragge
1.10
701         if (!pflag && !Xflag) {
ragge
1.1
702                 if (sflag==0)
703                         cunlink(tmp3);
704                 cunlink(tmp4);
705         }
ragge
1.70
706         if (eval == 100)
707                 _exit(eval);
ragge
1.36
708         exit(eval);
ragge
1.1
709 }
710
ragge
1.36
711 static void
712 ccerror(char *sva_list ap)
ragge
1.1
713 {
ragge
1.36
714         vfprintf(Eflag ? stderr : stdoutsap);
ragge
1.8
715         putc('\n'Eflagstderr : stdout);
ragge
1.1
716         exfail++;
717         cflag++;
718         eflag++;
719 }
720
ragge
1.36
721 /*
722  * complain a bit.
723  */
724 void
725 error(char *s, ...)
726 {
727         va_list ap;
728
729         va_start(aps);
730         ccerror(sap);
731         va_end(ap);
732 }
733
734 /*
735  * complain a bit and then exit.
736  */
ragge
1.18
737 void
ragge
1.36
738 errorx(int evalchar *s, ...)
ragge
1.18
739 {
ragge
1.36
740         va_list ap;
741
742         va_start(aps);
743         ccerror(sap);
744         va_end(ap);
745         dexit(eval);
ragge
1.18
746 }
ragge
1.3
747
ragge
1.4
748 int
stefan
1.72
749 getsuf(char *as)
ragge
1.1
750 {
751         register char *s;
752
ragge
1.4
753         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
754                 return s[1];
ragge
1.3
755         return(0);
ragge
1.1
756 }
757
ragge
1.36
758 /*
759  * Get basename of string s and change its suffix to ch.
760  */
ragge
1.1
761 char *
ragge
1.36
762 setsuf(char *schar ch)
ragge
1.1
763 {
ragge
1.91
764         char *as;
765
766         s = copy(basename(s), 2);
767         if ((as = strrchr(s'.')) == NULL) {
768                 as = s + strlen(s);
769                 as[0] = '.';
770         }
771         as[1] = ch;
772         as[2] = '\0';
ragge
1.36
773         return(s);
ragge
1.1
774 }
775
gmcgarry
1.89
776 #ifdef WIN32
ragge
1.4
777 int
gmcgarry
1.86
778 callsys(char *fchar *v[])
ragge
1.56
779 {
stefan
1.75
780         int tstatus = 0;
gmcgarry
1.89
781         char cmd[MAX_PATH];
gmcgarry
1.86
782         STARTUPINFO si;
783         PROCESS_INFORMATION pi;
784         DWORD exitCode;
785         BOOL ok;
786
gmcgarry
1.89
787         len = strlcpy(cmdfMAX_PATH);
788         for (t = 1v[t] && len < MAX_PATHt++) {
789                 len = strlcat(cmd" "MAX_PATH);
790                 len = strlcat(cmdv[t], MAX_PATH);
791         }
792
793         if (vflag)
794                 printf("%s\n"cmd);
795
gmcgarry
1.86
796         ZeroMemory(&sisizeof(STARTUPINFO));
797         si.cb = sizeof(STARTUPINFO);
gmcgarry
1.89
798         ZeroMemory(&pisizeof(PROCESS_INFORMATION));
799
800         ok = CreateProcess(NULL,  // the executable program
801                 cmd,   // the command line arguments
gmcgarry
1.86
802                 NULL,       // ignored
803                 NULL,       // ignored
804                 TRUE,       // inherit handles
gmcgarry
1.89
805                 HIGH_PRIORITY_CLASS,
gmcgarry
1.86
806                 NULL,       // ignored
807                 NULL,       // ignored
808                 &si,
809                 &pi);
810
811         if (!ok) {
812                 printf("Try Again\n");
813                 return 100;
814         }
815
gmcgarry
1.89
816         WaitForSingleObject(pi.hProcessINFINITE);
817         GetExitCodeProcess(pi.hProcess, &exitCode;
gmcgarry
1.86
818         return (exitCode != 0);
gmcgarry
1.89
819 }
820
821 #else
822
823 int
824 callsys(char *fchar *v[])
825 {
826         int tstatus = 0;
827         pid_t p;
828         char *s;
829
830         if (vflag) {
831                 fprintf(stderr"%s "f);
832                 for (t = 1v[t]; t++)
833                         fprintf(stderr"%s "v[t]);
834                 fprintf(stderr"\n");
gmcgarry
1.86
835         }
836
stefan
1.75
837         if ((p = fork()) == 0) {
ragge
1.47
838                 if (Bflag) {
ragge
1.56
839                         size_t len = strlen(Bflag) + 8;
ragge
1.53
840                         char *a = malloc(len);
stefan
1.71
841                         if (a == NULL) {
842                                 error("callsys: malloc failed");
843                                 exit(1);
844                         }
ragge
1.47
845                         if ((s = strrchr(f'/'))) {
ragge
1.53
846                                 strlcpy(aBflaglen);
847                                 strlcat(aslen);
ragge
1.47
848                                 execv(av);
849                         }
850                 }
gmcgarry
1.79
851                 execvp(fv);
ragge
1.4
852                 if ((s = strrchr(f'/')))
853                         execvp(s+1v);
stefan
1.75
854                 fprintf(stderr"Can't find %s\n"f);
855                 _exit(100);
856         } else {
857                 if (p == -1) {
ragge
1.3
858                         printf("Try again\n");
859                         return(100);
860                 }
stefan
1.75
861         }
862         while (waitpid(p, &status0) == -1 && errno == EINTR)
863                 ;
864         if (WIFEXITED(status))
865                 return (WEXITSTATUS(status));
866         if (WIFSIGNALED(status))
ragge
1.90
867                 dexit(eflag ? eflag :