Date: 07 Jul 1999 20:57:16 +0200 From: Dag-Erling Smorgrav <des@flood.ping.uio.no> To: Jamie Howard <howardjp@wam.umd.edu> Cc: freebsd-hackers@FreeBSD.ORG, tech-userlevel@netbsd.org, tech@openbsd.org Subject: Re: Replacement for grep(1) (part 2) Message-ID: <xzp7locthir.fsf@flood.ping.uio.no> In-Reply-To: Jamie Howard's message of "Mon, 5 Jul 1999 21:14:36 -0400 (EDT)" References: <Pine.GSO.4.10.9907052110250.13873-100000@uther.wam.umd.edu>
next in thread | previous in thread | raw e-mail | index | archive | help
Jamie Howard <howardjp@wam.umd.edu> writes: > I incorporated a huge patch from Dag-Erling Smorgrav [...] Here's more :) BTW, I assume you've read this: <URL:http://www.opengroup.org/onlinepubs/007908799/xcu/grep.html> I see you switched to using extended regexps by default, and made -E a no-op; this breaks the ports collection, so I changed it back. Sort your switch cases. Don't use err() indiscriminately after a malloc() failure; malloc() doesn't set errno. Don't use calloc() needlessly. DES -- Dag-Erling Smorgrav - des@flood.ping.uio.no --- grep-0.3/grep.c Mon Jul 5 21:23:18 1999 +++ des/grep.c Wed Jul 7 20:53:46 1999 @@ -28,6 +28,7 @@ #include <sys/types.h> #include <sys/stat.h> + #include <err.h> #include <regex.h> #include <stdio.h> @@ -89,9 +90,6 @@ case 'G': Gflag++; break; - case 'Z': - Zflag++; - break; case 'L': lflag = 0; Lflag = qflag = 1; @@ -100,6 +98,9 @@ fprintf(stderr, "Jamie's grep version %u.%u\n", VER_MAJ, VER_MIN); usage(); break; + case 'Z': + Zflag++; + break; case 'a': aflag = 1; break; @@ -115,11 +116,11 @@ case 'f': if (stat(optarg, &patternstat)) err(2, "%s", optarg); - if ((pattern = calloc(1, patternstat.st_size + 1)) == NULL) - err(1, "calloc"); + pattern = grep_malloc(patternstat.st_size + 1); if ((patternfile = fopen(optarg, "r")) == NULL) err(2, "%s", optarg); fread(pattern, patternstat.st_size, 1, patternfile); + pattern[patternstat.st_size] = 0; break; case 'h': oflag = 0; @@ -192,40 +193,37 @@ cflags |= REG_NOSPEC; while (pattern != NULL) { patterns++; - if ((pat = realloc(pat, patterns * sizeof(regex_t))) == NULL) - err(1, "realloc"); + pat = grep_realloc(pat, patterns * sizeof(regex_t)); if ((c = regcomp(&pat[patterns - 1], tmp, cflags))) { fprintf(stderr, "%s\n", tmp); - regerror(c, pat, (char *)&re_error, RE_ERROR_BUF); + regerror(c, pat, re_error, RE_ERROR_BUF); err(2, re_error); } tmp = strsep(&pattern, "\n"); } } else { - cflags |= REG_EXTENDED; + cflags |= Eflag ? REG_EXTENDED : REG_BASIC; if (xflag) { - if ((realpat = malloc(strlen(pattern) + sizeof("^(") + - sizeof(")$") + 1)) == NULL) - err(1, "malloc"); + realpat = grep_malloc(strlen(pattern) + sizeof("^(") + + sizeof(")$") + 1); strcpy(realpat, "^("); strcat(realpat, pattern); strcat(realpat, ")$"); } else if (wflag) { - if ((realpat = malloc(strlen(pattern) + sizeof("[[:<:]]") + - sizeof("[[:>:]]") + 1)) == NULL) - err(1, "malloc"); + realpat = grep_malloc(strlen(pattern) + sizeof("[[:<:]]") + + sizeof("[[:>:]]") + 1); strcpy(realpat, "[[:<:]]"); strcat(realpat, pattern); strcat(realpat, "[[:>:]]"); - } + } else { realpat = pattern; - if((pat = malloc(sizeof(regex_t))) == NULL) - err(1, "malloc"); - if((c = regcomp(pat, realpat, cflags))) { - regerror(c, pat, (char *)&re_error, RE_ERROR_BUF); + } + pat = grep_malloc(sizeof(regex_t)); + if ((c = regcomp(pat, realpat, cflags))) { + regerror(c, pat, re_error, RE_ERROR_BUF); err(2, re_error); } - if(wflag) + if (xflag || wflag) free(realpat); patterns = 1; } @@ -233,9 +231,8 @@ if ((argc == 0 || argc == 1) && !oflag) hflag = 1; if (argc == 0) - exit(!procfile((char *)NULL)); - c = 0; - for (i = 0; i < argc; i++) { + exit(!procfile(NULL)); + for (c = i = 0; i < argc; i++) { c += procfile(argv[i]); } if (Fflag) --- grep-0.3/grep.h Mon Jul 5 14:25:46 1999 +++ des/grep.h Wed Jul 7 20:28:28 1999 @@ -66,3 +66,5 @@ int procline(str_t ln); int seekable(FILE *f); void usage(void); +void *grep_malloc(size_t size); +void *grep_realloc(void *ptr, size_t size); --- grep-0.3/util.c Mon Jul 5 17:50:56 1999 +++ des/util.c Wed Jul 7 20:55:57 1999 @@ -27,7 +27,9 @@ */ #include <sys/types.h> + #include <err.h> +#include <errno.h> #include <regex.h> #include <stdio.h> #include <stdlib.h> @@ -60,12 +62,11 @@ */ gzf = gzdopen(STDIN_FILENO, "rb"); fn = "-"; - } else - if (!(gzf = gzopen(fn, "r"))) { - if (!sflag) - warn("%s", fn); - return 0; - } + } else if ((gzf = gzopen(fn, "r")) == NULL) { + if (!sflag) + warn("%s", fn); + return 0; + } if (aflag && gzbin_file(gzf)) return 0; @@ -74,8 +75,7 @@ ln.off = gztell(gzf); if ((tmp = gzfgetln(gzf, &ln.len)) == NULL) break; - if ((ln.dat = malloc(ln.len + 1)) == NULL) - err(1, "malloc"); + ln.dat = grep_malloc(ln.len + 1); memcpy(ln.dat, tmp, ln.len); ln.dat[ln.len] = 0; ln.line_no++; @@ -88,12 +88,11 @@ if (fn == NULL) { f = stdin; fn = "-"; - } else - if (!(f = fopen(fn, "r"))) { - if (!sflag) - warn("%s", fn); - return 0; - } + } else if ((f = fopen(fn, "r")) == NULL) { + if (!sflag) + warn("%s", fn); + return 0; + } if (aflag && bin_file(f)) return 0; @@ -102,8 +101,7 @@ ln.off = ftell(f); if ((tmp = fgetln(f, &ln.len)) == NULL) break; - if ((ln.dat = malloc(ln.len + 1)) == NULL) - err(1, "malloc"); + ln.dat = grep_malloc(ln.len + 1); memcpy(ln.dat, tmp, ln.len); ln.dat[ln.len] = 0; ln.line_no++; @@ -179,4 +177,26 @@ s[n - 1] = '\0'; *len = n; return s; +} + +void * +grep_malloc(size_t size) +{ + void *ptr; + + if ((ptr = malloc(size)) == NULL) { + errno = ENOMEM; + err(1, "malloc"); + } + return ptr; +} + +void * +grep_realloc(void *ptr, size_t size) +{ + if ((ptr = realloc(ptr, size)) == NULL) { + errno = ENOMEM; + err(1, "realloc"); + } + return ptr; } To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?xzp7locthir.fsf>