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