diff options
author | jmallett <jmallett@FreeBSD.org> | 2002-04-19 23:28:54 +0000 |
---|---|---|
committer | jmallett <jmallett@FreeBSD.org> | 2002-04-19 23:28:54 +0000 |
commit | 22be17968264e9796541bacb17a722aee628e8c2 (patch) | |
tree | 9d07f19a0a400de9d43a844a00d25af5890bea19 /usr.bin/xargs/strnsubst.c | |
parent | feb9b6ff6d055ca921f46f7fcd4ea49263c1af09 (diff) | |
download | FreeBSD-src-22be17968264e9796541bacb17a722aee628e8c2.zip FreeBSD-src-22be17968264e9796541bacb17a722aee628e8c2.tar.gz |
After 3 months...
Merge xargs(1) with that of xMach.
Bring in xargs(1) changes to add -L and -I as per the Single Unix Specification
version 3. Proper exit status numbers are implemented, and the manual page has
been updated to reflect reality.
The code has been ANSIfied, and a new file has been added to xargs(1) to do the
substring substitution as SUSv3 requires.
Traditional behaviour should not be affected, use of -J should be deprecated
in favor of the more portable -I (though -J has been left, for now).
Submitted by: me, tjr (the exit status stuff)
Obtained from: xMach
Diffstat (limited to 'usr.bin/xargs/strnsubst.c')
-rw-r--r-- | usr.bin/xargs/strnsubst.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/usr.bin/xargs/strnsubst.c b/usr.bin/xargs/strnsubst.c new file mode 100644 index 0000000..a06197e --- /dev/null +++ b/usr.bin/xargs/strnsubst.c @@ -0,0 +1,78 @@ +/* $xMach: strnsubst.c,v 1.3 2002/02/23 02:10:24 jmallett Exp $ */ + +/* + * Copyright (c) 2002 J. Mallett. All rights reserved. + * You may do whatever you want with this file as long as + * the above copyright and this notice remain intact, along + * with the following statement: + * For the man who taught me vi, and who got too old, too young. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <err.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +void strnsubst(char **, const char *, const char *, size_t); + +/* + * Replaces str with a string consisting of str with match replaced with + * replstr as many times as can be done before the constructed string is + * maxsize bytes large. It does not free the string pointed to by str, it + * is up to the calling program to be sure that the original contents of + * str as well as the new contents are handled in an appropriate manner. + * No value is returned. + */ +void +strnsubst(char **str, const char *match, const char *replstr, size_t maxsize) +{ + char *s1, *s2; + + s1 = *str; + if (s1 == NULL) + return; + s2 = calloc(maxsize, 1); + if (s2 == NULL) + err(1, "calloc"); + + for (;;) { + char *this; + + this = strstr(s1, match); + if (this == NULL) + break; + if ((strlen(s2) + ((uintptr_t)this - (uintptr_t)s1) + + (strlen(replstr) - 1)) > maxsize) { + strlcat(s2, s1, maxsize); + goto done; + } + strncat(s2, s1, (uintptr_t)this - (uintptr_t)s1); + strcat(s2, replstr); + s1 = this + strlen(match); + } + strcat(s2, s1); +done: + *str = s2; + return; +} + +#ifdef TEST +#include <stdio.h> + +int +main(void) +{ + char *x, *y; + + y = x = "{}{}{}"; + strnsubst(&x, "{}", "v ybir whyv! ", 12); + if (strcmp(x, "v ybir whyv! ") == 0) + printf("strnsubst() seems to work as expected.\n"); + printf("x: %s\ny: %s\n", x, y); + free(x); + return 0; +} +#endif |