2002-06-15 03:14:18 +04:00
|
|
|
/* $NetBSD: mkdep.c,v 1.11 2002/06/14 23:14:18 simonb Exp $ */
|
1999-01-26 01:10:42 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* 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
|
1999-01-26 01:10:42 +03:00
|
|
|
* 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>
|
2002-02-01 01:43:33 +03:00
|
|
|
#if defined(__COPYRIGHT) && !defined(lint)
|
1999-01-26 01:10:42 +03:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\n\
|
|
|
|
All rights reserved.\n");
|
|
|
|
#endif /* not lint */
|
|
|
|
|
2002-02-01 01:43:33 +03:00
|
|
|
#if defined(__RCSID) && !defined(lint)
|
2002-06-15 03:14:18 +04:00
|
|
|
__RCSID("$NetBSD: mkdep.c,v 1.11 2002/06/14 23:14:18 simonb Exp $");
|
1999-01-26 01:10:42 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2002-02-01 01:43:33 +03:00
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
1999-01-26 01:10:42 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <ctype.h>
|
2002-02-01 01:43:33 +03:00
|
|
|
#include <err.h>
|
1999-01-26 01:10:42 +03:00
|
|
|
#include <locale.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2002-06-15 03:14:18 +04:00
|
|
|
#include "findcc.h"
|
|
|
|
|
1999-01-26 01:10:42 +03:00
|
|
|
#define DEFAULT_PATH _PATH_DEFPATH
|
|
|
|
#define DEFAULT_FILENAME ".depend"
|
|
|
|
|
1999-03-31 15:26:45 +04:00
|
|
|
static void usage __P((void));
|
|
|
|
int main __P((int, char **));
|
1999-01-26 01:10:42 +03:00
|
|
|
|
1999-03-31 15:26:45 +04:00
|
|
|
static void
|
1999-01-26 01:10:42 +03:00
|
|
|
usage()
|
|
|
|
{
|
|
|
|
(void)fprintf(stderr,
|
2001-03-22 03:16:50 +03:00
|
|
|
"usage: %s [-a] [-p] [-f file] flags file ...\n",
|
|
|
|
getprogname());
|
1999-01-26 01:10:42 +03:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
1999-03-31 15:26:45 +04:00
|
|
|
/* LINTED local definition of index */
|
1999-01-26 01:10:42 +03:00
|
|
|
int aflag, pflag, index, tmpfd, status;
|
|
|
|
pid_t cpid, pid;
|
|
|
|
char *filename, *CC, *pathname, tmpfilename[MAXPATHLEN], **args;
|
1999-07-21 19:20:55 +04:00
|
|
|
const char *tmpdir;
|
1999-03-31 15:26:45 +04:00
|
|
|
/* LINTED local definition of tmpfile */
|
1999-01-26 01:10:42 +03:00
|
|
|
FILE *tmpfile, *dependfile;
|
|
|
|
char buffer[32768];
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
2001-02-21 03:06:22 +03:00
|
|
|
setprogname(argv[0]);
|
1999-01-26 01:10:42 +03:00
|
|
|
|
|
|
|
aflag = 0;
|
|
|
|
pflag = 0;
|
|
|
|
filename = DEFAULT_FILENAME;
|
2001-03-22 05:33:47 +03:00
|
|
|
|
|
|
|
/* XXX should use getopt(). */
|
|
|
|
for (index = 1; index < argc; index++) {
|
1999-01-26 01:10:42 +03:00
|
|
|
if (strcmp(argv[index], "-a") == 0)
|
|
|
|
aflag = 1;
|
2001-03-22 05:33:47 +03:00
|
|
|
else if (strcmp(argv[index], "-f") == 0) {
|
|
|
|
if (++index < argc)
|
|
|
|
filename = argv[index];
|
|
|
|
} else if (strcmp(argv[index], "-p") == 0)
|
|
|
|
pflag = 1;
|
1999-01-26 01:10:42 +03:00
|
|
|
else
|
2001-03-22 05:33:47 +03:00
|
|
|
break;
|
|
|
|
}
|
1999-01-26 01:10:42 +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) {
|
2001-03-22 03:16:50 +03:00
|
|
|
(void)fprintf(stderr, "%s: %s: not found\n", getprogname(), CC);
|
2001-03-22 05:33:47 +03:00
|
|
|
exit(EXIT_FAILURE);
|
1999-01-26 01:10:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((args = malloc((argc + 3) * sizeof(char *))) == NULL) {
|
2001-03-22 03:16:50 +03:00
|
|
|
perror(getprogname());
|
1999-01-26 01:10:42 +03:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
args[0] = CC;
|
|
|
|
args[1] = "-M";
|
|
|
|
(void)memcpy(&args[2], argv, (argc + 1) * sizeof(char *));
|
|
|
|
|
1999-07-21 19:20:55 +04:00
|
|
|
if ((tmpdir = getenv("TMPDIR")) == NULL)
|
|
|
|
tmpdir = _PATH_TMP;
|
|
|
|
(void)snprintf(tmpfilename, sizeof (tmpfilename), "%s/%s", tmpdir,
|
|
|
|
"mkdepXXXXXX");
|
1999-01-26 01:10:42 +03:00
|
|
|
if ((tmpfd = mkstemp (tmpfilename)) < 0) {
|
|
|
|
warn("unable to create temporary file %s", tmpfilename);
|
2001-03-22 05:33:47 +03:00
|
|
|
exit(EXIT_FAILURE);
|
1999-01-26 01:10:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (cpid = vfork()) {
|
|
|
|
case 0:
|
2001-03-22 05:33:47 +03:00
|
|
|
(void)dup2(tmpfd, STDOUT_FILENO);
|
|
|
|
(void)close(tmpfd);
|
1999-01-26 01:10:42 +03:00
|
|
|
|
2001-03-22 05:33:47 +03:00
|
|
|
(void)execv(pathname, args);
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
/* NOTREACHED */
|
1999-01-26 01:10:42 +03:00
|
|
|
|
|
|
|
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);
|
1999-01-26 01:10:42 +03:00
|
|
|
}
|
|
|
|
|
2001-03-22 05:33:47 +03:00
|
|
|
while (((pid = wait(&status)) != cpid) && (pid >= 0))
|
|
|
|
continue;
|
1999-01-26 01:10:42 +03:00
|
|
|
|
|
|
|
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);
|
1999-01-26 01:10:42 +03:00
|
|
|
}
|
|
|
|
|
1999-03-31 15:26:45 +04:00
|
|
|
(void)lseek(tmpfd, (off_t)0, SEEK_SET);
|
1999-01-26 01:10:42 +03:00
|
|
|
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);
|
1999-01-26 01:10:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((dependfile = fopen(filename, aflag ? "a" : "w")) == NULL) {
|
2001-03-22 03:16:50 +03:00
|
|
|
(void)fprintf(stderr, "%s: unable to %s to file %s\n",
|
2001-03-22 05:33:47 +03:00
|
|
|
getprogname(), aflag ? "append" : "write", filename);
|
1999-01-26 01:10:42 +03:00
|
|
|
(void)fclose(tmpfile);
|
|
|
|
(void)unlink(tmpfilename);
|
2001-03-22 05:33:47 +03:00
|
|
|
exit(EXIT_FAILURE);
|
1999-01-26 01:10:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
1999-01-26 01:10:42 +03:00
|
|
|
if (isspace(*ptr++))
|
|
|
|
if ((ptr[0] == '.') && (ptr[1] == '/'))
|
|
|
|
(void)strcpy(ptr, ptr + 2);
|
2001-03-22 05:33:47 +03:00
|
|
|
}
|
1999-01-26 01:10:42 +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);
|
1999-01-26 01:10:42 +03:00
|
|
|
}
|