diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2003-06-26 04:52:27 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2003-06-26 04:52:27 +0000 |
commit | fd7b4abf64f5db6376ebfed2c975bc00d82f4a59 (patch) | |
tree | 052ceab190a840edfe5bc53aacdcec2110280d6d /usr.bin | |
parent | 66b79825bd8d3fc6e7c2c04a33fd1a7beb11a16c (diff) |
Fix temp file handling and deal with the case where we might need 2 temp files.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/diff/diff.c | 14 | ||||
-rw-r--r-- | usr.bin/diff/diff.h | 8 | ||||
-rw-r--r-- | usr.bin/diff/diffreg.c | 71 | ||||
-rw-r--r-- | usr.bin/diff/pathnames.h | 4 |
4 files changed, 60 insertions, 37 deletions
diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c index 514bf394567..556086c45de 100644 --- a/usr.bin/diff/diff.c +++ b/usr.bin/diff/diff.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diff.c,v 1.12 2003/06/26 00:20:48 tedu Exp $ */ +/* $OpenBSD: diff.c,v 1.13 2003/06/26 04:52:26 millert Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -84,9 +84,9 @@ char **diffargv; /* option list to pass to recursive diffs */ char *file1, *file2, *efile1, *efile2; struct stat stb1, stb2; -char diff[] = _PATH_DIFF; -char diffh[] = _PATH_DIFFH; -char pr[] = _PATH_PR; +const char *diff = _PATH_DIFF; +const char *diffh = _PATH_DIFFH; +const char *pr = _PATH_PR; static void noroom(void); __dead void usage(void); @@ -215,8 +215,10 @@ max(int a, int b) __dead void done(int sig) { - if (tempfile) - unlink(tempfile); + if (tempfiles[0]) + unlink(tempfiles[0]); + if (tempfiles[1]) + unlink(tempfiles[1]); if (sig) _exit(status); exit(status); diff --git a/usr.bin/diff/diff.h b/usr.bin/diff/diff.h index 261a8e475f4..4c33f03dba0 100644 --- a/usr.bin/diff/diff.h +++ b/usr.bin/diff/diff.h @@ -1,4 +1,4 @@ -/* $OpenBSD: diff.h,v 1.8 2003/06/26 00:20:48 tedu Exp $ */ +/* $OpenBSD: diff.h,v 1.9 2003/06/26 04:52:26 millert Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -63,15 +63,15 @@ extern int aflag, bflag, hflag, iflag, lflag, rflag, sflag, tflag, wflag; extern char *start, *ifdef1, *ifdef2, *endifname; extern int opt, wantelses, inifdef, context, status, anychange; -extern char *tempfile, **diffargv; +extern char *tempfiles[], **diffargv; extern char *file1, *file2, *efile1, *efile2; extern struct stat stb1, stb2; -extern char diffh[], diff[], pr[]; +extern const char *diffh, *diff, *pr; void *emalloc(size_t); void *erealloc(void *, size_t); char *splice(char *, char *); -char *copytemp(void); +char *copytemp(const char *, int); void diffdir(char **); void diffreg(void); int max(int, int); diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c index a5467e1ae57..e1c4332a298 100644 --- a/usr.bin/diff/diffreg.c +++ b/usr.bin/diff/diffreg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diffreg.c,v 1.19 2003/06/26 04:47:30 vincent Exp $ */ +/* $OpenBSD: diffreg.c,v 1.20 2003/06/26 04:52:26 millert Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -236,30 +236,31 @@ diffreg(void) done(0); } chrtran = (iflag ? cup2low : clow2low); - if ((stb1.st_mode & S_IFMT) == S_IFDIR) { + if (strcmp(file1, "-") == 0 && strcmp(file2, "-") == 0) { + warnx("can't specify - -"); + done(0); + } + if (S_ISDIR(stb1.st_mode)) { file1 = splice(file1, file2); if (stat(file1, &stb1) < 0) { warn("%s", file1); done(0); } - } else if ((stb2.st_mode & S_IFMT) == S_IFDIR) { + } else if (!S_ISREG(stb1.st_mode) || strcmp(file1, "-") == 0) { + file1 = copytemp(file1, 1); + if (stat(file1, &stb1) < 0) { + warn("%s", file1); + done(0); + } + } + if (S_ISDIR(stb2.st_mode)) { file2 = splice(file2, file1); if (stat(file2, &stb2) < 0) { warn("%s", file2); done(0); } - } else if ((stb1.st_mode & S_IFMT) != S_IFREG || !strcmp(file1, "-")) { - if (!strcmp(file2, "-")) { - warnx("can't specify - -"); - done(0); - } - file1 = copytemp(); - if (stat(file1, &stb1) < 0) { - warn("%s", file1); - done(0); - } - } else if ((stb2.st_mode & S_IFMT) != S_IFREG || !strcmp(file2, "-")) { - file2 = copytemp(); + } else if (!S_ISREG(stb2.st_mode) || strcmp(file2, "-") == 0) { + file2 = copytemp(file2, 2); if (stat(file2, &stb2) < 0) { warn("%s", file2); done(0); @@ -274,7 +275,8 @@ diffreg(void) fclose(f1); done(0); } - if (stb1.st_size != stb2.st_size) + if (S_ISREG(stb1.st_mode) && S_ISREG(stb2.st_mode) && + stb1.st_size != stb2.st_size) goto notsame; for (;;) { i = fread(buf1, 1, BUFSIZ, f1); @@ -340,29 +342,48 @@ same: done(0); } -char *tempfile = _PATH_TMP; +char *tempfiles[2]; char * -copytemp(void) +copytemp(const char *file, int n) { - char buf[BUFSIZ]; - int i, f; + char buf[BUFSIZ], *tempdir, *tempfile; + int i, ifd, ofd; + + if (n != 1 && n != 2) + return (NULL); + + if (strcmp(file, "-") == 0) + ifd = STDIN_FILENO; + else if ((ifd = open(file, O_RDONLY, 0644)) < 0) { + warn("%s", file); + done(0); + } + + if ((tempdir = getenv("TMPDIR")) == NULL) + tempdir = _PATH_TMP; + if (asprintf(&tempfile, "%s/diff%d.XXXXXXXX", tempdir, n) == -1) { + warn(NULL); + done(0); + } + tempfiles[n - 1] = tempfile; signal(SIGHUP, done); signal(SIGINT, done); signal(SIGPIPE, done); signal(SIGTERM, done); - f = mkstemp(tempfile); - if (f < 0) { + ofd = mkstemp(tempfile); + if (ofd < 0) { warn("%s", tempfile); done(0); } - while ((i = read(0, buf, BUFSIZ)) > 0) - if (write(f, buf, i) != i) { + while ((i = read(ifd, buf, BUFSIZ)) > 0) + if (write(ofd, buf, i) != i) { warn("%s", tempfile); done(0); } - close(f); + close(ifd); + close(ofd); return (tempfile); } diff --git a/usr.bin/diff/pathnames.h b/usr.bin/diff/pathnames.h index eaf8402ac0b..eec82149fbf 100644 --- a/usr.bin/diff/pathnames.h +++ b/usr.bin/diff/pathnames.h @@ -29,8 +29,8 @@ * @(#)pathnames.h 8.1 (Berkeley) 6/6/93 */ +#include <paths.h> + #define _PATH_DIFF "/usr/bin/diff" #define _PATH_DIFFH "/usr/bin/diffh" #define _PATH_PR "/usr/bin/pr" -#undef _PATH_TMP -#define _PATH_TMP "/tmp/dXXXXX" |