-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlook.c
228 lines (206 loc) · 6.11 KB
/
look.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* $NetBSD: look.c,v 1.2 2005/06/30 16:25:05 christos Exp $ */
/* derived from: OpenBSD: look.c,v 1.3 2003/06/03 02:56:16 millert Exp */
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* David Hitz of Auspex Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
#if 0
static const char sccsid[] = "@(#)look.c 8.2 (Berkeley) 5/4/95";
#endif
static const char rcsid[] = "$NetBSD: look.c,v 1.2 2005/06/30 16:25:05 christos Exp $";
#endif /* not lint */
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
static u_char *binary_search(u_char *, u_char *, u_char *);
static u_char *linear_search(u_char *, u_char *, u_char *);
static int compare(u_char *, u_char *, u_char *);
char *
look(u_char *string, u_char *front, u_char *back)
{
u_char *s;
/* Convert string to lower case before searching. */
for (s = string; *s; s++) {
if (isupper(*s))
*s = _tolower(*s);
}
front = binary_search(string, front, back);
front = linear_search(string, front, back);
return front;
}
long
get_count(u_char *front, char delim)
{
size_t retsize = 10;
char *tab = strchr(front, delim);
char *ret = malloc(retsize);
if (ret == NULL)
err(EXIT_FAILURE, "malloc failed");
size_t offset = 0;
assert (tab != NULL);
tab++;
while (*tab != '\n') {
if (offset == retsize) {
retsize *= 2;
ret = realloc(ret, retsize);
if (ret == NULL)
err(EXIT_FAILURE, "malloc failed");
}
ret[offset++] = *tab++;
}
ret[offset] = 0;
long retval = strtol(ret, NULL, 0);
free(ret);
return retval;
}
/*
* Binary search for "string" in memory between "front" and "back".
*
* This routine is expected to return a pointer to the start of a line at
* *or before* the first word matching "string". Relaxing the constraint
* this way simplifies the algorithm.
*
* Invariants:
* front points to the beginning of a line at or before the first
* matching string.
*
* back points to the beginning of a line at or after the first
* matching line.
*
* Base of the Invariants.
* front = NULL;
* back = EOF;
*
* Advancing the Invariants:
*
* p = first newline after halfway point from front to back.
*
* If the string at "p" is not greater than the string to match,
* p is the new front. Otherwise it is the new back.
*
* Termination:
*
* The definition of the routine allows it return at any point,
* since front is always at or before the line to print.
*
* In fact, it returns when the chosen "p" equals "back". This
* implies that there exists a string is least half as long as
* (back - front), which in turn implies that a linear search will
* be no more expensive than the cost of simply printing a string or two.
*
* Trying to continue with binary search at this point would be
* more trouble than it's worth.
*/
#define SKIP_PAST_NEWLINE(p, back) \
while (p < back && *p++ != '\n') \
continue;
static u_char *
binary_search(u_char *string, u_char *front, u_char *back)
{
u_char *p;
p = front + (back - front) / 2;
SKIP_PAST_NEWLINE(p, back);
/*
* If the file changes underneath us, make sure we don't
* infinitely loop.
*/
while (p < back && back > front) {
if (compare(string, p, back) > 0)
front = p;
else
back = p;
p = front + (back - front) / 2;
SKIP_PAST_NEWLINE(p, back);
}
return (front);
}
/*
* Find the first line that matches string, linearly searching from front
* to back.
*
* Return NULL for no such line.
*
* This routine assumes:
*
* o front points at the first character in a line.
* o front is before or at the first line to be printed.
*/
static u_char *
linear_search(u_char *string, u_char *front, u_char *back)
{
int result;
while (front < back) {
result = compare(string, front, back);
if (result == 0)
return (front); /* found it */
if (result < 0)
return (NULL); /* not there */
SKIP_PAST_NEWLINE(front, back);
}
return (NULL);
}
static int
compare(u_char *s1, u_char *s2, u_char *back)
{
int ch;
/* Note that s1 is already upper case. */
for (;; ++s1, ++s2) {
if (*s2 == '\t' || s2 == back)
ch = '\0';
else if (isupper(*s2))
ch = _tolower(*s2);
else
ch = *s2;
if (*s1 != ch)
return (*s1 - ch);
if (ch == '\0')
return (0);
}
}
/*
int
main(int argc, char **argv)
{
int fd = open("dict/unigram.txt", 'r');
struct stat sb;
fstat(fd, &sb);
char *front = mmap(NULL, (size_t) sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
char *back = front + (size_t) sb.st_size;
look(argv[1], front, back);
}*/