util.c revision 332462
1/*
2 * Copyright (c) 1995 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
3 * Copyright (c) 1989, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * James A. Woods.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by the University of
20 *	California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * $FreeBSD: stable/11/usr.bin/locate/locate/util.c 332462 2018-04-13 02:40:10Z kevans $
38 */
39
40
41#include <stdlib.h>
42#include <string.h>
43#include <err.h>
44#include <sys/param.h>
45#include <arpa/inet.h>
46#include <stdio.h>
47
48#include "locate.h"
49
50char 	**colon(char **, char*, char*);
51char 	*patprep(char *);
52void print_matches(u_int);
53u_char 	*tolower_word(u_char *);
54int 	getwm(caddr_t);
55int 	getwf(FILE *);
56int	check_bigram_char(int);
57
58/*
59 * Validate bigram chars. If the test failed the database is corrupt
60 * or the database is obviously not a locate database.
61 */
62int
63check_bigram_char(ch)
64	int ch;
65{
66	/* legal bigram: 0, ASCII_MIN ... ASCII_MAX */
67	if (ch == 0 ||
68	    (ch >= ASCII_MIN && ch <= ASCII_MAX))
69		return(ch);
70
71	errx(1,
72		"locate database header corrupt, bigram char outside 0, %d-%d: %d",
73		ASCII_MIN, ASCII_MAX, ch);
74	exit(1);
75}
76
77/* split a colon separated string into a char vector
78 *
79 * "bla:foo" -> {"foo", "bla"}
80 * "bla:"    -> {"foo", dot}
81 * "bla"     -> {"bla"}
82 * ""	     -> do nothing
83 *
84 */
85char **
86colon(char **dbv, char *path, char *dot)
87{
88	int vlen, slen;
89	char *c, *ch, *p;
90	char **pv;
91
92	if (dbv == NULL) {
93		if ((dbv = malloc(sizeof(char *))) == NULL)
94			err(1, "malloc");
95		*dbv = NULL;
96	}
97
98	/* empty string */
99	if (*path == '\0') {
100		warnx("empty database name, ignored");
101		return(dbv);
102	}
103
104	/* length of string vector */
105	for(vlen = 0, pv = dbv; *pv != NULL; pv++, vlen++);
106
107	for (ch = c = path; ; ch++) {
108		if (*ch == ':' ||
109		    (!*ch && !(*(ch - 1) == ':' && ch == 1+ path))) {
110			/* single colon -> dot */
111			if (ch == c)
112				p = dot;
113			else {
114				/* a string */
115				slen = ch - c;
116				if ((p = malloc(sizeof(char) * (slen + 1)))
117				    == NULL)
118					err(1, "malloc");
119				bcopy(c, p, slen);
120				*(p + slen) = '\0';
121			}
122			/* increase dbv with element p */
123			if ((dbv = realloc(dbv, sizeof(char *) * (vlen + 2)))
124			    == NULL)
125				err(1, "realloc");
126			*(dbv + vlen) = p;
127			*(dbv + ++vlen) = NULL;
128			c = ch + 1;
129		}
130		if (*ch == '\0')
131			break;
132	}
133	return (dbv);
134}
135
136void
137print_matches(counter)
138	u_int counter;
139{
140	(void)printf("%d\n", counter);
141}
142
143
144/*
145 * extract last glob-free subpattern in name for fast pre-match; prepend
146 * '\0' for backwards match; return end of new pattern
147 */
148static char globfree[100];
149
150char *
151patprep(name)
152	char *name;
153{
154	register char *endmark, *p, *subp;
155
156	subp = globfree;
157	*subp++ = '\0';   /* set first element to '\0' */
158	p = name + strlen(name) - 1;
159
160	/* skip trailing metacharacters */
161	for (; p >= name; p--)
162		if (strchr(LOCATE_REG, *p) == NULL)
163			break;
164
165	/*
166	 * check if maybe we are in a character class
167	 *
168	 * 'foo.[ch]'
169	 *        |----< p
170	 */
171	if (p >= name &&
172	    (strchr(p, '[') != NULL || strchr(p, ']') != NULL)) {
173		for (p = name; *p != '\0'; p++)
174			if (*p == ']' || *p == '[')
175				break;
176		p--;
177
178		/*
179		 * cannot find a non-meta character, give up
180		 * '*\*[a-z]'
181		 *    |-------< p
182		 */
183		if (p >= name && strchr(LOCATE_REG, *p) != NULL)
184			p = name - 1;
185	}
186
187	if (p < name)
188		/* only meta chars: "???", force '/' search */
189		*subp++ = '/';
190
191	else {
192		for (endmark = p; p >= name; p--)
193			if (strchr(LOCATE_REG, *p) != NULL)
194				break;
195		for (++p;
196		    (p <= endmark) && subp < (globfree + sizeof(globfree));)
197			*subp++ = *p++;
198	}
199	*subp = '\0';
200	return(--subp);
201}
202
203/* tolower word */
204u_char *
205tolower_word(word)
206	u_char *word;
207{
208	register u_char *p;
209
210	for(p = word; *p != '\0'; p++)
211		*p = TOLOWER(*p);
212
213	return(word);
214}
215
216
217/*
218 * Read integer from mmap pointer.
219 * Essentially a simple ``return *(int *)p'' but avoids sigbus
220 * for integer alignment (SunOS 4.x, 5.x).
221 *
222 * Convert network byte order to host byte order if necessary.
223 * So we can read a locate database on FreeBSD/i386 (little endian)
224 * which was built on SunOS/sparc (big endian).
225 */
226
227int
228getwm(p)
229	caddr_t p;
230{
231	union {
232		char buf[INTSIZE];
233		int i;
234	} u;
235	register int i, hi;
236
237	for (i = 0; i < (int)INTSIZE; i++)
238		u.buf[i] = *p++;
239
240	i = u.i;
241
242	if (i > MAXPATHLEN || i < -(MAXPATHLEN)) {
243		hi = ntohl(i);
244		if (hi > MAXPATHLEN || hi < -(MAXPATHLEN))
245			errx(1, "integer out of +-MAXPATHLEN (%d): %u",
246			    MAXPATHLEN, abs(i) < abs(hi) ? i : hi);
247		return(hi);
248	}
249	return(i);
250}
251
252/*
253 * Read integer from stream.
254 *
255 * Convert network byte order to host byte order if necessary.
256 * So we can read on FreeBSD/i386 (little endian) a locate database
257 * which was built on SunOS/sparc (big endian).
258 */
259
260int
261getwf(fp)
262	FILE *fp;
263{
264	register int word, hword;
265
266	word = getw(fp);
267
268	if (word > MAXPATHLEN || word < -(MAXPATHLEN)) {
269		hword = ntohl(word);
270		if (hword > MAXPATHLEN || hword < -(MAXPATHLEN))
271			errx(1, "integer out of +-MAXPATHLEN (%d): %u",
272			    MAXPATHLEN, abs(word) < abs(hword) ? word : hword);
273		return(hword);
274	}
275	return(word);
276}
277