* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
* Copyright (c) 1988, 1989 by Adam de Boor
* Copyright (c) 1989 by Berkeley Softworks
* This code is derived from software contributed to Berkeley by
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)str.c 5.8 (Berkeley) 6/1/90";
* concatenate the two strings, inserting a space or slash between them,
* freeing them if requested.
* the resulting string in allocated space.
str_concat(s1
, s2
, flags
)
/* get the length of both strings */
/* allocate length plus separator plus EOS */
result
= emalloc((u_int
)(len1
+ len2
+ 2));
/* copy first string into place */
/* add separator character */
if (flags
& STR_ADDSPACE
) {
} else if (flags
& STR_ADDSLASH
) {
/* copy second string plus EOS into place */
bcopy(s2
, result
+ len1
, len2
+ 1);
/* free original strings */
if (flags
& STR_DOFREE
) {
* Fracture a string into an array of words (as delineated by tabs or
* spaces) taking quotation marks into account. Leading tabs/spaces
* Pointer to the array of pointers to the words. To make life easier,
* the first word is always the value of the .MAKE variable.
brk_string(str
, store_argc
)
static int argmax
, curlen
;
static char **argv
, *buf
;
register char inquote
, *p
, *start
, *t
;
/* save off pmake variable */
argv
= (char **)emalloc((argmax
= 50) * sizeof(char *));
argv
[0] = Var_Value(".MAKE", VAR_GLOBAL
);
/* skip leading space chars.
for (; *str == ' ' || *str == '\t'; ++str);
/* allocate room for a copy of the string */
if ((len
= strlen(str
) + 1) > curlen
)
buf
= emalloc(curlen
= len
);
* copy the string; at the same time, parse backslashes,
* quotes and build the argument list.
for (p
= str
, start
= t
= buf
;; ++p
) {
* end of a token -- make sure there's enough argv
* space and save off a pointer.
argmax
*= 2; /* ramp up fast */
if (!(argv
= (char **)realloc(argv
,
argmax
* sizeof(char *))))
if (ch
== '\n' || ch
== '\0')
/* hmmm; fix it up as best we can */
done
: argv
[argc
] = (char *)NULL
;
* Str_FindSubstring -- See if a string contains a particular substring.
* Results: If string contains substring, the return value is the location of
* the first matching instance of substring in string. If string doesn't
* contain substring, the return value is NULL. Matching is done on an exact
* character-for-character basis with no wildcards or special characters.
Str_FindSubstring(string
, substring
)
register char *string
; /* String to search. */
char *substring
; /* Substring to find in string */
* First scan quickly through the two strings looking for a single-
* character match. When it's found, then compare the rest of the
for (b
= substring
; *string
!= 0; string
+= 1) {
* See if a particular string matches a particular pattern.
* Results: Non-zero is returned if string matches pattern, 0 otherwise. The
* matching operation permits the following special characters in the
* pattern: *?\[] (see the man page for details on what these mean).
Str_Match(string
, pattern
)
register char *string
; /* String */
register char *pattern
; /* Pattern */
* See if we're at the end of both the pattern and the
* string. If, we succeeded. If we're at the end of the
* pattern but not at the end of the string, we failed.
if (*string
== 0 && *pattern
!= '*')
* Check for a "*" as the next pattern character. It matches
* any substring. We handle this by calling ourselves
* recursively for each postfix of string, until either we
* match or we reach the end of the string.
if (Str_Match(string
, pattern
))
* Check for a "?" as the next pattern character. It matches
* Check for a "[" as the next pattern character. It is
* followed by a list of characters that are acceptable, or
* by a range (two characters separated by "-").
if ((*pattern
== ']') || (*pattern
== 0))
if ((*pattern
<= *string
) &&
if ((*pattern
>= *string
) &&
while ((*pattern
!= ']') && (*pattern
!= 0))
* If the next pattern character is '/', just strip off the
* '/' so we do exact matching on the character that follows.
* There's no special character. Just make sure that the
* next characters of each string match.