]>
git.saurik.com Git - apple/shell_cmds.git/blob - locate/locate/util.c
0a94c9c28702c4a9f8ce8ede60ca4fa19be584d3
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.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
37 * $FreeBSD: src/usr.bin/locate/locate/util.c,v 1.10 2002/06/24 12:40:11 naddy Exp $
44 #include <sys/param.h>
45 #include <arpa/inet.h>
50 char **colon(char **, char*, char*);
51 char *patprep(char *);
52 void print_matches(u_int
);
53 u_char
*tolower_word(u_char
*);
56 int check_bigram_char(int);
59 * Validate bigram chars. If the test failed the database is corrupt
60 * or the database is obviously not a locate database.
66 /* legal bigram: 0, ASCII_MIN ... ASCII_MAX */
68 (ch
>= ASCII_MIN
&& ch
<= ASCII_MAX
))
72 "locate database header corrupt, bigram char outside 0, %d-%d: %d",
73 ASCII_MIN
, ASCII_MAX
, ch
);
77 /* split a colon separated string into a char vector
79 * "bla:foo" -> {"foo", "bla"}
80 * "bla:" -> {"foo", dot}
89 char *dot
; /* default for single ':' */
96 if ((dbv
= malloc(sizeof(char **))) == NULL
)
103 warnx("empty database name, ignored");
107 /* length of string vector */
108 for(vlen
= 0, pv
= dbv
; *pv
!= NULL
; pv
++, vlen
++);
110 for (ch
= c
= path
; ; ch
++) {
112 (!*ch
&& !(*(ch
- 1) == ':' && ch
== 1+ path
))) {
113 /* single colon -> dot */
119 if ((p
= malloc(sizeof(char) * (slen
+ 1)))
125 /* increase dbv with element p */
126 if ((dbv
= realloc(dbv
, sizeof(char **) * (vlen
+ 2)))
130 *(dbv
+ ++vlen
) = NULL
;
140 print_matches(counter
)
143 (void)printf("%d\n", counter
);
148 * extract last glob-free subpattern in name for fast pre-match; prepend
149 * '\0' for backwards match; return end of new pattern
151 static char globfree
[100];
157 register char *endmark
, *p
, *subp
;
160 *subp
++ = '\0'; /* set first element to '\0' */
161 p
= name
+ strlen(name
) - 1;
163 /* skip trailing metacharacters */
164 for (; p
>= name
; p
--)
165 if (index(LOCATE_REG
, *p
) == NULL
)
169 * check if maybe we are in a character class
175 (index(p
, '[') != NULL
|| index(p
, ']') != NULL
)) {
176 for (p
= name
; *p
!= '\0'; p
++)
177 if (*p
== ']' || *p
== '[')
182 * cannot find a non-meta character, give up
186 if (p
>= name
&& index(LOCATE_REG
, *p
) != NULL
)
191 /* only meta chars: "???", force '/' search */
195 for (endmark
= p
; p
>= name
; p
--)
196 if (index(LOCATE_REG
, *p
) != NULL
)
199 (p
<= endmark
) && subp
< (globfree
+ sizeof(globfree
));)
213 for(p
= word
; *p
!= '\0'; p
++)
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).
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).
240 for (i
= 0; i
< INTSIZE
; i
++)
245 if (i
> MAXPATHLEN
|| i
< -(MAXPATHLEN
)) {
247 if (i
> MAXPATHLEN
|| i
< -(MAXPATHLEN
))
248 errx(1, "integer out of +-MAXPATHLEN (%d): %d",
249 MAXPATHLEN
, abs(i
) < abs(htonl(i
)) ? i
: htonl(i
));
255 * Read integer from stream.
257 * Convert network byte order to host byte order if neccessary.
258 * So we can read on FreeBSD/i386 (little endian) a locate database
259 * which was built on SunOS/sparc (big endian).
270 if (word
> MAXPATHLEN
|| word
< -(MAXPATHLEN
)) {
272 if (word
> MAXPATHLEN
|| word
< -(MAXPATHLEN
))
273 errx(1, "integer out of +-MAXPATHLEN (%d): %d",
274 MAXPATHLEN
, abs(word
) < abs(htonl(word
)) ? word
: