summaryrefslogtreecommitdiff
path: root/src/libudev
diff options
context:
space:
mode:
authorAnthony G. Basile <blueness@gentoo.org>2013-06-04 12:53:32 -0400
committerAnthony G. Basile <blueness@gentoo.org>2013-06-04 12:53:32 -0400
commit6ca44589b07c2a809718c4a630005b999f53d58d (patch)
tree7a67c0c8237f8595d900e690f69cca8851e2dffd /src/libudev
parentfcb3dc92650f0ab01816f258b5e521b5daab29a3 (diff)
src/libudev/strv.{c,h}: bring in line with upstream
In doing so, we better alined util.{c,h} by movig the definitions of freep, fclosep, closep, closedirp, umaskp from util.c to util.h as static inline. We also add strv_env_get to env-util.{c,h} which we forgot to do earlier. Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
Diffstat (limited to 'src/libudev')
-rw-r--r--src/libudev/env-util.c5
-rw-r--r--src/libudev/env-util.h1
-rw-r--r--src/libudev/strv.c343
-rw-r--r--src/libudev/strv.h49
-rw-r--r--src/libudev/util.c70
-rw-r--r--src/libudev/util.h35
6 files changed, 150 insertions, 353 deletions
diff --git a/src/libudev/env-util.c b/src/libudev/env-util.c
index 0cb3977b37..c2381ec6a3 100644
--- a/src/libudev/env-util.c
+++ b/src/libudev/env-util.c
@@ -42,3 +42,8 @@ char *strv_env_get_n(char **l, const char *name, size_t k) {
return NULL;
}
+char *strv_env_get(char **l, const char *name) {
+ assert(name);
+
+ return strv_env_get_n(l, name, strlen(name));
+}
diff --git a/src/libudev/env-util.h b/src/libudev/env-util.h
index 61a5332d63..2aa30aeda3 100644
--- a/src/libudev/env-util.h
+++ b/src/libudev/env-util.h
@@ -22,3 +22,4 @@
***/
char *strv_env_get_n(char **l, const char *name, size_t k) _pure_;
+char *strv_env_get(char **x, const char *n) _pure_;
diff --git a/src/libudev/strv.c b/src/libudev/strv.c
index 6b76d0eaef..a5ce7e9593 100644
--- a/src/libudev/strv.c
+++ b/src/libudev/strv.c
@@ -64,15 +64,7 @@ void strv_free(char **l) {
free(l);
}
-void strv_freep(char ***l) {
- if (!l)
- return;
-
- strv_free(*l);
- *l = NULL;
-}
-
-char **strv_copy(char **l) {
+char **strv_copy(char * const *l) {
char **r, **k;
k = r = new(char*, strv_length(l) + 1);
@@ -92,7 +84,7 @@ char **strv_copy(char **l) {
return r;
}
-unsigned strv_length(char **l) {
+unsigned strv_length(char * const *l) {
unsigned n = 0;
if (!l)
@@ -305,6 +297,31 @@ char **strv_split_quoted(const char *s) {
return r;
}
+char **strv_split_newlines(const char *s) {
+ char **l;
+ unsigned n;
+
+ assert(s);
+
+ /* Special version of strv_split() that splits on newlines and
+ * suppresses an empty string at the end */
+
+ l = strv_split(s, NEWLINE);
+ if (!l)
+ return NULL;
+
+ n = strv_length(l);
+ if (n <= 0)
+ return l;
+
+ if (isempty(l[n-1])) {
+ free(l[n-1]);
+ l[n-1] = NULL;
+ }
+
+ return l;
+}
+
char *strv_join(char **l, const char *separator) {
char *r, *e;
char **s;
@@ -370,6 +387,43 @@ fail:
return NULL;
}
+int strv_push(char ***l, char *value) {
+ char **c;
+ unsigned n;
+
+ if (!value)
+ return 0;
+
+ n = strv_length(*l);
+ c = realloc(*l, sizeof(char*) * (n + 2));
+ if (!c)
+ return -ENOMEM;
+
+ c[n] = value;
+ c[n+1] = NULL;
+
+ *l = c;
+ return 0;
+}
+
+int strv_extend(char ***l, const char *value) {
+ char *v;
+ int r;
+
+ if (!value)
+ return 0;
+
+ v = strdup(value);
+ if (!v)
+ return -ENOMEM;
+
+ r = strv_push(l, v);
+ if (r < 0)
+ free(v);
+
+ return r;
+}
+
char **strv_uniq(char **l) {
char **i;
@@ -432,249 +486,6 @@ char **strv_remove_prefix(char **l, const char *s) {
return l;
}
-static int env_append(char **r, char ***k, char **a) {
- assert(r);
- assert(k);
-
- if (!a)
- return 0;
-
- /* Add the entries of a to *k unless they already exist in *r
- * in which case they are overridden instead. This assumes
- * there is enough space in the r array. */
-
- for (; *a; a++) {
- char **j;
- size_t n;
-
- n = strcspn(*a, "=");
-
- if ((*a)[n] == '=')
- n++;
-
- for (j = r; j < *k; j++)
- if (strncmp(*j, *a, n) == 0)
- break;
-
- if (j >= *k)
- (*k)++;
- else
- free(*j);
-
- *j = strdup(*a);
- if (!*j)
- return -ENOMEM;
- }
-
- return 0;
-}
-
-char **strv_env_merge(unsigned n_lists, ...) {
- size_t n = 0;
- char **l, **k, **r;
- va_list ap;
- unsigned i;
-
- /* Merges an arbitrary number of environment sets */
-
- va_start(ap, n_lists);
- for (i = 0; i < n_lists; i++) {
- l = va_arg(ap, char**);
- n += strv_length(l);
- }
- va_end(ap);
-
- r = new(char*, n+1);
- if (!r)
- return NULL;
-
- k = r;
-
- va_start(ap, n_lists);
- for (i = 0; i < n_lists; i++) {
- l = va_arg(ap, char**);
- if (env_append(r, &k, l) < 0)
- goto fail;
- }
- va_end(ap);
-
- *k = NULL;
-
- return r;
-
-fail:
- va_end(ap);
- strv_free(r);
-
- return NULL;
-}
-
-static bool env_match(const char *t, const char *pattern) {
- assert(t);
- assert(pattern);
-
- /* pattern a matches string a
- * a matches a=
- * a matches a=b
- * a= matches a=
- * a=b matches a=b
- * a= does not match a
- * a=b does not match a=
- * a=b does not match a
- * a=b does not match a=c */
-
- if (streq(t, pattern))
- return true;
-
- if (!strchr(pattern, '=')) {
- size_t l = strlen(pattern);
-
- return strncmp(t, pattern, l) == 0 && t[l] == '=';
- }
-
- return false;
-}
-
-char **strv_env_delete(char **x, unsigned n_lists, ...) {
- size_t n, i = 0;
- char **k, **r;
- va_list ap;
-
- /* Deletes every entry from x that is mentioned in the other
- * string lists */
-
- n = strv_length(x);
-
- r = new(char*, n+1);
- if (!r)
- return NULL;
-
- STRV_FOREACH(k, x) {
- unsigned v;
-
- va_start(ap, n_lists);
- for (v = 0; v < n_lists; v++) {
- char **l, **j;
-
- l = va_arg(ap, char**);
- STRV_FOREACH(j, l)
- if (env_match(*k, *j))
- goto skip;
- }
- va_end(ap);
-
- r[i] = strdup(*k);
- if (!r[i]) {
- strv_free(r);
- return NULL;
- }
-
- i++;
- continue;
-
- skip:
- va_end(ap);
- }
-
- r[i] = NULL;
-
- assert(i <= n);
-
- return r;
-}
-
-char **strv_env_unset(char **l, const char *p) {
-
- char **f, **t;
-
- if (!l)
- return NULL;
-
- assert(p);
-
- /* Drops every occurrence of the env var setting p in the
- * string list. edits in-place. */
-
- for (f = t = l; *f; f++) {
-
- if (env_match(*f, p)) {
- free(*f);
- continue;
- }
-
- *(t++) = *f;
- }
-
- *t = NULL;
- return l;
-}
-
-char **strv_env_set(char **x, const char *p) {
-
- char **k, **r;
- char* m[2] = { (char*) p, NULL };
-
- /* Overrides the env var setting of p, returns a new copy */
-
- r = new(char*, strv_length(x)+2);
- if (!r)
- return NULL;
-
- k = r;
- if (env_append(r, &k, x) < 0)
- goto fail;
-
- if (env_append(r, &k, m) < 0)
- goto fail;
-
- *k = NULL;
-
- return r;
-
-fail:
- strv_free(r);
- return NULL;
-
-}
-
-char *strv_env_get_with_length(char **l, const char *name, size_t k) {
- char **i;
-
- assert(name);
-
- STRV_FOREACH(i, l)
- if (strncmp(*i, name, k) == 0 &&
- (*i)[k] == '=')
- return *i + k + 1;
-
- return NULL;
-}
-
-char *strv_env_get(char **l, const char *name) {
- return strv_env_get_with_length(l, name, strlen(name));
-}
-
-char **strv_env_clean(char **l) {
- char **r, **ret;
-
- for (r = ret = l; *l; l++) {
- const char *equal;
-
- equal = strchr(*l, '=');
-
- if (equal && equal[1] == 0) {
- free(*l);
- continue;
- }
-
- *(r++) = *l;
- }
-
- *r = NULL;
-
- return ret;
-}
-
char **strv_parse_nulstr(const char *s, size_t l) {
const char *p;
unsigned c = 0, i = 0;
@@ -721,6 +532,22 @@ char **strv_parse_nulstr(const char *s, size_t l) {
return v;
}
+char **strv_split_nulstr(const char *s) {
+ const char *i;
+ char **r = NULL;
+
+ NULSTR_FOREACH(i, s)
+ if (strv_extend(&r, i) < 0) {
+ strv_free(r);
+ return NULL;
+ }
+
+ if (!r)
+ return strv_new(NULL, NULL);
+
+ return r;
+}
+
bool strv_overlap(char **a, char **b) {
char **i, **j;
@@ -748,3 +575,13 @@ char **strv_sort(char **l) {
qsort(l, strv_length(l), sizeof(char*), str_compare);
return l;
}
+
+void strv_print(char **l) {
+ char **s;
+
+ if (!l)
+ return;
+
+ STRV_FOREACH(s, l)
+ puts(*s);
+}
diff --git a/src/libudev/strv.h b/src/libudev/strv.h
index 45558d8960..e35118752f 100644
--- a/src/libudev/strv.h
+++ b/src/libudev/strv.h
@@ -26,17 +26,24 @@
#include "macro.h"
-char *strv_find(char **l, const char *name);
-char *strv_find_prefix(char **l, const char *name);
+char *strv_find(char **l, const char *name) _pure_;
+char *strv_find_prefix(char **l, const char *name) _pure_;
void strv_free(char **l);
-void strv_freep(char ***l);
-char **strv_copy(char **l) _malloc_;
-unsigned strv_length(char **l);
+static inline void strv_freep(char ***l) {
+ strv_free(*l);
+}
+
+#define _cleanup_strv_free_ _cleanup_(strv_freep)
+
+char **strv_copy(char * const *l);
+unsigned strv_length(char * const *l) _pure_;
char **strv_merge(char **a, char **b);
char **strv_merge_concat(char **a, char **b, const char *suffix);
char **strv_append(char **l, const char *s);
+int strv_extend(char ***l, const char *value);
+int strv_push(char ***l, char *value);
char **strv_remove(char **l, const char *s);
char **strv_remove_prefix(char **l, const char *s);
@@ -44,36 +51,27 @@ char **strv_uniq(char **l);
#define strv_contains(l, s) (!!strv_find((l), (s)))
-char **strv_new(const char *x, ...) _sentinel_ _malloc_;
-char **strv_new_ap(const char *x, va_list ap) _malloc_;
+char **strv_new(const char *x, ...) _sentinel_;
+char **strv_new_ap(const char *x, va_list ap);
static inline const char* STRV_IFNOTNULL(const char *x) {
return x ? x : (const char *) -1;
}
-static inline bool strv_isempty(char **l) {
+static inline bool strv_isempty(char * const *l) {
return !l || !*l;
}
-char **strv_split(const char *s, const char *separator) _malloc_;
-char **strv_split_quoted(const char *s) _malloc_;
-
-char *strv_join(char **l, const char *separator) _malloc_;
-
-char **strv_env_merge(unsigned n_lists, ...);
-char **strv_env_delete(char **x, unsigned n_lists, ...);
+char **strv_split(const char *s, const char *separator);
+char **strv_split_quoted(const char *s);
+char **strv_split_newlines(const char *s);
-char **strv_env_set(char **x, const char *p);
-char **strv_env_unset(char **l, const char *p);
-
-char *strv_env_get_with_length(char **l, const char *name, size_t k);
-char *strv_env_get(char **x, const char *n);
-
-char **strv_env_clean(char **l);
+char *strv_join(char **l, const char *separator);
char **strv_parse_nulstr(const char *s, size_t l);
+char **strv_split_nulstr(const char *s);
-bool strv_overlap(char **a, char **b);
+bool strv_overlap(char **a, char **b) _pure_;
#define STRV_FOREACH(s, l) \
for ((s) = (l); (s) && *(s); (s)++)
@@ -81,4 +79,9 @@ bool strv_overlap(char **a, char **b);
#define STRV_FOREACH_BACKWARDS(s, l) \
for (; (l) && ((s) >= (l)); (s)--)
+#define STRV_FOREACH_PAIR(x, y, l) \
+ for ((x) = (l), (y) = (x+1); (x) && *(x) && *(y); (x) += 2, (y) = (x + 1))
+
+
char **strv_sort(char **l);
+void strv_print(char **l);
diff --git a/src/libudev/util.c b/src/libudev/util.c
index b93b5c1b68..09c7033274 100644
--- a/src/libudev/util.c
+++ b/src/libudev/util.c
@@ -5948,76 +5948,6 @@ int get_home_dir(char **_h) {
return 0;
}
-int get_shell(char **_sh) {
- char *sh;
- const char *e;
- uid_t u;
- struct passwd *p;
-
- assert(_sh);
-
- /* Take the user specified one */
- e = getenv("SHELL");
- if (e) {
- sh = strdup(e);
- if (!sh)
- return -ENOMEM;
-
- *_sh = sh;
- return 0;
- }
-
- /* Hardcode home directory for root to avoid NSS */
- u = getuid();
- if (u == 0) {
- sh = strdup("/bin/sh");
- if (!sh)
- return -ENOMEM;
-
- *_sh = sh;
- return 0;
- }
-
- /* Check the database... */
- errno = 0;
- p = getpwuid(u);
- if (!p)
- return errno ? -errno : -ESRCH;
-
- if (!path_is_absolute(p->pw_shell))
- return -EINVAL;
-
- sh = strdup(p->pw_shell);
- if (!sh)
- return -ENOMEM;
-
- *_sh = sh;
- return 0;
-}
-
-void freep(void *p) {
- free(*(void**) p);
-}
-
-void fclosep(FILE **f) {
- if (*f)
- fclose(*f);
-}
-
-void closep(int *fd) {
- if (*fd >= 0)
- close_nointr_nofail(*fd);
-}
-
-void closedirp(DIR **d) {
- if (*d)
- closedir(*d);
-}
-
-void umaskp(mode_t *u) {
- umask(*u);
-}
-
bool filename_is_safe(const char *p) {
if (isempty(p))
diff --git a/src/libudev/util.h b/src/libudev/util.h
index 6748076943..fc58943b38 100644
--- a/src/libudev/util.h
+++ b/src/libudev/util.h
@@ -553,7 +553,7 @@ char *format_bytes(char *buf, size_t l, off_t t);
int fd_wait_for_event(int fd, int event, usec_t timeout);
-void* memdup(const void *p, size_t l) _malloc_;
+void* memdup(const void *p, size_t l) _alloc_(2);
int is_kernel_thread(pid_t pid);
@@ -575,14 +575,35 @@ bool in_initrd(void);
void warn_melody(void);
-int get_shell(char **ret);
int get_home_dir(char **ret);
-void freep(void *p);
-void fclosep(FILE **f);
-void closep(int *fd);
-void closedirp(DIR **d);
-void umaskp(mode_t *u);
+static inline void freep(void *p) {
+ free(*(void**) p);
+}
+
+static inline void fclosep(FILE **f) {
+ if (*f)
+ fclose(*f);
+}
+
+static inline void pclosep(FILE **f) {
+ if (*f)
+ pclose(*f);
+}
+
+static inline void closep(int *fd) {
+ if (*fd >= 0)
+ close_nointr_nofail(*fd);
+}
+
+static inline void closedirp(DIR **d) {
+ if (*d)
+ closedir(*d);
+}
+
+static inline void umaskp(mode_t *u) {
+ umask(*u);
+}
#define _cleanup_free_ _cleanup_(freep)
#define _cleanup_fclose_ _cleanup_(fclosep)