From owner-freebsd-hackers Wed Jul 7 11:58: 2 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from flood.ping.uio.no (flood.ping.uio.no [129.240.78.31]) by hub.freebsd.org (Postfix) with ESMTP id E68761544E for ; Wed, 7 Jul 1999 11:57:51 -0700 (PDT) (envelope-from des@flood.ping.uio.no) Received: (from des@localhost) by flood.ping.uio.no (8.9.3/8.9.1) id UAA90456; Wed, 7 Jul 1999 20:57:16 +0200 (CEST) (envelope-from des) To: Jamie Howard Cc: freebsd-hackers@FreeBSD.ORG, tech-userlevel@netbsd.org, tech@openbsd.org Subject: Re: Replacement for grep(1) (part 2) References: From: Dag-Erling Smorgrav Date: 07 Jul 1999 20:57:16 +0200 In-Reply-To: Jamie Howard's message of "Mon, 5 Jul 1999 21:14:36 -0400 (EDT)" Message-ID: Lines: 236 X-Mailer: Gnus v5.5/Emacs 19.34 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Jamie Howard writes: > I incorporated a huge patch from Dag-Erling Smorgrav [...] Here's more :) BTW, I assume you've read this: 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 #include + #include #include #include @@ -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 + #include +#include #include #include #include @@ -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