Home | History | Annotate | Line # | Download | only in grep
file.c revision 1.1
      1  1.1  cjep /*-
      2  1.1  cjep  * Copyright (c) 1999 James Howard and Dag-Erling Codan Smrgrav
      3  1.1  cjep  * All rights reserved.
      4  1.1  cjep  *
      5  1.1  cjep  * Redistribution and use in source and binary forms, with or without
      6  1.1  cjep  * modification, are permitted provided that the following conditions
      7  1.1  cjep  * are met:
      8  1.1  cjep  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cjep  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cjep  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cjep  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cjep  *    documentation and/or other materials provided with the distribution.
     13  1.1  cjep  *
     14  1.1  cjep  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     15  1.1  cjep  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  1.1  cjep  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     17  1.1  cjep  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     18  1.1  cjep  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     19  1.1  cjep  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     20  1.1  cjep  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     21  1.1  cjep  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     22  1.1  cjep  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     23  1.1  cjep  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     24  1.1  cjep  * SUCH DAMAGE.
     25  1.1  cjep  *
     26  1.1  cjep  *	$Id: file.c,v 1.1 2004/01/02 14:58:45 cjep Exp $
     27  1.1  cjep  */
     28  1.1  cjep 
     29  1.1  cjep #include <sys/param.h>
     30  1.1  cjep 
     31  1.1  cjep #include <err.h>
     32  1.1  cjep #include <stdio.h>
     33  1.1  cjep #include <stdlib.h>
     34  1.1  cjep #include <zlib.h>
     35  1.1  cjep 
     36  1.1  cjep #include "grep.h"
     37  1.1  cjep 
     38  1.1  cjep static char	 fname[MAXPATHLEN];
     39  1.1  cjep static char	*lnbuf;
     40  1.1  cjep static int	 lnbuflen;
     41  1.1  cjep 
     42  1.1  cjep #define FILE_STDIO	0
     43  1.1  cjep #define FILE_MMAP	1
     44  1.1  cjep #define FILE_GZIP	2
     45  1.1  cjep 
     46  1.1  cjep struct file {
     47  1.1  cjep 	int	 type;
     48  1.1  cjep 	FILE	*f;
     49  1.1  cjep 	mmf_t	*mmf;
     50  1.1  cjep 	gzFile	*gzf;
     51  1.1  cjep };
     52  1.1  cjep 
     53  1.1  cjep static char *
     54  1.1  cjep gzfgetln(gzFile *f, size_t *len)
     55  1.1  cjep {
     56  1.1  cjep 	size_t		n;
     57  1.1  cjep 	int		c;
     58  1.1  cjep 
     59  1.1  cjep 	for (n = 0; ; ++n) {
     60  1.1  cjep 		c = gzgetc(f);
     61  1.1  cjep 		if (c == -1) {
     62  1.1  cjep 			const char *gzerrstr;
     63  1.1  cjep 			int gzerr;
     64  1.1  cjep 
     65  1.1  cjep 			if (gzeof(f))
     66  1.1  cjep 				break;
     67  1.1  cjep 
     68  1.1  cjep 			gzerrstr = gzerror(f, &gzerr);
     69  1.1  cjep 			if (gzerr == Z_ERRNO)
     70  1.1  cjep 				err(1, "%s", fname);
     71  1.1  cjep 			else
     72  1.1  cjep 				errx(1, "%s: %s", fname, gzerrstr);
     73  1.1  cjep 		}
     74  1.1  cjep 		if (c == '\n')
     75  1.1  cjep 			break;
     76  1.1  cjep 		if (n >= lnbuflen) {
     77  1.1  cjep 			lnbuflen *= 2;
     78  1.1  cjep 			lnbuf = grep_realloc(lnbuf, ++lnbuflen);
     79  1.1  cjep 		}
     80  1.1  cjep 		lnbuf[n] = c;
     81  1.1  cjep 	}
     82  1.1  cjep 
     83  1.1  cjep 	if (gzeof(f) && n == 0)
     84  1.1  cjep 		return NULL;
     85  1.1  cjep 	*len = n;
     86  1.1  cjep 	return lnbuf;
     87  1.1  cjep }
     88  1.1  cjep 
     89  1.1  cjep file_t *
     90  1.1  cjep grep_fdopen(int fd, char *mode)
     91  1.1  cjep {
     92  1.1  cjep 	file_t *f;
     93  1.1  cjep 
     94  1.1  cjep 	if (fd == 0)
     95  1.1  cjep 		sprintf(fname, "(standard input)");
     96  1.1  cjep 	else
     97  1.1  cjep 		sprintf(fname, "(fd %d)", fd);
     98  1.1  cjep 
     99  1.1  cjep 	f = grep_malloc(sizeof *f);
    100  1.1  cjep 
    101  1.1  cjep 	if (Zflag) {
    102  1.1  cjep 		f->type = FILE_GZIP;
    103  1.1  cjep 		if ((f->gzf = gzdopen(fd, mode)) != NULL)
    104  1.1  cjep 			return f;
    105  1.1  cjep 	} else {
    106  1.1  cjep 		f->type = FILE_STDIO;
    107  1.1  cjep 		if ((f->f = fdopen(fd, mode)) != NULL)
    108  1.1  cjep 			return f;
    109  1.1  cjep 	}
    110  1.1  cjep 
    111  1.1  cjep 	free(f);
    112  1.1  cjep 	return NULL;
    113  1.1  cjep }
    114  1.1  cjep 
    115  1.1  cjep file_t *
    116  1.1  cjep grep_open(char *path, char *mode)
    117  1.1  cjep {
    118  1.1  cjep 	file_t *f;
    119  1.1  cjep 
    120  1.1  cjep 	snprintf(fname, MAXPATHLEN, "%s", path);
    121  1.1  cjep 
    122  1.1  cjep 	f = grep_malloc(sizeof *f);
    123  1.1  cjep 
    124  1.1  cjep 	if (Zflag) {
    125  1.1  cjep 		f->type = FILE_GZIP;
    126  1.1  cjep 		if ((f->gzf = gzopen(fname, mode)) != NULL)
    127  1.1  cjep 			return f;
    128  1.1  cjep 	} else {
    129  1.1  cjep 		/* try mmap first; if it fails, try stdio */
    130  1.1  cjep 		if ((f->mmf = mmopen(fname, mode)) != NULL) {
    131  1.1  cjep 			f->type = FILE_MMAP;
    132  1.1  cjep 			return f;
    133  1.1  cjep 		}
    134  1.1  cjep 		f->type = FILE_STDIO;
    135  1.1  cjep 		if ((f->f = fopen(path, mode)) != NULL)
    136  1.1  cjep 			return f;
    137  1.1  cjep 	}
    138  1.1  cjep 
    139  1.1  cjep 	free(f);
    140  1.1  cjep 	return NULL;
    141  1.1  cjep }
    142  1.1  cjep 
    143  1.1  cjep int
    144  1.1  cjep grep_bin_file(file_t *f)
    145  1.1  cjep {
    146  1.1  cjep 	switch (f->type) {
    147  1.1  cjep 	case FILE_STDIO:
    148  1.1  cjep 		return bin_file(f->f);
    149  1.1  cjep 	case FILE_MMAP:
    150  1.1  cjep 		return mmbin_file(f->mmf);
    151  1.1  cjep 	case FILE_GZIP:
    152  1.1  cjep 		return gzbin_file(f->gzf);
    153  1.1  cjep 	default:
    154  1.1  cjep 		/* can't happen */
    155  1.1  cjep 		errx(1, "invalid file type");
    156  1.1  cjep 	}
    157  1.1  cjep }
    158  1.1  cjep 
    159  1.1  cjep long
    160  1.1  cjep grep_tell(file_t *f)
    161  1.1  cjep {
    162  1.1  cjep 	switch (f->type) {
    163  1.1  cjep 	case FILE_STDIO:
    164  1.1  cjep 		return ftell(f->f);
    165  1.1  cjep 	case FILE_MMAP:
    166  1.1  cjep 		return mmtell(f->mmf);
    167  1.1  cjep 	case FILE_GZIP:
    168  1.1  cjep 		return gztell(f->gzf);
    169  1.1  cjep 	default:
    170  1.1  cjep 		/* can't happen */
    171  1.1  cjep 		errx(1, "invalid file type");
    172  1.1  cjep 	}
    173  1.1  cjep }
    174  1.1  cjep 
    175  1.1  cjep char *
    176  1.1  cjep grep_fgetln(file_t *f, size_t *l)
    177  1.1  cjep {
    178  1.1  cjep 	switch (f->type) {
    179  1.1  cjep 	case FILE_STDIO:
    180  1.1  cjep 		return fgetln(f->f, l);
    181  1.1  cjep 	case FILE_MMAP:
    182  1.1  cjep 		return mmfgetln(f->mmf, l);
    183  1.1  cjep 	case FILE_GZIP:
    184  1.1  cjep 		return gzfgetln(f->gzf, l);
    185  1.1  cjep 	default:
    186  1.1  cjep 		/* can't happen */
    187  1.1  cjep 		errx(1, "invalid file type");
    188  1.1  cjep 	}
    189  1.1  cjep }
    190  1.1  cjep 
    191  1.1  cjep void
    192  1.1  cjep grep_close(file_t *f)
    193  1.1  cjep {
    194  1.1  cjep 	switch (f->type) {
    195  1.1  cjep 	case FILE_STDIO:
    196  1.1  cjep 		fclose(f->f);
    197  1.1  cjep 		break;
    198  1.1  cjep 	case FILE_MMAP:
    199  1.1  cjep 		mmclose(f->mmf);
    200  1.1  cjep 		break;
    201  1.1  cjep 	case FILE_GZIP:
    202  1.1  cjep 		gzclose(f->gzf);
    203  1.1  cjep 		break;
    204  1.1  cjep 	default:
    205  1.1  cjep 		/* can't happen */
    206  1.1  cjep 		errx(1, "invalid file type");
    207  1.1  cjep 	}
    208  1.1  cjep }
    209