]>
Commit | Line | Data |
---|---|---|
07630cea LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | #pragma once | |
4 | ||
5 | /*** | |
6 | This file is part of systemd. | |
7 | ||
8 | Copyright 2010 Lennart Poettering | |
9 | ||
10 | systemd is free software; you can redistribute it and/or modify it | |
11 | under the terms of the GNU Lesser General Public License as published by | |
12 | the Free Software Foundation; either version 2.1 of the License, or | |
13 | (at your option) any later version. | |
14 | ||
15 | systemd is distributed in the hope that it will be useful, but | |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | Lesser General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU Lesser General Public License | |
21 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
22 | ***/ | |
23 | ||
24 | #include <stdbool.h> | |
25 | #include <string.h> | |
26 | ||
27 | #include "macro.h" | |
28 | ||
29 | #define streq(a,b) (strcmp((a),(b)) == 0) | |
30 | #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0) | |
31 | #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0) | |
32 | #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0) | |
33 | ||
34 | int strcmp_ptr(const char *a, const char *b) _pure_; | |
35 | ||
36 | static inline bool streq_ptr(const char *a, const char *b) { | |
37 | return strcmp_ptr(a, b) == 0; | |
38 | } | |
39 | ||
40 | static inline const char* strempty(const char *s) { | |
41 | return s ? s : ""; | |
42 | } | |
43 | ||
44 | static inline const char* strnull(const char *s) { | |
45 | return s ? s : "(null)"; | |
46 | } | |
47 | ||
48 | static inline const char *strna(const char *s) { | |
49 | return s ? s : "n/a"; | |
50 | } | |
51 | ||
52 | static inline bool isempty(const char *p) { | |
53 | return !p || !p[0]; | |
54 | } | |
55 | ||
56 | static inline char *startswith(const char *s, const char *prefix) { | |
57 | size_t l; | |
58 | ||
59 | l = strlen(prefix); | |
60 | if (strncmp(s, prefix, l) == 0) | |
61 | return (char*) s + l; | |
62 | ||
63 | return NULL; | |
64 | } | |
65 | ||
66 | static inline char *startswith_no_case(const char *s, const char *prefix) { | |
67 | size_t l; | |
68 | ||
69 | l = strlen(prefix); | |
70 | if (strncasecmp(s, prefix, l) == 0) | |
71 | return (char*) s + l; | |
72 | ||
73 | return NULL; | |
74 | } | |
75 | ||
76 | char *endswith(const char *s, const char *postfix) _pure_; | |
77 | char *endswith_no_case(const char *s, const char *postfix) _pure_; | |
78 | ||
79 | char *first_word(const char *s, const char *word) _pure_; | |
80 | ||
81 | const char* split(const char **state, size_t *l, const char *separator, bool quoted); | |
82 | ||
83 | #define FOREACH_WORD(word, length, s, state) \ | |
84 | _FOREACH_WORD(word, length, s, WHITESPACE, false, state) | |
85 | ||
86 | #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \ | |
87 | _FOREACH_WORD(word, length, s, separator, false, state) | |
88 | ||
89 | #define FOREACH_WORD_QUOTED(word, length, s, state) \ | |
90 | _FOREACH_WORD(word, length, s, WHITESPACE, true, state) | |
91 | ||
92 | #define _FOREACH_WORD(word, length, s, separator, quoted, state) \ | |
93 | for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted))) | |
94 | ||
95 | char *strappend(const char *s, const char *suffix); | |
96 | char *strnappend(const char *s, const char *suffix, size_t length); | |
97 | ||
98 | char *strjoin(const char *x, ...) _sentinel_; | |
99 | ||
100 | #define strjoina(a, ...) \ | |
101 | ({ \ | |
102 | const char *_appendees_[] = { a, __VA_ARGS__ }; \ | |
103 | char *_d_, *_p_; \ | |
104 | int _len_ = 0; \ | |
105 | unsigned _i_; \ | |
106 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ | |
107 | _len_ += strlen(_appendees_[_i_]); \ | |
108 | _p_ = _d_ = alloca(_len_ + 1); \ | |
109 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ | |
110 | _p_ = stpcpy(_p_, _appendees_[_i_]); \ | |
111 | *_p_ = 0; \ | |
112 | _d_; \ | |
113 | }) | |
114 | ||
115 | char *strstrip(char *s); | |
116 | char *delete_chars(char *s, const char *bad); | |
117 | char *truncate_nl(char *s); | |
118 | ||
119 | char *ascii_strlower(char *path); | |
120 | ||
121 | bool chars_intersect(const char *a, const char *b) _pure_; | |
122 | ||
123 | static inline bool _pure_ in_charset(const char *s, const char* charset) { | |
124 | assert(s); | |
125 | assert(charset); | |
126 | return s[strspn(s, charset)] == '\0'; | |
127 | } | |
128 | ||
129 | bool string_has_cc(const char *p, const char *ok) _pure_; | |
130 | ||
131 | char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent); | |
132 | char *ellipsize(const char *s, size_t length, unsigned percent); | |
133 | ||
134 | bool nulstr_contains(const char*nulstr, const char *needle); | |
135 | ||
136 | char* strshorten(char *s, size_t l); | |
137 | ||
138 | char *strreplace(const char *text, const char *old_string, const char *new_string); | |
139 | ||
140 | char *strip_tab_ansi(char **p, size_t *l); | |
141 | ||
142 | char *strextend(char **x, ...) _sentinel_; | |
143 | ||
144 | char *strrep(const char *s, unsigned n); | |
145 | ||
146 | int split_pair(const char *s, const char *sep, char **l, char **r); | |
147 | ||
148 | int free_and_strdup(char **p, const char *s); | |
149 | ||
150 | /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */ | |
151 | static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) { | |
152 | ||
153 | if (needlelen <= 0) | |
154 | return (void*) haystack; | |
155 | ||
156 | if (haystacklen < needlelen) | |
157 | return NULL; | |
158 | ||
159 | assert(haystack); | |
160 | assert(needle); | |
161 | ||
162 | return memmem(haystack, haystacklen, needle, needlelen); | |
163 | } | |
164 | ||
165 | #define memory_erase(p, l) memset((p), 'x', (l)) | |
166 | void string_erase(char *x); | |
167 | ||
168 | char *string_free_erase(char *s); | |
169 | DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase); | |
170 | #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep) |