NetBSD/usr.bin/mkdep/mkdep.c

242 lines
6.3 KiB
C
Raw Normal View History

2003-03-28 10:38:15 +03:00
/* $NetBSD: mkdep.c,v 1.16 2003/03/28 07:38:15 msaitoh Exp $ */
/*-
* Copyright (c) 1999 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Matthias Scheler.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2001-03-22 05:41:02 +03:00
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(__COPYRIGHT) && !defined(lint)
__COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\n\
All rights reserved.\n");
#endif /* not lint */
#if defined(__RCSID) && !defined(lint)
2003-03-28 10:38:15 +03:00
__RCSID("$NetBSD: mkdep.c,v 1.16 2003/03/28 07:38:15 msaitoh Exp $");
#endif /* not lint */
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/param.h>
#include <sys/wait.h>
#include <ctype.h>
#include <err.h>
#include <locale.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "findcc.h"
#define DEFAULT_PATH _PATH_DEFPATH
#define DEFAULT_FILENAME ".depend"
int tmpfd;
char tmpfilename[MAXPATHLEN];
static void finish __P((int));
static void usage __P((void));
int main __P((int, char **));
static void
usage()
{
(void)fprintf(stderr,
"usage: %s [-a] [-p] [-f file] flags file ...\n",
getprogname());
exit(EXIT_FAILURE);
}
void
finish(signo)
int signo;
{
if (tmpfd != -1) {
(void)close(tmpfd);
(void)unlink(tmpfilename);
}
exit(EXIT_FAILURE);
}
int
main(argc, argv)
int argc;
char **argv;
{
2003-01-19 03:42:45 +03:00
/* LINTED local definition of index */
int aflag, pflag, index, status;
pid_t cpid, pid;
char *filename, *CC, *pathname, **args;
const char *tmpdir;
/* LINTED local definition of tmpfile */
FILE *tmpfile, *dependfile;
char buffer[32768];
setlocale(LC_ALL, "");
setprogname(argv[0]);
aflag = 0;
pflag = 0;
filename = DEFAULT_FILENAME;
2001-03-22 05:33:47 +03:00
2003-01-19 03:42:45 +03:00
/* XXX should use getopt(). */
for (index = 1; index < argc; index++) {
if (strcmp(argv[index], "-a") == 0)
aflag = 1;
2003-01-19 03:42:45 +03:00
else if (strcmp(argv[index], "-f") == 0) {
if (++index < argc)
filename = argv[index];
} else if (strcmp(argv[index], "-p") == 0)
2001-03-22 05:33:47 +03:00
pflag = 1;
2003-01-19 03:42:45 +03:00
else
2001-03-22 05:33:47 +03:00
break;
}
2003-01-19 03:42:45 +03:00
argc -= index;
argv += index;
if (argc == 0)
usage();
if ((CC = getenv("CC")) == NULL)
CC = DEFAULT_CC;
if ((pathname = findcc(CC)) == NULL)
if (!setenv("PATH", DEFAULT_PATH, 1))
pathname = findcc(CC);
if (pathname == NULL) {
(void)fprintf(stderr, "%s: %s: not found\n", getprogname(), CC);
2001-03-22 05:33:47 +03:00
exit(EXIT_FAILURE);
}
if ((args = malloc((argc + 3) * sizeof(char *))) == NULL) {
perror(getprogname());
exit(EXIT_FAILURE);
}
args[0] = CC;
args[1] = "-M";
(void)memcpy(&args[2], argv, (argc + 1) * sizeof(char *));
if ((tmpdir = getenv("TMPDIR")) == NULL)
tmpdir = _PATH_TMP;
(void)snprintf(tmpfilename, sizeof (tmpfilename), "%s/%s", tmpdir,
"mkdepXXXXXX");
/* set signal handler */
tmpfd = -1;
(void)signal(SIGINT, finish);
(void)signal(SIGHUP, finish);
(void)signal(SIGQUIT, finish);
2003-03-28 10:38:15 +03:00
(void)signal(SIGPIPE, finish);
(void)signal(SIGTERM, finish);
if ((tmpfd = mkstemp (tmpfilename)) < 0) {
warn("unable to create temporary file %s", tmpfilename);
2001-03-22 05:33:47 +03:00
exit(EXIT_FAILURE);
}
switch (cpid = vfork()) {
case 0:
2001-03-22 05:33:47 +03:00
(void)dup2(tmpfd, STDOUT_FILENO);
(void)close(tmpfd);
2001-03-22 05:33:47 +03:00
(void)execv(pathname, args);
_exit(EXIT_FAILURE);
/* NOTREACHED */
case -1:
2001-03-22 05:33:47 +03:00
(void)fprintf(stderr, "%s: unable to fork.\n", getprogname());
(void)close(tmpfd);
(void)unlink(tmpfilename);
exit(EXIT_FAILURE);
}
2001-03-22 05:33:47 +03:00
while (((pid = wait(&status)) != cpid) && (pid >= 0))
continue;
if (status) {
2001-03-22 05:33:47 +03:00
(void)fprintf(stderr, "%s: compile failed.\n", getprogname());
(void)close(tmpfd);
(void)unlink(tmpfilename);
exit(EXIT_FAILURE);
}
(void)lseek(tmpfd, (off_t)0, SEEK_SET);
if ((tmpfile = fdopen(tmpfd, "r")) == NULL) {
2001-03-22 05:41:02 +03:00
(void)fprintf(stderr, "%s: unable to read temporary file %s\n",
2001-03-22 05:33:47 +03:00
getprogname(), tmpfilename);
(void)close(tmpfd);
(void)unlink(tmpfilename);
exit(EXIT_FAILURE);
}
if ((dependfile = fopen(filename, aflag ? "a" : "w")) == NULL) {
(void)fprintf(stderr, "%s: unable to %s to file %s\n",
2001-03-22 05:33:47 +03:00
getprogname(), aflag ? "append" : "write", filename);
(void)fclose(tmpfile);
(void)unlink(tmpfilename);
2001-03-22 05:33:47 +03:00
exit(EXIT_FAILURE);
}
while (fgets(buffer, sizeof(buffer), tmpfile) != NULL) {
char *ptr;
if (pflag && ((ptr = strstr(buffer, ".o")) != NULL)) {
char *colon;
colon = ptr + 2;
while (isspace(*colon)) colon++;
if (*colon == ':')
(void)strcpy(ptr, colon);
}
ptr = buffer;
2001-03-22 05:33:47 +03:00
while (*ptr) {
if (isspace(*ptr++))
if ((ptr[0] == '.') && (ptr[1] == '/'))
(void)strcpy(ptr, ptr + 2);
2001-03-22 05:33:47 +03:00
}
(void)fputs(buffer, dependfile);
}
(void)fclose(dependfile);
(void)fclose(tmpfile);
(void)unlink(tmpfilename);
2001-03-22 05:33:47 +03:00
exit(EXIT_SUCCESS);
}