mkdep.c revision 1.11 1 /* $NetBSD: mkdep.c,v 1.11 2002/06/14 23:14:18 simonb Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthias Scheler.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #if defined(__COPYRIGHT) && !defined(lint)
41 __COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\n\
42 All rights reserved.\n");
43 #endif /* not lint */
44
45 #if defined(__RCSID) && !defined(lint)
46 __RCSID("$NetBSD: mkdep.c,v 1.11 2002/06/14 23:14:18 simonb Exp $");
47 #endif /* not lint */
48
49 #if HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include <sys/param.h>
54 #include <sys/wait.h>
55 #include <ctype.h>
56 #include <err.h>
57 #include <locale.h>
58 #include <paths.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 #include "findcc.h"
65
66 #define DEFAULT_PATH _PATH_DEFPATH
67 #define DEFAULT_FILENAME ".depend"
68
69 static void usage __P((void));
70 int main __P((int, char **));
71
72 static void
73 usage()
74 {
75 (void)fprintf(stderr,
76 "usage: %s [-a] [-p] [-f file] flags file ...\n",
77 getprogname());
78 exit(EXIT_FAILURE);
79 }
80
81 int
82 main(argc, argv)
83 int argc;
84 char **argv;
85 {
86 /* LINTED local definition of index */
87 int aflag, pflag, index, tmpfd, status;
88 pid_t cpid, pid;
89 char *filename, *CC, *pathname, tmpfilename[MAXPATHLEN], **args;
90 const char *tmpdir;
91 /* LINTED local definition of tmpfile */
92 FILE *tmpfile, *dependfile;
93 char buffer[32768];
94
95 setlocale(LC_ALL, "");
96 setprogname(argv[0]);
97
98 aflag = 0;
99 pflag = 0;
100 filename = DEFAULT_FILENAME;
101
102 /* XXX should use getopt(). */
103 for (index = 1; index < argc; index++) {
104 if (strcmp(argv[index], "-a") == 0)
105 aflag = 1;
106 else if (strcmp(argv[index], "-f") == 0) {
107 if (++index < argc)
108 filename = argv[index];
109 } else if (strcmp(argv[index], "-p") == 0)
110 pflag = 1;
111 else
112 break;
113 }
114
115 argc -= index;
116 argv += index;
117 if (argc == 0)
118 usage();
119
120 if ((CC = getenv("CC")) == NULL)
121 CC = DEFAULT_CC;
122 if ((pathname = findcc(CC)) == NULL)
123 if (!setenv("PATH", DEFAULT_PATH, 1))
124 pathname = findcc(CC);
125 if (pathname == NULL) {
126 (void)fprintf(stderr, "%s: %s: not found\n", getprogname(), CC);
127 exit(EXIT_FAILURE);
128 }
129
130 if ((args = malloc((argc + 3) * sizeof(char *))) == NULL) {
131 perror(getprogname());
132 exit(EXIT_FAILURE);
133 }
134 args[0] = CC;
135 args[1] = "-M";
136 (void)memcpy(&args[2], argv, (argc + 1) * sizeof(char *));
137
138 if ((tmpdir = getenv("TMPDIR")) == NULL)
139 tmpdir = _PATH_TMP;
140 (void)snprintf(tmpfilename, sizeof (tmpfilename), "%s/%s", tmpdir,
141 "mkdepXXXXXX");
142 if ((tmpfd = mkstemp (tmpfilename)) < 0) {
143 warn("unable to create temporary file %s", tmpfilename);
144 exit(EXIT_FAILURE);
145 }
146
147 switch (cpid = vfork()) {
148 case 0:
149 (void)dup2(tmpfd, STDOUT_FILENO);
150 (void)close(tmpfd);
151
152 (void)execv(pathname, args);
153 _exit(EXIT_FAILURE);
154 /* NOTREACHED */
155
156 case -1:
157 (void)fprintf(stderr, "%s: unable to fork.\n", getprogname());
158 (void)close(tmpfd);
159 (void)unlink(tmpfilename);
160 exit(EXIT_FAILURE);
161 }
162
163 while (((pid = wait(&status)) != cpid) && (pid >= 0))
164 continue;
165
166 if (status) {
167 (void)fprintf(stderr, "%s: compile failed.\n", getprogname());
168 (void)close(tmpfd);
169 (void)unlink(tmpfilename);
170 exit(EXIT_FAILURE);
171 }
172
173 (void)lseek(tmpfd, (off_t)0, SEEK_SET);
174 if ((tmpfile = fdopen(tmpfd, "r")) == NULL) {
175 (void)fprintf(stderr, "%s: unable to read temporary file %s\n",
176 getprogname(), tmpfilename);
177 (void)close(tmpfd);
178 (void)unlink(tmpfilename);
179 exit(EXIT_FAILURE);
180 }
181
182 if ((dependfile = fopen(filename, aflag ? "a" : "w")) == NULL) {
183 (void)fprintf(stderr, "%s: unable to %s to file %s\n",
184 getprogname(), aflag ? "append" : "write", filename);
185 (void)fclose(tmpfile);
186 (void)unlink(tmpfilename);
187 exit(EXIT_FAILURE);
188 }
189
190 while (fgets(buffer, sizeof(buffer), tmpfile) != NULL) {
191 char *ptr;
192
193 if (pflag && ((ptr = strstr(buffer, ".o")) != NULL)) {
194 char *colon;
195
196 colon = ptr + 2;
197 while (isspace(*colon)) colon++;
198 if (*colon == ':')
199 (void)strcpy(ptr, colon);
200 }
201
202 ptr = buffer;
203 while (*ptr) {
204 if (isspace(*ptr++))
205 if ((ptr[0] == '.') && (ptr[1] == '/'))
206 (void)strcpy(ptr, ptr + 2);
207 }
208
209 (void)fputs(buffer, dependfile);
210 }
211
212 (void)fclose(dependfile);
213 (void)fclose(tmpfile);
214 (void)unlink(tmpfilename);
215
216 exit(EXIT_SUCCESS);
217 }
218