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