Merge branch 'vendor/OPENSSH'
[dragonfly.git] / usr.bin / locate / locate / util.c
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: src/usr.bin/locate/locate/util.c,v 1.6.2.1 2001/12/20 04:21:48 mikeh Exp $
38  * $DragonFly: src/usr.bin/locate/locate/util.c,v 1.4 2005/08/04 17:31:23 drhodus Exp $
39  */
40
41
42 #include <stdlib.h>
43 #include <string.h>
44 #include <err.h>
45 #include <sys/param.h>
46 #include <stdio.h>
47
48 #include "locate.h"
49
50 char    **colon(char **, char*, char*);
51 char    *patprep(char *);
52 void print_matches(u_int);
53 u_char  *tolower_word(u_char *);
54 int     getwm(caddr_t);
55 int     getwf(FILE *);
56 int     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  */
62 int
63 check_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  */
85 char **
86 colon(dbv, path, dot)
87         char **dbv;
88         char *path;
89         char *dot; /* default for single ':' */
90 {
91         int vlen, slen;
92         char *c, *ch, *p;
93         char **pv;
94
95         if (dbv == NULL) {
96                 if ((dbv = malloc(sizeof(char **))) == NULL)
97                         err(1, "malloc");
98                 *dbv = NULL;
99         }
100
101         /* empty string */
102         if (*path == '\0') {
103                 warnx("empty database name, ignored");
104                 return(dbv);
105         }
106
107         /* length of string vector */
108         for(vlen = 0, pv = dbv; *pv != NULL; pv++, vlen++);
109
110         for (ch = c = path; ; ch++) {
111                 if (*ch == ':' ||
112                     (!*ch && !(*(ch - 1) == ':' && ch == 1+ path))) {
113                         /* single colon -> dot */
114                         if (ch == c)
115                                 p = dot;
116                         else {
117                                 /* a string */
118                                 slen = ch - c;
119                                 if ((p = malloc(sizeof(char) * (slen + 1))) 
120                                     == NULL)
121                                         err(1, "malloc");
122                                 bcopy(c, p, slen);
123                                 *(p + slen) = '\0';
124                         }
125                         /* increase dbv with element p */
126                         if ((dbv = realloc(dbv, sizeof(char **) * (vlen + 2)))
127                             == NULL)
128                                 err(1, "realloc");
129                         *(dbv + vlen) = p;
130                         *(dbv + ++vlen) = NULL;
131                         c = ch + 1;
132                 }
133                 if (*ch == '\0')
134                         break;
135         }
136         return (dbv);
137 }
138
139 void 
140 print_matches(counter)
141         u_int counter;
142 {
143         (void)printf("%d\n", counter);
144 }
145
146
147 /*
148  * extract last glob-free subpattern in name for fast pre-match; prepend
149  * '\0' for backwards match; return end of new pattern
150  */
151 static char globfree[100];
152
153 char *
154 patprep(name)
155         char *name;
156 {
157         char *endmark, *p, *subp;
158
159         subp = globfree;
160         *subp++ = '\0';   /* set first element to '\0' */
161         p = name + strlen(name) - 1;
162
163         /* skip trailing metacharacters */
164         for (; p >= name; p--)
165                 if (index(LOCATE_REG, *p) == NULL)
166                         break;
167
168         /* 
169          * check if maybe we are in a character class
170          *
171          * 'foo.[ch]'
172          *        |----< p
173          */
174         if (p >= name && 
175             (index(p, '[') != NULL || index(p, ']') != NULL)) {
176                 for (p = name; *p != '\0'; p++)
177                         if (*p == ']' || *p == '[')
178                                 break;
179                 p--;
180
181                 /* 
182                  * cannot find a non-meta character, give up
183                  * '*\*[a-z]'
184                  *    |-------< p
185                  */
186                 if (p >= name && index(LOCATE_REG, *p) != NULL)
187                         p = name - 1;
188         }
189         
190         if (p < name)                   
191                 /* only meta chars: "???", force '/' search */
192                 *subp++ = '/';
193
194         else {
195                 for (endmark = p; p >= name; p--)
196                         if (index(LOCATE_REG, *p) != NULL)
197                                 break;
198                 for (++p;
199                     (p <= endmark) && subp < (globfree + sizeof(globfree));)
200                         *subp++ = *p++;
201         }
202         *subp = '\0';
203         return(--subp);
204 }
205
206 /* tolower word */
207 u_char *
208 tolower_word(word)
209         u_char *word;
210 {
211         u_char *p;
212
213         for(p = word; *p != '\0'; p++)
214                 *p = TOLOWER(*p);
215
216         return(word);
217 }
218
219
220 /*
221  * Read integer from mmap pointer. 
222  * Essential a simple  ``return *(int *)p'' but avoid sigbus 
223  * for integer alignment (SunOS 4.x, 5.x).
224  *
225  * Convert network byte order to host byte order if neccessary. 
226  * So we can read on FreeBSD/i386 (little endian) a locate database
227  * which was built on SunOS/sparc (big endian).
228  */
229
230 int 
231 getwm(p)
232         caddr_t p;
233 {
234         static char buf[INTSIZE];
235         int i;
236
237         for (i = 0; i < INTSIZE; i++)
238                 buf[i] = *p++;
239
240         i = *(int *)buf;
241
242         if (i > MAXPATHLEN || i < -(MAXPATHLEN)) {
243                 i = ntohl(i);
244                 if (i > MAXPATHLEN || i < -(MAXPATHLEN))
245                         errx(1, "integer out of +-MAXPATHLEN (%d): %d",
246                             MAXPATHLEN, abs(i) < abs(htonl(i)) ? i : htonl(i));
247         }
248         return(i);
249 }
250
251 /*
252  * Read integer from stream.
253  *
254  * Convert network byte order to host byte order if neccessary. 
255  * So we can read on FreeBSD/i386 (little endian) a locate database
256  * which was built on SunOS/sparc (big endian).
257  */
258
259 int
260 getwf(fp)
261         FILE *fp;
262 {
263         int word;
264
265         word = getw(fp);
266
267         if (word > MAXPATHLEN || word < -(MAXPATHLEN)) {
268                 word = ntohl(word);
269                 if (word > MAXPATHLEN || word < -(MAXPATHLEN))
270                         errx(1, "integer out of +-MAXPATHLEN (%d): %d",
271                             MAXPATHLEN, abs(word) < abs(htonl(word)) ? word :
272                                 htonl(word));
273         }
274         return(word);
275 }