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