View | Details | Raw Unified | Return to bug 264846
Collapse All | Expand All

(-)usr.bin/xargs/strnsubst.c (-4 / +12 lines)
Lines 16-22 Link Here
16
#include <string.h>
16
#include <string.h>
17
#include <unistd.h>
17
#include <unistd.h>
18
18
19
void	strnsubst(char **, const char *, const char *, size_t);
19
enum error {
20
	STRNSUBST_NOERROR = 0,
21
	STRNSUBST_ERROR,
22
	STRNSUBST_TRUNCATED
23
};
24
25
int	strnsubst(char **, const char *, const char *, size_t);
20
26
21
/*
27
/*
22
 * Replaces str with a string consisting of str with match replaced with
28
 * Replaces str with a string consisting of str with match replaced with
Lines 28-41 Link Here
28
 * that we can still pretend to do somewhat meaningful substitution.
34
 * that we can still pretend to do somewhat meaningful substitution.
29
 * No value is returned.
35
 * No value is returned.
30
 */
36
 */
31
void
37
int
32
strnsubst(char **str, const char *match, const char *replstr, size_t maxsize)
38
strnsubst(char **str, const char *match, const char *replstr, size_t maxsize)
33
{
39
{
34
	char *s1, *s2, *this;
40
	char *s1, *s2, *this;
41
	int error = STRNSUBST_NOERROR;
35
42
36
	s1 = *str;
43
	s1 = *str;
37
	if (s1 == NULL)
44
	if (s1 == NULL)
38
		return;
45
		return STRNSUBST_ERROR;
39
	/*
46
	/*
40
	 * If maxsize is 0 then set it to the length of s1, because we have
47
	 * If maxsize is 0 then set it to the length of s1, because we have
41
	 * to duplicate s1.  XXX we maybe should double-check whether the match
48
	 * to duplicate s1.  XXX we maybe should double-check whether the match
Lines 67-72 Link Here
67
		if ((strlen(s2) + strlen(s1) + strlen(replstr) -
74
		if ((strlen(s2) + strlen(s1) + strlen(replstr) -
68
		    strlen(match) + 1) > maxsize) {
75
		    strlen(match) + 1) > maxsize) {
69
			strlcat(s2, s1, maxsize);
76
			strlcat(s2, s1, maxsize);
77
			error = STRNSUBST_TRUNCATED;
70
			goto done;
78
			goto done;
71
		}
79
		}
72
		strncat(s2, s1, (uintptr_t)this - (uintptr_t)s1);
80
		strncat(s2, s1, (uintptr_t)this - (uintptr_t)s1);
Lines 76-82 Link Here
76
	strcat(s2, s1);
84
	strcat(s2, s1);
77
done:
85
done:
78
	*str = s2;
86
	*str = s2;
79
	return;
87
	return error;
80
}
88
}
81
89
82
#ifdef TEST
90
#ifdef TEST
(-)usr.bin/xargs/xargs.c (-2 / +5 lines)
Lines 73-79 Link Here
73
static int	prompt(void);
73
static int	prompt(void);
74
static void	run(char **);
74
static void	run(char **);
75
static void	usage(void);
75
static void	usage(void);
76
void		strnsubst(char **, const char *, const char *, size_t);
76
int		strnsubst(char **, const char *, const char *, size_t);
77
static pid_t	xwait(int block, int *status);
77
static pid_t	xwait(int block, int *status);
78
static void	xexit(const char *, const int);
78
static void	xexit(const char *, const int);
79
static void	waitchildren(const char *, int);
79
static void	waitchildren(const char *, int);
Lines 517-523 Link Here
517
	while (--argc) {
517
	while (--argc) {
518
		*tmp = *avj++;
518
		*tmp = *avj++;
519
		if (repls && strstr(*tmp, replstr) != NULL) {
519
		if (repls && strstr(*tmp, replstr) != NULL) {
520
			strnsubst(tmp++, replstr, inpline, (size_t)Sflag);
520
			if (strnsubst(tmp++, replstr, inpline, (size_t)Sflag) > 0) {
521
				warnx("comamnd line cannot be assembled, too long");
522
				xexit(*argv, 1);
523
			}
521
			if (repls > 0)
524
			if (repls > 0)
522
				repls--;
525
				repls--;
523
		} else {
526
		} else {

Return to bug 264846