mkdep.c revision 1.5 1 /* $NetBSD: mkdep.c,v 1.5 2001/02/21 00:08:37 cgd 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.5 2001/02/21 00:08:37 cgd 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 static void usage __P((void));
66 static char *findcc __P((const char *));
67 int main __P((int, char **));
68
69 static 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 static 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 /* LINTED local definition of index */
120 int aflag, pflag, index, tmpfd, status;
121 pid_t cpid, pid;
122 char *filename, *CC, *pathname, tmpfilename[MAXPATHLEN], **args;
123 const char *tmpdir;
124 /* LINTED local definition of tmpfile */
125 FILE *tmpfile, *dependfile;
126 char buffer[32768];
127
128 setlocale(LC_ALL, "");
129 setprogname(argv[0]);
130
131 aflag = 0;
132 pflag = 0;
133 filename = DEFAULT_FILENAME;
134 for (index=1; index< argc; index++)
135 if (strcmp(argv[index], "-a") == 0)
136 aflag = 1;
137 else
138 if (strcmp(argv[index], "-f") == 0) {
139 if (++index < argc)
140 filename = argv[index];
141 }
142 else
143 if (strcmp(argv[index], "-p") == 0)
144 pflag = 1;
145 else
146 break;
147
148 argc -= index;
149 argv += index;
150 if (argc == 0)
151 usage();
152
153 if ((CC = getenv("CC")) == NULL)
154 CC = DEFAULT_CC;
155 if ((pathname = findcc(CC)) == NULL)
156 if (!setenv("PATH", DEFAULT_PATH, 1))
157 pathname = findcc(CC);
158 if (pathname == NULL) {
159 (void)fprintf(stderr, "mkdep: %s: not found\n", CC);
160 return EXIT_FAILURE;
161 }
162
163 if ((args = malloc((argc + 3) * sizeof(char *))) == NULL) {
164 perror("mkdep");
165 exit(EXIT_FAILURE);
166 }
167 args[0] = CC;
168 args[1] = "-M";
169 (void)memcpy(&args[2], argv, (argc + 1) * sizeof(char *));
170
171 if ((tmpdir = getenv("TMPDIR")) == NULL)
172 tmpdir = _PATH_TMP;
173 (void)snprintf(tmpfilename, sizeof (tmpfilename), "%s/%s", tmpdir,
174 "mkdepXXXXXX");
175 if ((tmpfd = mkstemp (tmpfilename)) < 0) {
176 warn("unable to create temporary file %s", tmpfilename);
177 return EXIT_FAILURE;
178 }
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 /* NOTREACHED */
188
189 case -1:
190 (void)fputs("mkdep: unable to fork.\n", stderr);
191 (void)close(tmpfd);
192 (void)unlink(tmpfilename);
193 return EXIT_FAILURE;
194 }
195
196 while (((pid = wait(&status)) != cpid) && (pid >= 0));
197
198 if (status) {
199 (void)fputs("mkdep: compile failed.\n", stderr);
200 (void)close(tmpfd);
201 (void)unlink(tmpfilename);
202 return EXIT_FAILURE;
203 }
204
205 (void)lseek(tmpfd, (off_t)0, SEEK_SET);
206 if ((tmpfile = fdopen(tmpfd, "r")) == NULL) {
207 (void)fprintf(stderr,
208 "mkdep: unable to read temporary file %s\n",
209 tmpfilename);
210 (void)close(tmpfd);
211 (void)unlink(tmpfilename);
212 return EXIT_FAILURE;
213 }
214
215 if ((dependfile = fopen(filename, aflag ? "a" : "w")) == NULL) {
216 (void)fprintf(stderr,
217 "mkdep: unable to %s to file %s\n",
218 aflag ? "append" : "write", filename);
219 (void)fclose(tmpfile);
220 (void)unlink(tmpfilename);
221 return EXIT_FAILURE;
222 }
223
224 while (fgets(buffer, sizeof(buffer), tmpfile) != NULL) {
225 char *ptr;
226
227 if (pflag && ((ptr = strstr(buffer, ".o")) != NULL)) {
228 char *colon;
229
230 colon = ptr + 2;
231 while (isspace(*colon)) colon++;
232 if (*colon == ':')
233 (void)strcpy(ptr, colon);
234 }
235
236 ptr = buffer;
237 while (*ptr)
238 if (isspace(*ptr++))
239 if ((ptr[0] == '.') && (ptr[1] == '/'))
240 (void)strcpy(ptr, ptr + 2);
241
242 (void)fputs(buffer, dependfile);
243 }
244
245 (void)fclose(dependfile);
246 (void)fclose(tmpfile);
247 (void)unlink(tmpfilename);
248
249 return EXIT_SUCCESS;
250 }
251