]>
Commit | Line | Data |
---|---|---|
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 | |
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. | |
29 | */ | |
30 | ||
31 | #include <stdarg.h> | |
32 | #include <stdio.h> | |
33 | #include <string.h> | |
34 | ||
35 | #include "strxcpyx.h" | |
36 | ||
37 | size_t strpcpy(char **dest, size_t size, const char *src) { | |
38 | size_t len; | |
39 | ||
40 | assert(dest); | |
41 | assert(src); | |
42 | ||
43 | if (size == 0) | |
44 | return 0; | |
45 | ||
46 | len = strlen(src); | |
47 | if (len >= size) { | |
48 | if (size > 1) | |
49 | *dest = mempcpy(*dest, src, size-1); | |
50 | size = 0; | |
51 | } else { | |
52 | if (len > 0) { | |
53 | *dest = mempcpy(*dest, src, len); | |
54 | size -= len; | |
55 | } | |
56 | } | |
57 | *dest[0] = '\0'; | |
58 | return size; | |
59 | } | |
60 | ||
61 | size_t strpcpyf(char **dest, size_t size, const char *src, ...) { | |
62 | va_list va; | |
63 | int i; | |
64 | ||
65 | assert(dest); | |
66 | assert(src); | |
67 | ||
68 | if (size == 0) | |
69 | return 0; | |
70 | ||
71 | va_start(va, src); | |
72 | i = vsnprintf(*dest, size, src, va); | |
73 | if (i < (int)size) { | |
74 | *dest += i; | |
75 | size -= i; | |
76 | } else { | |
77 | size = 0; | |
78 | } | |
79 | va_end(va); | |
80 | return size; | |
81 | } | |
82 | ||
83 | size_t strpcpyl(char **dest, size_t size, const char *src, ...) { | |
84 | va_list va; | |
85 | ||
86 | assert(dest); | |
87 | assert(src); | |
88 | ||
89 | va_start(va, src); | |
90 | do { | |
91 | size = strpcpy(dest, size, src); | |
92 | src = va_arg(va, char *); | |
93 | } while (src != NULL); | |
94 | va_end(va); | |
95 | return size; | |
96 | } | |
97 | ||
98 | size_t strscpy(char *dest, size_t size, const char *src) { | |
99 | char *s; | |
100 | ||
101 | assert(dest); | |
102 | assert(src); | |
103 | ||
104 | s = dest; | |
105 | return strpcpy(&s, size, src); | |
106 | } | |
107 | ||
108 | size_t strscpyl(char *dest, size_t size, const char *src, ...) { | |
109 | va_list va; | |
110 | char *s; | |
111 | ||
112 | assert(dest); | |
113 | assert(src); | |
114 | ||
115 | va_start(va, src); | |
116 | s = dest; | |
117 | do { | |
118 | size = strpcpy(&s, size, src); | |
119 | src = va_arg(va, char *); | |
120 | } while (src != NULL); | |
121 | va_end(va); | |
122 | ||
123 | return size; | |
124 | } |