coverity appeasement - redundant check
[minix.git] / commands / grep / mmfile.c
blob8cdfefe21918f0e25e722ad7c2c8a1ea45904193
1 #ifndef __minix
2 /* $OpenBSD: mmfile.c,v 1.11 2006/09/19 05:52:23 otto Exp $ */
4 /*-
5 * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #include <sys/param.h>
31 #include <sys/mman.h>
32 #include <sys/stat.h>
34 #include <err.h>
35 #include <fcntl.h>
36 #include <stdlib.h>
37 #include <unistd.h>
39 #include "grep.h"
41 #define MAX_MAP_LEN 1048576
43 mmf_t *
44 mmopen(char *fn, char *mode)
46 mmf_t *mmf;
47 struct stat st;
49 /* XXX ignore mode for now */
50 mode = mode;
52 mmf = grep_malloc(sizeof *mmf);
53 if ((mmf->fd = open(fn, O_RDONLY)) == -1)
54 goto ouch1;
55 if (fstat(mmf->fd, &st) == -1)
56 goto ouch2;
57 if (st.st_size > SIZE_T_MAX) /* too big to mmap */
58 goto ouch2;
59 if (!S_ISREG(st.st_mode)) /* only mmap regular files */
60 goto ouch2;
61 mmf->len = (size_t)st.st_size;
62 mmf->base = mmap(NULL, mmf->len, PROT_READ, MAP_PRIVATE, mmf->fd, (off_t)0);
63 if (mmf->base == MAP_FAILED)
64 goto ouch2;
65 mmf->ptr = mmf->base;
66 mmf->end = mmf->base + mmf->len;
67 #ifndef __minix
68 madvise(mmf->base, mmf->len, MADV_SEQUENTIAL);
69 #endif
70 return mmf;
72 ouch2:
73 close(mmf->fd);
74 ouch1:
75 free(mmf);
76 return NULL;
79 void
80 mmclose(mmf_t *mmf)
82 munmap(mmf->base, mmf->len);
83 close(mmf->fd);
84 free(mmf);
87 char *
88 mmfgetln(mmf_t *mmf, size_t *l)
90 static char *p;
91 char *start = mmf->ptr; /* Remove speed bump */
92 char *end = mmf->end; /* Remove speed bump */
94 if (start >= end)
95 return NULL;
96 for (p = mmf->ptr; mmf->ptr < mmf->end; ++mmf->ptr)
97 if (*mmf->ptr == '\n')
98 break;
100 *l = mmf->ptr - p;
101 ++mmf->ptr;
102 return p;
104 #endif