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

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

Initial revision

  • Property svn:eol-style set to native
File size: 18.6 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
161 ProgramName = argv[0];
162
163 while (psymp->s_name)
164 {
165 define2(psymp->s_name, psymp->s_value, &maininclist);
166 psymp++;
167 }
168 if (argc == 2 && argv[1][0] == '@') {
169 struct stat ast;
170 int afd;
171 char *args;
172 char **nargv;
173 int nargc;
174 char quotechar = '\0';
175
176 nargc = 1;
177 if ((afd = open(argv[1]+1, O_RDONLY)) < 0)
178 fatalerr("cannot open \"%s\"\n", argv[1]+1);
179 fstat(afd, &ast);
180 args = (char *)malloc(ast.st_size + 1);
181 if ((ast.st_size = read(afd, args, ast.st_size)) < 0)
182 fatalerr("failed to read %s\n", argv[1]+1);
183 args[ast.st_size] = '\0';
184 close(afd);
185 for (p = args; *p; p++) {
186 if (quotechar) {
187 if (quotechar == '\\' ||
188 (*p == quotechar && p[-1] != '\\'))
189 quotechar = '\0';
190 continue;
191 }
192 switch (*p) {
193 case '\\':
194 case '"':
195 case '\'':
196 quotechar = *p;
197 break;
198 case ' ':
199 case '\n':
200 *p = '\0';
201 if (p > args && p[-1])
202 nargc++;
203 break;
204 }
205 }
206 if (p[-1])
207 nargc++;
208 nargv = (char **)malloc(nargc * sizeof(char *));
209 nargv[0] = argv[0];
210 argc = 1;
211 for (p = args; argc < nargc; p += strlen(p) + 1)
212 if (*p) nargv[argc++] = p;
213 argv = nargv;
214 }
215 for(argc--, argv++; argc; argc--, argv++) {
216 /* if looking for endmarker then check before parsing */
217 if (endmarker && strcmp (endmarker, *argv) == 0) {
218 endmarker = NULL;
219 continue;
220 }
221 if (**argv != '-') {
222 /* treat +thing as an option for C++ */
223 if (endmarker && **argv == '+')
224 continue;
225 *fp++ = argv[0];
226 continue;
227 }
228 switch(argv[0][1]) {
229 case '-':
230 endmarker = &argv[0][2];
231 if (endmarker[0] == '\0') endmarker = "--";
232 break;
233 case 'D':
234 if (argv[0][2] == '\0') {
235 argv++;
236 argc--;
237 }
238 for (p=argv[0] + 2; *p ; p++)
239 if (*p == '=') {
240 *p = ' ';
241 break;
242 }
243 define(argv[0] + 2, &maininclist);
244 break;
245 case 'I':
246 if (incp >= includedirs + MAXDIRS)
247 fatalerr("Too many -I flags.\n");
248 *incp++ = argv[0]+2;
249 if (**(incp-1) == '\0') {
250 *(incp-1) = *(++argv);
251 argc--;
252 }
253 break;
254 case 'U':
255 /* Undef's override all -D's so save them up */
256 numundefs++;
257 if (numundefs == 1)
258 undeflist = malloc(sizeof(char *));
259 else
260 undeflist = realloc(undeflist,
261 numundefs * sizeof(char *));
262 if (argv[0][2] == '\0') {
263 argv++;
264 argc--;
265 }
266 undeflist[numundefs - 1] = argv[0] + 2;
267 break;
268 case 'Y':
269 defincdir = argv[0]+2;
270 break;
271 /* do not use if endmarker processing */
272 case 'a':
273 if (endmarker) break;
274 append = TRUE;
275 break;
276 case 'w':
277 if (endmarker) break;
278 if (argv[0][2] == '\0') {
279 argv++;
280 argc--;
281 width = atoi(argv[0]);
282 } else
283 width = atoi(argv[0]+2);
284 break;
285 case 'o':
286 if (endmarker) break;
287 if (argv[0][2] == '\0') {
288 argv++;
289 argc--;
290 objsuffix = argv[0];
291 } else
292 objsuffix = argv[0]+2;
293 break;
294 case 'p':
295 if (endmarker) break;
296 if (argv[0][2] == '\0') {
297 argv++;
298 argc--;
299 objprefix = argv[0];
300 } else
301 objprefix = argv[0]+2;
302 break;
303 case 'v':
304 if (endmarker) break;
305 verbose = TRUE;
306#ifdef DEBUG
307 if (argv[0][2])
308 _debugmask = atoi(argv[0]+2);
309#endif
310 break;
311 case 's':
312 if (endmarker) break;
313 startat = argv[0]+2;
314 if (*startat == '\0') {
315 startat = *(++argv);
316 argc--;
317 }
318 if (*startat != '#')
319 fatalerr("-s flag's value should start %s\n",
320 "with '#'.");
321 break;
322 case 'f':
323 if (endmarker) break;
324 makefile = argv[0]+2;
325 if (*makefile == '\0') {
326 makefile = *(++argv);
327 argc--;
328 }
329 break;
330
331 case 'm':
332 warn_multiple = TRUE;
333 break;
334
335 /* Ignore -O, -g so we can just pass ${CFLAGS} to
336 makedepend
337 */
338 case 'O':
339 case 'g':
340 break;
341 case 'i':
342 if (strcmp(&argv[0][1],"include") == 0) {
343 char *buf;
344 if (argc<2)
345 fatalerr("option -include is a "
346 "missing its parameter\n");
347 if (cmdinc_count >= MAXINCFILES)
348 fatalerr("Too many -include flags.\n");
349 argc--;
350 argv++;
351 buf = malloc(strlen(DASH_INC_PRE) +
352 strlen(argv[0]) +
353 strlen(DASH_INC_POST) + 1);
354 if(!buf)
355 fatalerr("out of memory at "
356 "-include string\n");
357 cmdinc_list[2 * cmdinc_count + 0] = argv[0];
358 cmdinc_list[2 * cmdinc_count + 1] = buf;
359 cmdinc_count++;
360 break;
361 }
362 /* intentional fall through */
363 default:
364 if (endmarker) break;
365 /* fatalerr("unknown opt = %s\n", argv[0]); */
366 warning("ignoring option %s\n", argv[0]);
367 }
368 }
369 /* Now do the undefs from the command line */
370 for (i = 0; i < numundefs; i++)
371 undefine(undeflist[i], &maininclist);
372 if (numundefs > 0)
373 free(undeflist);
374
375 if (!defincdir) {
376#ifdef PREINCDIR
377 if (incp >= includedirs + MAXDIRS)
378 fatalerr("Too many -I flags.\n");
379 *incp++ = PREINCDIR;
380#endif
381#ifdef __UNIXOS2__
382 {
383 char *emxinc = getenv("C_INCLUDE_PATH");
384 /* can have more than one component */
385 if (emxinc) {
386 char *beg, *end;
387 beg= (char*)strdup(emxinc);
388 for (;;) {
389 end = (char*)strchr(beg,';');
390 if (end) *end = 0;
391 if (incp >= includedirs + MAXDIRS)
392 fatalerr("Too many include dirs\n");
393 *incp++ = beg;
394 if (!end) break;
395 beg = end+1;
396 }
397 }
398 }
399#else /* !__UNIXOS2__, does not use INCLUDEDIR at all */
400 if (incp >= includedirs + MAXDIRS)
401 fatalerr("Too many -I flags.\n");
402 *incp++ = INCLUDEDIR;
403#endif
404
405#ifdef EXTRAINCDIR
406 if (incp >= includedirs + MAXDIRS)
407 fatalerr("Too many -I flags.\n");
408 *incp++ = EXTRAINCDIR;
409#endif
410
411#ifdef POSTINCDIR
412 if (incp >= includedirs + MAXDIRS)
413 fatalerr("Too many -I flags.\n");
414 *incp++ = POSTINCDIR;
415#endif
416 } else if (*defincdir) {
417 if (incp >= includedirs + MAXDIRS)
418 fatalerr("Too many -I flags.\n");
419 *incp++ = defincdir;
420 }
421
422 redirect(startat, makefile);
423
424 /*
425 * catch signals.
426 */
427#ifdef USGISH
428/* should really reset SIGINT to SIG_IGN if it was. */
429#ifdef SIGHUP
430 signal (SIGHUP, catch);
431#endif
432 signal (SIGINT, catch);
433#ifdef SIGQUIT
434 signal (SIGQUIT, catch);
435#endif
436 signal (SIGILL, catch);
437#ifdef SIGBUS
438 signal (SIGBUS, catch);
439#endif
440 signal (SIGSEGV, catch);
441#ifdef SIGSYS
442 signal (SIGSYS, catch);
443#endif
444#else
445 sig_act.sa_handler = catch;
446#if defined(_POSIX_SOURCE) || !defined(X_NOT_POSIX)
447 sigemptyset(&sig_act.sa_mask);
448 sigaddset(&sig_act.sa_mask, SIGINT);
449 sigaddset(&sig_act.sa_mask, SIGQUIT);
450#ifdef SIGBUS
451 sigaddset(&sig_act.sa_mask, SIGBUS);
452#endif
453 sigaddset(&sig_act.sa_mask, SIGILL);
454 sigaddset(&sig_act.sa_mask, SIGSEGV);
455 sigaddset(&sig_act.sa_mask, SIGHUP);
456 sigaddset(&sig_act.sa_mask, SIGPIPE);
457#ifdef SIGSYS
458 sigaddset(&sig_act.sa_mask, SIGSYS);
459#endif
460#else
461 sig_act.sa_mask = ((1<<(SIGINT -1))
462 |(1<<(SIGQUIT-1))
463#ifdef SIGBUS
464 |(1<<(SIGBUS-1))
465#endif
466 |(1<<(SIGILL-1))
467 |(1<<(SIGSEGV-1))
468 |(1<<(SIGHUP-1))
469 |(1<<(SIGPIPE-1))
470#ifdef SIGSYS
471 |(1<<(SIGSYS-1))
472#endif
473 );
474#endif /* _POSIX_SOURCE */
475 sig_act.sa_flags = 0;
476 sigaction(SIGHUP, &sig_act, (struct sigaction *)0);
477 sigaction(SIGINT, &sig_act, (struct sigaction *)0);
478 sigaction(SIGQUIT, &sig_act, (struct sigaction *)0);
479 sigaction(SIGILL, &sig_act, (struct sigaction *)0);
480#ifdef SIGBUS
481 sigaction(SIGBUS, &sig_act, (struct sigaction *)0);
482#endif
483 sigaction(SIGSEGV, &sig_act, (struct sigaction *)0);
484#ifdef SIGSYS
485 sigaction(SIGSYS, &sig_act, (struct sigaction *)0);
486#endif
487#endif /* USGISH */
488
489 /*
490 * now peruse through the list of files.
491 */
492 for(fp=filelist; *fp; fp++) {
493 DBG_PRINT(stderr,"file: %s\n",*fp);
494 filecontent = getfile(*fp);
495 setfile_cmdinc(filecontent, cmdinc_count, cmdinc_list);
496 ip = newinclude(*fp, (char *)NULL);
497
498 find_includes(filecontent, ip, ip, 0, FALSE);
499 freefile(filecontent);
500 recursive_pr_include(ip, ip->i_file, base_name(*fp));
501 inc_clean();
502 }
503 if (printed)
504 printf("\n");
505 return 0;
506}
507
508#ifdef __UNIXOS2__
509/*
510 * eliminate \r chars from file
511 */
512static int
513elim_cr(char *buf, int sz)
514{
515 int i,wp;
516 for (i= wp = 0; i<sz; i++) {
517 if (buf[i] != '\r')
518 buf[wp++] = buf[i];
519 }
520 return wp;
521}
522#endif
523
524struct filepointer *
525getfile(char *file)
526{
527 int fd;
528 struct filepointer *content;
529 struct stat st;
530
531 content = (struct filepointer *)malloc(sizeof(struct filepointer));
532 content->f_name = file;
533 if ((fd = open(file, O_RDONLY)) < 0) {
534 warning("cannot open \"%s\"\n", file);
535 content->f_p = content->f_base = content->f_end = (char *)malloc(1);
536 *content->f_p = '\0';
537 return(content);
538 }
539 fstat(fd, &st);
540 content->f_base = (char *)malloc(st.st_size+1);
541 if (content->f_base == NULL)
542 fatalerr("cannot allocate mem\n");
543 if ((st.st_size = read(fd, content->f_base, st.st_size)) < 0)
544 fatalerr("failed to read %s\n", file);
545#ifdef __UNIXOS2__
546 st.st_size = elim_cr(content->f_base,st.st_size);
547#endif
548 close(fd);
549 content->f_len = st.st_size+1;
550 content->f_p = content->f_base;
551 content->f_end = content->f_base + st.st_size;
552 *content->f_end = '\0';
553 content->f_line = 0;
554 content->cmdinc_count = 0;
555 content->cmdinc_list = NULL;
556 content->cmdinc_line = 0;
557 return(content);
558}
559
560void
561setfile_cmdinc(struct filepointer* filep, long count, char** list)
562{
563 filep->cmdinc_count = count;
564 filep->cmdinc_list = list;
565 filep->cmdinc_line = 0;
566}
567
568void
569freefile(struct filepointer *fp)
570{
571 free(fp->f_base);
572 free(fp);
573}
574
575char *copy(char *str)
576{
577 char *p = (char *)malloc(strlen(str) + 1);
578
579 strcpy(p, str);
580 return(p);
581}
582
583int
584match(char *str, char **list)
585{
586 int i;
587
588 for (i=0; *list; i++, list++)
589 if (strcmp(str, *list) == 0)
590 return(i);
591 return(-1);
592}
593
594/*
595 * Get the next line. We only return lines beginning with '#' since that
596 * is all this program is ever interested in.
597 */
598char *getnextline(struct filepointer *filep)
599{
600 char *p, /* walking pointer */
601 *eof, /* end of file pointer */
602 *bol; /* beginning of line pointer */
603 int lineno; /* line number */
604 boolean whitespace = FALSE;
605
606 /*
607 * Fake the "-include" line files in form of #include to the
608 * start of each file.
609 */
610 if (filep->cmdinc_line < filep->cmdinc_count) {
611 char *inc = filep->cmdinc_list[2 * filep->cmdinc_line + 0];
612 char *buf = filep->cmdinc_list[2 * filep->cmdinc_line + 1];
613 filep->cmdinc_line++;
614 sprintf(buf,"%s%s%s",DASH_INC_PRE,inc,DASH_INC_POST);
615 DBG_PRINT(stderr,"%s\n",buf);
616 return(buf);
617 }
618
619 p = filep->f_p;
620 eof = filep->f_end;
621 if (p >= eof)
622 return((char *)NULL);
623 lineno = filep->f_line;
624
625 for (bol = p--; ++p < eof; ) {
626 if ((bol == p) && ((*p == ' ') || (*p == '\t')))
627 {
628 /* Consume leading white-spaces for this line */
629 while (((p+1) < eof) && ((*p == ' ') || (*p == '\t')))
630 {
631 p++;
632 bol++;
633 }
634 whitespace = TRUE;
635 }
636
637 if (*p == '/' && (p+1) < eof && *(p+1) == '*') {
638 /* Consume C comments */
639 *(p++) = ' ';
640 *(p++) = ' ';
641 while (p < eof && *p) {
642 if (*p == '*' && (p+1) < eof && *(p+1) == '/') {
643 *(p++) = ' ';
644 *(p++) = ' ';
645 break;
646 }
647 if (*p == '\n')
648 lineno++;
649 *(p++) = ' ';
650 }
651 --p;
652 }
653 else if (*p == '/' && (p+1) < eof && *(p+1) == '/') {
654 /* Consume C++ comments */
655 *(p++) = ' ';
656 *(p++) = ' ';
657 while (p < eof && *p) {
658 if (*p == '\\' && (p+1) < eof &&
659 *(p+1) == '\n') {
660 *(p++) = ' ';
661 lineno++;
662 }
663 else if (*p == '?' && (p+3) < eof &&
664 *(p+1) == '?' &&
665 *(p+2) == '/' &&
666 *(p+3) == '\n') {
667 *(p++) = ' ';
668 *(p++) = ' ';
669 *(p++) = ' ';
670 lineno++;
671 }
672 else if (*p == '\n')
673 break; /* to process end of line */
674 *(p++) = ' ';
675 }
676 --p;
677 }
678 else if (*p == '\\' && (p+1) < eof && *(p+1) == '\n') {
679 /* Consume backslash line terminations */
680 *(p++) = ' ';
681 *p = ' ';
682 lineno++;
683 }
684 else if (*p == '?' && (p+3) < eof &&
685 *(p+1) == '?' && *(p+2) == '/' && *(p+3) == '\n') {
686 /* Consume trigraph'ed backslash line terminations */
687 *(p++) = ' ';
688 *(p++) = ' ';
689 *(p++) = ' ';
690 *p = ' ';
691 lineno++;
692 }
693 else if (*p == '\n') {
694 lineno++;
695 if (*bol == '#') {
696 char *cp;
697
698 *(p++) = '\0';
699 /* punt lines with just # (yacc generated) */
700 for (cp = bol+1;
701 *cp && (*cp == ' ' || *cp == '\t'); cp++);
702 if (*cp) goto done;
703 --p;
704 }
705 bol = p+1;
706 whitespace = FALSE;
707 }
708 }
709 if (*bol != '#')
710 bol = NULL;
711done:
712 if (bol && whitespace) {
713 warning("%s: non-portable whitespace encountered at line %d\n",
714 filep->f_name, lineno);
715 }
716 filep->f_p = p;
717 filep->f_line = lineno;
718#ifdef DEBUG_DUMP
719 if (bol)
720 DBG_PRINT(stderr,"%s\n",bol);
721#endif
722 return(bol);
723}
724
725/*
726 * Strip the file name down to what we want to see in the Makefile.
727 * It will have objprefix and objsuffix around it.
728 */
729char *base_name(char *file)
730{
731 char *p;
732
733 file = copy(file);
734 for(p=file+strlen(file); p>file && *p != '.'; p--) ;
735
736 if (*p == '.')
737 *p = '\0';
738 return(file);
739}
740
741#if defined(USG) && !defined(CRAY) && !defined(SVR4) && !defined(__UNIXOS2__) && !defined(clipper) && !defined(__clipper__)
742int rename (char *from, char *to)
743{
744 (void) unlink (to);
745 if (link (from, to) == 0) {
746 unlink (from);
747 return 0;
748 } else {
749 return -1;
750 }
751}
752#endif /* USGISH */
753
754void
755redirect(char *line, char *makefile)
756{
757 struct stat st;
758 FILE *fdin, *fdout;
759 char backup[ BUFSIZ ],
760 buf[ BUFSIZ ];
761 boolean found = FALSE;
762 int len;
763
764 /*
765 * if makefile is "-" then let it pour onto stdout.
766 */
767 if (makefile && *makefile == '-' && *(makefile+1) == '\0') {
768 puts(line);
769 return;
770 }
771
772 /*
773 * use a default makefile is not specified.
774 */
775 if (!makefile) {
776 if (stat("Makefile", &st) == 0)
777 makefile = "Makefile";
778 else if (stat("makefile", &st) == 0)
779 makefile = "makefile";
780 else
781 fatalerr("[mM]akefile is not present\n");
782 }
783 else
784 stat(makefile, &st);
785 if ((fdin = fopen(makefile, "r")) == NULL)
786 fatalerr("cannot open \"%s\"\n", makefile);
787 sprintf(backup, "%s.bak", makefile);
788 unlink(backup);
789#if defined(WIN32) || defined(__UNIXOS2__) || defined(__CYGWIN__)
790 fclose(fdin);
791#endif
792 if (rename(makefile, backup) < 0)
793 fatalerr("cannot rename %s to %s\n", makefile, backup);
794#if defined(WIN32) || defined(__UNIXOS2__) || defined(__CYGWIN__)
795 if ((fdin = fopen(backup, "r")) == NULL)
796 fatalerr("cannot open \"%s\"\n", backup);
797#endif
798 if ((fdout = freopen(makefile, "w", stdout)) == NULL)
799 fatalerr("cannot open \"%s\"\n", backup);
800 len = strlen(line);
801 while (!found && fgets(buf, BUFSIZ, fdin)) {
802 if (*buf == '#' && strncmp(line, buf, len) == 0)
803 found = TRUE;
804 fputs(buf, fdout);
805 }
806 if (!found) {
807 if (verbose)
808 warning("Adding new delimiting line \"%s\" and dependencies...\n",
809 line);
810 puts(line); /* same as fputs(fdout); but with newline */
811 } else if (append) {
812 while (fgets(buf, BUFSIZ, fdin)) {
813 fputs(buf, fdout);
814 }
815 }
816 fflush(fdout);
817#if defined(USGISH) || defined(_SEQUENT_) || defined(USE_CHMOD)
818 chmod(makefile, st.st_mode);
819#else
820 fchmod(fileno(fdout), st.st_mode);
821#endif /* USGISH */
822}
823
824void
825fatalerr(char *msg, ...)
826{
827 va_list args;
828 fprintf(stderr, "%s: error: ", ProgramName);
829 va_start(args, msg);
830 vfprintf(stderr, msg, args);
831 va_end(args);
832 exit (1);
833}
834
835void
836warning(char *msg, ...)
837{
838 va_list args;
839 fprintf(stderr, "%s: warning: ", ProgramName);
840 va_start(args, msg);
841 vfprintf(stderr, msg, args);
842 va_end(args);
843}
844
845void
846warning1(char *msg, ...)
847{
848 va_list args;
849 va_start(args, msg);
850 vfprintf(stderr, msg, args);
851 va_end(args);
852}
Note: See TracBrowser for help on using the repository browser.