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