2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1995 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 #include <sys/param.h>
47 #include <arpa/inet.h>
52 char **colon(char **, char*, char*);
53 char *patprep(char *);
54 void print_matches(u_int);
55 u_char *tolower_word(u_char *);
58 int check_bigram_char(int);
61 * Validate bigram chars. If the test failed the database is corrupt
62 * or the database is obviously not a locate database.
68 /* legal bigram: 0, ASCII_MIN ... ASCII_MAX */
70 (ch >= ASCII_MIN && ch <= ASCII_MAX))
74 "locate database header corrupt, bigram char outside 0, %d-%d: %d",
75 ASCII_MIN, ASCII_MAX, ch);
79 /* split a colon separated string into a char vector
81 * "bla:foo" -> {"foo", "bla"}
82 * "bla:" -> {"foo", dot}
88 colon(char **dbv, char *path, char *dot)
95 if ((dbv = malloc(sizeof(char *))) == NULL)
102 warnx("empty database name, ignored");
106 /* length of string vector */
107 for(vlen = 0, pv = dbv; *pv != NULL; pv++, vlen++);
109 for (ch = c = path; ; ch++) {
111 (!*ch && !(*(ch - 1) == ':' && ch == 1+ path))) {
112 /* single colon -> dot */
118 if ((p = malloc(sizeof(char) * (slen + 1)))
124 /* increase dbv with element p */
125 if ((dbv = realloc(dbv, sizeof(char *) * (vlen + 2)))
129 *(dbv + ++vlen) = NULL;
139 print_matches(counter)
142 (void)printf("%d\n", counter);
147 * extract last glob-free subpattern in name for fast pre-match; prepend
148 * '\0' for backwards match; return end of new pattern
150 static char globfree[100];
156 register char *endmark, *p, *subp;
159 *subp++ = '\0'; /* set first element to '\0' */
160 p = name + strlen(name) - 1;
162 /* skip trailing metacharacters */
163 for (; p >= name; p--)
164 if (strchr(LOCATE_REG, *p) == NULL)
168 * check if maybe we are in a character class
174 (strchr(p, '[') != NULL || strchr(p, ']') != NULL)) {
175 for (p = name; *p != '\0'; p++)
176 if (*p == ']' || *p == '[')
181 * cannot find a non-meta character, give up
185 if (p >= name && strchr(LOCATE_REG, *p) != NULL)
190 /* only meta chars: "???", force '/' search */
194 for (endmark = p; p >= name; p--)
195 if (strchr(LOCATE_REG, *p) != NULL)
198 (p <= endmark) && subp < (globfree + sizeof(globfree));)
212 for(p = word; *p != '\0'; p++)
220 * Read integer from mmap pointer.
221 * Essentially a simple ``return *(int *)p'' but avoids sigbus
222 * for integer alignment (SunOS 4.x, 5.x).
224 * Convert network byte order to host byte order if necessary.
225 * So we can read a locate database on FreeBSD/i386 (little endian)
226 * which was built on SunOS/sparc (big endian).
239 for (i = 0; i < (int)INTSIZE; i++)
244 if (i > MAXPATHLEN || i < -(MAXPATHLEN)) {
246 if (hi > MAXPATHLEN || hi < -(MAXPATHLEN))
247 errx(1, "integer out of +-MAXPATHLEN (%d): %u",
248 MAXPATHLEN, abs(i) < abs(hi) ? i : hi);
255 * Read integer from stream.
257 * Convert network byte order to host byte order if necessary.
258 * So we can read on FreeBSD/i386 (little endian) a locate database
259 * which was built on SunOS/sparc (big endian).
266 register int word, hword;
270 if (word > MAXPATHLEN || word < -(MAXPATHLEN)) {
272 if (hword > MAXPATHLEN || hword < -(MAXPATHLEN))
273 errx(1, "integer out of +-MAXPATHLEN (%d): %u",
274 MAXPATHLEN, abs(word) < abs(hword) ? word : hword);