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