]>
Commit | Line | Data |
---|---|---|
d5a89d7d KS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2013 Kay Sievers | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
20 | /* | |
21 | * Concatenates/copies strings. In any case, terminates in all cases | |
9e5bd85a VC |
22 | * with '\0' and moves the @dest pointer forward to the added '\0'. |
23 | * Returns the remaining size, and 0 if the string was truncated. | |
24 | * | |
25 | * Due to the intended usage, these helpers silently noop invocations | |
26 | * having zero size. This is technically an exception to the above | |
27 | * statement "terminates in all cases". It's unexpected for such calls to | |
28 | * occur outside of a loop where this is the preferred behavior. | |
d5a89d7d KS |
29 | */ |
30 | ||
11c3a366 | 31 | #include <stdarg.h> |
d5a89d7d KS |
32 | #include <stdio.h> |
33 | #include <string.h> | |
cf0fbc49 | 34 | |
d5a89d7d KS |
35 | #include "strxcpyx.h" |
36 | ||
f168c273 | 37 | size_t strpcpy(char **dest, size_t size, const char *src) { |
d5a89d7d KS |
38 | size_t len; |
39 | ||
9e5bd85a VC |
40 | if (size == 0) |
41 | return 0; | |
42 | ||
d5a89d7d KS |
43 | len = strlen(src); |
44 | if (len >= size) { | |
45 | if (size > 1) | |
46 | *dest = mempcpy(*dest, src, size-1); | |
47 | size = 0; | |
48 | } else { | |
49 | if (len > 0) { | |
50 | *dest = mempcpy(*dest, src, len); | |
51 | size -= len; | |
52 | } | |
53 | } | |
54 | *dest[0] = '\0'; | |
55 | return size; | |
56 | } | |
57 | ||
f168c273 | 58 | size_t strpcpyf(char **dest, size_t size, const char *src, ...) { |
d5a89d7d KS |
59 | va_list va; |
60 | int i; | |
61 | ||
9e5bd85a VC |
62 | if (size == 0) |
63 | return 0; | |
64 | ||
d5a89d7d KS |
65 | va_start(va, src); |
66 | i = vsnprintf(*dest, size, src, va); | |
67 | if (i < (int)size) { | |
68 | *dest += i; | |
69 | size -= i; | |
70 | } else { | |
d5a89d7d KS |
71 | size = 0; |
72 | } | |
73 | va_end(va); | |
d5a89d7d KS |
74 | return size; |
75 | } | |
76 | ||
f168c273 | 77 | size_t strpcpyl(char **dest, size_t size, const char *src, ...) { |
d5a89d7d KS |
78 | va_list va; |
79 | ||
80 | va_start(va, src); | |
81 | do { | |
82 | size = strpcpy(dest, size, src); | |
83 | src = va_arg(va, char *); | |
84 | } while (src != NULL); | |
85 | va_end(va); | |
86 | return size; | |
87 | } | |
88 | ||
f168c273 | 89 | size_t strscpy(char *dest, size_t size, const char *src) { |
d5a89d7d KS |
90 | char *s; |
91 | ||
92 | s = dest; | |
93 | return strpcpy(&s, size, src); | |
94 | } | |
95 | ||
96 | size_t strscpyl(char *dest, size_t size, const char *src, ...) { | |
97 | va_list va; | |
98 | char *s; | |
99 | ||
100 | va_start(va, src); | |
101 | s = dest; | |
102 | do { | |
103 | size = strpcpy(&s, size, src); | |
104 | src = va_arg(va, char *); | |
105 | } while (src != NULL); | |
106 | va_end(va); | |
107 | ||
108 | return size; | |
109 | } |