]>
Commit | Line | Data |
---|---|---|
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> | |
30 | #include "strxcpyx.h" | |
31 | ||
32 | size_t strpcpy(char **dest, size_t size, const char *src) | |
33 | { | |
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 | ||
51 | size_t strpcpyf(char **dest, size_t size, const char *src, ...) | |
52 | { | |
53 | va_list va; | |
54 | int i; | |
55 | ||
56 | va_start(va, src); | |
57 | i = vsnprintf(*dest, size, src, va); | |
58 | if (i < (int)size) { | |
59 | *dest += i; | |
60 | size -= i; | |
61 | } else { | |
62 | *dest += size; | |
63 | size = 0; | |
64 | } | |
65 | va_end(va); | |
66 | *dest[0] = '\0'; | |
67 | return size; | |
68 | } | |
69 | ||
70 | size_t strpcpyl(char **dest, size_t size, const char *src, ...) | |
71 | { | |
72 | va_list va; | |
73 | ||
74 | va_start(va, src); | |
75 | do { | |
76 | size = strpcpy(dest, size, src); | |
77 | src = va_arg(va, char *); | |
78 | } while (src != NULL); | |
79 | va_end(va); | |
80 | return size; | |
81 | } | |
82 | ||
83 | size_t strscpy(char *dest, size_t size, const char *src) | |
84 | { | |
85 | char *s; | |
86 | ||
87 | s = dest; | |
88 | return strpcpy(&s, size, src); | |
89 | } | |
90 | ||
91 | size_t strscpyl(char *dest, size_t size, const char *src, ...) { | |
92 | va_list va; | |
93 | char *s; | |
94 | ||
95 | va_start(va, src); | |
96 | s = dest; | |
97 | do { | |
98 | size = strpcpy(&s, size, src); | |
99 | src = va_arg(va, char *); | |
100 | } while (src != NULL); | |
101 | va_end(va); | |
102 | ||
103 | return size; | |
104 | } |