]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/strxcpyx.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / basic / strxcpyx.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
d5a89d7d
KS
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
9e5bd85a
VC
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.
d5a89d7d
KS
30 */
31
11c3a366 32#include <stdarg.h>
d5a89d7d
KS
33#include <stdio.h>
34#include <string.h>
cf0fbc49 35
d5a89d7d
KS
36#include "strxcpyx.h"
37
f168c273 38size_t strpcpy(char **dest, size_t size, const char *src) {
d5a89d7d
KS
39 size_t len;
40
f91049d5
VC
41 assert(dest);
42 assert(src);
43
9e5bd85a
VC
44 if (size == 0)
45 return 0;
46
d5a89d7d
KS
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
f168c273 62size_t strpcpyf(char **dest, size_t size, const char *src, ...) {
d5a89d7d
KS
63 va_list va;
64 int i;
65
f91049d5
VC
66 assert(dest);
67 assert(src);
68
9e5bd85a
VC
69 if (size == 0)
70 return 0;
71
d5a89d7d
KS
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 {
d5a89d7d
KS
78 size = 0;
79 }
80 va_end(va);
d5a89d7d
KS
81 return size;
82}
83
f168c273 84size_t strpcpyl(char **dest, size_t size, const char *src, ...) {
d5a89d7d
KS
85 va_list va;
86
f91049d5
VC
87 assert(dest);
88 assert(src);
89
d5a89d7d
KS
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
f168c273 99size_t strscpy(char *dest, size_t size, const char *src) {
d5a89d7d
KS
100 char *s;
101
f91049d5
VC
102 assert(dest);
103 assert(src);
104
d5a89d7d
KS
105 s = dest;
106 return strpcpy(&s, size, src);
107}
108
109size_t strscpyl(char *dest, size_t size, const char *src, ...) {
110 va_list va;
111 char *s;
112
f91049d5
VC
113 assert(dest);
114 assert(src);
115
d5a89d7d
KS
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}