string-util.c revision c174983474d4a010a18e3bb9a59e351a442480f5
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "alloc-util.h"
#include "gunicode.h"
#include "macro.h"
#include "string-util.h"
#include "utf8.h"
#include "util.h"
int strcmp_ptr(const char *a, const char *b) {
/* Like strcmp(), but tries to make sense of NULL pointers */
if (a && b)
return strcmp(a, b);
if (!a && b)
return -1;
if (a && !b)
return 1;
return 0;
}
assert(s);
if (pl == 0)
return (char*) s + sl;
return NULL;
return NULL;
}
char* endswith_no_case(const char *s, const char *postfix) {
assert(s);
if (pl == 0)
return (char*) s + sl;
return NULL;
return NULL;
}
char* first_word(const char *s, const char *word) {
const char *p;
assert(s);
/* Checks if the string starts with the specified word, either
* followed by NUL or by whitespace. Returns a pointer to the
* NUL or the first character after the whitespace. */
return NULL;
if (wl == 0)
return (char*) s;
return NULL;
p = s + wl;
if (*p == 0)
return (char*) p;
if (!strchr(WHITESPACE, *p))
return NULL;
p += strspn(p, WHITESPACE);
return (char*) p;
}
bool escaped = false;
int n;
for (n=0; s[n]; n++) {
if (escaped)
escaped = false;
else if (s[n] == '\\')
escaped = true;
break;
}
/* if s ends in \, return index of previous char */
return n - escaped;
}
/* Split a string into words. */
const char *current;
if (!*current) {
return NULL;
}
if (!*current) {
return NULL;
}
/* right quote missing or garbage at the end */
return NULL;
}
} else if (quoted) {
/* unfinished escape */
return NULL;
}
} else {
}
return current;
}
size_t a;
char *r;
if (!s && !suffix)
return strdup("");
if (!s)
if (!suffix)
return strdup(s);
assert(s);
a = strlen(s);
if (b > ((size_t) -1) - a)
return NULL;
r = new(char, a+b+1);
if (!r)
return NULL;
memcpy(r, s, a);
r[a+b] = 0;
return r;
}
}
char *strjoin(const char *x, ...) {
size_t l;
char *r, *p;
if (x) {
l = strlen(x);
for (;;) {
const char *t;
size_t n;
if (!t)
break;
n = strlen(t);
if (n > ((size_t) -1) - l) {
return NULL;
}
l += n;
}
} else
l = 0;
r = new(char, l+1);
if (!r)
return NULL;
if (x) {
p = stpcpy(r, x);
for (;;) {
const char *t;
if (!t)
break;
p = stpcpy(p, t);
}
} else
r[0] = 0;
return r;
}
char *strstrip(char *s) {
char *e;
/* Drops trailing whitespace. Modifies the string in
* place. Returns pointer to first non-space character */
s += strspn(s, WHITESPACE);
for (e = strchr(s, 0); e > s; e --)
break;
*e = 0;
return s;
}
char *delete_chars(char *s, const char *bad) {
char *f, *t;
/* Drops all whitespace, regardless where in the string */
for (f = s, t = s; *f; f++) {
continue;
*(t++) = *f;
}
*t = 0;
return s;
}
char *truncate_nl(char *s) {
assert(s);
return s;
}
char ascii_tolower(char x) {
if (x >= 'A' && x <= 'Z')
return x - 'A' + 'a';
return x;
}
char *ascii_strlower(char *t) {
char *p;
assert(t);
for (p = t; *p; p++)
*p = ascii_tolower(*p);
return t;
}
char *ascii_strlower_n(char *t, size_t n) {
size_t i;
if (n <= 0)
return t;
for (i = 0; i < n; i++)
t[i] = ascii_tolower(t[i]);
return t;
}
int ascii_strcasecmp_n(const char *a, const char *b, size_t n) {
for (; n > 0; a++, b++, n--) {
int x, y;
x = (int) (uint8_t) ascii_tolower(*a);
y = (int) (uint8_t) ascii_tolower(*b);
if (x != y)
return x - y;
}
return 0;
}
int r;
r = ascii_strcasecmp_n(a, b, MIN(n, m));
if (r != 0)
return r;
if (n < m)
return -1;
else if (n > m)
return 1;
else
return 0;
}
bool chars_intersect(const char *a, const char *b) {
const char *p;
/* Returns true if any of the chars in a are in b. */
for (p = a; *p; p++)
if (strchr(b, *p))
return true;
return false;
}
bool string_has_cc(const char *p, const char *ok) {
const char *t;
assert(p);
/*
* Check if a string contains control characters. If 'ok' is
* non-NULL it may be a string containing additional CCs to be
* considered OK.
*/
for (t = p; *t; t++) {
continue;
if (*t > 0 && *t < ' ')
return true;
if (*t == 127)
return true;
}
return false;
}
static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
size_t x;
char *r;
assert(s);
return strndup(s, old_length);
if (!r)
return NULL;
if (x > new_length - 3)
x = new_length - 3;
memcpy(r, s, x);
r[x] = '.';
r[x+1] = '.';
r[x+2] = '.';
memcpy(r + x + 3,
new_length - x - 3);
return r;
}
size_t x;
char *e;
const char *i, *j;
assert(s);
/* if no multibyte characters use ascii_ellipsize_mem for speed */
if (ascii_is_valid(s))
return strndup(s, old_length);
if (x > new_length - 3)
x = new_length - 3;
k = 0;
for (i = s; k < x && i < s + old_length; i = utf8_next_char(i)) {
int c;
c = utf8_encoded_to_unichar(i);
if (c < 0)
return NULL;
}
if (k > x) /* last character was wide and went over quota */
x ++;
for (j = s + old_length; k < new_length && j > i; ) {
int c;
j = utf8_prev_char(j);
c = utf8_encoded_to_unichar(j);
if (c < 0)
return NULL;
}
assert(i <= j);
/* we don't actually need to ellipsize */
if (i == j)
/* make space for ellipsis */
j = utf8_next_char(j);
len = i - s;
len2 = s + old_length - j;
if (!e)
return NULL;
/*
printf("old_length=%zu new_length=%zu x=%zu len=%u len2=%u k=%u\n",
old_length, new_length, x, len, len2, k);
*/
return e;
}
}
const char *i;
if (!nulstr)
return false;
NULSTR_FOREACH(i, nulstr)
return true;
return false;
}
char* strshorten(char *s, size_t l) {
assert(s);
if (l < strlen(s))
s[l] = 0;
return s;
}
const char *f;
char *t, *r;
r = new(char, l+1);
if (!r)
return NULL;
f = text;
t = r;
while (*f) {
char *a;
if (!startswith(f, old_string)) {
*(t++) = *(f++);
continue;
}
d = t - r;
if (!a)
goto oom;
l = nl;
r = a;
t = r + d;
t = stpcpy(t, new_string);
f += old_len;
}
*t = 0;
return r;
oom:
free(r);
return NULL;
}
enum {
} state = STATE_OTHER;
FILE *f;
/* Strips ANSI color and replaces TABs by 8 spaces */
if (!f)
return NULL;
switch (state) {
case STATE_OTHER:
break;
else if (*i == '\x1B')
else if (*i == '\t')
fputs(" ", f);
else
fputc(*i, f);
break;
case STATE_ESCAPE:
fputc('\x1B', f);
break;
} else if (*i == '[') {
begin = i + 1;
} else {
fputc('\x1B', f);
fputc(*i, f);
state = STATE_OTHER;
}
break;
case STATE_BRACKET:
(!(*i >= '0' && *i <= '9') && *i != ';' && *i != 'm')) {
fputc('\x1B', f);
fputc('[', f);
state = STATE_OTHER;
i = begin-1;
} else if (*i == 'm')
state = STATE_OTHER;
break;
}
}
if (ferror(f)) {
fclose(f);
return NULL;
}
fclose(f);
if (_isz)
return obuf;
}
char *strextend(char **x, ...) {
size_t f, l;
char *r, *p;
assert(x);
l = f = *x ? strlen(*x) : 0;
for (;;) {
const char *t;
size_t n;
if (!t)
break;
n = strlen(t);
if (n > ((size_t) -1) - l) {
return NULL;
}
l += n;
}
r = realloc(*x, l+1);
if (!r)
return NULL;
p = r + f;
for (;;) {
const char *t;
if (!t)
break;
p = stpcpy(p, t);
}
*p = 0;
*x = r;
return r + l;
}
char *strrep(const char *s, unsigned n) {
size_t l;
char *r, *p;
unsigned i;
assert(s);
l = strlen(s);
p = r = malloc(l * n + 1);
if (!r)
return NULL;
for (i = 0; i < n; i++)
p = stpcpy(p, s);
*p = 0;
return r;
}
int split_pair(const char *s, const char *sep, char **l, char **r) {
char *x, *a, *b;
assert(s);
assert(l);
assert(r);
return -EINVAL;
if (!x)
return -EINVAL;
a = strndup(s, x - s);
if (!a)
return -ENOMEM;
if (!b) {
free(a);
return -ENOMEM;
}
*l = a;
*r = b;
return 0;
}
int free_and_strdup(char **p, const char *s) {
char *t;
assert(p);
/* Replaces a string pointer with an strdup()ed new string,
* possibly freeing the old one. */
if (streq_ptr(*p, s))
return 0;
if (s) {
t = strdup(s);
if (!t)
return -ENOMEM;
} else
t = NULL;
free(*p);
*p = t;
return 1;
}
#pragma GCC push_options
void* memory_erase(void *p, size_t l) {
/* This basically does what memset() does, but hopefully isn't
* optimized away by the compiler. One of those days, when
* glibc learns memset_s() we should replace this call by
* memset_s(), but until then this has to do. */
for (; l > 0; l--)
*(x++) = 'x';
return p;
}
#pragma GCC pop_options
char* string_erase(char *x) {
if (!x)
return NULL;
/* A delicious drop of snake-oil! To be called on memory where
* we stored passphrases or so, after we used them. */
return memory_erase(x, strlen(x));
}
char *string_free_erase(char *s) {
return mfree(string_erase(s));
}
bool string_is_safe(const char *p) {
const char *t;
if (!p)
return false;
for (t = p; *t; t++) {
if (*t > 0 && *t < ' ') /* no control characters */
return false;
return false;
}
return true;
}