Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:stefan:20071110094445

Diff

Diff from 1.75 to:

Annotations

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

Annotated File View

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