Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20080106171855

Diff

Diff from 1.80 to:

Annotations

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

Annotated File View

ragge
1.80
1 /*      $Id: cc.c,v 1.80 2008/01/06 17:18:55 ragge Exp $        */
ragge
1.3
2 /*
3  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code and documentation must retain the above
10  * copyright notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditionsand the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * All advertising materials mentioning features or use of this software
15  * must display the following acknowledgement:
16  *      This product includes software developed or owned by Caldera
17  *      International, Inc.
18  * Neither the name of Caldera International, Inc. nor the names of other
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
ragge
1.2
35
ragge
1.1
36 /*
ragge
1.3
37  * Front-end to the C compiler.
ragge
1.18
38  *
39  * Brief description of its syntax:
40  * - Files that end with .c are passed via cpp->ccom->as->ld
ragge
1.59
41  * - Files that end with .i are passed via ccom->as->ld
ragge
1.18
42  * - Files that end with .s are passed as->ld
43  * - Files that end with .o are passed directly to ld
44  * - Multiple files may be given on the command line.
45  * - Unrecognized options are all sent directly to ld.
46  * -c or -S cannot be combined with -o if multiple files are given.
47  *
48  * This file should be rewritten readable.
ragge
1.1
49  */
ragge
1.80
50 #include "config.h"
51
ragge
1.9
52 #include <sys/types.h>
ragge
1.36
53 #include <sys/wait.h>
ragge
1.2
54
ragge
1.1
55 #include <ctype.h>
stefan
1.72
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <libgen.h>
ragge
1.1
59 #include <signal.h>
stefan
1.72
60 #include <stdarg.h>
61 #include <stdio.h>
62 #include <stdlib.h>
ragge
1.4
63 #include <string.h>
64 #include <unistd.h>
ragge
1.24
65
66 #include "ccconfig.h"
ragge
1.3
67 /* C command */
ragge
1.1
68
ragge
1.20
69 #define MKS(x) _MKS(x)
70 #define _MKS(x) #x
71
72 /*
73  * Many specific definitions, should be declared elsewhere.
74  */
gmcgarry
1.79
75
76 #ifndef STDINC
77 #define STDINC          "/usr/include/"
78 #endif
79
80 #ifndef PREPROCESSOR
81 #define PREPROCESSOR    "cpp"
82 #endif
83
84 #ifndef COMPILER
85 #define COMPILER        "ccom";
86 #endif
87
88 #ifndef ASSEMBLER
89 #define ASSEMBLER       "as"
90 #endif
91
92 #ifndef LINKER
93 #define LINKER          "ld"
94 #endif
ragge
1.20
95
ragge
1.62
96 #define MAXFIL 10000
ragge
1.12
97 #define MAXLIB 10000
ragge
1.14
98 #define MAXAV  10000
ragge
1.3
99 #define MAXOPT 100
100 char    *tmp3;
101 char    *tmp4;
ragge
1.1
102 char    *outfile;
stefan
1.72
103 char *copy(char *),*setsuf(char *, char);
104 int getsuf(char *);
ragge
1.4
105 int main(intchar *[]);
ragge
1.36
106 void error(char *, ...);
stefan
1.72
107 void errorx(intchar *, ...);
ragge
1.4
108 int callsys(char [], char *[]);
109 int cunlink(char *);
stefan
1.72
110 void dexit(int);
ragge
1.4
111 void idexit(int);
pj
1.30
112 char *gettmp();
ragge
1.14
113 char    *av[MAXAV];
ragge
1.3
114 char    *clist[MAXFIL];
115 char    *llist[MAXLIB];
ragge
1.4
116 char    alist[20];
ragge
1.43
117 char    *xlist[100];
118 int     xnum;
gmcgarry
1.78
119 char    *mlist[100];
120 int     nm;
ragge
1.63
121 int     Cflag;
ragge
1.43
122 int     dflag;
ragge
1.3
123 int     pflag;
124 int     sflag;
125 int     cflag;
126 int     eflag;
127 int     gflag;
ragge
1.5
128 int     vflag;
ragge
1.7
129 int     tflag;
ragge
1.8
130 int     Eflag;
ragge
1.4
131 int     Oflag;
ragge
1.58
132 int     kflag;  /* generate PIC/pic code */
133 #define F_PIC   1
134 #define F_pic   2
ragge
1.51
135 int     Mflag;  /* dependencies only */
ragge
1.52
136 int     pgflag;
ragge
1.1
137 int     exfail;
ragge
1.10
138 int     Xflag;
ragge
1.14
139 int     nostartfilesBstatic;
ragge
1.52
140 int     nostdincnostdlib;
ragge
1.43
141 int     onlyas;
ragge
1.52
142 int     pthreads;
ragge
1.14
143
gmcgarry
1.79
144 char    *passp = LIBEXECDIR "/" PREPROCESSOR;
145 char    *pass0 = LIBEXECDIR "/" COMPILER;
146 char    *as = ASSEMBLER;
147 char    *ld = LINKER;
ragge
1.47
148 char    *Bflag;
ragge
1.25
149 char *cppadd[] = CPPADD;
150 char *dynlinker[] = DYNLINKER;
151 char *crt0file = CRT0FILE;
152 char *startfiles[] = STARTFILES;
153 char *endfiles[] = ENDFILES;
154 char *cppmdadd[] = CPPMDADD;
ragge
1.50
155 #ifdef LIBCLIBS
156 char *libclibs[] = LIBCLIBS;
157 #else
158 char *libclibs[] = { "-lc"NULL };
159 #endif
ragge
1.48
160 #ifndef STARTLABEL
161 #define STARTLABEL "__start"
162 #endif
ragge
1.1
163
ragge
1.4
164 int
pj
1.26
165 main(int argcchar *argv[])
166 {
ragge
1.15
167         char *t, *u;
ragge
1.1
168         char *assource;
ragge
1.3
169         char **pv, *ptemp[MAXOPT], **pvt;
ragge
1.36
170         int ncnlijcnxona;
ragge
1.3
171
ragge
1.36
172         i = nc = nl = nxo = 0;
ragge
1.3
173         pv = ptemp;
174         while(++i < argc) {
ragge
1.66
175                 if (argv[i][0] == '-') {
176                         switch (argv[i][1]) {
177                         default:
178                                 goto passa;
179 #ifdef notyet
180         /* must add library options first (-L/-l/...) */
181                                 error("unrecognized option `-%c'"argv[i][1]);
182                                 break;
183 #endif
ragge
1.22
184
ragge
1.66
185                         case 'B'/* other search paths for binaries */
186                                 Bflag = &argv[i][2];
187                                 break;
ragge
1.22
188
ragge
1.66
189                         case 'X':
190                                 Xflag++;
191                                 break;
192                         case 'W'/* Ignore (most of) W-flags */
193                                 if (strncmp(argv[i], "-Wl,"4) == 0) {
194                                         /* options to the linker */
195                                         t = &argv[i][4];
196                                         while ((u = strchr(t','))) {
197                                                 *u++ = 0;
198                                                 llist[nl++] = t;
199                                                 t = u;
200                                         }
ragge
1.15
201                                         llist[nl++] = t;
202                                 }
ragge
1.66
203                                 break;
ragge
1.15
204
ragge
1.66
205                         case 'f'/* GCC compatibility flags */
206                                 if (strcmp(argv[i], "-fPIC") == 0)
207                                         kflag = F_PIC;
208                                 if (strcmp(argv[i], "-fpic") == 0)
209                                         kflag = F_pic;
210                                 /* silently ignore the rest */
211                                 break;
ragge
1.14
212
ragge
1.66
213                         case 'g'/* create debug output */
214                                 gflag++;
215                                 break;
ragge
1.25
216
ragge
1.66
217                         case 'i':
218                                 if (strcmp(argv[i], "-isystem") == 0) {
219                                         *pv++ = "-S";
220                                         *pv++ = argv[++i];
221                                 } else if (strcmp(argv[i], "-include") == 0) {
222                                         *pv++ = "-i";
223                                         *pv++ = argv[++i];
224                                 } else
225                                         goto passa;
226                                 break;
ragge
1.20
227
ragge
1.66
228                         case 'k'/* generate PIC code */
229                                 kflag = F_pic;
230                                 break;
ragge
1.49
231
gmcgarry
1.78
232                         case 'm'/* target-dependent options */
233                                 mlist[nm++] = argv[i];
234                                 break;
235
ragge
1.66
236                         case 'n'/* handle -n flags */
237                                 if (strcmp(argv[i], "-nostdinc") == 0)
238                                         nostdinc++;
239                                 else if (strcmp(argv[i], "-nostdlib") == 0) {
240                                         nostdlib++;
241                                         nostartfiles++;
242                                 } else if (strcmp(argv[i], "-nostartfiles") == 0)
243                                         nostartfiles = 1;
244                                 else
245                                         goto passa;
246                                 break;
ragge
1.14
247
ragge
1.66
248                         case 'p':
249                                 if (strcmp(argv[i], "-pg") == 0)
250                                         pgflag++;
251                                 else if (strcmp(argv[i], "-pthread") == 0)
252                                         pthreads++;
253                                 else
254                                         errorx(1"unknown option %s"argv[i]);
255                                 break;
ragge
1.52
256
ragge
1.66
257                         case 'x':
258                                 xlist[xnum++] = argv[i];
259                                 break;
260                         case 't':
261                                 tflag++;
262                                 break;
263                         case 'S':
264                                 sflag++;
265                                 cflag++;
266                                 break;
267                         case 'o':
268                                 if (outfile)
269                                         errorx(8"too many -o");
270                                 outfile = argv[++i];
271                                 break;
272                         case 'O':
273                                 Oflag++;
274                                 break;
275                         case 'E':
276                                 Eflag++;
277                                 break;
278                         case 'P':
279                                 pflag++;
280                                 *pv++ = argv[i];
281                         case 'c':
282                                 cflag++;
283                                 break;
ragge
1.3
284
ragge
1.24
285 #if 0
ragge
1.66
286                         case '2':
287                                 if(argv[i][2] == '\0')
288                                         pref = "/lib/crt2.o";
289                                 else {
290                                         pref = "/lib/crt20.o";
291                                 }
292                                 break;
ragge
1.24
293 #endif
ragge
1.66
294                         case 'C':
295                                 Cflag = 1;
296                                 break;
297                         case 'D':
298                         case 'I':
299                         case 'U':
300                                 *pv++ = argv[i];
301                                 if (argv[i][2] == 0)
302                                         *pv++ = argv[++i];
303                                 if (pv >= ptemp+MAXOPT) {
304                                         error("Too many DIU options");
305                                         --pv;
ragge
1.3
306                                 }
ragge
1.66
307                                 break;
ragge
1.3
308
ragge
1.66
309                         case 'M':
310                                 Mflag++;
311                                 break;
ragge
1.51
312
ragge
1.66
313                         case 'd':
314                                 dflag++;
ragge
1.67
315                                 strlcpy(alistargv[i], sizeof (alist));
ragge
1.66
316                                 break;
317                         case 'v':
318                                 printf("%s\n"VERSSTR);
319                                 vflag++;
320                                 break;
ragge
1.14
321
ragge
1.66
322                         case 's':
323                                 if (strcmp(argv[i], "-static") == 0)
324                                         Bstatic = 1;
325                                 else
326                                         goto passa;
327                                 break;
328                         }
ragge
1.3
329                 } else {
330                 passa:
331                         t = argv[i];
ragge
1.11
332                         if (*argv[i] == '-' && argv[i][1] == 'L')
333                                 ;
ragge
1.59
334                         else if((c=getsuf(t))=='c' || c=='S' || c=='i' ||
ragge
1.51
335                             c=='s'|| Eflag) {
ragge
1.3
336                                 clist[nc++] = t;
ragge
1.66
337                                 if (nc>=MAXFIL) {
ragge
1.36
338                                         error("Too many source files");
ragge
1.3
339                                         exit(1);
ragge
1.66
340                                 }
ragge
1.3
341                                 t = setsuf(t'o');
342                         }
stefan
1.76
343
344                         /* Check for duplicate .o files. */
345                         for (j = getsuf(t) == 'o' ? 0 : nlj < nlj++) {
346                                 if (strcmp(llist[j], t) == 0)
347                                         break;
348                         }
349                         if (j == nl) {
ragge
1.3
350                                 llist[nl++] = t;
ragge
1.66
351                                 if (nl >= MAXLIB) {
ragge
1.36
352                                         error("Too many object/library files");
ragge
1.3
353                                         exit(1);
ragge
1.66
354                                 }
ragge
1.3
355                                 if (getsuf(t)=='o')
356                                         nxo++;
ragge
1.1
357                         }
358                 }
359         }
ragge
1.18
360         /* Sanity checking */
361         if (nc == 0 && nl == 0)
ragge
1.36
362                 errorx(8"no input files");
ragge
1.61
363         if (outfile && (cflag || sflag || Eflag) && nc > 1)
364                 errorx(8"-o given with -c || -E || -S and more than one file");
ragge
1.19
365         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
ragge
1.36
366                 errorx(8"output file will be clobbered");
ragge
1.4
367         if (gflagOflag = 0;
ragge
1.24
368 #if 0
369         if (proflag)
ragge
1.3
370                 pref = "/lib/mcrt0.o";
ragge
1.24
371 #endif
ragge
1.3
372         if(nc==0)
373                 goto nocom;
374         if (pflag==0) {
stefan
1.74
375                 if (!sflag)
376                         tmp3 = gettmp();
pj
1.30
377                 tmp4 = gettmp();
ragge
1.1
378         }
ragge
1.3
379         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
ragge
1.1
380                 signal(SIGINTidexit);
ragge
1.3
381         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
ragge
1.1
382                 signal(SIGTERMidexit);
ragge
1.3
383         pvt = pv;
ragge
1.1
384         for (i=0i<nci++) {
ragge
1.18
385                 /*
386                  * C preprocessor
387                  */
ragge
1.37
388                 if (nc>1 && !Eflag)
ragge
1.1
389                         printf("%s:\n"clist[i]);
ragge
1.43
390                 onlyas = 0;
pj
1.32
391                 assource = tmp3;
ragge
1.59
392                 if (getsuf(clist[i])=='i') {
393                         if(Eflag)
394                                 continue;
395                         goto com;
396                 } else if (getsuf(clist[i])=='s') {
ragge
1.1
397                         assource = clist[i];
ragge
1.43
398                         goto assemble;
stefan
1.73
399                 } else if (getsuf(clist[i])=='S')
ragge
1.43
400                         onlyas = 1;
ragge
1.1
401                 if (pflag)
402                         tmp4 = setsuf(clist[i], 'i');
ragge
1.5
403                 na = 0;
404                 av[na++] = "cpp";
ragge
1.60
405                 if (vflag)
406                         av[na++] = "-v";
ragge
1.20
407                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
ragge
1.40
408                 av[na++] = "-D__PCC_MINOR__=" MKS(PCC_MINOR);
409                 av[na++] = "-D__PCC_MINORMINOR__=" MKS(PCC_MINORMINOR);
ragge
1.52
410                 if (getsuf(clist[i])=='S')
411                         av[na++] = "-D__ASSEMBLER__";
412                 if (pthreads)
413                         av[na++] = "-D_PTHREADS";
ragge
1.63
414                 if (Cflag)
415                         av[na++] = "-C";
ragge
1.51
416                 if (Mflag)
417                         av[na++] = "-M";
ragge
1.57
418                 if (dflag)
419                         av[na++] = alist;
ragge
1.5
420                 for (j = 0cppadd[j]; j++)
421                         av[na++] = cppadd[j];
ragge
1.24
422                 for (j = 0cppmdadd[j]; j++)
423                         av[na++] = cppmdadd[j];
ragge
1.7
424                 if (tflag)
ragge
1.20
425                         av[na++] = "-t";
ragge
1.7
426                 for(pv=ptemppv <pvtpv++)
427                         av[na++] = *pv;
ragge
1.52
428                 if (!nostdinc)
429                         av[na++] = "-S"av[na++] = STDINC;
ragge
1.5
430                 av[na++] = clist[i];
ragge
1.51
431                 if (!Eflag && !Mflag)
ragge
1.20
432                         av[na++] = tmp4;
ragge
1.61
433                 if (Eflag && outfile)
434                          av[na++] = outfile;
ragge
1.3
435                 av[na++]=0;
436                 if (callsys(passpav))
437                         {exfail++; eflag++;}
ragge
1.51
438                 if (Eflag || Mflag)
ragge
1.37
439                         continue;
stefan
1.73
440                 if (onlyas) {
441                         assource = tmp4;
ragge
1.7
442                         goto assemble;
stefan
1.73
443                 }
ragge
1.18
444
445                 /*
446                  * C compiler
447                  */
ragge
1.59
448         com:
ragge
1.25
449                 na = 0;
450                 av[na++]= "ccom";
ragge
1.60
451                 if (vflag)
452                         av[na++] = "-v";
ragge
1.25
453                 if (gflag)
454                         av[na++] = "-g";
ragge
1.49
455                 if (kflag)
456                         av[na++] = "-k";
ragge
1.44
457                 if (Oflag) {
458                         av[na++] = "-xtemps";
ragge
1.64
459                         av[na++] = "-xdeljumps";
ragge
1.44
460                 }
ragge
1.45
461                 for (j = 0j < xnumj++)
462                         av[na++] = xlist[j];
gmcgarry
1.78
463                 for (j = 0j < nmj++)
464                         av[na++] = mlist[j];
ragge
1.68
465                 if (getsuf(clist[i])=='i')
466                         av[na++] = clist[i];
467                 else
468                         av[na++] = tmp4/* created by cpp */
ragge
1.3
469                 if (pflag || exfail)
470                         {
ragge
1.1
471                         cflag++;
472                         continue;
ragge
1.3
473                         }
ragge
1.16
474                 if(sflag) {
475                         if (outfile)
stefan
1.77
476                                 tmp3 = outfile;
ragge
1.16
477                         else
stefan
1.77
478                                 tmp3 = setsuf(clist[i], 's');
ragge
1.16
479                 }
ragge
1.25
480                 av[na++] = tmp3;
481 #if 0
ragge
1.3
482                 if (proflag) {
483                         av[3] = "-XP";
484                         av[4] = 0;
485                 } else
486                         av[3] = 0;
ragge
1.25
487 #endif
488                 av[na++] = NULL;
ragge
1.3
489                 if (callsys(pass0av)) {
ragge
1.1
490                         cflag++;
491                         eflag++;
492                         continue;
493                 }
494                 if (sflag)
495                         continue;
ragge
1.18
496
497                 /*
498                  * Assembler
499                  */
ragge
1.1
500         assemble:
ragge
1.49
501                 na = 0;
gmcgarry
1.79
502                 av[na++] = as;
ragge
1.60
503                 if (vflag)
504                         av[na++] = "-v";
ragge
1.49
505                 if (kflag)
506                         av[na++] = "-k";
507                 av[na++] = "-o";
ragge
1.18
508                 if (outfile && cflag)
ragge
1.49
509                         av[na++] = outfile;
ragge
1.18
510                 else
ragge
1.49
511                         av[na++] = setsuf(clist[i], 'o');
stefan
1.73
512                 av[na++] = assource;
ragge
1.49
513                 if (dflag)
514                         av[na++] = alist;
515                 av[na++] = 0;
gmcgarry
1.79
516                 if (callsys(asav)) {
ragge
1.1
517                         cflag++;
518                         eflag++;
ragge
1.7
519                         cunlink(tmp4);
ragge
1.1
520                         continue;
521                 }
ragge
1.7
522                 cunlink(tmp4);
ragge
1.1
523         }
ragge
1.18
524
ragge
1.51
525         if (Eflag || Mflag)
ragge
1.37
526                 dexit(eflag);
527
ragge
1.18
528         /*
529          * Linker
530          */
ragge
1.1
531 nocom:
532         if (cflag==0 && nl!=0) {
ragge
1.6
533                 j = 0;
gmcgarry
1.79
534                 av[j++] = ld;
ragge
1.60
535                 if (vflag)
536                         av[j++] = "-v";
ragge
1.6
537                 av[j++] = "-X";
538                 av[j++] = "-d";
539                 av[j++] = "-e";
ragge
1.48
540                 av[j++] = STARTLABEL;
ragge
1.14
541                 if (Bstatic == 0) { /* Dynamic linkage */
ragge
1.24
542                         for (i = 0dynlinker[i]; i++)
543                                 av[j++] = dynlinker[i];
ragge
1.54
544                 } else
545                         av[j++] = "-Bstatic";
ragge
1.1
546                 if (outfile) {
ragge
1.3
547                         av[j++] = "-o";
548                         av[j++] = outfile;
549                 }
ragge
1.14
550                 if (!nostartfiles) {
ragge
1.24
551                         av[j++] = crt0file;
552                         for (i = 0startfiles[i]; i++)
553                                 av[j++] = startfiles[i];
ragge
1.14
554                 }
ragge
1.24
555                 i = 0;
ragge
1.14
556                 while(i<nl) {
ragge
1.3
557                         av[j++] = llist[i++];
ragge
1.14
558                         if (j >= MAXAV)
ragge
1.36
559                                 error("Too many ld options");
ragge
1.14
560                 }
ragge
1.65
561                 if (gflag)
562                         av[j++] = "-g";
ragge
1.11
563 #if 0
ragge
1.3
564                 if (gflag)
565                         av[j++] = "-lg";
ragge
1.11
566 #endif
ragge
1.52
567                 if (pthreads)
568                         av[j++] = "-lpthread";
569                 if (!nostdlib)
570                         for (i = 0libclibs[i]; i++)
571                                 av[j++] = libclibs[i];
ragge
1.14
572                 if (!nostartfiles) {
ragge
1.24
573                         for (i = 0endfiles[i]; i++)
574                                 av[j++] = endfiles[i];
ragge
1.14
575                 }
ragge
1.3
576                 av[j++] = 0;
gmcgarry
1.79
577                 eflag |= callsys(ldav);
ragge
1.1
578                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
579                         cunlink(setsuf(clist[0], 'o'));
ragge
1.46
580                 else if (nc > 0 && eflag == 0) {
581                         /* remove .o files XXX ugly */
582                         for (i = 0i < nci++)
583                                 cunlink(setsuf(clist[i], 'o'));
584                 }
ragge
1.1
585         }
ragge
1.36
586         dexit(eflag);
ragge
1.4
587         return 0;
ragge
1.1
588 }
589
ragge
1.36
590 /*
591  * exit and cleanup after interrupt.
592  */
ragge
1.4
593 void
594 idexit(int arg)
ragge
1.1
595 {
ragge
1.36
596         dexit(100);
ragge
1.1
597 }
598
ragge
1.36
599 /*
600  * exit and cleanup.
601  */
ragge
1.4
602 void
ragge
1.36
603 dexit(int eval)
ragge
1.1
604 {
ragge
1.10
605         if (!pflag && !Xflag) {
ragge
1.1
606                 if (sflag==0)
607                         cunlink(tmp3);
608                 cunlink(tmp4);
609         }
ragge
1.70
610         if (eval == 100)
611                 _exit(eval);
ragge
1.36
612         exit(eval);
ragge
1.1
613 }
614
ragge
1.36
615 static void
616 ccerror(char *sva_list ap)
ragge
1.1
617 {
ragge
1.36
618         vfprintf(Eflag ? stderr : stdoutsap);
ragge
1.8
619         putc('\n'Eflagstderr : stdout);
ragge
1.1
620         exfail++;
621         cflag++;
622         eflag++;
623 }
624
ragge
1.36
625 /*
626  * complain a bit.
627  */
628 void
629 error(char *s, ...)
630 {
631         va_list ap;
632
633         va_start(aps);
634         ccerror(sap);
635         va_end(ap);
636 }
637
638 /*
639  * complain a bit and then exit.
640  */
ragge
1.18
641 void
ragge
1.36
642 errorx(int evalchar *s, ...)
ragge
1.18
643 {
ragge
1.36
644         va_list ap;
645
646         va_start(aps);
647         ccerror(sap);
648         va_end(ap);
649         dexit(eval);
ragge
1.18
650 }
ragge
1.3
651
ragge
1.4
652 int
stefan
1.72
653 getsuf(char *as)
ragge
1.1
654 {
655         register char *s;
656
ragge
1.4
657         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
658                 return s[1];
ragge
1.3
659         return(0);
ragge
1.1
660 }
661
ragge
1.36
662 /*
663  * Get basename of string s and change its suffix to ch.
664  */
ragge
1.1
665 char *
ragge
1.36
666 setsuf(char *schar ch)
ragge
1.1
667 {
ragge
1.36
668         s = copy(basename(s));
pj
1.31
669         s[strlen(s) - 1] = ch;
ragge
1.36
670         return(s);
ragge
1.1
671 }
672
ragge
1.4
673 int
ragge
1.56
674 callsys(char f[], char *v[])
675 {
stefan
1.75
676         int tstatus = 0;
677         pid_t p;
ragge
1.4
678         char *s;
ragge
1.5
679
680         if (vflag) {
ragge
1.41
681                 fprintf(stderr"%s "f);
682                 for (t = 1v[t]; t++)
ragge
1.6
683                         fprintf(stderr"%s "v[t]);
684                 fprintf(stderr"\n");
ragge
1.5
685         }
ragge
1.1
686
stefan
1.75
687         if ((p = fork()) == 0) {
ragge
1.47
688                 if (Bflag) {
ragge
1.56
689                         size_t len = strlen(Bflag) + 8;
ragge
1.53
690                         char *a = malloc(len);
stefan
1.71
691                         if (a == NULL) {
692                                 error("callsys: malloc failed");
693                                 exit(1);
694                         }
ragge
1.47
695                         if ((s = strrchr(f'/'))) {
ragge
1.53
696                                 strlcpy(aBflaglen);
697                                 strlcat(aslen);
ragge
1.47
698                                 execv(av);
699                         }
700                 }
gmcgarry
1.79
701                 execvp(fv);
ragge
1.4
702                 if ((s = strrchr(f'/')))
703                         execvp(s+1v);
stefan
1.75
704                 fprintf(stderr"Can't find %s\n"f);
705                 _exit(100);
706         } else {
707                 if (p == -1) {
ragge
1.3
708                         printf("Try again\n");
709                         return(100);
710                 }
stefan
1.75
711         }
712         while (waitpid(p, &status0) == -1 && errno == EINTR)
713                 ;
714         if (WIFEXITED(status))
715                 return (WEXITSTATUS(status));
716         if (WIFSIGNALED(status))
ragge
1.36
717                 dexit(eflag);
stefan
1.75
718         errorx(8"Fatal error in %s"f);
ragge
1.1
719 }
720
ragge
1.3
721 char *
pj
1.28
722 copy(char *as)
ragge
1.3
723 {
pj
1.28
724         char *p;
725
ragge
1.36
726         if ((p = strdup(as)) == NULL)
727                 errorx(8"no space for file names");
ragge
1.3
728
pj
1.28
729         return p;
ragge
1.3
730 }
731
ragge
1.4
732 int
stefan
1.72
733 cunlink(char *f)
ragge
1.1
734 {
ragge
1.13
735         if (f==0 || Xflag)
ragge
1.3
736                 return(0);
737         return(unlink(f));
ragge
1.1
738 }
pj
1.30
739
740 char *
stefan
1.69
741 gettmp(void)
pj
1.30
742 {
stefan
1.69
743         char *sfn = copy("/tmp/ctm.XXXXXX");
pj
1.30
744         int fd = -1;
745
746         if ((fd = mkstemp(sfn)) == -1) {
747                 fprintf(stderr"%s: %s\n"sfnstrerror(errno));
748                 exit(8);
749         }
750         close(fd);
751
752         return sfn;
753 }
FishEye: Open Source License registered to PCC.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-11-01 10:48 +0100