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 |