mkdep.c revision 1.1 1 /* $NetBSD: mkdep.c,v 1.1 1999/01/25 22:10:42 tron 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.1 1999/01/25 22:10:42 tron 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 (strchr(progname, '/') != NULL)
85 return access(progname, X_OK) ? NULL : strdup(progname);
86
87 if (((path = getenv("PATH")) == NULL) ||
88 ((path = strdup(path)) == NULL))
89 return NULL;
90
91 dir = path;
92 while (dir != NULL) {
93 if ((next = strchr(dir, ':')) != NULL)
94 *next++ = '\0';
95
96 if (snprintf(buffer, sizeof(buffer),
97 "%s/%s", dir, progname) < sizeof(buffer)) {
98 if (!access(buffer, X_OK)) {
99 free(path);
100 return strdup(buffer);
101 }
102 }
103 dir = next;
104 }
105
106 free(path);
107 return NULL;
108 }
109
110 int
111 main(argc, argv)
112 int argc;
113 char **argv;
114 {
115 int aflag, pflag, index, tmpfd, status;
116 pid_t cpid, pid;
117 char *filename, *CC, *pathname, tmpfilename[MAXPATHLEN], **args;
118 FILE *tmpfile, *dependfile;
119 char buffer[32768];
120
121 setlocale(LC_ALL, "");
122
123 aflag = 0;
124 pflag = 0;
125 filename = DEFAULT_FILENAME;
126 for (index=1; index< argc; index++)
127 if (strcmp(argv[index], "-a") == 0)
128 aflag = 1;
129 else
130 if (strcmp(argv[index], "-f") == 0) {
131 if (++index < argc)
132 filename = argv[index];
133 }
134 else
135 if (strcmp(argv[index], "-p") == 0)
136 pflag = 1;
137 else
138 break;
139
140 argc -= index;
141 argv += index;
142 if (argc == 0)
143 usage();
144
145 if ((CC = getenv("CC")) == NULL)
146 CC = DEFAULT_CC;
147 if ((pathname = findcc(CC)) == NULL)
148 if (!setenv("PATH", DEFAULT_PATH, 1))
149 pathname = findcc(CC);
150 if (pathname == NULL) {
151 (void)fprintf(stderr, "mkdep: %s: not found\n", CC);
152 return EXIT_FAILURE;
153 }
154
155 if ((args = malloc((argc + 3) * sizeof(char *))) == NULL) {
156 perror("mkdep");
157 exit(EXIT_FAILURE);
158 }
159 args[0] = CC;
160 args[1] = "-M";
161 (void)memcpy(&args[2], argv, (argc + 1) * sizeof(char *));
162
163 (void)strcpy(tmpfilename, _PATH_TMP "mkdepXXXXXX");
164 if ((tmpfd = mkstemp (tmpfilename)) < 0) {
165 warn("unable to create temporary file %s", tmpfilename);
166 return EXIT_FAILURE;
167 }
168
169 #ifdef __GNUC__ /* to shut up gcc warnings */
170 (void)&aflag;
171 (void)&pflag;
172 (void)&filename;
173 (void)&pathname;
174 #endif
175
176 switch (cpid = vfork()) {
177 case 0:
178 (void)dup2(tmpfd, STDOUT_FILENO);
179 (void)close(tmpfd);
180
181 (void)execv(pathname, args);
182 _exit(EXIT_FAILURE);
183
184 case -1:
185 (void)fputs("mkdep: unable to fork.\n", stderr);
186 (void)close(tmpfd);
187 (void)unlink(tmpfilename);
188 return EXIT_FAILURE;
189 }
190
191 while (((pid = wait(&status)) != cpid) && (pid >= 0));
192
193 if (status) {
194 (void)fputs("mkdep: compile failed.\n", stderr);
195 (void)close(tmpfd);
196 (void)unlink(tmpfilename);
197 return EXIT_FAILURE;
198 }
199
200 (void)lseek(tmpfd, 0, SEEK_SET);
201 if ((tmpfile = fdopen(tmpfd, "r")) == NULL) {
202 (void)fprintf(stderr,
203 "mkdep: unable to read temporary file %s\n",
204 tmpfilename);
205 (void)close(tmpfd);
206 (void)unlink(tmpfilename);
207 return EXIT_FAILURE;
208 }
209
210 if ((dependfile = fopen(filename, aflag ? "a" : "w")) == NULL) {
211 (void)fprintf(stderr,
212 "mkdep: unable to %s to file %s\n",
213 aflag ? "append" : "write", filename);
214 (void)fclose(tmpfile);
215 (void)unlink(tmpfilename);
216 return EXIT_FAILURE;
217 }
218
219 while (fgets(buffer, sizeof(buffer), tmpfile) != NULL) {
220 char *ptr;
221
222 if (pflag && ((ptr = strstr(buffer, ".o")) != NULL)) {
223 char *colon;
224
225 colon = ptr + 2;
226 while (isspace(*colon)) colon++;
227 if (*colon == ':')
228 (void)strcpy(ptr, colon);
229 }
230
231 ptr = buffer;
232 while (*ptr)
233 if (isspace(*ptr++))
234 if ((ptr[0] == '.') && (ptr[1] == '/'))
235 (void)strcpy(ptr, ptr + 2);
236
237 (void)fputs(buffer, dependfile);
238 }
239
240 (void)fclose(dependfile);
241 (void)fclose(tmpfile);
242 (void)unlink(tmpfilename);
243
244 return EXIT_SUCCESS;
245 }
246