mkdep.c revision 1.6
1/* $NetBSD: mkdep.c,v 1.6 2001/03/22 00:16:50 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.6 2001/03/22 00:16:50 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
65static void	usage __P((void));
66static char    *findcc __P((const char *));
67int		main __P((int, char **));
68
69static void
70usage()
71{
72	(void)fprintf(stderr,
73	    "usage: %s [-a] [-p] [-f file] flags file ...\n",
74	    getprogname());
75	exit(EXIT_FAILURE);
76}
77
78static char *
79findcc(progname)
80	const char	*progname;
81{
82	char   *path, *dir, *next;
83	char   buffer[MAXPATHLEN];
84
85	if ((next = strchr(progname, ' ')) != NULL) {
86		*next = '\0';
87	}
88
89	if (strchr(progname, '/') != NULL)
90		return access(progname, X_OK) ? NULL : strdup(progname);
91
92	if (((path = getenv("PATH")) == NULL) ||
93	    ((path = strdup(path)) == NULL))
94		return NULL;
95
96	dir = path;
97	while (dir != NULL) {
98		if ((next = strchr(dir, ':')) != NULL)
99			*next++ = '\0';
100
101		if (snprintf(buffer, sizeof(buffer),
102			     "%s/%s", dir, progname) < sizeof(buffer)) {
103			if (!access(buffer, X_OK)) {
104				free(path);
105				return strdup(buffer);
106			}
107		}
108		dir = next;
109	}
110
111	free(path);
112	return NULL;
113}
114
115int
116main(argc, argv)
117	int     argc;
118	char  **argv;
119{
120	/* LINTED local definition of index */
121	int 	aflag, pflag, index, tmpfd, status;
122	pid_t	cpid, pid;
123	char   *filename, *CC, *pathname, tmpfilename[MAXPATHLEN], **args;
124	const char *tmpdir;
125	/* LINTED local definition of tmpfile */
126	FILE   *tmpfile, *dependfile;
127	char	buffer[32768];
128
129	setlocale(LC_ALL, "");
130	setprogname(argv[0]);
131
132	aflag = 0;
133	pflag = 0;
134	filename = DEFAULT_FILENAME;
135	for (index=1; index< argc; index++)
136		if (strcmp(argv[index], "-a") == 0)
137			aflag = 1;
138		else
139			if (strcmp(argv[index], "-f") == 0) {
140				if (++index < argc)
141					filename = argv[index];
142			}
143			else
144				if (strcmp(argv[index], "-p") == 0)
145					pflag = 1;
146				else
147					break;
148
149	argc -= index;
150	argv += index;
151	if (argc == 0)
152		usage();
153
154	if ((CC = getenv("CC")) == NULL)
155		CC = DEFAULT_CC;
156	if ((pathname = findcc(CC)) == NULL)
157		if (!setenv("PATH", DEFAULT_PATH, 1))
158			pathname = findcc(CC);
159	if (pathname == NULL) {
160		(void)fprintf(stderr, "%s: %s: not found\n", getprogname(), CC);
161		return EXIT_FAILURE;
162	}
163
164	if ((args = malloc((argc + 3) * sizeof(char *))) == NULL) {
165		perror(getprogname());
166		exit(EXIT_FAILURE);
167	}
168	args[0] = CC;
169	args[1] = "-M";
170	(void)memcpy(&args[2], argv, (argc + 1) * sizeof(char *));
171
172	if ((tmpdir = getenv("TMPDIR")) == NULL)
173		tmpdir = _PATH_TMP;
174	(void)snprintf(tmpfilename, sizeof (tmpfilename), "%s/%s", tmpdir,
175	    "mkdepXXXXXX");
176	if ((tmpfd = mkstemp (tmpfilename)) < 0) {
177		warn("unable to create temporary file %s", tmpfilename);
178		return EXIT_FAILURE;
179	}
180
181	switch (cpid = vfork()) {
182	case 0:
183	    (void)dup2(tmpfd, STDOUT_FILENO);
184	    (void)close(tmpfd);
185
186	    (void)execv(pathname, args);
187	    _exit(EXIT_FAILURE);
188	    /* NOTREACHED */
189
190	case -1:
191	    (void)fprintf(stderr, "%s: unable to fork.\n", getprogname());
192	    (void)close(tmpfd);
193	    (void)unlink(tmpfilename);
194	    return EXIT_FAILURE;
195	}
196
197	while (((pid = wait(&status)) != cpid) && (pid >= 0));
198
199	if (status) {
200	    (void)fprintf(stderr, "%s: compile failed.\n", getprogname());
201	    (void)close(tmpfd);
202	    (void)unlink(tmpfilename);
203	    return EXIT_FAILURE;
204	}
205
206	(void)lseek(tmpfd, (off_t)0, SEEK_SET);
207	if ((tmpfile = fdopen(tmpfd, "r")) == NULL) {
208	    (void)fprintf(stderr,
209			  "%s: unable to read temporary file %s\n",
210			  getprogname(), tmpfilename);
211	    (void)close(tmpfd);
212	    (void)unlink(tmpfilename);
213	    return EXIT_FAILURE;
214	}
215
216	if ((dependfile = fopen(filename, aflag ? "a" : "w")) == NULL) {
217		(void)fprintf(stderr, "%s: unable to %s to file %s\n",
218			      getprogname(), aflag ? "append" : "write",
219			      filename);
220		(void)fclose(tmpfile);
221		(void)unlink(tmpfilename);
222		return EXIT_FAILURE;
223	}
224
225	while (fgets(buffer, sizeof(buffer), tmpfile) != NULL) {
226		char   *ptr;
227
228		if (pflag && ((ptr = strstr(buffer, ".o")) != NULL)) {
229			char   *colon;
230
231			colon = ptr + 2;
232			while (isspace(*colon)) colon++;
233			if (*colon == ':')
234				(void)strcpy(ptr, colon);
235		}
236
237		ptr = buffer;
238		while (*ptr)
239			if (isspace(*ptr++))
240				if ((ptr[0] == '.') && (ptr[1] == '/'))
241					(void)strcpy(ptr, ptr + 2);
242
243		(void)fputs(buffer, dependfile);
244	}
245
246	(void)fclose(dependfile);
247	(void)fclose(tmpfile);
248	(void)unlink(tmpfilename);
249
250	return EXIT_SUCCESS;
251}
252