source: trunk/src/makedep/main.c@ 168

Last change on this file since 168 was 168, checked in by bird, 21 years ago

Merged in mozilla changes and config.

  • Property svn:eol-style set to native
File size: 18.9 KB
Line 
1/* $Xorg: main.c,v 1.5 2001/02/09 02:03:16 xorgcvs Exp $ */
2/*
3
4Copyright (c) 1993, 1994, 1998 The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall not be
23used in advertising or otherwise to promote the sale, use or other dealings
24in this Software without prior written authorization from The Open Group.
25
26*/
27/* $XFree86: xc/config/makedepend/main.c,v 3.31tsi Exp $ */
28
29#include "def.h"
30#ifdef hpux
31#define sigvec sigvector
32#endif /* hpux */
33
34#ifdef X_POSIX_C_SOURCE
35#define _POSIX_C_SOURCE X_POSIX_C_SOURCE
36#include <signal.h>
37#undef _POSIX_C_SOURCE
38#else
39#if defined(X_NOT_POSIX) || defined(_POSIX_SOURCE)
40#include <signal.h>
41#else
42#define _POSIX_SOURCE
43#include <signal.h>
44#undef _POSIX_SOURCE
45#endif
46#endif
47
48#include <stdarg.h>
49
50#ifdef MINIX
51#define USE_CHMOD 1
52#endif
53
54#ifdef DEBUG
55int _debugmask;
56#endif
57
58/* #define DEBUG_DUMP */
59#ifdef DEBUG_DUMP
60#define DBG_PRINT(file, fmt, args) fprintf(file, fmt, args)
61#else
62#define DBG_PRINT(file, fmt, args) /* empty */
63#endif
64
65#define DASH_INC_PRE "#include \""
66#define DASH_INC_POST "\""
67
68char *ProgramName;
69
70char *directives[] = {
71 "if",
72 "ifdef",
73 "ifndef",
74 "else",
75 "endif",
76 "define",
77 "undef",
78 "include",
79 "line",
80 "pragma",
81 "error",
82 "ident",
83 "sccs",
84 "elif",
85 "eject",
86 "warning",
87 "include_next",
88 NULL
89};
90
91#define MAKEDEPEND
92#include "imakemdep.h" /* from config sources */
93#undef MAKEDEPEND
94
95struct inclist inclist[ MAXFILES ],
96 *inclistp = inclist,
97 *inclistnext = inclist,
98 maininclist;
99
100static char *filelist[ MAXFILES ];
101char *includedirs[ MAXDIRS + 1 ],
102 **includedirsnext = includedirs;
103char *notdotdot[ MAXDIRS ];
104static int cmdinc_count = 0;
105static char *cmdinc_list[ 2 * MAXINCFILES ];
106char *objprefix = "";
107char *objsuffix = OBJSUFFIX;
108static char *startat = "# DO NOT DELETE";
109int width = 78;
110static boolean append = FALSE;
111boolean printed = FALSE;
112boolean verbose = FALSE;
113boolean show_where_not = FALSE;
114/* Warn on multiple includes of same file */
115boolean warn_multiple = FALSE;
116
117static void setfile_cmdinc(struct filepointer *filep, long count, char **list);
118static void redirect(char *line, char *makefile);
119
120static
121#ifdef SIGNALRETURNSINT
122int
123#else
124void
125#endif
126catch (int sig)
127{
128 fflush (stdout);
129 fatalerr ("got signal %d\n", sig);
130}
131
132#if defined(USG) || (defined(i386) && defined(SYSV)) || defined(WIN32) || defined(__UNIXOS2__) || defined(Lynx_22) || defined(__CYGWIN__)
133#define USGISH
134#endif
135
136#ifndef USGISH
137#ifdef X_NOT_POSIX
138#define sigaction sigvec
139#define sa_handler sv_handler
140#define sa_mask sv_mask
141#define sa_flags sv_flags
142#endif
143struct sigaction sig_act;
144#endif /* USGISH */
145
146int
147main(int argc, char *argv[])
148{
149 char **fp = filelist;
150 char **incp = includedirs;
151 char *p;
152 struct inclist *ip;
153 char *makefile = NULL;
154 struct filepointer *filecontent;
155 struct symtab *psymp = predefs;
156 char *endmarker = NULL;
157 char *defincdir = NULL;
158 char **undeflist = NULL;
159 int numundefs = 0, i;
160 register char offset; /* mozilla */
161
162 ProgramName = argv[0];
163
164 while (psymp->s_name)
165 {
166 define2(psymp->s_name, psymp->s_value, &maininclist);
167 psymp++;
168 }
169 if (argc == 2 && argv[1][0] == '@') {
170 struct stat ast;
171 int afd;
172 char *args;
173 char **nargv;
174 int nargc;
175 char quotechar = '\0';
176
177 nargc = 1;
178 if ((afd = open(argv[1]+1, O_RDONLY)) < 0)
179 fatalerr("cannot open \"%s\"\n", argv[1]+1);
180 fstat(afd, &ast);
181 args = (char *)malloc(ast.st_size + 1);
182 if ((ast.st_size = read(afd, args, ast.st_size)) < 0)
183 fatalerr("failed to read %s\n", argv[1]+1);
184 args[ast.st_size] = '\0';
185 close(afd);
186 for (p = args; *p; p++) {
187 if (quotechar) {
188 if (quotechar == '\\' ||
189 (*p == quotechar && p[-1] != '\\'))
190 quotechar = '\0';
191 continue;
192 }
193 switch (*p) {
194 case '\\':
195 case '"':
196 case '\'':
197 quotechar = *p;
198 break;
199 case ' ':
200 case '\n':
201 *p = '\0';
202 if (p > args && p[-1])
203 nargc++;
204 break;
205 }
206 }
207 if (p[-1])
208 nargc++;
209 nargv = (char **)malloc(nargc * sizeof(char *));
210 nargv[0] = argv[0];
211 argc = 1;
212 for (p = args; argc < nargc; p += strlen(p) + 1)
213 if (*p) nargv[argc++] = p;
214 argv = nargv;
215 }
216 for(argc--, argv++; argc; argc--, argv++) {
217 /* if looking for endmarker then check before parsing */
218 if (endmarker && strcmp (endmarker, *argv) == 0) {
219 endmarker = NULL;
220 continue;
221 }
222 if (**argv != '-') {
223 /* treat +thing as an option for C++ */
224 if (endmarker && **argv == '+')
225 continue;
226 *fp++ = argv[0];
227 continue;
228 }
229 switch(argv[0][1]) {
230 case '-':
231 endmarker = &argv[0][2];
232 if (endmarker[0] == '\0') endmarker = "--";
233 break;
234 case 'D':
235 offset = 2; /* mozilla */
236Imaginary Buffer Line
237 if (argv[0][2] == '\0') {
238 argv++;
239 argc--;
240 offset = 0;
241 }
242 /* offset +1 here since first def letter
243 * cannot be `=`
244 */
245 for (p = argv[0] + offset + 1; *p; p++) /* mozilla */
246 if (*p == '=') {
247 *p = ' ';
248 break;
249 }
250 define(argv[0] + offset, &maininclist); /* mozilla */
251 break;
252 case 'I':
253 if (incp >= includedirs + MAXDIRS)
254 fatalerr("Too many -I flags.\n");
255 *incp++ = argv[0]+2;
256 if (**(incp-1) == '\0') {
257 *(incp-1) = *(++argv);
258 argc--;
259 }
260 break;
261 case 'U':
262 /* Undef's override all -D's so save them up */
263 numundefs++;
264 if (numundefs == 1)
265 undeflist = malloc(sizeof(char *));
266 else
267 undeflist = realloc(undeflist,
268 numundefs * sizeof(char *));
269 offset = 2; /* mozilla */
270 if (argv[0][2] == '\0') {
271 argv++;
272 argc--;
273 offset = 0; /* mozilla */
274 }
275 undeflist[numundefs - 1] = argv[0] + offset; /* mozilla */
276 break;
277 case 'Y':
278 defincdir = argv[0]+2;
279 break;
280 /* do not use if endmarker processing */
281 case 'a':
282 if (endmarker) break;
283 append = TRUE;
284 break;
285 case 'w':
286 if (endmarker) break;
287 if (argv[0][2] == '\0') {
288 argv++;
289 argc--;
290 width = atoi(argv[0]);
291 } else
292 width = atoi(argv[0]+2);
293 break;
294 case 'o':
295 if (endmarker) break;
296 if (argv[0][2] == '\0') {
297 argv++;
298 argc--;
299 objsuffix = argv[0];
300 } else
301 objsuffix = argv[0]+2;
302 break;
303 case 'p':
304 if (endmarker) break;
305 if (argv[0][2] == '\0') {
306 argv++;
307 argc--;
308 objprefix = argv[0];
309 } else
310 objprefix = argv[0]+2;
311 break;
312 case 'v':
313 if (endmarker) break;
314 verbose = TRUE;
315#ifdef DEBUG
316 if (argv[0][2])
317 _debugmask = atoi(argv[0]+2);
318#endif
319 break;
320 case 's':
321 if (endmarker) break;
322 startat = argv[0]+2;
323 if (*startat == '\0') {
324 startat = *(++argv);
325 argc--;
326 }
327 if (*startat != '#')
328 fatalerr("-s flag's value should start %s\n",
329 "with '#'.");
330 break;
331 case 'f':
332 if (endmarker) break;
333 makefile = argv[0]+2;
334 if (*makefile == '\0') {
335 makefile = *(++argv);
336 argc--;
337 }
338 break;
339
340 case 'm':
341 warn_multiple = TRUE;
342 break;
343
344 /* Ignore -O, -g so we can just pass ${CFLAGS} to
345 makedepend
346 */
347 case 'O':
348 case 'g':
349 break;
350 case 'i':
351 if (strcmp(&argv[0][1],"include") == 0) {
352 char *buf;
353 if (argc<2)
354 fatalerr("option -include is a "
355 "missing its parameter\n");
356 if (cmdinc_count >= MAXINCFILES)
357 fatalerr("Too many -include flags.\n");
358 argc--;
359 argv++;
360 buf = malloc(strlen(DASH_INC_PRE) +
361 strlen(argv[0]) +
362 strlen(DASH_INC_POST) + 1);
363 if(!buf)
364 fatalerr("out of memory at "
365 "-include string\n");
366 cmdinc_list[2 * cmdinc_count + 0] = argv[0];
367 cmdinc_list[2 * cmdinc_count + 1] = buf;
368 cmdinc_count++;
369 break;
370 }
371 /* intentional fall through */
372 default:
373 if (endmarker) break;
374 /* fatalerr("unknown opt = %s\n", argv[0]); */
375 warning("ignoring option %s\n", argv[0]);
376 }
377 }
378 /* Now do the undefs from the command line */
379 for (i = 0; i < numundefs; i++)
380 undefine(undeflist[i], &maininclist);
381 if (numundefs > 0)
382 free(undeflist);
383
384 if (!defincdir) {
385#ifdef PREINCDIR
386 if (incp >= includedirs + MAXDIRS)
387 fatalerr("Too many -I flags.\n");
388 *incp++ = PREINCDIR;
389#endif
390#ifdef __UNIXOS2__
391 {
392 char *emxinc = getenv("C_INCLUDE_PATH");
393 /* can have more than one component */
394 if (emxinc) {
395 char *beg, *end;
396 beg= (char*)strdup(emxinc);
397 for (;;) {
398 end = (char*)strchr(beg,';');
399 if (end) *end = 0;
400 if (incp >= includedirs + MAXDIRS)
401 fatalerr("Too many include dirs\n");
402 *incp++ = beg;
403 if (!end) break;
404 beg = end+1;
405 }
406 }
407 }
408#else /* !__UNIXOS2__, does not use INCLUDEDIR at all */
409 if (incp >= includedirs + MAXDIRS)
410 fatalerr("Too many -I flags.\n");
411 *incp++ = INCLUDEDIR;
412#endif
413
414#ifdef EXTRAINCDIR
415 if (incp >= includedirs + MAXDIRS)
416 fatalerr("Too many -I flags.\n");
417 *incp++ = EXTRAINCDIR;
418#endif
419
420#ifdef POSTINCDIR
421 if (incp >= includedirs + MAXDIRS)
422 fatalerr("Too many -I flags.\n");
423 *incp++ = POSTINCDIR;
424#endif
425 } else if (*defincdir) {
426 if (incp >= includedirs + MAXDIRS)
427 fatalerr("Too many -I flags.\n");
428 *incp++ = defincdir;
429 }
430
431 redirect(startat, makefile);
432
433 /*
434 * catch signals.
435 */
436#ifdef USGISH
437/* should really reset SIGINT to SIG_IGN if it was. */
438#ifdef SIGHUP
439 signal (SIGHUP, catch);
440#endif
441 signal (SIGINT, catch);
442#ifdef SIGQUIT
443 signal (SIGQUIT, catch);
444#endif
445 signal (SIGILL, catch);
446#ifdef SIGBUS
447 signal (SIGBUS, catch);
448#endif
449 signal (SIGSEGV, catch);
450#ifdef SIGSYS
451 signal (SIGSYS, catch);
452#endif
453#else
454 sig_act.sa_handler = catch;
455#if defined(_POSIX_SOURCE) || !defined(X_NOT_POSIX)
456 sigemptyset(&sig_act.sa_mask);
457 sigaddset(&sig_act.sa_mask, SIGINT);
458 sigaddset(&sig_act.sa_mask, SIGQUIT);
459#ifdef SIGBUS
460 sigaddset(&sig_act.sa_mask, SIGBUS);
461#endif
462 sigaddset(&sig_act.sa_mask, SIGILL);
463 sigaddset(&sig_act.sa_mask, SIGSEGV);
464 sigaddset(&sig_act.sa_mask, SIGHUP);
465 sigaddset(&sig_act.sa_mask, SIGPIPE);
466#ifdef SIGSYS
467 sigaddset(&sig_act.sa_mask, SIGSYS);
468#endif
469#else
470 sig_act.sa_mask = ((1<<(SIGINT -1))
471 |(1<<(SIGQUIT-1))
472#ifdef SIGBUS
473 |(1<<(SIGBUS-1))
474#endif
475 |(1<<(SIGILL-1))
476 |(1<<(SIGSEGV-1))
477 |(1<<(SIGHUP-1))
478 |(1<<(SIGPIPE-1))
479#ifdef SIGSYS
480 |(1<<(SIGSYS-1))
481#endif
482 );
483#endif /* _POSIX_SOURCE */
484 sig_act.sa_flags = 0;
485 sigaction(SIGHUP, &sig_act, (struct sigaction *)0);
486 sigaction(SIGINT, &sig_act, (struct sigaction *)0);
487 sigaction(SIGQUIT, &sig_act, (struct sigaction *)0);
488 sigaction(SIGILL, &sig_act, (struct sigaction *)0);
489#ifdef SIGBUS
490 sigaction(SIGBUS, &sig_act, (struct sigaction *)0);
491#endif
492 sigaction(SIGSEGV, &sig_act, (struct sigaction *)0);
493#ifdef SIGSYS
494 sigaction(SIGSYS, &sig_act, (struct sigaction *)0);
495#endif
496#endif /* USGISH */
497
498 /*
499 * now peruse through the list of files.
500 */
501 for(fp=filelist; *fp; fp++) {
502 DBG_PRINT(stderr,"file: %s\n",*fp);
503 filecontent = getfile(*fp);
504 setfile_cmdinc(filecontent, cmdinc_count, cmdinc_list);
505 ip = newinclude(*fp, (char *)NULL);
506
507 find_includes(filecontent, ip, ip, 0, FALSE);
508 freefile(filecontent);
509 recursive_pr_include(ip, ip->i_file, base_name(*fp));
510 inc_clean();
511 }
512 if (printed)
513 printf("\n");
514 return 0;
515}
516
517#ifdef __UNIXOS2__
518/*
519 * eliminate \r chars from file
520 */
521static int
522elim_cr(char *buf, int sz)
523{
524 int i,wp;
525 for (i= wp = 0; i<sz; i++) {
526 if (buf[i] != '\r')
527 buf[wp++] = buf[i];
528 }
529 return wp;
530}
531#endif
532
533struct filepointer *
534getfile(char *file)
535{
536 int fd;
537 struct filepointer *content;
538 struct stat st;
539
540 content = (struct filepointer *)malloc(sizeof(struct filepointer));
541 content->f_name = file;
542 if ((fd = open(file, O_RDONLY)) < 0) {
543 warning("cannot open \"%s\"\n", file);
544 content->f_p = content->f_base = content->f_end = (char *)malloc(1);
545 *content->f_p = '\0';
546 return(content);
547 }
548 fstat(fd, &st);
549 content->f_base = (char *)malloc(st.st_size+1);
550 if (content->f_base == NULL)
551 fatalerr("cannot allocate mem\n");
552 if ((st.st_size = read(fd, content->f_base, st.st_size)) < 0)
553 fatalerr("failed to read %s\n", file);
554#ifdef __UNIXOS2__
555 st.st_size = elim_cr(content->f_base,st.st_size);
556#endif
557 close(fd);
558 content->f_len = st.st_size+1;
559 content->f_p = content->f_base;
560 content->f_end = content->f_base + st.st_size;
561 *content->f_end = '\0';
562 content->f_line = 0;
563 content->cmdinc_count = 0;
564 content->cmdinc_list = NULL;
565 content->cmdinc_line = 0;
566 return(content);
567}
568
569void
570setfile_cmdinc(struct filepointer* filep, long count, char** list)
571{
572 filep->cmdinc_count = count;
573 filep->cmdinc_list = list;
574 filep->cmdinc_line = 0;
575}
576
577void
578freefile(struct filepointer *fp)
579{
580 free(fp->f_base);
581 free(fp);
582}
583
584char *copy(char *str)
585{
586 char *p = (char *)malloc(strlen(str) + 1);
587
588 strcpy(p, str);
589 return(p);
590}
591
592int
593match(char *str, char **list)
594{
595 int i;
596
597 for (i=0; *list; i++, list++)
598 if (strcmp(str, *list) == 0)
599 return(i);
600 return(-1);
601}
602
603/*
604 * Get the next line. We only return lines beginning with '#' since that
605 * is all this program is ever interested in.
606 */
607char *getnextline(struct filepointer *filep)
608{
609 char *p, /* walking pointer */
610 *eof, /* end of file pointer */
611 *bol; /* beginning of line pointer */
612 int lineno; /* line number */
613 boolean whitespace = FALSE;
614
615 /*
616 * Fake the "-include" line files in form of #include to the
617 * start of each file.
618 */
619 if (filep->cmdinc_line < filep->cmdinc_count) {
620 char *inc = filep->cmdinc_list[2 * filep->cmdinc_line + 0];
621 char *buf = filep->cmdinc_list[2 * filep->cmdinc_line + 1];
622 filep->cmdinc_line++;
623 sprintf(buf,"%s%s%s",DASH_INC_PRE,inc,DASH_INC_POST);
624 DBG_PRINT(stderr,"%s\n",buf);
625 return(buf);
626 }
627
628 p = filep->f_p;
629 eof = filep->f_end;
630 if (p >= eof)
631 return((char *)NULL);
632 lineno = filep->f_line;
633
634 for (bol = p--; ++p < eof; ) {
635 if ((bol == p) && ((*p == ' ') || (*p == '\t')))
636 {
637 /* Consume leading white-spaces for this line */
638 while (((p+1) < eof) && ((*p == ' ') || (*p == '\t')))
639 {
640 p++;
641 bol++;
642 }
643 whitespace = TRUE;
644 }
645
646 if (*p == '/' && (p+1) < eof && *(p+1) == '*') {
647 /* Consume C comments */
648 *(p++) = ' ';
649 *(p++) = ' ';
650 while (p < eof && *p) {
651 if (*p == '*' && (p+1) < eof && *(p+1) == '/') {
652 *(p++) = ' ';
653 *(p++) = ' ';
654 break;
655 }
656 if (*p == '\n')
657 lineno++;
658 *(p++) = ' ';
659 }
660 --p;
661 }
662 else if (*p == '/' && (p+1) < eof && *(p+1) == '/') {
663 /* Consume C++ comments */
664 *(p++) = ' ';
665 *(p++) = ' ';
666 while (p < eof && *p) {
667 if (*p == '\\' && (p+1) < eof &&
668 *(p+1) == '\n') {
669 *(p++) = ' ';
670 lineno++;
671 }
672 else if (*p == '?' && (p+3) < eof &&
673 *(p+1) == '?' &&
674 *(p+2) == '/' &&
675 *(p+3) == '\n') {
676 *(p++) = ' ';
677 *(p++) = ' ';
678 *(p++) = ' ';
679 lineno++;
680 }
681 else if (*p == '\n')
682 break; /* to process end of line */
683 *(p++) = ' ';
684 }
685 --p;
686 }
687 else if (*p == '\\' && (p+1) < eof && *(p+1) == '\n') {
688 /* Consume backslash line terminations */
689 *(p++) = ' ';
690 *p = ' ';
691 lineno++;
692 }
693 else if (*p == '?' && (p+3) < eof &&
694 *(p+1) == '?' && *(p+2) == '/' && *(p+3) == '\n') {
695 /* Consume trigraph'ed backslash line terminations */
696 *(p++) = ' ';
697 *(p++) = ' ';
698 *(p++) = ' ';
699 *p = ' ';
700 lineno++;
701 }
702 else if (*p == '\n') {
703 lineno++;
704 if (*bol == '#') {
705 char *cp;
706
707 *(p++) = '\0';
708 /* punt lines with just # (yacc generated) */
709 for (cp = bol+1;
710 *cp && (*cp == ' ' || *cp == '\t'); cp++);
711 if (*cp) goto done;
712 --p;
713 }
714 bol = p+1;
715 whitespace = FALSE;
716 }
717 }
718 if (*bol != '#')
719 bol = NULL;
720done:
721 if (bol && whitespace) {
722 warning("%s: non-portable whitespace encountered at line %d\n",
723 filep->f_name, lineno);
724 }
725 filep->f_p = p;
726 filep->f_line = lineno;
727#ifdef DEBUG_DUMP
728 if (bol)
729 DBG_PRINT(stderr,"%s\n",bol);
730#endif
731 return(bol);
732}
733
734/*
735 * Strip the file name down to what we want to see in the Makefile.
736 * It will have objprefix and objsuffix around it.
737 */
738char *base_name(char *file)
739{
740 char *p;
741
742 file = copy(file);
743 for(p=file+strlen(file); p>file && *p != '.'; p--) ;
744
745 if (*p == '.')
746 *p = '\0';
747 return(file);
748}
749
750#if defined(USG) && !defined(CRAY) && !defined(SVR4) && !defined(__UNIXOS2__) && !defined(clipper) && !defined(__clipper__)
751int rename (char *from, char *to)
752{
753 (void) unlink (to);
754 if (link (from, to) == 0) {
755 unlink (from);
756 return 0;
757 } else {
758 return -1;
759 }
760}
761#endif /* USGISH */
762
763void
764redirect(char *line, char *makefile)
765{
766 struct stat st;
767 FILE *fdin, *fdout;
768 char backup[ BUFSIZ ],
769 buf[ BUFSIZ ];
770 boolean found = FALSE;
771 int len;
772
773 /*
774 * if makefile is "-" then let it pour onto stdout.
775 */
776 if (makefile && *makefile == '-' && *(makefile+1) == '\0') {
777 puts(line);
778 return;
779 }
780
781 /*
782 * use a default makefile is not specified.
783 */
784 if (!makefile) {
785 if (stat("Makefile", &st) == 0)
786 makefile = "Makefile";
787 else if (stat("makefile", &st) == 0)
788 makefile = "makefile";
789 else
790 fatalerr("[mM]akefile is not present\n");
791 }
792 else
793 stat(makefile, &st);
794 if ((fdin = fopen(makefile, "r")) == NULL)
795 fatalerr("cannot open \"%s\"\n", makefile);
796 sprintf(backup, "%s.bak", makefile);
797 unlink(backup);
798#if defined(WIN32) || defined(__UNIXOS2__) || defined(__CYGWIN__)
799 fclose(fdin);
800#endif
801 if (rename(makefile, backup) < 0)
802 fatalerr("cannot rename %s to %s\n", makefile, backup);
803#if defined(WIN32) || defined(__UNIXOS2__) || defined(__CYGWIN__)
804 if ((fdin = fopen(backup, "r")) == NULL)
805 fatalerr("cannot open \"%s\"\n", backup);
806#endif
807 if ((fdout = freopen(makefile, "w", stdout)) == NULL)
808 fatalerr("cannot open \"%s\"\n", backup);
809 len = strlen(line);
810 while (!found && fgets(buf, BUFSIZ, fdin)) {
811 if (*buf == '#' && strncmp(line, buf, len) == 0)
812 found = TRUE;
813 fputs(buf, fdout);
814 }
815 if (!found) {
816 if (verbose)
817 warning("Adding new delimiting line \"%s\" and dependencies...\n",
818 line);
819 puts(line); /* same as fputs(fdout); but with newline */
820 } else if (append) {
821 while (fgets(buf, BUFSIZ, fdin)) {
822 fputs(buf, fdout);
823 }
824 }
825 fflush(fdout);
826#if defined(USGISH) || defined(_SEQUENT_) || defined(USE_CHMOD)
827 chmod(makefile, st.st_mode);
828#else
829 fchmod(fileno(fdout), st.st_mode);
830#endif /* USGISH */
831}
832
833void
834fatalerr(char *msg, ...)
835{
836 va_list args;
837 fprintf(stderr, "%s: error: ", ProgramName);
838 va_start(args, msg);
839 vfprintf(stderr, msg, args);
840 va_end(args);
841 exit (1);
842}
843
844void
845warning(char *msg, ...)
846{
847 va_list args;
848 fprintf(stderr, "%s: warning: ", ProgramName);
849 va_start(args, msg);
850 vfprintf(stderr, msg, args);
851 va_end(args);
852}
853
854void
855warning1(char *msg, ...)
856{
857 va_list args;
858 va_start(args, msg);
859 vfprintf(stderr, msg, args);
860 va_end(args);
861}
Note: See TracBrowser for help on using the repository browser.