From owner-freebsd-hackers Wed Jul 7 12:15:20 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 67CDC153ED for ; Wed, 7 Jul 1999 12:15:15 -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 VAA90956; Wed, 7 Jul 1999 21:15:06 +0200 (CEST) (envelope-from des) To: Dag-Erling Smorgrav Cc: Jamie Howard , 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 21:15:05 +0200 In-Reply-To: Dag-Erling Smorgrav's message of "07 Jul 1999 20:57:16 +0200" Message-ID: Lines: 240 X-Mailer: Gnus v5.5/Emacs 19.34 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Dag-Erling Smorgrav writes: > Jamie Howard writes: > > I incorporated a huge patch from Dag-Erling Smorgrav [...] > Here's more :) Following up on myself, I introduced a bug in the previous round of patches; here's a corrected patch against 0.3. (the bug was that grep would always bail out if no pattern was specified after the options, even if one was provided with the -e or -F option) 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 21:13:29 1999 @@ -28,6 +28,7 @@ #include #include + #include #include #include @@ -89,17 +90,18 @@ case 'G': Gflag++; break; - case 'Z': - Zflag++; - break; case 'L': lflag = 0; Lflag = qflag = 1; break; case 'V': fprintf(stderr, "Jamie's grep version %u.%u\n", VER_MAJ, VER_MIN); + fprintf(stderr, argv[0]); usage(); break; + case 'Z': + Zflag++; + break; case 'a': aflag = 1; break; @@ -115,11 +117,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; @@ -162,7 +164,7 @@ argc -= optind; argv += optind; - if (argc == 0) + if (argc == 0 && !pattern) usage(); if (!pattern) { @@ -192,40 +194,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 +232,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