X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=util.c;h=4ae57bbd65857b684a1136cbe2bef0a8b2f24d80;hp=33b143d3bbcf225c9cc37df3a1206d642cbf4e9d;hb=2882c7eda072e218516df584d2489f996d01dcf6;hpb=a7334b0952ab66c17ee787e36e6d2c5ceb387de6 diff --git a/util.c b/util.c index 33b143d3b..4ae57bbd6 100644 --- a/util.c +++ b/util.c @@ -30,16 +30,21 @@ #include #include #include +#include +#include +#include #include "macro.h" #include "util.h" #include "ioprio.h" #include "missing.h" +#include "log.h" +#include "strv.h" -usec_t now(clockid_t clock) { +usec_t now(clockid_t clock_id) { struct timespec ts; - assert_se(clock_gettime(clock, &ts) == 0); + assert_se(clock_gettime(clock_id, &ts) == 0); return timespec_load(&ts); } @@ -269,7 +274,7 @@ int safe_atolli(const char *s, long long int *ret_lli) { } /* Split a string into words. */ -char *split_spaces(const char *c, size_t *l, char **state) { +char *split(const char *c, size_t *l, const char *separator, char **state) { char *current; current = *state ? *state : (char*) c; @@ -277,8 +282,8 @@ char *split_spaces(const char *c, size_t *l, char **state) { if (!*current || *c == 0) return NULL; - current += strspn(current, WHITESPACE); - *l = strcspn(current, WHITESPACE); + current += strspn(current, separator); + *l = strcspn(current, separator); *state = current+*l; return (char*) current; @@ -321,6 +326,21 @@ char *split_quoted(const char *c, size_t *l, char **state) { return (char*) current; } +char **split_path_and_make_absolute(const char *p) { + char **l; + assert(p); + + if (!(l = strv_split(p, ":"))) + return NULL; + + if (!strv_path_make_absolute_cwd(l)) { + strv_free(l); + return NULL; + } + + return l; +} + int get_parent_of_pid(pid_t pid, pid_t *_ppid) { int r; FILE *f; @@ -494,6 +514,9 @@ char *path_make_absolute(const char *p, const char *prefix) { assert(p); + /* Makes every item in the list an absolute path by prepending + * the prefix, if specified and necessary */ + if (path_is_absolute(p) || !prefix) return strdup(p); @@ -503,6 +526,46 @@ char *path_make_absolute(const char *p, const char *prefix) { return r; } +char *path_make_absolute_cwd(const char *p) { + char *cwd, *r; + + assert(p); + + /* Similar to path_make_absolute(), but prefixes with the + * current working directory. */ + + if (path_is_absolute(p)) + return strdup(p); + + if (!(cwd = get_current_dir_name())) + return NULL; + + r = path_make_absolute(p, cwd); + free(cwd); + + return r; +} + +char **strv_path_make_absolute_cwd(char **l) { + char **s; + + /* Goes through every item in the string list and makes it + * absolute. This works in place and won't rollback any + * changes on failure. */ + + STRV_FOREACH(s, l) { + char *t; + + if (!(t = path_make_absolute_cwd(*s))) + return NULL; + + free(*s); + *s = t; + } + + return l; +} + int reset_all_signal_handlers(void) { int sig; @@ -523,7 +586,7 @@ int reset_all_signal_handlers(void) { return -errno; } - return 0; + return 0; } char *strstrip(char *s) { @@ -574,6 +637,39 @@ char *file_in_same_dir(const char *path, const char *filename) { return r; } +int mkdir_parents(const char *path, mode_t mode) { + const char *p, *e; + + assert(path); + + /* Creates every parent directory in the path except the last + * component. */ + + p = path + strspn(path, "/"); + for (;;) { + int r; + char *t; + + e = p + strcspn(p, "/"); + p = e + strspn(e, "/"); + + /* Is this the last component? If so, then we're + * done */ + if (*p == 0) + return 0; + + if (!(t = strndup(path, e - path))) + return -ENOMEM; + + r = mkdir(t, mode); + + free(t); + + if (r < 0 && errno != EEXIST) + return -errno; + } +} + char hexchar(int x) { static const char table[16] = "0123456789abcdef"; @@ -606,6 +702,18 @@ int unoctchar(char c) { return -1; } +char decchar(int x) { + return '0' + (x % 10); +} + +int undecchar(char c) { + + if (c >= '0' && c <= '9') + return c - '0'; + + return -1; +} + char *cescape(const char *s) { char *r, *t; const char *f; @@ -808,8 +916,8 @@ char *xescape(const char *s, const char *bad) { for (f = s, t = r; *f; f++) { - if (*f < ' ' || *f >= 127 || - *f == '\\' || strchr(bad, *f)) { + if ((*f < ' ') || (*f >= 127) || + (*f == '\\') || strchr(bad, *f)) { *(t++) = '\\'; *(t++) = 'x'; *(t++) = hexchar(*f >> 4); @@ -824,11 +932,11 @@ char *xescape(const char *s, const char *bad) { } char *bus_path_escape(const char *s) { - assert(s); - char *r, *t; const char *f; + assert(s); + /* Escapes all chars that D-Bus' object path cannot deal * with. Can be reverse with bus_path_unescape() */ @@ -853,11 +961,11 @@ char *bus_path_escape(const char *s) { } char *bus_path_unescape(const char *s) { - assert(s); - char *r, *t; const char *f; + assert(s); + if (!(r = new(char, strlen(s)+1))) return NULL; @@ -951,16 +1059,68 @@ bool path_startswith(const char *path, const char *prefix) { } } -char *ascii_strlower(char *path) { +char *ascii_strlower(char *t) { char *p; - assert(path); + assert(t); - for (p = path; *p; p++) + for (p = t; *p; p++) if (*p >= 'A' && *p <= 'Z') *p = *p - 'A' + 'a'; - return p; + return t; +} + +bool ignore_file(const char *filename) { + assert(filename); + + return + filename[0] == '.' || + endswith(filename, "~") || + endswith(filename, ".rpmnew") || + endswith(filename, ".rpmsave") || + endswith(filename, ".rpmorig") || + endswith(filename, ".dpkg-old") || + endswith(filename, ".dpkg-new") || + endswith(filename, ".swp"); +} + +int fd_nonblock(int fd, bool nonblock) { + int flags; + + assert(fd >= 0); + + if ((flags = fcntl(fd, F_GETFL, 0)) < 0) + return -errno; + + if (nonblock) + flags |= O_NONBLOCK; + else + flags &= ~O_NONBLOCK; + + if (fcntl(fd, F_SETFL, flags) < 0) + return -errno; + + return 0; +} + +int fd_cloexec(int fd, bool cloexec) { + int flags; + + assert(fd >= 0); + + if ((flags = fcntl(fd, F_GETFD, 0)) < 0) + return -errno; + + if (cloexec) + flags |= FD_CLOEXEC; + else + flags &= ~FD_CLOEXEC; + + if (fcntl(fd, F_SETFD, flags) < 0) + return -errno; + + return 0; } static const char *const ioprio_class_table[] = {