Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20071126050807

Diff

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