Home | History | Annotate | Line # | Download | only in make
str.c revision 1.1.1.2
      1      1.1  cgd /*-
      2  1.1.1.2  tls  * Copyright (c) 1988, 1989, 1990, 1993
      3  1.1.1.2  tls  *	The Regents of the University of California.  All rights reserved.
      4      1.1  cgd  * Copyright (c) 1989 by Berkeley Softworks
      5      1.1  cgd  * All rights reserved.
      6      1.1  cgd  *
      7      1.1  cgd  * This code is derived from software contributed to Berkeley by
      8      1.1  cgd  * Adam de Boor.
      9      1.1  cgd  *
     10      1.1  cgd  * Redistribution and use in source and binary forms, with or without
     11      1.1  cgd  * modification, are permitted provided that the following conditions
     12      1.1  cgd  * are met:
     13      1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     14      1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     15      1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  cgd  *    documentation and/or other materials provided with the distribution.
     18      1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     19      1.1  cgd  *    must display the following acknowledgement:
     20      1.1  cgd  *	This product includes software developed by the University of
     21      1.1  cgd  *	California, Berkeley and its contributors.
     22      1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     23      1.1  cgd  *    may be used to endorse or promote products derived from this software
     24      1.1  cgd  *    without specific prior written permission.
     25      1.1  cgd  *
     26      1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27      1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28      1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29      1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30      1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31      1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32      1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33      1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34      1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35      1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36      1.1  cgd  * SUCH DAMAGE.
     37      1.1  cgd  */
     38      1.1  cgd 
     39      1.1  cgd #ifndef lint
     40  1.1.1.2  tls static char     sccsid[] = "@(#)str.c	8.6 (Berkeley) 4/28/95";
     41  1.1.1.2  tls #endif /* not lint */
     42      1.1  cgd 
     43      1.1  cgd #include "make.h"
     44      1.1  cgd 
     45  1.1.1.2  tls static char **argv, *buffer;
     46  1.1.1.2  tls static int argmax, curlen;
     47  1.1.1.2  tls 
     48  1.1.1.2  tls /*
     49  1.1.1.2  tls  * str_init --
     50  1.1.1.2  tls  *	Initialize the strings package
     51  1.1.1.2  tls  *
     52  1.1.1.2  tls  */
     53  1.1.1.2  tls void
     54  1.1.1.2  tls str_init()
     55  1.1.1.2  tls {
     56  1.1.1.2  tls     char *p1;
     57  1.1.1.2  tls     argv = (char **)emalloc((argmax = 50) * sizeof(char *));
     58  1.1.1.2  tls     argv[0] = Var_Value(".MAKE", VAR_GLOBAL, &p1);
     59  1.1.1.2  tls }
     60  1.1.1.2  tls 
     61  1.1.1.2  tls 
     62  1.1.1.2  tls /*
     63  1.1.1.2  tls  * str_end --
     64  1.1.1.2  tls  *	Cleanup the strings package
     65  1.1.1.2  tls  *
     66  1.1.1.2  tls  */
     67  1.1.1.2  tls void
     68  1.1.1.2  tls str_end()
     69  1.1.1.2  tls {
     70  1.1.1.2  tls     if (argv[0]) {
     71  1.1.1.2  tls 	free(argv[0]);
     72  1.1.1.2  tls 	free((Address) argv);
     73  1.1.1.2  tls     }
     74  1.1.1.2  tls     if (buffer)
     75  1.1.1.2  tls 	free(buffer);
     76  1.1.1.2  tls }
     77  1.1.1.2  tls 
     78  1.1.1.2  tls 
     79      1.1  cgd /*-
     80      1.1  cgd  * str_concat --
     81      1.1  cgd  *	concatenate the two strings, inserting a space or slash between them,
     82      1.1  cgd  *	freeing them if requested.
     83      1.1  cgd  *
     84      1.1  cgd  * returns --
     85      1.1  cgd  *	the resulting string in allocated space.
     86      1.1  cgd  */
     87      1.1  cgd char *
     88      1.1  cgd str_concat(s1, s2, flags)
     89      1.1  cgd 	char *s1, *s2;
     90      1.1  cgd 	int flags;
     91      1.1  cgd {
     92      1.1  cgd 	register int len1, len2;
     93      1.1  cgd 	register char *result;
     94      1.1  cgd 
     95      1.1  cgd 	/* get the length of both strings */
     96      1.1  cgd 	len1 = strlen(s1);
     97      1.1  cgd 	len2 = strlen(s2);
     98      1.1  cgd 
     99      1.1  cgd 	/* allocate length plus separator plus EOS */
    100      1.1  cgd 	result = emalloc((u_int)(len1 + len2 + 2));
    101      1.1  cgd 
    102      1.1  cgd 	/* copy first string into place */
    103  1.1.1.2  tls 	memcpy(result, s1, len1);
    104      1.1  cgd 
    105      1.1  cgd 	/* add separator character */
    106      1.1  cgd 	if (flags & STR_ADDSPACE) {
    107      1.1  cgd 		result[len1] = ' ';
    108      1.1  cgd 		++len1;
    109      1.1  cgd 	} else if (flags & STR_ADDSLASH) {
    110      1.1  cgd 		result[len1] = '/';
    111      1.1  cgd 		++len1;
    112      1.1  cgd 	}
    113      1.1  cgd 
    114      1.1  cgd 	/* copy second string plus EOS into place */
    115  1.1.1.2  tls 	memcpy(result + len1, s2, len2 + 1);
    116      1.1  cgd 
    117      1.1  cgd 	/* free original strings */
    118      1.1  cgd 	if (flags & STR_DOFREE) {
    119      1.1  cgd 		(void)free(s1);
    120      1.1  cgd 		(void)free(s2);
    121      1.1  cgd 	}
    122      1.1  cgd 	return(result);
    123      1.1  cgd }
    124      1.1  cgd 
    125      1.1  cgd /*-
    126      1.1  cgd  * brk_string --
    127      1.1  cgd  *	Fracture a string into an array of words (as delineated by tabs or
    128      1.1  cgd  *	spaces) taking quotation marks into account.  Leading tabs/spaces
    129      1.1  cgd  *	are ignored.
    130      1.1  cgd  *
    131      1.1  cgd  * returns --
    132      1.1  cgd  *	Pointer to the array of pointers to the words.  To make life easier,
    133      1.1  cgd  *	the first word is always the value of the .MAKE variable.
    134      1.1  cgd  */
    135      1.1  cgd char **
    136  1.1.1.2  tls brk_string(str, store_argc, expand)
    137      1.1  cgd 	register char *str;
    138      1.1  cgd 	int *store_argc;
    139  1.1.1.2  tls 	Boolean expand;
    140      1.1  cgd {
    141      1.1  cgd 	register int argc, ch;
    142      1.1  cgd 	register char inquote, *p, *start, *t;
    143      1.1  cgd 	int len;
    144      1.1  cgd 
    145  1.1.1.2  tls 	/* skip leading space chars. */
    146  1.1.1.2  tls 	for (; *str == ' ' || *str == '\t'; ++str)
    147  1.1.1.2  tls 		continue;
    148      1.1  cgd 
    149      1.1  cgd 	/* allocate room for a copy of the string */
    150  1.1.1.2  tls 	if ((len = strlen(str) + 1) > curlen) {
    151  1.1.1.2  tls 		if (buffer)
    152  1.1.1.2  tls 		    free(buffer);
    153  1.1.1.2  tls 		buffer = emalloc(curlen = len);
    154  1.1.1.2  tls 	}
    155      1.1  cgd 
    156      1.1  cgd 	/*
    157      1.1  cgd 	 * copy the string; at the same time, parse backslashes,
    158      1.1  cgd 	 * quotes and build the argument list.
    159      1.1  cgd 	 */
    160      1.1  cgd 	argc = 1;
    161      1.1  cgd 	inquote = '\0';
    162  1.1.1.2  tls 	for (p = str, start = t = buffer;; ++p) {
    163      1.1  cgd 		switch(ch = *p) {
    164      1.1  cgd 		case '"':
    165      1.1  cgd 		case '\'':
    166      1.1  cgd 			if (inquote)
    167      1.1  cgd 				if (inquote == ch)
    168  1.1.1.2  tls 					inquote = '\0';
    169      1.1  cgd 				else
    170      1.1  cgd 					break;
    171  1.1.1.2  tls 			else {
    172  1.1.1.2  tls 				inquote = (char) ch;
    173  1.1.1.2  tls 				/* Don't miss "" or '' */
    174  1.1.1.2  tls 				if (start == NULL && p[1] == inquote) {
    175  1.1.1.2  tls 					start = t + 1;
    176  1.1.1.2  tls 					break;
    177  1.1.1.2  tls 				}
    178  1.1.1.2  tls 			}
    179  1.1.1.2  tls 			if (!expand) {
    180  1.1.1.2  tls 				if (!start)
    181  1.1.1.2  tls 					start = t;
    182  1.1.1.2  tls 				*t++ = ch;
    183  1.1.1.2  tls 			}
    184      1.1  cgd 			continue;
    185      1.1  cgd 		case ' ':
    186      1.1  cgd 		case '\t':
    187  1.1.1.2  tls 		case '\n':
    188      1.1  cgd 			if (inquote)
    189      1.1  cgd 				break;
    190      1.1  cgd 			if (!start)
    191      1.1  cgd 				continue;
    192      1.1  cgd 			/* FALLTHROUGH */
    193      1.1  cgd 		case '\0':
    194      1.1  cgd 			/*
    195      1.1  cgd 			 * end of a token -- make sure there's enough argv
    196      1.1  cgd 			 * space and save off a pointer.
    197      1.1  cgd 			 */
    198  1.1.1.2  tls 			if (!start)
    199  1.1.1.2  tls 			    goto done;
    200  1.1.1.2  tls 
    201      1.1  cgd 			*t++ = '\0';
    202      1.1  cgd 			if (argc == argmax) {
    203      1.1  cgd 				argmax *= 2;		/* ramp up fast */
    204      1.1  cgd 				if (!(argv = (char **)realloc(argv,
    205      1.1  cgd 				    argmax * sizeof(char *))))
    206      1.1  cgd 				enomem();
    207      1.1  cgd 			}
    208      1.1  cgd 			argv[argc++] = start;
    209      1.1  cgd 			start = (char *)NULL;
    210      1.1  cgd 			if (ch == '\n' || ch == '\0')
    211      1.1  cgd 				goto done;
    212      1.1  cgd 			continue;
    213      1.1  cgd 		case '\\':
    214  1.1.1.2  tls 			if (!expand) {
    215  1.1.1.2  tls 				if (!start)
    216  1.1.1.2  tls 					start = t;
    217  1.1.1.2  tls 				*t++ = '\\';
    218  1.1.1.2  tls 				ch = *++p;
    219  1.1.1.2  tls 				break;
    220  1.1.1.2  tls 			}
    221  1.1.1.2  tls 
    222      1.1  cgd 			switch (ch = *++p) {
    223      1.1  cgd 			case '\0':
    224      1.1  cgd 			case '\n':
    225      1.1  cgd 				/* hmmm; fix it up as best we can */
    226      1.1  cgd 				ch = '\\';
    227      1.1  cgd 				--p;
    228      1.1  cgd 				break;
    229      1.1  cgd 			case 'b':
    230      1.1  cgd 				ch = '\b';
    231      1.1  cgd 				break;
    232      1.1  cgd 			case 'f':
    233      1.1  cgd 				ch = '\f';
    234      1.1  cgd 				break;
    235      1.1  cgd 			case 'n':
    236      1.1  cgd 				ch = '\n';
    237      1.1  cgd 				break;
    238      1.1  cgd 			case 'r':
    239      1.1  cgd 				ch = '\r';
    240      1.1  cgd 				break;
    241      1.1  cgd 			case 't':
    242      1.1  cgd 				ch = '\t';
    243      1.1  cgd 				break;
    244      1.1  cgd 			}
    245      1.1  cgd 			break;
    246      1.1  cgd 		}
    247      1.1  cgd 		if (!start)
    248      1.1  cgd 			start = t;
    249  1.1.1.2  tls 		*t++ = (char) ch;
    250      1.1  cgd 	}
    251      1.1  cgd done:	argv[argc] = (char *)NULL;
    252      1.1  cgd 	*store_argc = argc;
    253      1.1  cgd 	return(argv);
    254      1.1  cgd }
    255      1.1  cgd 
    256      1.1  cgd /*
    257      1.1  cgd  * Str_FindSubstring -- See if a string contains a particular substring.
    258      1.1  cgd  *
    259      1.1  cgd  * Results: If string contains substring, the return value is the location of
    260      1.1  cgd  * the first matching instance of substring in string.  If string doesn't
    261      1.1  cgd  * contain substring, the return value is NULL.  Matching is done on an exact
    262      1.1  cgd  * character-for-character basis with no wildcards or special characters.
    263      1.1  cgd  *
    264      1.1  cgd  * Side effects: None.
    265      1.1  cgd  */
    266      1.1  cgd char *
    267      1.1  cgd Str_FindSubstring(string, substring)
    268      1.1  cgd 	register char *string;		/* String to search. */
    269      1.1  cgd 	char *substring;		/* Substring to find in string */
    270      1.1  cgd {
    271      1.1  cgd 	register char *a, *b;
    272      1.1  cgd 
    273      1.1  cgd 	/*
    274      1.1  cgd 	 * First scan quickly through the two strings looking for a single-
    275      1.1  cgd 	 * character match.  When it's found, then compare the rest of the
    276      1.1  cgd 	 * substring.
    277      1.1  cgd 	 */
    278      1.1  cgd 
    279      1.1  cgd 	for (b = substring; *string != 0; string += 1) {
    280      1.1  cgd 		if (*string != *b)
    281      1.1  cgd 			continue;
    282      1.1  cgd 		a = string;
    283      1.1  cgd 		for (;;) {
    284      1.1  cgd 			if (*b == 0)
    285      1.1  cgd 				return(string);
    286      1.1  cgd 			if (*a++ != *b++)
    287      1.1  cgd 				break;
    288      1.1  cgd 		}
    289      1.1  cgd 		b = substring;
    290      1.1  cgd 	}
    291      1.1  cgd 	return((char *) NULL);
    292      1.1  cgd }
    293      1.1  cgd 
    294      1.1  cgd /*
    295      1.1  cgd  * Str_Match --
    296      1.1  cgd  *
    297      1.1  cgd  * See if a particular string matches a particular pattern.
    298      1.1  cgd  *
    299      1.1  cgd  * Results: Non-zero is returned if string matches pattern, 0 otherwise. The
    300      1.1  cgd  * matching operation permits the following special characters in the
    301      1.1  cgd  * pattern: *?\[] (see the man page for details on what these mean).
    302      1.1  cgd  *
    303      1.1  cgd  * Side effects: None.
    304      1.1  cgd  */
    305  1.1.1.2  tls int
    306      1.1  cgd Str_Match(string, pattern)
    307      1.1  cgd 	register char *string;		/* String */
    308      1.1  cgd 	register char *pattern;		/* Pattern */
    309      1.1  cgd {
    310      1.1  cgd 	char c2;
    311      1.1  cgd 
    312      1.1  cgd 	for (;;) {
    313      1.1  cgd 		/*
    314      1.1  cgd 		 * See if we're at the end of both the pattern and the
    315      1.1  cgd 		 * string. If, we succeeded.  If we're at the end of the
    316      1.1  cgd 		 * pattern but not at the end of the string, we failed.
    317      1.1  cgd 		 */
    318      1.1  cgd 		if (*pattern == 0)
    319      1.1  cgd 			return(!*string);
    320      1.1  cgd 		if (*string == 0 && *pattern != '*')
    321      1.1  cgd 			return(0);
    322      1.1  cgd 		/*
    323      1.1  cgd 		 * Check for a "*" as the next pattern character.  It matches
    324      1.1  cgd 		 * any substring.  We handle this by calling ourselves
    325      1.1  cgd 		 * recursively for each postfix of string, until either we
    326      1.1  cgd 		 * match or we reach the end of the string.
    327      1.1  cgd 		 */
    328      1.1  cgd 		if (*pattern == '*') {
    329      1.1  cgd 			pattern += 1;
    330      1.1  cgd 			if (*pattern == 0)
    331      1.1  cgd 				return(1);
    332      1.1  cgd 			while (*string != 0) {
    333      1.1  cgd 				if (Str_Match(string, pattern))
    334      1.1  cgd 					return(1);
    335      1.1  cgd 				++string;
    336      1.1  cgd 			}
    337      1.1  cgd 			return(0);
    338      1.1  cgd 		}
    339      1.1  cgd 		/*
    340      1.1  cgd 		 * Check for a "?" as the next pattern character.  It matches
    341      1.1  cgd 		 * any single character.
    342      1.1  cgd 		 */
    343      1.1  cgd 		if (*pattern == '?')
    344      1.1  cgd 			goto thisCharOK;
    345      1.1  cgd 		/*
    346      1.1  cgd 		 * Check for a "[" as the next pattern character.  It is
    347      1.1  cgd 		 * followed by a list of characters that are acceptable, or
    348      1.1  cgd 		 * by a range (two characters separated by "-").
    349      1.1  cgd 		 */
    350      1.1  cgd 		if (*pattern == '[') {
    351      1.1  cgd 			++pattern;
    352      1.1  cgd 			for (;;) {
    353      1.1  cgd 				if ((*pattern == ']') || (*pattern == 0))
    354      1.1  cgd 					return(0);
    355      1.1  cgd 				if (*pattern == *string)
    356      1.1  cgd 					break;
    357      1.1  cgd 				if (pattern[1] == '-') {
    358      1.1  cgd 					c2 = pattern[2];
    359      1.1  cgd 					if (c2 == 0)
    360      1.1  cgd 						return(0);
    361      1.1  cgd 					if ((*pattern <= *string) &&
    362      1.1  cgd 					    (c2 >= *string))
    363      1.1  cgd 						break;
    364      1.1  cgd 					if ((*pattern >= *string) &&
    365      1.1  cgd 					    (c2 <= *string))
    366      1.1  cgd 						break;
    367      1.1  cgd 					pattern += 2;
    368      1.1  cgd 				}
    369      1.1  cgd 				++pattern;
    370      1.1  cgd 			}
    371      1.1  cgd 			while ((*pattern != ']') && (*pattern != 0))
    372      1.1  cgd 				++pattern;
    373      1.1  cgd 			goto thisCharOK;
    374      1.1  cgd 		}
    375      1.1  cgd 		/*
    376      1.1  cgd 		 * If the next pattern character is '/', just strip off the
    377      1.1  cgd 		 * '/' so we do exact matching on the character that follows.
    378      1.1  cgd 		 */
    379      1.1  cgd 		if (*pattern == '\\') {
    380      1.1  cgd 			++pattern;
    381      1.1  cgd 			if (*pattern == 0)
    382      1.1  cgd 				return(0);
    383      1.1  cgd 		}
    384      1.1  cgd 		/*
    385      1.1  cgd 		 * There's no special character.  Just make sure that the
    386      1.1  cgd 		 * next characters of each string match.
    387      1.1  cgd 		 */
    388      1.1  cgd 		if (*pattern != *string)
    389      1.1  cgd 			return(0);
    390      1.1  cgd thisCharOK:	++pattern;
    391      1.1  cgd 		++string;
    392      1.1  cgd 	}
    393  1.1.1.2  tls }
    394  1.1.1.2  tls 
    395  1.1.1.2  tls 
    396  1.1.1.2  tls /*-
    397  1.1.1.2  tls  *-----------------------------------------------------------------------
    398  1.1.1.2  tls  * Str_SYSVMatch --
    399  1.1.1.2  tls  *	Check word against pattern for a match (% is wild),
    400  1.1.1.2  tls  *
    401  1.1.1.2  tls  * Results:
    402  1.1.1.2  tls  *	Returns the beginning position of a match or null. The number
    403  1.1.1.2  tls  *	of characters matched is returned in len.
    404  1.1.1.2  tls  *
    405  1.1.1.2  tls  * Side Effects:
    406  1.1.1.2  tls  *	None
    407  1.1.1.2  tls  *
    408  1.1.1.2  tls  *-----------------------------------------------------------------------
    409  1.1.1.2  tls  */
    410  1.1.1.2  tls char *
    411  1.1.1.2  tls Str_SYSVMatch(word, pattern, len)
    412  1.1.1.2  tls     char	*word;		/* Word to examine */
    413  1.1.1.2  tls     char	*pattern;	/* Pattern to examine against */
    414  1.1.1.2  tls     int		*len;		/* Number of characters to substitute */
    415  1.1.1.2  tls {
    416  1.1.1.2  tls     char *p = pattern;
    417  1.1.1.2  tls     char *w = word;
    418  1.1.1.2  tls     char *m;
    419  1.1.1.2  tls 
    420  1.1.1.2  tls     if (*p == '\0') {
    421  1.1.1.2  tls 	/* Null pattern is the whole string */
    422  1.1.1.2  tls 	*len = strlen(w);
    423  1.1.1.2  tls 	return w;
    424  1.1.1.2  tls     }
    425  1.1.1.2  tls 
    426  1.1.1.2  tls     if ((m = strchr(p, '%')) != NULL) {
    427  1.1.1.2  tls 	/* check that the prefix matches */
    428  1.1.1.2  tls 	for (; p != m && *w && *w == *p; w++, p++)
    429  1.1.1.2  tls 	     continue;
    430  1.1.1.2  tls 
    431  1.1.1.2  tls 	if (p != m)
    432  1.1.1.2  tls 	    return NULL;	/* No match */
    433  1.1.1.2  tls 
    434  1.1.1.2  tls 	if (*++p == '\0') {
    435  1.1.1.2  tls 	    /* No more pattern, return the rest of the string */
    436  1.1.1.2  tls 	    *len = strlen(w);
    437  1.1.1.2  tls 	    return w;
    438  1.1.1.2  tls 	}
    439  1.1.1.2  tls     }
    440  1.1.1.2  tls 
    441  1.1.1.2  tls     m = w;
    442  1.1.1.2  tls 
    443  1.1.1.2  tls     /* Find a matching tail */
    444  1.1.1.2  tls     do
    445  1.1.1.2  tls 	if (strcmp(p, w) == 0) {
    446  1.1.1.2  tls 	    *len = w - m;
    447  1.1.1.2  tls 	    return m;
    448  1.1.1.2  tls 	}
    449  1.1.1.2  tls     while (*w++ != '\0');
    450  1.1.1.2  tls 
    451  1.1.1.2  tls     return NULL;
    452  1.1.1.2  tls }
    453  1.1.1.2  tls 
    454  1.1.1.2  tls 
    455  1.1.1.2  tls /*-
    456  1.1.1.2  tls  *-----------------------------------------------------------------------
    457  1.1.1.2  tls  * Str_SYSVSubst --
    458  1.1.1.2  tls  *	Substitute '%' on the pattern with len characters from src.
    459  1.1.1.2  tls  *	If the pattern does not contain a '%' prepend len characters
    460  1.1.1.2  tls  *	from src.
    461  1.1.1.2  tls  *
    462  1.1.1.2  tls  * Results:
    463  1.1.1.2  tls  *	None
    464  1.1.1.2  tls  *
    465  1.1.1.2  tls  * Side Effects:
    466  1.1.1.2  tls  *	Places result on buf
    467  1.1.1.2  tls  *
    468  1.1.1.2  tls  *-----------------------------------------------------------------------
    469  1.1.1.2  tls  */
    470  1.1.1.2  tls void
    471  1.1.1.2  tls Str_SYSVSubst(buf, pat, src, len)
    472  1.1.1.2  tls     Buffer buf;
    473  1.1.1.2  tls     char *pat;
    474  1.1.1.2  tls     char *src;
    475  1.1.1.2  tls     int   len;
    476  1.1.1.2  tls {
    477  1.1.1.2  tls     char *m;
    478  1.1.1.2  tls 
    479  1.1.1.2  tls     if ((m = strchr(pat, '%')) != NULL) {
    480  1.1.1.2  tls 	/* Copy the prefix */
    481  1.1.1.2  tls 	Buf_AddBytes(buf, m - pat, (Byte *) pat);
    482  1.1.1.2  tls 	/* skip the % */
    483  1.1.1.2  tls 	pat = m + 1;
    484  1.1.1.2  tls     }
    485  1.1.1.2  tls 
    486  1.1.1.2  tls     /* Copy the pattern */
    487  1.1.1.2  tls     Buf_AddBytes(buf, len, (Byte *) src);
    488  1.1.1.2  tls 
    489  1.1.1.2  tls     /* append the rest */
    490  1.1.1.2  tls     Buf_AddBytes(buf, strlen(pat), (Byte *) pat);
    491      1.1  cgd }
    492