Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20071130230515

Diff

Diff from 1.79 to:

Annotations

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

Annotated File View

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