]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/locate/locate/util.c
Import DTS files for arm, arm64, riscv from Linux 5.8
[FreeBSD/FreeBSD.git] / usr.bin / locate / locate / util.c
1 /*
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
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.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * James A. Woods.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
26  *
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
37  * SUCH DAMAGE.
38  *
39  * $FreeBSD$
40  */
41
42
43 #include <stdlib.h>
44 #include <string.h>
45 #include <err.h>
46 #include <sys/param.h>
47 #include <arpa/inet.h>
48 #include <stdio.h>
49
50 #include "locate.h"
51
52 char    **colon(char **, char*, char*);
53 char    *patprep(char *);
54 void print_matches(u_int);
55 u_char  *tolower_word(u_char *);
56 int     getwm(caddr_t);
57 int     getwf(FILE *);
58 int     check_bigram_char(int);
59
60 /* 
61  * Validate bigram chars. If the test failed the database is corrupt 
62  * or the database is obviously not a locate database.
63  */
64 int
65 check_bigram_char(ch)
66         int ch;
67 {
68         /* legal bigram: 0, ASCII_MIN ... ASCII_MAX */
69         if (ch == 0 ||
70             (ch >= ASCII_MIN && ch <= ASCII_MAX))
71                 return(ch);
72
73         errx(1,
74                 "locate database header corrupt, bigram char outside 0, %d-%d: %d",  
75                 ASCII_MIN, ASCII_MAX, ch);
76         exit(1);
77 }
78
79 /* split a colon separated string into a char vector
80  *
81  * "bla:foo" -> {"foo", "bla"}
82  * "bla:"    -> {"foo", dot}
83  * "bla"     -> {"bla"}
84  * ""        -> do nothing
85  *
86  */
87 char **
88 colon(char **dbv, char *path, char *dot)
89 {
90         int vlen, slen;
91         char *c, *ch, *p;
92         char **pv;
93
94         if (dbv == NULL) {
95                 if ((dbv = malloc(sizeof(char *))) == NULL)
96                         err(1, "malloc");
97                 *dbv = NULL;
98         }
99
100         /* empty string */
101         if (*path == '\0') {
102                 warnx("empty database name, ignored");
103                 return(dbv);
104         }
105
106         /* length of string vector */
107         for(vlen = 0, pv = dbv; *pv != NULL; pv++, vlen++);
108
109         for (ch = c = path; ; ch++) {
110                 if (*ch == ':' ||
111                     (!*ch && !(*(ch - 1) == ':' && ch == 1+ path))) {
112                         /* single colon -> dot */
113                         if (ch == c)
114                                 p = dot;
115                         else {
116                                 /* a string */
117                                 slen = ch - c;
118                                 if ((p = malloc(sizeof(char) * (slen + 1))) 
119                                     == NULL)
120                                         err(1, "malloc");
121                                 bcopy(c, p, slen);
122                                 *(p + slen) = '\0';
123                         }
124                         /* increase dbv with element p */
125                         if ((dbv = realloc(dbv, sizeof(char *) * (vlen + 2)))
126                             == NULL)
127                                 err(1, "realloc");
128                         *(dbv + vlen) = p;
129                         *(dbv + ++vlen) = NULL;
130                         c = ch + 1;
131                 }
132                 if (*ch == '\0')
133                         break;
134         }
135         return (dbv);
136 }
137
138 void 
139 print_matches(counter)
140         u_int counter;
141 {
142         (void)printf("%d\n", counter);
143 }
144
145
146 /*
147  * extract last glob-free subpattern in name for fast pre-match; prepend
148  * '\0' for backwards match; return end of new pattern
149  */
150 static char globfree[100];
151
152 char *
153 patprep(name)
154         char *name;
155 {
156         register char *endmark, *p, *subp;
157
158         subp = globfree;
159         *subp++ = '\0';   /* set first element to '\0' */
160         p = name + strlen(name) - 1;
161
162         /* skip trailing metacharacters */
163         for (; p >= name; p--)
164                 if (strchr(LOCATE_REG, *p) == NULL)
165                         break;
166
167         /* 
168          * check if maybe we are in a character class
169          *
170          * 'foo.[ch]'
171          *        |----< p
172          */
173         if (p >= name && 
174             (strchr(p, '[') != NULL || strchr(p, ']') != NULL)) {
175                 for (p = name; *p != '\0'; p++)
176                         if (*p == ']' || *p == '[')
177                                 break;
178                 p--;
179
180                 /* 
181                  * cannot find a non-meta character, give up
182                  * '*\*[a-z]'
183                  *    |-------< p
184                  */
185                 if (p >= name && strchr(LOCATE_REG, *p) != NULL)
186                         p = name - 1;
187         }
188         
189         if (p < name)                   
190                 /* only meta chars: "???", force '/' search */
191                 *subp++ = '/';
192
193         else {
194                 for (endmark = p; p >= name; p--)
195                         if (strchr(LOCATE_REG, *p) != NULL)
196                                 break;
197                 for (++p;
198                     (p <= endmark) && subp < (globfree + sizeof(globfree));)
199                         *subp++ = *p++;
200         }
201         *subp = '\0';
202         return(--subp);
203 }
204
205 /* tolower word */
206 u_char *
207 tolower_word(word)
208         u_char *word;
209 {
210         register u_char *p;
211
212         for(p = word; *p != '\0'; p++)
213                 *p = TOLOWER(*p);
214
215         return(word);
216 }
217
218
219 /*
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).
223  *
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).
227  */
228
229 int
230 getwm(p)
231         caddr_t p;
232 {
233         union {
234                 char buf[INTSIZE];
235                 int i;
236         } u;
237         register int i, hi;
238
239         for (i = 0; i < (int)INTSIZE; i++)
240                 u.buf[i] = *p++;
241
242         i = u.i;
243
244         if (i > MAXPATHLEN || i < -(MAXPATHLEN)) {
245                 hi = ntohl(i);
246                 if (hi > MAXPATHLEN || hi < -(MAXPATHLEN))
247                         errx(1, "integer out of +-MAXPATHLEN (%d): %u",
248                             MAXPATHLEN, abs(i) < abs(hi) ? i : hi);
249                 return(hi);
250         }
251         return(i);
252 }
253
254 /*
255  * Read integer from stream.
256  *
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).
260  */
261
262 int
263 getwf(fp)
264         FILE *fp;
265 {
266         register int word, hword;
267
268         word = getw(fp);
269
270         if (word > MAXPATHLEN || word < -(MAXPATHLEN)) {
271                 hword = ntohl(word);
272                 if (hword > MAXPATHLEN || hword < -(MAXPATHLEN))
273                         errx(1, "integer out of +-MAXPATHLEN (%d): %u",
274                             MAXPATHLEN, abs(word) < abs(hword) ? word : hword);
275                 return(hword);
276         }
277         return(word);
278 }