summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2003-06-26 18:19:30 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2003-06-26 18:19:30 +0000
commit138be56b4cb60252fc1f487b0ec6df1c45548d8a (patch)
tree5be413c1725e3594f74983431196f0b76692e6ca /usr.bin
parent9b204b3b913998e74384425206e6298610ff2ae8 (diff)
Fix temp file handling.
o honor TMPDIR environment variable as per man page o need 2 temp files if both file1 and file2 are devices o add error() and errorx() which cleanup temp file and then call err() and errx() respectively. OK tedu@
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/diff/diff.c54
-rw-r--r--usr.bin/diff/diff.h4
-rw-r--r--usr.bin/diff/diffdir.c32
-rw-r--r--usr.bin/diff/diffreg.c96
4 files changed, 86 insertions, 100 deletions
diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c
index 21122b48466..59605ced06e 100644
--- a/usr.bin/diff/diff.c
+++ b/usr.bin/diff/diff.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: diff.c,v 1.14 2003/06/26 07:20:12 deraadt Exp $ */
+/* $OpenBSD: diff.c,v 1.15 2003/06/26 18:19:29 millert Exp $ */
/*
* Copyright (C) Caldera International Inc. 2001-2002.
@@ -34,7 +34,9 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
+#include <errno.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <unistd.h>
#include "diff.h"
@@ -88,7 +90,6 @@ const char *diff = _PATH_DIFF;
const char *diffh = _PATH_DIFFH;
const char *pr = _PATH_PR;
-static void noroom(void);
__dead void usage(void);
int
@@ -178,19 +179,19 @@ main(int argc, char **argv)
argv += optind;
if (argc != 2)
- errx(1, "two filename arguments required");
+ errorx("two filename arguments required");
file1 = argv[0];
file2 = argv[1];
if (hflag && opt)
- errx(1, "-h doesn't support -D, -c, -C, -e, -f, -I, -n, -u or -U");
+ errorx("-h doesn't support -D, -c, -C, -e, -f, -I, -n, -u or -U");
if (!strcmp(file1, "-"))
stb1.st_mode = S_IFREG;
else if (stat(file1, &stb1) < 0)
- err(1, "%s", file1);
+ error("%s", file1);
if (!strcmp(file2, "-"))
stb2.st_mode = S_IFREG;
else if (stat(file2, &stb2) < 0)
- err(1, "%s", file2);
+ error("%s", file2);
if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode))
diffdir(argv);
else
@@ -215,9 +216,9 @@ max(int a, int b)
__dead void
done(int sig)
{
- if (tempfiles[0])
+ if (tempfiles[0] != NULL)
unlink(tempfiles[0]);
- if (tempfiles[1])
+ if (tempfiles[1] != NULL)
unlink(tempfiles[1]);
if (sig)
_exit(status);
@@ -230,7 +231,7 @@ emalloc(size_t n)
void *p;
if ((p = malloc(n)) == NULL)
- noroom();
+ error("files too big, try -h");
return (p);
}
@@ -240,15 +241,38 @@ erealloc(void *p, size_t n)
void *q;
if ((q = realloc(p, n)) == NULL)
- noroom();
+ error("files too big, try -h");
return (q);
}
-static void
-noroom(void)
+__dead void
+error(const char *fmt, ...)
{
- warn("files too big, try -h");
- done(0);
+ va_list ap;
+ int sverrno = errno;
+
+ if (tempfiles[0] != NULL)
+ unlink(tempfiles[0]);
+ if (tempfiles[1] != NULL)
+ unlink(tempfiles[1]);
+ errno = sverrno;
+ va_start(ap, fmt);
+ verr(status, fmt, ap);
+ va_end(ap);
+}
+
+__dead void
+errorx(const char *fmt, ...)
+{
+ va_list ap;
+
+ if (tempfiles[0] != NULL)
+ unlink(tempfiles[0]);
+ if (tempfiles[1] != NULL)
+ unlink(tempfiles[1]);
+ va_start(ap, fmt);
+ verrx(status, fmt, ap);
+ va_end(ap);
}
__dead void
@@ -262,5 +286,5 @@ usage(void)
" diff [-biwt] [-c | -e | -f | -h | -n | -u ] "
"[-l] [-r] [-s] [-S name]\n dir1 dir2\n");
- exit(1);
+ exit(2);
}
diff --git a/usr.bin/diff/diff.h b/usr.bin/diff/diff.h
index 4c33f03dba0..fc155fcf2a9 100644
--- a/usr.bin/diff/diff.h
+++ b/usr.bin/diff/diff.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: diff.h,v 1.9 2003/06/26 04:52:26 millert Exp $ */
+/* $OpenBSD: diff.h,v 1.10 2003/06/26 18:19:29 millert Exp $ */
/*
* Copyright (C) Caldera International Inc. 2001-2002.
@@ -76,4 +76,6 @@ void diffdir(char **);
void diffreg(void);
int max(int, int);
int min(int, int);
+__dead void error(const char *, ...);
+__dead void errorx(const char *, ...);
__dead void done(int);
diff --git a/usr.bin/diff/diffdir.c b/usr.bin/diff/diffdir.c
index d34638f1853..1ba5117482a 100644
--- a/usr.bin/diff/diffdir.c
+++ b/usr.bin/diff/diffdir.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: diffdir.c,v 1.13 2003/06/25 22:14:43 millert Exp $ */
+/* $OpenBSD: diffdir.c,v 1.14 2003/06/26 18:19:29 millert Exp $ */
/*
* Copyright (C) Caldera International Inc. 2001-2002.
@@ -186,7 +186,7 @@ setfile(char **fpp, char **epp, char *file)
*fpp = emalloc(MAXPATHLEN);
len = strlcpy(*fpp, file, MAXPATHLEN);
if (len >= MAXPATHLEN - 1)
- errx(1, "%s: %s", file, strerror(ENAMETOOLONG));
+ errorx("%s: %s", file, strerror(ENAMETOOLONG));
cp = *fpp + len - 1;
if (*cp == '/')
++cp;
@@ -239,10 +239,8 @@ setupdir(char *cp)
DIR *dirp;
dirp = opendir(cp);
- if (dirp == NULL) {
- warn("%s", cp);
- done(0);
- }
+ if (dirp == NULL)
+ error("%s", cp);
nitems = 0;
dp = emalloc(sizeof(struct dir));
while ((rp = readdir(dirp))) {
@@ -284,12 +282,12 @@ compare(struct dir *dp)
strlcpy(efile2, dp->d_entry, file2 + MAXPATHLEN - efile2);
f1 = open(file1, 0);
if (f1 < 0) {
- perror(file1);
+ warn("%s", file1);
return;
}
f2 = open(file2, 0);
if (f2 < 0) {
- perror(file2);
+ warn("%s", file2);
close(f1);
return;
}
@@ -378,10 +376,8 @@ calldiff(char *wantpr)
"%s %s", file1, file2);
pipe(pv);
pid = fork();
- if (pid == -1) {
- warnx("No more processes");
- done(0);
- }
+ if (pid == -1)
+ errorx("No more processes");
if (pid == 0) {
close(0);
dup(pv[0]);
@@ -389,15 +385,12 @@ calldiff(char *wantpr)
close(pv[1]);
execv(pr + 4, prargs);
execv(pr, prargs);
- perror(pr);
- done(0);
+ errorx("%s", pr);
}
}
pid = fork();
- if (pid == -1) {
- warnx("No more processes");
- done(0);
- }
+ if (pid == -1)
+ errorx("No more processes");
if (pid == 0) {
if (wantpr) {
close(1);
@@ -407,8 +400,7 @@ calldiff(char *wantpr)
}
execv(diff + 4, diffargv);
execv(diff, diffargv);
- perror(diff);
- done(0);
+ error("%s", diff);
}
if (wantpr) {
close(pv[0]);
diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c
index e1c4332a298..0707749ae40 100644
--- a/usr.bin/diff/diffreg.c
+++ b/usr.bin/diff/diffreg.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: diffreg.c,v 1.20 2003/06/26 04:52:26 millert Exp $ */
+/* $OpenBSD: diffreg.c,v 1.21 2003/06/26 18:19:29 millert Exp $ */
/*
* Copyright (C) Caldera International Inc. 2001-2002.
@@ -232,49 +232,33 @@ diffreg(void)
if (hflag) {
diffargv[0] = "diffh";
execv(diffh, diffargv);
- warn("%s", diffh);
- done(0);
+ error("%s", diffh);
}
chrtran = (iflag ? cup2low : clow2low);
- if (strcmp(file1, "-") == 0 && strcmp(file2, "-") == 0) {
- warnx("can't specify - -");
- done(0);
- }
+ if (strcmp(file1, "-") == 0 && strcmp(file2, "-") == 0)
+ errorx("can't specify - -");
if (S_ISDIR(stb1.st_mode)) {
file1 = splice(file1, file2);
- if (stat(file1, &stb1) < 0) {
- warn("%s", file1);
- done(0);
- }
+ if (stat(file1, &stb1) < 0)
+ error("%s", file1);
} 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 (stat(file1, &stb1) < 0)
+ error("%s", file1);
}
if (S_ISDIR(stb2.st_mode)) {
file2 = splice(file2, file1);
- if (stat(file2, &stb2) < 0) {
- warn("%s", file2);
- done(0);
- }
+ if (stat(file2, &stb2) < 0)
+ error("%s", file2);
} else if (!S_ISREG(stb2.st_mode) || strcmp(file2, "-") == 0) {
file2 = copytemp(file2, 2);
- if (stat(file2, &stb2) < 0) {
- warn("%s", file2);
- done(0);
- }
- }
- if ((f1 = fopen(file1, "r")) == NULL) {
- warn("%s", file1);
- done(0);
- }
- if ((f2 = fopen(file2, "r")) == NULL) {
- warn("%s", file2);
- fclose(f1);
- done(0);
+ if (stat(file2, &stb2) < 0)
+ error("%s", file2);
}
+ if ((f1 = fopen(file1, "r")) == NULL)
+ error("%s", file1);
+ if ((f2 = fopen(file2, "r")) == NULL)
+ error("%s", file2);
if (S_ISREG(stb1.st_mode) && S_ISREG(stb2.st_mode) &&
stb1.st_size != stb2.st_size)
goto notsame;
@@ -300,9 +284,7 @@ notsame:
status = 1;
if (!asciifile(f1) || !asciifile(f2)) {
printf("Binary files %s and %s differ\n", file1, file2);
- fclose(f1);
- fclose(f2);
- done(0);
+ exit(status);
}
prepare(0, f1);
prepare(1, f2);
@@ -339,7 +321,6 @@ notsame:
same:
if (anychange == 0 && (opt == D_CONTEXT || opt == D_UNIFIED))
printf("No differences encountered\n");
- done(0);
}
char *tempfiles[2];
@@ -355,17 +336,13 @@ copytemp(const char *file, int n)
if (strcmp(file, "-") == 0)
ifd = STDIN_FILENO;
- else if ((ifd = open(file, O_RDONLY, 0644)) < 0) {
- warn("%s", file);
- done(0);
- }
+ else if ((ifd = open(file, O_RDONLY, 0644)) < 0)
+ error("%s", file);
if ((tempdir = getenv("TMPDIR")) == NULL)
tempdir = _PATH_TMP;
- if (asprintf(&tempfile, "%s/diff%d.XXXXXXXX", tempdir, n) == -1) {
- warn(NULL);
- done(0);
- }
+ if (asprintf(&tempfile, "%s/diff%d.XXXXXXXX", tempdir, n) == -1)
+ error(NULL);
tempfiles[n - 1] = tempfile;
signal(SIGHUP, done);
@@ -373,15 +350,12 @@ copytemp(const char *file, int n)
signal(SIGPIPE, done);
signal(SIGTERM, done);
ofd = mkstemp(tempfile);
- if (ofd < 0) {
- warn("%s", tempfile);
- done(0);
+ if (ofd < 0)
+ error("%s", tempfile);
+ while ((i = read(ifd, buf, BUFSIZ)) > 0) {
+ if (write(ofd, buf, i) != i)
+ error("%s", tempfile);
}
- while ((i = read(ifd, buf, BUFSIZ)) > 0)
- if (write(ofd, buf, i) != i) {
- warn("%s", tempfile);
- done(0);
- }
close(ifd);
close(ofd);
return (tempfile);
@@ -393,10 +367,8 @@ splice(char *dir, char *file)
char *tail, *buf;
size_t len;
- if (!strcmp(file, "-")) {
- warnx("can't specify - with other arg directory");
- done(0);
- }
+ if (!strcmp(file, "-"))
+ errorx("can't specify - with other arg directory");
tail = strrchr(file, '/');
if (tail == NULL)
tail = file;
@@ -573,14 +545,10 @@ check(void)
int i, j, jackpot, c, d;
long ctold, ctnew;
- if ((input[0] = fopen(file1, "r")) == NULL) {
- perror(file1);
- done(0);
- }
- if ((input[1] = fopen(file2, "r")) == NULL) {
- perror(file2);
- done(0);
- }
+ if ((input[0] = fopen(file1, "r")) == NULL)
+ error("%s", file1);
+ if ((input[1] = fopen(file2, "r")) == NULL)
+ error("%s", file2);
j = 1;
ixold[0] = ixnew[0] = 0;
jackpot = 0;