Home | History | Annotate | Line # | Download | only in xargs
strnsubst.c revision 1.1.10.2
      1  1.1.10.2  bouyer /* $xMach: strnsubst.c,v 1.3 2002/02/23 02:10:24 jmallett Exp $ */
      2  1.1.10.2  bouyer 
      3  1.1.10.2  bouyer /*
      4  1.1.10.2  bouyer  * Copyright (c) 2002 J. Mallett.  All rights reserved.
      5  1.1.10.2  bouyer  * You may do whatever you want with this file as long as
      6  1.1.10.2  bouyer  * the above copyright and this notice remain intact, along
      7  1.1.10.2  bouyer  * with the following statement:
      8  1.1.10.2  bouyer  * 	For the man who taught me vi, and who got too old, too young.
      9  1.1.10.2  bouyer  */
     10  1.1.10.2  bouyer 
     11  1.1.10.2  bouyer #include <sys/cdefs.h>
     12  1.1.10.2  bouyer #ifndef lint
     13  1.1.10.2  bouyer #if 0
     14  1.1.10.2  bouyer __FBSDID("$FreeBSD: src/usr.bin/xargs/strnsubst.c,v 1.8 2005/12/30 23:22:50 jmallett Exp $");
     15  1.1.10.2  bouyer #endif
     16  1.1.10.2  bouyer __RCSID("$NetBSD: strnsubst.c,v 1.1.10.2 2008/01/21 21:17:55 bouyer Exp $");
     17  1.1.10.2  bouyer #endif /* not lint */
     18  1.1.10.2  bouyer 
     19  1.1.10.2  bouyer #include <err.h>
     20  1.1.10.2  bouyer #include <stdlib.h>
     21  1.1.10.2  bouyer #include <string.h>
     22  1.1.10.2  bouyer #include <unistd.h>
     23  1.1.10.2  bouyer #include <stdint.h>
     24  1.1.10.2  bouyer 
     25  1.1.10.2  bouyer void	strnsubst(char **, const char *, const char *, size_t);
     26  1.1.10.2  bouyer 
     27  1.1.10.2  bouyer /*
     28  1.1.10.2  bouyer  * Replaces str with a string consisting of str with match replaced with
     29  1.1.10.2  bouyer  * replstr as many times as can be done before the constructed string is
     30  1.1.10.2  bouyer  * maxsize bytes large.  It does not free the string pointed to by str, it
     31  1.1.10.2  bouyer  * is up to the calling program to be sure that the original contents of
     32  1.1.10.2  bouyer  * str as well as the new contents are handled in an appropriate manner.
     33  1.1.10.2  bouyer  * If replstr is NULL, then that internally is changed to a nil-string, so
     34  1.1.10.2  bouyer  * that we can still pretend to do somewhat meaningful substitution.
     35  1.1.10.2  bouyer  * No value is returned.
     36  1.1.10.2  bouyer  */
     37  1.1.10.2  bouyer void
     38  1.1.10.2  bouyer strnsubst(char **str, const char *match, const char *replstr, size_t maxsize)
     39  1.1.10.2  bouyer {
     40  1.1.10.2  bouyer 	char *s1, *s2, *this;
     41  1.1.10.2  bouyer 
     42  1.1.10.2  bouyer 	s1 = *str;
     43  1.1.10.2  bouyer 	if (s1 == NULL)
     44  1.1.10.2  bouyer 		return;
     45  1.1.10.2  bouyer 	/*
     46  1.1.10.2  bouyer 	 * If maxsize is 0 then set it to to the length of s1, because we have
     47  1.1.10.2  bouyer 	 * to duplicate s1.  XXX we maybe should double-check whether the match
     48  1.1.10.2  bouyer 	 * appears in s1.  If it doesn't, then we also have to set the length
     49  1.1.10.2  bouyer 	 * to the length of s1, to avoid modifying the argument.  It may make
     50  1.1.10.2  bouyer 	 * sense to check if maxsize is <= strlen(s1), because in that case we
     51  1.1.10.2  bouyer 	 * want to return the unmodified string, too.
     52  1.1.10.2  bouyer 	 */
     53  1.1.10.2  bouyer 	if (maxsize == 0) {
     54  1.1.10.2  bouyer 		match = NULL;
     55  1.1.10.2  bouyer 		maxsize = strlen(s1) + 1;
     56  1.1.10.2  bouyer 	}
     57  1.1.10.2  bouyer 	s2 = calloc(maxsize, 1);
     58  1.1.10.2  bouyer 	if (s2 == NULL)
     59  1.1.10.2  bouyer 		err(1, "calloc");
     60  1.1.10.2  bouyer 
     61  1.1.10.2  bouyer 	if (replstr == NULL)
     62  1.1.10.2  bouyer 		replstr = "";
     63  1.1.10.2  bouyer 
     64  1.1.10.2  bouyer 	if (match == NULL || replstr == NULL || maxsize == strlen(s1)) {
     65  1.1.10.2  bouyer 		(void)strlcpy(s2, s1, maxsize);
     66  1.1.10.2  bouyer 		goto done;
     67  1.1.10.2  bouyer 	}
     68  1.1.10.2  bouyer 
     69  1.1.10.2  bouyer 	for (;;) {
     70  1.1.10.2  bouyer 		this = strstr(s1, match);
     71  1.1.10.2  bouyer 		if (this == NULL)
     72  1.1.10.2  bouyer 			break;
     73  1.1.10.2  bouyer 		if ((strlen(s2) + strlen(s1) + strlen(replstr) -
     74  1.1.10.2  bouyer 		    strlen(match) + 1) > maxsize) {
     75  1.1.10.2  bouyer 			(void)strlcat(s2, s1, maxsize);
     76  1.1.10.2  bouyer 			goto done;
     77  1.1.10.2  bouyer 		}
     78  1.1.10.2  bouyer 		(void)strncat(s2, s1, (uintptr_t)this - (uintptr_t)s1);
     79  1.1.10.2  bouyer 		(void)strcat(s2, replstr);
     80  1.1.10.2  bouyer 		s1 = this + strlen(match);
     81  1.1.10.2  bouyer 	}
     82  1.1.10.2  bouyer 	(void)strcat(s2, s1);
     83  1.1.10.2  bouyer done:
     84  1.1.10.2  bouyer 	*str = s2;
     85  1.1.10.2  bouyer 	return;
     86  1.1.10.2  bouyer }
     87  1.1.10.2  bouyer 
     88  1.1.10.2  bouyer #ifdef TEST
     89  1.1.10.2  bouyer #include <stdio.h>
     90  1.1.10.2  bouyer 
     91  1.1.10.2  bouyer int
     92  1.1.10.2  bouyer main(void)
     93  1.1.10.2  bouyer {
     94  1.1.10.2  bouyer 	char *x, *y, *z, *za;
     95  1.1.10.2  bouyer 
     96  1.1.10.2  bouyer 	x = "{}%$";
     97  1.1.10.2  bouyer 	strnsubst(&x, "%$", "{} enpury!", 255);
     98  1.1.10.2  bouyer 	y = x;
     99  1.1.10.2  bouyer 	strnsubst(&y, "}{}", "ybir", 255);
    100  1.1.10.2  bouyer 	z = y;
    101  1.1.10.2  bouyer 	strnsubst(&z, "{", "v ", 255);
    102  1.1.10.2  bouyer 	za = z;
    103  1.1.10.2  bouyer 	strnsubst(&z, NULL, za, 255);
    104  1.1.10.2  bouyer 	if (strcmp(z, "v ybir enpury!") == 0)
    105  1.1.10.2  bouyer 		(void)printf("strnsubst() seems to work!\n");
    106  1.1.10.2  bouyer 	else
    107  1.1.10.2  bouyer 		(void)printf("strnsubst() is broken.\n");
    108  1.1.10.2  bouyer 	(void)printf("%s\n", z);
    109  1.1.10.2  bouyer 	free(x);
    110  1.1.10.2  bouyer 	free(y);
    111  1.1.10.2  bouyer 	free(z);
    112  1.1.10.2  bouyer 	free(za);
    113  1.1.10.2  bouyer 	return 0;
    114  1.1.10.2  bouyer }
    115  1.1.10.2  bouyer #endif
    116