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