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