Home | History | Annotate | Line # | Download | only in mkdep
mkdep.c revision 1.12
      1 /* $NetBSD: mkdep.c,v 1.12 2003/01/16 08:05:09 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.12 2003/01/16 08:05:09 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 
     70 int tmpfd;
     71 char tmpfilename[MAXPATHLEN];
     72 
     73 static void	finish __P((int));
     74 static void	usage __P((void));
     75 int		main __P((int, char **));
     76 
     77 static void
     78 usage()
     79 {
     80 	(void)fprintf(stderr,
     81 	    "usage: %s [-a] [-p] [-f file] flags file ...\n",
     82 	    getprogname());
     83 	exit(EXIT_FAILURE);
     84 }
     85 
     86 void
     87 finish(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 
     98 int
     99 main(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(SIGTERM, finish);
    165 	if ((tmpfd = mkstemp (tmpfilename)) < 0) {
    166 		warn("unable to create temporary file %s", tmpfilename);
    167 		exit(EXIT_FAILURE);
    168 	}
    169 
    170 	switch (cpid = vfork()) {
    171 	case 0:
    172 		(void)dup2(tmpfd, STDOUT_FILENO);
    173 		(void)close(tmpfd);
    174 
    175 		(void)execv(pathname, args);
    176 		_exit(EXIT_FAILURE);
    177 		/* NOTREACHED */
    178 
    179 	case -1:
    180 		(void)fprintf(stderr, "%s: unable to fork.\n", getprogname());
    181 		(void)close(tmpfd);
    182 		(void)unlink(tmpfilename);
    183 		exit(EXIT_FAILURE);
    184 	}
    185 
    186 	while (((pid = wait(&status)) != cpid) && (pid >= 0))
    187 		continue;
    188 
    189 	if (status) {
    190 		(void)fprintf(stderr, "%s: compile failed.\n", getprogname());
    191 		(void)close(tmpfd);
    192 		(void)unlink(tmpfilename);
    193 		exit(EXIT_FAILURE);
    194 	}
    195 
    196 	(void)lseek(tmpfd, (off_t)0, SEEK_SET);
    197 	if ((tmpfile = fdopen(tmpfd, "r")) == NULL) {
    198 		(void)fprintf(stderr, "%s: unable to read temporary file %s\n",
    199 		    getprogname(), tmpfilename);
    200 		(void)close(tmpfd);
    201 		(void)unlink(tmpfilename);
    202 		exit(EXIT_FAILURE);
    203 	}
    204 
    205 	if ((dependfile = fopen(filename, aflag ? "a" : "w")) == NULL) {
    206 		(void)fprintf(stderr, "%s: unable to %s to file %s\n",
    207 		    getprogname(), aflag ? "append" : "write", filename);
    208 		(void)fclose(tmpfile);
    209 		(void)unlink(tmpfilename);
    210 		exit(EXIT_FAILURE);
    211 	}
    212 
    213 	while (fgets(buffer, sizeof(buffer), tmpfile) != NULL) {
    214 		char   *ptr;
    215 
    216 		if (pflag && ((ptr = strstr(buffer, ".o")) != NULL)) {
    217 			char   *colon;
    218 
    219 			colon = ptr + 2;
    220 			while (isspace(*colon)) colon++;
    221 			if (*colon == ':')
    222 				(void)strcpy(ptr, colon);
    223 		}
    224 
    225 		ptr = buffer;
    226 		while (*ptr) {
    227 			if (isspace(*ptr++))
    228 				if ((ptr[0] == '.') && (ptr[1] == '/'))
    229 					(void)strcpy(ptr, ptr + 2);
    230 		}
    231 
    232 		(void)fputs(buffer, dependfile);
    233 	}
    234 
    235 	(void)fclose(dependfile);
    236 	(void)fclose(tmpfile);
    237 	(void)unlink(tmpfilename);
    238 
    239 	exit(EXIT_SUCCESS);
    240 }
    241