mkdep.c revision 1.2 1 /* $NetBSD: mkdep.c,v 1.2 1999/03/18 22:01:48 sommerfe 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 #ifndef lint
41 __COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\n\
42 All rights reserved.\n");
43 #endif /* not lint */
44
45 #ifndef lint
46 __RCSID("$NetBSD: mkdep.c,v 1.2 1999/03/18 22:01:48 sommerfe Exp $");
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #include <sys/wait.h>
51
52 #include <ctype.h>
53 #include <err.h>
54 #include <locale.h>
55 #include <paths.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 #define DEFAULT_CC "cc"
62 #define DEFAULT_PATH _PATH_DEFPATH
63 #define DEFAULT_FILENAME ".depend"
64
65 void usage __P((void));
66 char *findcc __P((const char *));
67 int main __P((int, char **));
68
69 void
70 usage()
71 {
72 (void)fprintf(stderr,
73 "usage: mkdep [-a] [-p] [-f file ...] flags file ...\n");
74 exit(EXIT_FAILURE);
75 }
76
77 char *
78 findcc(progname)
79 const char *progname;
80 {
81 char *path, *dir, *next;
82 char buffer[MAXPATHLEN];
83
84 if ((next = strchr(progname, ' ')) != NULL) {
85 *next = '\0';
86 }
87
88 if (strchr(progname, '/') != NULL)
89 return access(progname, X_OK) ? NULL : strdup(progname);
90
91 if (((path = getenv("PATH")) == NULL) ||
92 ((path = strdup(path)) == NULL))
93 return NULL;
94
95 dir = path;
96 while (dir != NULL) {
97 if ((next = strchr(dir, ':')) != NULL)
98 *next++ = '\0';
99
100 if (snprintf(buffer, sizeof(buffer),
101 "%s/%s", dir, progname) < sizeof(buffer)) {
102 if (!access(buffer, X_OK)) {
103 free(path);
104 return strdup(buffer);
105 }
106 }
107 dir = next;
108 }
109
110 free(path);
111 return NULL;
112 }
113
114 int
115 main(argc, argv)
116 int argc;
117 char **argv;
118 {
119 int aflag, pflag, index, tmpfd, status;
120 pid_t cpid, pid;
121 char *filename, *CC, *pathname, tmpfilename[MAXPATHLEN], **args;
122 FILE *tmpfile, *dependfile;
123 char buffer[32768];
124
125 setlocale(LC_ALL, "");
126
127 aflag = 0;
128 pflag = 0;
129 filename = DEFAULT_FILENAME;
130 for (index=1; index< argc; index++)
131 if (strcmp(argv[index], "-a") == 0)
132 aflag = 1;
133 else
134 if (strcmp(argv[index], "-f") == 0) {
135 if (++index < argc)
136 filename = argv[index];
137 }
138 else
139 if (strcmp(argv[index], "-p") == 0)
140 pflag = 1;
141 else
142 break;
143
144 argc -= index;
145 argv += index;
146 if (argc == 0)
147 usage();
148
149 if ((CC = getenv("CC")) == NULL)
150 CC = DEFAULT_CC;
151 if ((pathname = findcc(CC)) == NULL)
152 if (!setenv("PATH", DEFAULT_PATH, 1))
153 pathname = findcc(CC);
154 if (pathname == NULL) {
155 (void)fprintf(stderr, "mkdep: %s: not found\n", CC);
156 return EXIT_FAILURE;
157 }
158
159 if ((args = malloc((argc + 3) * sizeof(char *))) == NULL) {
160 perror("mkdep");
161 exit(EXIT_FAILURE);
162 }
163 args[0] = CC;
164 args[1] = "-M";
165 (void)memcpy(&args[2], argv, (argc + 1) * sizeof(char *));
166
167 (void)strcpy(tmpfilename, _PATH_TMP "mkdepXXXXXX");
168 if ((tmpfd = mkstemp (tmpfilename)) < 0) {
169 warn("unable to create temporary file %s", tmpfilename);
170 return EXIT_FAILURE;
171 }
172
173 #ifdef __GNUC__ /* to shut up gcc warnings */
174 (void)&aflag;
175 (void)&pflag;
176 (void)&filename;
177 (void)&pathname;
178 #endif
179
180 switch (cpid = vfork()) {
181 case 0:
182 (void)dup2(tmpfd, STDOUT_FILENO);
183 (void)close(tmpfd);
184
185 (void)execv(pathname, args);
186 _exit(EXIT_FAILURE);
187
188 case -1:
189 (void)fputs("mkdep: unable to fork.\n", stderr);
190 (void)close(tmpfd);
191 (void)unlink(tmpfilename);
192 return EXIT_FAILURE;
193 }
194
195 while (((pid = wait(&status)) != cpid) && (pid >= 0));
196
197 if (status) {
198 (void)fputs("mkdep: compile failed.\n", stderr);
199 (void)close(tmpfd);
200 (void)unlink(tmpfilename);
201 return EXIT_FAILURE;
202 }
203
204 (void)lseek(tmpfd, 0, SEEK_SET);
205 if ((tmpfile = fdopen(tmpfd, "r")) == NULL) {
206 (void)fprintf(stderr,
207 "mkdep: unable to read temporary file %s\n",
208 tmpfilename);
209 (void)close(tmpfd);
210 (void)unlink(tmpfilename);
211 return EXIT_FAILURE;
212 }
213
214 if ((dependfile = fopen(filename, aflag ? "a" : "w")) == NULL) {
215 (void)fprintf(stderr,
216 "mkdep: unable to %s to file %s\n",
217 aflag ? "append" : "write", filename);
218 (void)fclose(tmpfile);
219 (void)unlink(tmpfilename);
220 return EXIT_FAILURE;
221 }
222
223 while (fgets(buffer, sizeof(buffer), tmpfile) != NULL) {
224 char *ptr;
225
226 if (pflag && ((ptr = strstr(buffer, ".o")) != NULL)) {
227 char *colon;
228
229 colon = ptr + 2;
230 while (isspace(*colon)) colon++;
231 if (*colon == ':')
232 (void)strcpy(ptr, colon);
233 }
234
235 ptr = buffer;
236 while (*ptr)
237 if (isspace(*ptr++))
238 if ((ptr[0] == '.') && (ptr[1] == '/'))
239 (void)strcpy(ptr, ptr + 2);
240
241 (void)fputs(buffer, dependfile);
242 }
243
244 (void)fclose(dependfile);
245 (void)fclose(tmpfile);
246 (void)unlink(tmpfilename);
247
248 return EXIT_SUCCESS;
249 }
250