diff options
author | Ray Lai <ray@cvs.openbsd.org> | 2007-05-29 18:24:57 +0000 |
---|---|---|
committer | Ray Lai <ray@cvs.openbsd.org> | 2007-05-29 18:24:57 +0000 |
commit | f6c22bb816c2c22cb5578ac08054385b940e4b03 (patch) | |
tree | 838c7d9313ea206db17c5f541b14c57265910fe2 /usr.bin/diff | |
parent | c1821f5cc50d3d5d3a99c8d48845e59a0bfffdd0 (diff) |
Bring in some changes from rcsdiff:
1. Replace all the e*alloc functions with the x*alloc versions.
2. Whitespace syncs according to style.
3. Remove the __inline stuff.
4. Remove the min/max functions, using the MAX/MIN macros instead.
OK millert@
Diffstat (limited to 'usr.bin/diff')
-rw-r--r-- | usr.bin/diff/Makefile | 4 | ||||
-rw-r--r-- | usr.bin/diff/diff.c | 57 | ||||
-rw-r--r-- | usr.bin/diff/diffdir.c | 21 | ||||
-rw-r--r-- | usr.bin/diff/diffreg.c | 111 | ||||
-rw-r--r-- | usr.bin/diff/xmalloc.c | 91 | ||||
-rw-r--r-- | usr.bin/diff/xmalloc.h | 30 |
6 files changed, 194 insertions, 120 deletions
diff --git a/usr.bin/diff/Makefile b/usr.bin/diff/Makefile index 92854469cef..4f1c9d5d168 100644 --- a/usr.bin/diff/Makefile +++ b/usr.bin/diff/Makefile @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile,v 1.2 2003/06/25 02:42:50 deraadt Exp $ +# $OpenBSD: Makefile,v 1.3 2007/05/29 18:24:56 ray Exp $ PROG= diff -SRCS= diff.c diffdir.c diffreg.c +SRCS= diff.c diffdir.c diffreg.c xmalloc.c COPTS+= -Wall .include <bsd.prog.mk> diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c index af77b37b9c5..82f9984f556 100644 --- a/usr.bin/diff/diff.c +++ b/usr.bin/diff/diff.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diff.c,v 1.49 2007/03/01 21:48:32 jmc Exp $ */ +/* $OpenBSD: diff.c,v 1.50 2007/05/29 18:24:56 ray Exp $ */ /* * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com> @@ -21,7 +21,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: diff.c,v 1.49 2007/03/01 21:48:32 jmc Exp $"; +static const char rcsid[] = "$OpenBSD: diff.c,v 1.50 2007/05/29 18:24:56 ray Exp $"; #endif /* not lint */ #include <sys/param.h> @@ -39,6 +39,7 @@ static const char rcsid[] = "$OpenBSD: diff.c,v 1.49 2007/03/01 21:48:32 jmc Exp #include <unistd.h> #include "diff.h" +#include "xmalloc.h" int aflag, bflag, dflag, iflag, lflag, Nflag, Pflag, pflag, rflag; int sflag, tflag, Tflag, wflag; @@ -274,41 +275,6 @@ main(int argc, char **argv) exit(status); } -void * -emalloc(size_t n) -{ - void *p; - - if ((p = malloc(n)) == NULL) - err(2, NULL); - return (p); -} - -void * -erealloc(void *p, size_t n) -{ - void *q; - - if ((q = realloc(p, n)) == NULL) - err(2, NULL); - return (q); -} - -int -easprintf(char **ret, const char *fmt, ...) -{ - int len; - va_list ap; - - va_start(ap, fmt); - len = vasprintf(ret, fmt, ap); - va_end(ap); - - if (len == -1) - err(2, NULL); - return (len); -} - void set_argstr(char **av, char **ave) { @@ -316,7 +282,7 @@ set_argstr(char **av, char **ave) char **ap; argsize = 4 + *ave - *av + 1; - diffargs = emalloc(argsize); + diffargs = xmalloc(argsize); strlcpy(diffargs, "diff", argsize); for (ap = av + 1; ap < ave; ap++) { if (strcmp(*ap, "--") != 0) { @@ -343,7 +309,7 @@ read_excludes_file(char *file) while ((buf = fgetln(fp, &len)) != NULL) { if (buf[len - 1] == '\n') len--; - pattern = emalloc(len + 1); + pattern = xmalloc(len + 1); memcpy(pattern, buf, len); pattern[len] = '\0'; push_excludes(pattern); @@ -360,7 +326,7 @@ push_excludes(char *pattern) { struct excludes *entry; - entry = emalloc(sizeof(*entry)); + entry = xmalloc(sizeof(*entry)); entry->pattern = pattern; entry->next = excludes_list; excludes_list = entry; @@ -371,15 +337,12 @@ push_ignore_pats(char *pattern) { size_t len; - if (ignore_pats == NULL) { - /* XXX: estrdup */ - len = strlen(pattern) + 1; - ignore_pats = emalloc(len); - strlcpy(ignore_pats, pattern, len); - } else { + if (ignore_pats == NULL) + ignore_pats = xstrdup(pattern); + else { /* old + "|" + new + NUL */ len = strlen(ignore_pats) + strlen(pattern) + 2; - ignore_pats = erealloc(ignore_pats, len); + ignore_pats = xrealloc(ignore_pats, 1, len); strlcat(ignore_pats, "|", len); strlcat(ignore_pats, pattern, len); } diff --git a/usr.bin/diff/diffdir.c b/usr.bin/diff/diffdir.c index 764cd9d535a..bd5a7fcca08 100644 --- a/usr.bin/diff/diffdir.c +++ b/usr.bin/diff/diffdir.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diffdir.c,v 1.30 2005/06/15 18:44:01 millert Exp $ */ +/* $OpenBSD: diffdir.c,v 1.31 2007/05/29 18:24:56 ray Exp $ */ /* * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com> @@ -21,7 +21,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: diffdir.c,v 1.30 2005/06/15 18:44:01 millert Exp $"; +static const char rcsid[] = "$OpenBSD: diffdir.c,v 1.31 2007/05/29 18:24:56 ray Exp $"; #endif /* not lint */ #include <sys/param.h> @@ -39,6 +39,7 @@ static const char rcsid[] = "$OpenBSD: diffdir.c,v 1.30 2005/06/15 18:44:01 mill #include <unistd.h> #include "diff.h" +#include "xmalloc.h" static int dircompare(const void *, const void *); static int excluded(const char *); @@ -146,12 +147,12 @@ diffdir(char *p1, char *p2) } if (dirbuf1 != NULL) { - free(dirp1); - free(dirbuf1); + xfree(dirp1); + xfree(dirbuf1); } if (dirbuf2 != NULL) { - free(dirp2); - free(dirbuf2); + xfree(dirp2); + xfree(dirbuf2); } } @@ -190,20 +191,20 @@ slurpdir(char *path, char **bufp, int enoentok) need = roundup(sb.st_blksize, sizeof(struct dirent)); have = bufsize = roundup(MAX(sb.st_size, sb.st_blksize), sizeof(struct dirent)) + need; - ebuf = buf = emalloc(bufsize); + ebuf = buf = xmalloc(bufsize); do { if (have < need) { bufsize += need; have += need; - cp = erealloc(buf, bufsize); + cp = xrealloc(buf, 1, bufsize); ebuf = cp + (ebuf - buf); buf = cp; } nbytes = getdirentries(fd, ebuf, have, &base); if (nbytes == -1) { warn("%s", path); - free(buf); + xfree(buf); close(fd); return (NULL); } @@ -225,7 +226,7 @@ slurpdir(char *path, char **bufp, int enoentok) break; cp += dp->d_reclen; } - dirlist = emalloc(sizeof(struct dirent *) * (entries + 1)); + dirlist = xmalloc(sizeof(struct dirent *) * (entries + 1)); for (entries = 0, cp = buf; cp < ebuf; ) { dp = (struct dirent *)cp; if (dp->d_fileno != 0 && !excluded(dp->d_name)) { diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c index 7dc19b6a142..e215ba9ae56 100644 --- a/usr.bin/diff/diffreg.c +++ b/usr.bin/diff/diffreg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diffreg.c,v 1.67 2007/03/18 21:12:27 espie Exp $ */ +/* $OpenBSD: diffreg.c,v 1.68 2007/05/29 18:24:56 ray Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -65,7 +65,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: diffreg.c,v 1.67 2007/03/18 21:12:27 espie Exp $"; +static const char rcsid[] = "$OpenBSD: diffreg.c,v 1.68 2007/05/29 18:24:56 ray Exp $"; #endif /* not lint */ #include <sys/param.h> @@ -84,6 +84,7 @@ static const char rcsid[] = "$OpenBSD: diffreg.c,v 1.67 2007/03/18 21:12:27 espi #include "diff.h" #include "pathnames.h" +#include "xmalloc.h" /* * diff - compare two files. @@ -153,14 +154,14 @@ static const char rcsid[] = "$OpenBSD: diffreg.c,v 1.67 2007/03/18 21:12:27 espi */ struct cand { - int x; - int y; - int pred; + int x; + int y; + int pred; }; struct line { - int serial; - int value; + int serial; + int value; } *file[2]; /* @@ -169,10 +170,10 @@ struct line { * understand the highly mnemonic field names) */ struct context_vec { - int a; /* start line in old file */ - int b; /* end line in old file */ - int c; /* start line in new file */ - int d; /* end line in new file */ + int a; /* start line in old file */ + int b; /* end line in old file */ + int c; /* start line in new file */ + int d; /* end line in new file */ }; static int *J; /* will be overlaid on class */ @@ -225,8 +226,6 @@ static int isqrt(int); static int stone(int *, int, int *, int *); static int readhash(FILE *); static int files_differ(FILE *, FILE *, int); -static __inline int min(int, int); -static __inline int max(int, int); static char *match_function(const long *, int, FILE *); static char *preadline(int, size_t, off_t); @@ -374,7 +373,7 @@ diffreg(char *ofile1, char *ofile2, int flags) char *header; char *prargv[] = { "pr", "-h", NULL, "-f", NULL }; - easprintf(&header, "%s %s %s", diffargs, file1, file2); + xasprintf(&header, "%s %s %s", diffargs, file1, file2); prargv[2] = header; fflush(stdout); rewind(stdout); @@ -383,7 +382,7 @@ diffreg(char *ofile1, char *ofile2, int flags) case -1: warnx("No more processes"); status |= 2; - free(header); + xfree(header); return (D_ERROR); case 0: /* child */ @@ -403,7 +402,7 @@ diffreg(char *ofile1, char *ofile2, int flags) } close(pfd[0]); rewind(stdout); - free(header); + xfree(header); } } prepare(0, f1, stb1.st_size); @@ -414,27 +413,27 @@ diffreg(char *ofile1, char *ofile2, int flags) member = (int *)file[1]; equiv(sfile[0], slen[0], sfile[1], slen[1], member); - member = erealloc(member, (slen[1] + 2) * sizeof(int)); + member = xrealloc(member, slen[1] + 2, sizeof(int)); class = (int *)file[0]; unsort(sfile[0], slen[0], class); - class = erealloc(class, (slen[0] + 2) * sizeof(int)); + class = xrealloc(class, slen[0] + 2, sizeof(int)); - klist = emalloc((slen[0] + 2) * sizeof(int)); + klist = xmalloc((slen[0] + 2) * sizeof(int)); clen = 0; clistlen = 100; - clist = emalloc(clistlen * sizeof(struct cand)); + clist = xmalloc(clistlen * sizeof(struct cand)); i = stone(class, slen[0], member, klist); - free(member); - free(class); + xfree(member); + xfree(class); - J = erealloc(J, (len[0] + 2) * sizeof(int)); + J = xrealloc(J, len[0] + 2, sizeof(int)); unravel(klist[i]); - free(clist); - free(klist); + xfree(clist); + xfree(klist); - ixold = erealloc(ixold, (len[0] + 2) * sizeof(long)); - ixnew = erealloc(ixnew, (len[1] + 2) * sizeof(long)); + ixold = xrealloc(ixold, len[0] + 2, sizeof(long)); + ixnew = xrealloc(ixnew, len[1] + 2, sizeof(long)); check(file1, f1, file2, f2); output(file1, f1, file2, f2, (flags & D_HEADER)); if (ostdout != -1) { @@ -461,9 +460,9 @@ closem: if (f2 != NULL) fclose(f2); if (file1 != ofile1) - free(file1); + xfree(file1); if (file2 != ofile2) - free(file2); + xfree(file2); return (rval); } @@ -543,7 +542,7 @@ splice(char *dir, char *file) tail = file; else tail++; - easprintf(&buf, "%s/%s", dir, tail); + xasprintf(&buf, "%s/%s", dir, tail); return (buf); } @@ -560,11 +559,11 @@ prepare(int i, FILE *fd, off_t filesize) if (sz < 100) sz = 100; - p = emalloc((sz + 3) * sizeof(struct line)); + p = xmalloc((sz + 3) * sizeof(struct line)); for (j = 0; (h = readhash(fd));) { if (j == sz) { sz = sz * 3 / 2; - p = erealloc(p, (sz + 3) * sizeof(struct line)); + p = xrealloc(p, sz + 3, sizeof(struct line)); } p[++j].value = h; } @@ -628,7 +627,7 @@ isqrt(int n) int y, x = 1; if (n == 0) - return(0); + return (0); do { /* newton was a stinker */ y = x; @@ -647,7 +646,7 @@ stone(int *a, int n, int *b, int *c) int oldc, tc, oldl; u_int numtries; - const u_int bound = dflag ? UINT_MAX : max(256, isqrt(n)); + const u_int bound = dflag ? UINT_MAX : MAX(256, isqrt(n)); k = 0; c[0] = newcand(0, 0, 0); @@ -690,7 +689,7 @@ newcand(int x, int y, int pred) if (clen == clistlen) { clistlen = clistlen * 11 / 10; - clist = erealloc(clist, clistlen * sizeof(struct cand)); + clist = xrealloc(clist, clistlen, sizeof(struct cand)); } q = clist + clen; q->x = x; @@ -878,12 +877,12 @@ unsort(struct line *f, int l, int *b) { int *a, i; - a = emalloc((l + 1) * sizeof(int)); + a = xmalloc((l + 1) * sizeof(int)); for (i = 1; i <= l; i++) a[f[i].serial] = f[i].value; for (i = 1; i <= l; i++) b[i] = a[i]; - free(a); + xfree(a); } static int @@ -950,7 +949,7 @@ output(char *file1, FILE *f1, char *file2, FILE *f2, int flags) } } -static __inline void +static void range(int a, int b, char *separator) { printf("%d", a > b ? b : a); @@ -958,7 +957,7 @@ range(int a, int b, char *separator) printf("%s%d", separator, b); } -static __inline void +static void uni_range(int a, int b) { if (a < b) @@ -975,7 +974,7 @@ preadline(int fd, size_t len, off_t off) char *line; ssize_t nr; - line = emalloc(len + 1); + line = xmalloc(len + 1); if ((nr = pread(fd, line, len, off)) < 0) err(1, "preadline"); if (nr > 0 && line[nr-1] == '\n') @@ -990,7 +989,7 @@ ignoreline(char *line) int ret; ret = regexec(&ignore_re, line, 0, NULL, 0); - free(line); + xfree(line); return (ret == 0); /* if it matched, it should be ignored. */ } @@ -1048,8 +1047,8 @@ proceed: if (context_vec_ptr == context_vec_end - 1) { ptrdiff_t offset = context_vec_ptr - context_vec_start; max_context <<= 1; - context_vec_start = erealloc(context_vec_start, - max_context * sizeof(struct context_vec)); + context_vec_start = xrealloc(context_vec_start, + max_context, sizeof(struct context_vec)); context_vec_end = context_vec_start + max_context; context_vec_ptr = context_vec_start + offset; } @@ -1293,16 +1292,6 @@ asciifile(FILE *f) return (1); } -static __inline int min(int a, int b) -{ - return (a < b ? a : b); -} - -static __inline int max(int a, int b) -{ - return (a > b ? a : b); -} - #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0) static char * @@ -1364,10 +1353,10 @@ dump_context_vec(FILE *f1, FILE *f2) return; b = d = 0; /* gcc */ - lowa = max(1, cvp->a - context); - upb = min(len[0], context_vec_ptr->b + context); - lowc = max(1, cvp->c - context); - upd = min(len[1], context_vec_ptr->d + context); + lowa = MAX(1, cvp->a - context); + upb = MIN(len[0], context_vec_ptr->b + context); + lowc = MAX(1, cvp->c - context); + upd = MIN(len[1], context_vec_ptr->d + context); printf("***************"); if (pflag) { @@ -1469,10 +1458,10 @@ dump_unified_vec(FILE *f1, FILE *f2) return; b = d = 0; /* gcc */ - lowa = max(1, cvp->a - context); - upb = min(len[0], context_vec_ptr->b + context); - lowc = max(1, cvp->c - context); - upd = min(len[1], context_vec_ptr->d + context); + lowa = MAX(1, cvp->a - context); + upb = MIN(len[0], context_vec_ptr->b + context); + lowc = MAX(1, cvp->c - context); + upd = MIN(len[1], context_vec_ptr->d + context); fputs("@@ -", stdout); uni_range(lowa, upb); diff --git a/usr.bin/diff/xmalloc.c b/usr.bin/diff/xmalloc.c new file mode 100644 index 00000000000..125a6deea3b --- /dev/null +++ b/usr.bin/diff/xmalloc.c @@ -0,0 +1,91 @@ +/* $OpenBSD: xmalloc.c,v 1.1 2007/05/29 18:24:56 ray Exp $ */ +/* + * Author: Tatu Ylonen <ylo@cs.hut.fi> + * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland + * All rights reserved + * Versions of malloc and friends that check their results, and never return + * failure (they call fatal if they encounter an error). + * + * As far as I am concerned, the code I have written for this software + * can be used freely for any purpose. Any derived versions of this + * software must be clearly marked as such, and if the derived work is + * incompatible with the protocol description in the RFC file, it must be + * called by a name other than "ssh" or "Secure Shell". + */ + +#include <err.h> +#include <limits.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "xmalloc.h" + +void * +xmalloc(size_t size) +{ + void *ptr; + + if (size == 0) + errx(2, NULL); + ptr = malloc(size); + if (ptr == NULL) + errx(2, NULL); + return ptr; +} + +void * +xrealloc(void *ptr, size_t nmemb, size_t size) +{ + void *new_ptr; + size_t new_size = nmemb * size; + + if (new_size == 0) + errx(2, NULL); + if (SIZE_T_MAX / nmemb < size) + errx(2, NULL); + if (ptr == NULL) + new_ptr = malloc(new_size); + else + new_ptr = realloc(ptr, new_size); + if (new_ptr == NULL) + errx(2, NULL); + return new_ptr; +} + +void +xfree(void *ptr) +{ + if (ptr == NULL) + errx(2, NULL); + free(ptr); +} + +char * +xstrdup(const char *str) +{ + size_t len; + char *cp; + + len = strlen(str) + 1; + cp = xmalloc(len); + strlcpy(cp, str, len); + return cp; +} + +int +xasprintf(char **ret, const char *fmt, ...) +{ + va_list ap; + int i; + + va_start(ap, fmt); + i = vasprintf(ret, fmt, ap); + va_end(ap); + + if (i < 0 || *ret == NULL) + errx(2, NULL); + + return (i); +} diff --git a/usr.bin/diff/xmalloc.h b/usr.bin/diff/xmalloc.h new file mode 100644 index 00000000000..1a17ac107d8 --- /dev/null +++ b/usr.bin/diff/xmalloc.h @@ -0,0 +1,30 @@ +/* $OpenBSD: xmalloc.h,v 1.1 2007/05/29 18:24:56 ray Exp $ */ + +/* + * Author: Tatu Ylonen <ylo@cs.hut.fi> + * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland + * All rights reserved + * Created: Mon Mar 20 22:09:17 1995 ylo + * + * Versions of malloc and friends that check their results, and never return + * failure (they call fatal if they encounter an error). + * + * As far as I am concerned, the code I have written for this software + * can be used freely for any purpose. Any derived versions of this + * software must be clearly marked as such, and if the derived work is + * incompatible with the protocol description in the RFC file, it must be + * called by a name other than "ssh" or "Secure Shell". + */ + +#ifndef XMALLOC_H +#define XMALLOC_H + +void *xmalloc(size_t); +void *xrealloc(void *, size_t, size_t); +void xfree(void *); +char *xstrdup(const char *); +int xasprintf(char **, const char *, ...) + __attribute__((__format__ (printf, 2, 3))) + __attribute__((__nonnull__ (2))); + +#endif /* XMALLOC_H */ |