]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/strxcpyx.c
strxcpyx: assert throughout on non-NULL src/dest
[thirdparty/systemd.git] / src / basic / strxcpyx.c
CommitLineData
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 37size_t strpcpy(char **dest, size_t size, const char *src) {
d5a89d7d
KS
38 size_t len;
39
f91049d5
VC
40 assert(dest);
41 assert(src);
42
9e5bd85a
VC
43 if (size == 0)
44 return 0;
45
d5a89d7d
KS
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
f168c273 61size_t strpcpyf(char **dest, size_t size, const char *src, ...) {
d5a89d7d
KS
62 va_list va;
63 int i;
64
f91049d5
VC
65 assert(dest);
66 assert(src);
67
9e5bd85a
VC
68 if (size == 0)
69 return 0;
70
d5a89d7d
KS
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 {
d5a89d7d
KS
77 size = 0;
78 }
79 va_end(va);
d5a89d7d
KS
80 return size;
81}
82
f168c273 83size_t strpcpyl(char **dest, size_t size, const char *src, ...) {
d5a89d7d
KS
84 va_list va;
85
f91049d5
VC
86 assert(dest);
87 assert(src);
88
d5a89d7d
KS
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
f168c273 98size_t strscpy(char *dest, size_t size, const char *src) {
d5a89d7d
KS
99 char *s;
100
f91049d5
VC
101 assert(dest);
102 assert(src);
103
d5a89d7d
KS
104 s = dest;
105 return strpcpy(&s, size, src);
106}
107
108size_t strscpyl(char *dest, size_t size, const char *src, ...) {
109 va_list va;
110 char *s;
111
f91049d5
VC
112 assert(dest);
113 assert(src);
114
d5a89d7d
KS
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}