2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2015-10-24 22:58:24 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2010 Lennart Poettering
|
|
|
|
***/
|
|
|
|
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <alloca.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stdbool.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <stddef.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "macro.h"
|
|
|
|
|
2015-11-03 12:25:29 +01:00
|
|
|
/* What is interpreted as whitespace? */
|
|
|
|
#define WHITESPACE " \t\n\r"
|
|
|
|
#define NEWLINE "\n\r"
|
|
|
|
#define QUOTES "\"\'"
|
|
|
|
#define COMMENTS "#;"
|
|
|
|
#define GLOB_CHARS "*?["
|
|
|
|
#define DIGITS "0123456789"
|
|
|
|
#define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
#define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
#define LETTERS LOWERCASE_LETTERS UPPERCASE_LETTERS
|
|
|
|
#define ALPHANUMERICAL LETTERS DIGITS
|
2016-05-17 15:44:32 +02:00
|
|
|
#define HEXDIGITS DIGITS "abcdefABCDEF"
|
2015-11-03 12:25:29 +01:00
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#define streq(a,b) (strcmp((a),(b)) == 0)
|
|
|
|
#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
|
|
|
|
#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
|
|
|
|
#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
|
|
|
|
|
|
|
|
int strcmp_ptr(const char *a, const char *b) _pure_;
|
|
|
|
|
|
|
|
static inline bool streq_ptr(const char *a, const char *b) {
|
|
|
|
return strcmp_ptr(a, b) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char* strempty(const char *s) {
|
2018-01-10 17:11:19 +01:00
|
|
|
return s ?: "";
|
2015-10-24 22:58:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char* strnull(const char *s) {
|
2018-01-10 17:11:19 +01:00
|
|
|
return s ?: "(null)";
|
2015-10-24 22:58:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char *strna(const char *s) {
|
2018-01-10 17:11:19 +01:00
|
|
|
return s ?: "n/a";
|
2015-10-24 22:58:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool isempty(const char *p) {
|
|
|
|
return !p || !p[0];
|
|
|
|
}
|
|
|
|
|
2016-05-30 17:59:43 +02:00
|
|
|
static inline const char *empty_to_null(const char *p) {
|
|
|
|
return isempty(p) ? NULL : p;
|
|
|
|
}
|
|
|
|
|
2018-05-10 18:55:46 +02:00
|
|
|
static inline const char *empty_to_dash(const char *str) {
|
2016-08-31 20:06:57 +02:00
|
|
|
return isempty(str) ? "-" : str;
|
|
|
|
}
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
static inline char *startswith(const char *s, const char *prefix) {
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
l = strlen(prefix);
|
|
|
|
if (strncmp(s, prefix, l) == 0)
|
|
|
|
return (char*) s + l;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *startswith_no_case(const char *s, const char *prefix) {
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
l = strlen(prefix);
|
|
|
|
if (strncasecmp(s, prefix, l) == 0)
|
|
|
|
return (char*) s + l;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *endswith(const char *s, const char *postfix) _pure_;
|
|
|
|
char *endswith_no_case(const char *s, const char *postfix) _pure_;
|
|
|
|
|
|
|
|
char *first_word(const char *s, const char *word) _pure_;
|
|
|
|
|
|
|
|
const char* split(const char **state, size_t *l, const char *separator, bool quoted);
|
|
|
|
|
|
|
|
#define FOREACH_WORD(word, length, s, state) \
|
|
|
|
_FOREACH_WORD(word, length, s, WHITESPACE, false, state)
|
|
|
|
|
|
|
|
#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
|
|
|
|
_FOREACH_WORD(word, length, s, separator, false, state)
|
|
|
|
|
|
|
|
#define _FOREACH_WORD(word, length, s, separator, quoted, state) \
|
|
|
|
for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
|
|
|
|
|
|
|
|
char *strappend(const char *s, const char *suffix);
|
|
|
|
char *strnappend(const char *s, const char *suffix, size_t length);
|
|
|
|
|
2016-10-23 17:43:27 +02:00
|
|
|
char *strjoin_real(const char *x, ...) _sentinel_;
|
|
|
|
#define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
|
2015-10-24 22:58:24 +02:00
|
|
|
|
|
|
|
#define strjoina(a, ...) \
|
|
|
|
({ \
|
|
|
|
const char *_appendees_[] = { a, __VA_ARGS__ }; \
|
|
|
|
char *_d_, *_p_; \
|
2017-09-25 21:56:57 +02:00
|
|
|
size_t _len_ = 0; \
|
tree-wide: be more careful with the type of array sizes
Previously we were a bit sloppy with the index and size types of arrays,
we'd regularly use unsigned. While I don't think this ever resulted in
real issues I think we should be more careful there and follow a
stricter regime: unless there's a strong reason not to use size_t for
array sizes and indexes, size_t it should be. Any allocations we do
ultimately will use size_t anyway, and converting forth and back between
unsigned and size_t will always be a source of problems.
Note that on 32bit machines "unsigned" and "size_t" are equivalent, and
on 64bit machines our arrays shouldn't grow that large anyway, and if
they do we have a problem, however that kind of overly large allocation
we have protections for usually, but for overflows we do not have that
so much, hence let's add it.
So yeah, it's a story of the current code being already "good enough",
but I think some extra type hygiene is better.
This patch tries to be comprehensive, but it probably isn't and I missed
a few cases. But I guess we can cover that later as we notice it. Among
smaller fixes, this changes:
1. strv_length()' return type becomes size_t
2. the unit file changes array size becomes size_t
3. DNS answer and query array sizes become size_t
Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
2018-04-27 14:09:31 +02:00
|
|
|
size_t _i_; \
|
2015-10-24 22:58:24 +02:00
|
|
|
for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
|
|
|
|
_len_ += strlen(_appendees_[_i_]); \
|
|
|
|
_p_ = _d_ = alloca(_len_ + 1); \
|
|
|
|
for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
|
|
|
|
_p_ = stpcpy(_p_, _appendees_[_i_]); \
|
|
|
|
*_p_ = 0; \
|
|
|
|
_d_; \
|
|
|
|
})
|
|
|
|
|
|
|
|
char *strstrip(char *s);
|
|
|
|
char *delete_chars(char *s, const char *bad);
|
2017-11-09 11:12:47 +01:00
|
|
|
char *delete_trailing_chars(char *s, const char *bad);
|
2015-10-24 22:58:24 +02:00
|
|
|
char *truncate_nl(char *s);
|
|
|
|
|
2017-11-09 11:12:47 +01:00
|
|
|
static inline char *skip_leading_chars(const char *s, const char *bad) {
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!bad)
|
|
|
|
bad = WHITESPACE;
|
|
|
|
|
|
|
|
return (char*) s + strspn(s, bad);
|
|
|
|
}
|
|
|
|
|
2016-01-07 19:43:26 +01:00
|
|
|
char ascii_tolower(char x);
|
|
|
|
char *ascii_strlower(char *s);
|
|
|
|
char *ascii_strlower_n(char *s, size_t n);
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2016-07-19 20:43:54 +02:00
|
|
|
char ascii_toupper(char x);
|
|
|
|
char *ascii_strupper(char *s);
|
|
|
|
|
2016-01-13 02:21:16 +01:00
|
|
|
int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
|
2016-01-13 19:45:05 +01:00
|
|
|
int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
|
2016-01-13 02:21:16 +01:00
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
bool chars_intersect(const char *a, const char *b) _pure_;
|
|
|
|
|
|
|
|
static inline bool _pure_ in_charset(const char *s, const char* charset) {
|
|
|
|
assert(s);
|
|
|
|
assert(charset);
|
|
|
|
return s[strspn(s, charset)] == '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
bool string_has_cc(const char *p, const char *ok) _pure_;
|
|
|
|
|
|
|
|
char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
|
2018-06-02 17:52:07 +02:00
|
|
|
static inline char *ellipsize(const char *s, size_t length, unsigned percent) {
|
|
|
|
return ellipsize_mem(s, strlen(s), length, percent);
|
|
|
|
}
|
|
|
|
|
2018-05-17 10:55:21 +02:00
|
|
|
char *cellescape(char *buf, size_t len, const char *s);
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2018-05-17 11:09:07 +02:00
|
|
|
/* This limit is arbitrary, enough to give some idea what the string contains */
|
|
|
|
#define CELLESCAPE_DEFAULT_LENGTH 64
|
|
|
|
|
2017-07-25 01:32:34 +02:00
|
|
|
bool nulstr_contains(const char *nulstr, const char *needle);
|
2015-10-24 22:58:24 +02:00
|
|
|
|
|
|
|
char* strshorten(char *s, size_t l);
|
|
|
|
|
|
|
|
char *strreplace(const char *text, const char *old_string, const char *new_string);
|
|
|
|
|
2018-01-27 13:00:09 +01:00
|
|
|
char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2017-11-28 16:37:53 +01:00
|
|
|
char *strextend_with_separator(char **x, const char *separator, ...) _sentinel_;
|
|
|
|
|
|
|
|
#define strextend(x, ...) strextend_with_separator(x, NULL, __VA_ARGS__)
|
2015-10-24 22:58:24 +02:00
|
|
|
|
|
|
|
char *strrep(const char *s, unsigned n);
|
|
|
|
|
|
|
|
int split_pair(const char *s, const char *sep, char **l, char **r);
|
|
|
|
|
|
|
|
int free_and_strdup(char **p, const char *s);
|
|
|
|
|
|
|
|
/* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
|
|
|
|
static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
|
|
|
|
|
|
|
|
if (needlelen <= 0)
|
|
|
|
return (void*) haystack;
|
|
|
|
|
|
|
|
if (haystacklen < needlelen)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
assert(haystack);
|
|
|
|
assert(needle);
|
|
|
|
|
|
|
|
return memmem(haystack, haystacklen, needle, needlelen);
|
|
|
|
}
|
|
|
|
|
2017-10-03 10:32:34 +02:00
|
|
|
#if !HAVE_EXPLICIT_BZERO
|
2017-02-06 02:05:27 +01:00
|
|
|
void explicit_bzero(void *p, size_t l);
|
|
|
|
#endif
|
|
|
|
|
2015-11-01 20:28:55 +01:00
|
|
|
char *string_erase(char *x);
|
2015-10-24 22:58:24 +02:00
|
|
|
|
|
|
|
char *string_free_erase(char *s);
|
|
|
|
DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase);
|
|
|
|
#define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)
|
2015-10-26 21:26:33 +01:00
|
|
|
|
|
|
|
bool string_is_safe(const char *p) _pure_;
|
2017-07-20 11:38:15 +02:00
|
|
|
|
|
|
|
static inline size_t strlen_ptr(const char *s) {
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return strlen(s);
|
|
|
|
}
|
2018-05-30 13:07:37 +02:00
|
|
|
|
|
|
|
/* Like startswith(), but operates on arbitrary memory blocks */
|
|
|
|
static inline void *memory_startswith(const void *p, size_t sz, const char *token) {
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
assert(token);
|
|
|
|
|
|
|
|
n = strlen(token);
|
|
|
|
if (sz < n)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
assert(p);
|
|
|
|
|
|
|
|
if (memcmp(p, token, n) != 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return (uint8_t*) p + n;
|
|
|
|
}
|