summaryrefslogtreecommitdiffstats
path: root/usr.bin/diff
diff options
context:
space:
mode:
authormillert <millert@openbsd.org>2003-06-25 21:43:49 +0000
committermillert <millert@openbsd.org>2003-06-25 21:43:49 +0000
commit49dffe13d7fe900b7adc58a2f8d6809cffbe02d7 (patch)
tree11281c9540cd069a01afa492aa4d03de8c82fbf6 /usr.bin/diff
parentdelete junk proto (diff)
downloadwireguard-openbsd-49dffe13d7fe900b7adc58a2f8d6809cffbe02d7.tar.xz
wireguard-openbsd-49dffe13d7fe900b7adc58a2f8d6809cffbe02d7.zip
o use S_ISDIR instead of doing it by hand
o rename talloc -> emalloc and ralloc -> erealloc o struct direct -> struct dirent (POSIX) o kill remaining strcpy() o fix unterminated string in setfile() deraadt@ OK
Diffstat (limited to 'usr.bin/diff')
-rw-r--r--usr.bin/diff/diff.c11
-rw-r--r--usr.bin/diff/diff.h7
-rw-r--r--usr.bin/diff/diffdir.c47
-rw-r--r--usr.bin/diff/diffreg.c46
4 files changed, 62 insertions, 49 deletions
diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c
index 09b9fe98b6a..3ae107214af 100644
--- a/usr.bin/diff/diff.c
+++ b/usr.bin/diff/diff.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: diff.c,v 1.7 2003/06/25 19:56:57 millert Exp $ */
+/* $OpenBSD: diff.c,v 1.8 2003/06/25 21:43:49 millert Exp $ */
/*
* Copyright (C) Caldera International Inc. 2001-2002.
@@ -142,10 +142,9 @@ main(int argc, char **argv)
stb2.st_mode = S_IFREG;
else if (stat(file2, &stb2) < 0)
err(1, "%s", file2);
- if ((stb1.st_mode & S_IFMT) == S_IFDIR &&
- (stb2.st_mode & S_IFMT) == S_IFDIR) {
+ if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode))
diffdir(argv);
- } else
+ else
diffreg();
done(0);
}
@@ -175,7 +174,7 @@ done(int sig)
}
void *
-talloc(size_t n)
+emalloc(size_t n)
{
void *p;
@@ -185,7 +184,7 @@ talloc(size_t n)
}
void *
-ralloc(void *p, size_t n)
+erealloc(void *p, size_t n)
{
void *q;
diff --git a/usr.bin/diff/diff.h b/usr.bin/diff/diff.h
index b49f5cd8264..a6985add4bc 100644
--- a/usr.bin/diff/diff.h
+++ b/usr.bin/diff/diff.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: diff.h,v 1.6 2003/06/25 17:49:22 millert Exp $ */
+/* $OpenBSD: diff.h,v 1.7 2003/06/25 21:43:49 millert Exp $ */
/*
* Copyright (C) Caldera International Inc. 2001-2002.
@@ -42,7 +42,6 @@
#include <sys/param.h>
#include <sys/stat.h>
-#include <sys/dir.h>
#include <ctype.h>
#include <err.h>
@@ -116,8 +115,8 @@ char **diffargv; /* option list to pass to recursive diffs */
char *file1, *file2, *efile1, *efile2;
struct stat stb1, stb2;
-void *talloc(size_t);
-void *ralloc(void *, size_t);
+void *emalloc(size_t);
+void *erealloc(void *, size_t);
char *splice(char *, char *);
char *copytemp(void);
void diffdir(char **);
diff --git a/usr.bin/diff/diffdir.c b/usr.bin/diff/diffdir.c
index 12236c84e19..8c524b98bb4 100644
--- a/usr.bin/diff/diffdir.c
+++ b/usr.bin/diff/diffdir.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: diffdir.c,v 1.11 2003/06/25 17:49:22 millert Exp $ */
+/* $OpenBSD: diffdir.c,v 1.12 2003/06/25 21:43:49 millert Exp $ */
/*
* Copyright (C) Caldera International Inc. 2001-2002.
@@ -37,11 +37,13 @@
#include <sys/types.h>
#include <sys/wait.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
-#include <fcntl.h>
-#include <unistd.h>
#include <string.h>
+#include <unistd.h>
#include "diff.h"
@@ -163,8 +165,10 @@ diffdir(char **argv)
for (d1 = dir1; d1->d_entry; d1++) {
if ((d1->d_flags & DIRECT) == 0)
continue;
- strcpy(efile1, d1->d_entry);
- strcpy(efile2, d1->d_entry);
+ strlcpy(efile1, d1->d_entry,
+ file1 + MAXPATHLEN - efile1);
+ strlcpy(efile2, d1->d_entry,
+ file2 + MAXPATHLEN - efile2);
calldiff(0);
}
}
@@ -175,12 +179,21 @@ void
setfile(char **fpp, char **epp, char *file)
{
char *cp;
+ size_t len;
- *fpp = talloc(BUFSIZ);
- strcpy(*fpp, file);
- for (cp = *fpp; *cp; cp++)
- continue;
- *cp++ = '/';
+ if (*file == '\0')
+ file = ".";
+ *fpp = emalloc(MAXPATHLEN);
+ len = strlcpy(*fpp, file, MAXPATHLEN);
+ if (len >= MAXPATHLEN - 1)
+ errx(1, "%s: %s", file, strerror(ENAMETOOLONG));
+ cp = *fpp + len - 1;
+ if (*cp == '/')
+ ++cp;
+ else {
+ *++cp = '/';
+ *++cp = '\0';
+ }
*epp = cp;
}
@@ -221,7 +234,7 @@ struct dir *
setupdir(char *cp)
{
struct dir *dp, *ep;
- struct direct *rp;
+ struct dirent *rp;
int nitems;
DIR *dirp;
@@ -231,7 +244,7 @@ setupdir(char *cp)
done(0);
}
nitems = 0;
- dp = talloc(sizeof(struct dir));
+ dp = emalloc(sizeof(struct dir));
while ((rp = readdir(dirp))) {
ep = &dp[nitems++];
ep->d_reclen = rp->d_reclen;
@@ -239,10 +252,10 @@ setupdir(char *cp)
ep->d_entry = 0;
ep->d_flags = 0;
if (ep->d_namlen > 0) {
- ep->d_entry = talloc(ep->d_namlen + 1);
- strcpy(ep->d_entry, rp->d_name);
+ ep->d_entry = emalloc(ep->d_namlen + 1);
+ strlcpy(ep->d_entry, rp->d_name, ep->d_namlen + 1);
}
- dp = ralloc(dp, (nitems + 1) * sizeof(struct dir));
+ dp = erealloc(dp, (nitems + 1) * sizeof(struct dir));
}
dp[nitems].d_entry = 0; /* delimiter */
closedir(dirp);
@@ -267,8 +280,8 @@ compare(struct dir *dp)
int i, j, f1, f2, fmt1, fmt2;
struct stat stb1, stb2;
- strcpy(efile1, dp->d_entry);
- strcpy(efile2, dp->d_entry);
+ strlcpy(efile1, dp->d_entry, file1 + MAXPATHLEN - efile1);
+ strlcpy(efile2, dp->d_entry, file2 + MAXPATHLEN - efile2);
f1 = open(file1, 0);
if (f1 < 0) {
perror(file1);
diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c
index 195002401b3..2b868776f77 100644
--- a/usr.bin/diff/diffreg.c
+++ b/usr.bin/diff/diffreg.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: diffreg.c,v 1.15 2003/06/25 17:49:22 millert Exp $ */
+/* $OpenBSD: diffreg.c,v 1.16 2003/06/25 21:43:49 millert Exp $ */
/*
* Copyright (C) Caldera International Inc. 2001-2002.
@@ -311,25 +311,25 @@ notsame:
member = (int *)file[1];
equiv(sfile[0], slen[0], sfile[1], slen[1], member);
- member = ralloc(member, (slen[1] + 2) * sizeof(int));
+ member = erealloc(member, (slen[1] + 2) * sizeof(int));
class = (int *)file[0];
unsort(sfile[0], slen[0], class);
- class = ralloc(class, (slen[0] + 2) * sizeof(int));
+ class = erealloc(class, (slen[0] + 2) * sizeof(int));
- klist = talloc((slen[0] + 2) * sizeof(int));
- clist = talloc(sizeof(cand));
+ klist = emalloc((slen[0] + 2) * sizeof(int));
+ clist = emalloc(sizeof(cand));
i = stone(class, slen[0], member, klist);
free(member);
free(class);
- J = talloc((len[0] + 2) * sizeof(int));
+ J = emalloc((len[0] + 2) * sizeof(int));
unravel(klist[i]);
free(clist);
free(klist);
- ixold = talloc((len[0] + 2) * sizeof(long));
- ixnew = talloc((len[1] + 2) * sizeof(long));
+ ixold = emalloc((len[0] + 2) * sizeof(long));
+ ixnew = emalloc((len[1] + 2) * sizeof(long));
check();
output();
status = anychange;
@@ -368,19 +368,22 @@ copytemp(void)
char *
splice(char *dir, char *file)
{
- char *tail, buf[BUFSIZ];
+ char *tail, *buf;
+ size_t len;
if (!strcmp(file, "-")) {
warnx("can't specify - with other arg directory");
done(0);
}
tail = strrchr(file, '/');
- if (tail == 0)
+ if (tail == NULL)
tail = file;
else
tail++;
- snprintf(buf, sizeof buf, "%s/%s", dir, tail);
- return (strdup(buf));
+ len = strlen(dir) + strlen(tail) + 1;
+ buf = emalloc(len);
+ snprintf(buf, len, "%s/%s", dir, tail);
+ return (buf);
}
static void
@@ -390,9 +393,9 @@ prepare(int i, FILE *fd)
int j, h;
fseek(fd, 0L, SEEK_SET);
- p = talloc(3 * sizeof(struct line));
+ p = emalloc(3 * sizeof(struct line));
for (j = 0; (h = readhash(fd));) {
- p = ralloc(p, (++j + 3) * sizeof(struct line));
+ p = erealloc(p, (++j + 3) * sizeof(struct line));
p[j].value = h;
}
len[i] = j;
@@ -491,7 +494,7 @@ newcand(int x, int y, int pred)
{
struct cand *q;
- clist = ralloc(clist, ++clen * sizeof(cand));
+ clist = erealloc(clist, ++clen * sizeof(cand));
q = clist + clen - 1;
q->x = x;
q->y = y;
@@ -537,11 +540,11 @@ unravel(int p)
}
/*
- * check does double duty: 1. ferret out any fortuitous correspondences due
- * to confounding by hashing (which result in "jackpot") 2. collect random
- * access indexes to the two files
+ * Check does double duty:
+ * 1. ferret out any fortuitous correspondences due
+ * to confounding by hashing (which result in "jackpot")
+ * 2. collect random access indexes to the two files
*/
-
static void
check(void)
{
@@ -678,7 +681,7 @@ unsort(struct line *f, int l, int *b)
{
int *a, i;
- a = talloc((l + 1) * sizeof(int));
+ a = emalloc((l + 1) * sizeof(int));
for (i = 1; i <= l; i++)
a[f[i].serial] = f[i].value;
for (i = 1; i <= l; i++)
@@ -788,7 +791,7 @@ change(int a, int b, int c, int d)
stat(file2, &stbuf);
printf("%s", ctime(&stbuf.st_mtime));
- context_vec_start = talloc(MAX_CONTEXT *
+ context_vec_start = emalloc(MAX_CONTEXT *
sizeof(struct context_vec));
context_vec_end = context_vec_start + MAX_CONTEXT;
context_vec_ptr = context_vec_start - 1;
@@ -1015,7 +1018,6 @@ asciifile(FILE *f)
return (1);
}
-
/* dump accumulated "context" diff changes */
static void
dump_context_vec(void)