X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Futil.c;h=b4ed2c407c4ad6d544adf5899760991f9ae05191;hb=4545a231fcccc2ef670ef70f7b38f4e0a04f86ae;hp=deb74c4655be8678fc2ad8b807df78f8ea50ccde;hpb=4561be3a64534a911ee405ffb51950a624a0cd3f;p=elogind.git diff --git a/src/shared/util.c b/src/shared/util.c index deb74c465..b4ed2c407 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -373,17 +373,21 @@ char *split(const char *c, size_t *l, const char *separator, char **state) { /* Split a string into words, but consider strings enclosed in '' and * "" as words even if they include spaces. */ char *split_quoted(const char *c, size_t *l, char **state) { - char *current, *e; + const char *current, *e; bool escaped = false; - current = *state ? *state : (char*) c; + assert(c); + assert(l); + assert(state); - if (!*current || *c == 0) - return NULL; + current = *state ? *state : c; current += strspn(current, WHITESPACE); - if (*current == '\'') { + if (*current == 0) + return NULL; + + else if (*current == '\'') { current ++; for (e = current; *e; e++) { @@ -396,7 +400,8 @@ char *split_quoted(const char *c, size_t *l, char **state) { } *l = e-current; - *state = *e == 0 ? e : e+1; + *state = (char*) (*e == 0 ? e : e+1); + } else if (*current == '\"') { current ++; @@ -410,7 +415,8 @@ char *split_quoted(const char *c, size_t *l, char **state) { } *l = e-current; - *state = *e == 0 ? e : e+1; + *state = (char*) (*e == 0 ? e : e+1); + } else { for (e = current; *e; e++) { if (escaped) @@ -421,7 +427,7 @@ char *split_quoted(const char *c, size_t *l, char **state) { break; } *l = e-current; - *state = e; + *state = (char*) e; } return (char*) current; @@ -491,7 +497,7 @@ int get_starttime_of_pid(pid_t pid, unsigned long long *st) { f = fopen(p, "re"); if (!f) - return -errno; + return errno == ENOENT ? -ESRCH : -errno; if (!fgets(line, sizeof(line), f)) { if (ferror(f)) @@ -557,6 +563,7 @@ char *truncate_nl(char *s) { int get_process_comm(pid_t pid, char **name) { const char *p; + int r; assert(name); assert(pid >= 0); @@ -566,7 +573,11 @@ int get_process_comm(pid_t pid, char **name) { else p = procfs_file_alloca(pid, "comm"); - return read_one_line_file(p, name); + r = read_one_line_file(p, name); + if (r == -ENOENT) + return -ESRCH; + + return r; } int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) { @@ -723,7 +734,7 @@ int get_process_exe(pid_t pid, char **name) { r = readlink_malloc(p, name); if (r < 0) - return r; + return r == -ENOENT ? -ESRCH : r; d = endswith(*name, " (deleted)"); if (d) @@ -1356,78 +1367,6 @@ char *xescape(const char *s, const char *bad) { return r; } -char *bus_path_escape(const char *s) { - char *r, *t; - const char *f; - - assert(s); - - /* Escapes all chars that D-Bus' object path cannot deal - * with. Can be reversed with bus_path_unescape(). We special - * case the empty string. */ - - if (*s == 0) - return strdup("_"); - - r = new(char, strlen(s)*3 + 1); - if (!r) - return NULL; - - for (f = s, t = r; *f; f++) { - - /* Escape everything that is not a-zA-Z0-9. We also - * escape 0-9 if it's the first character */ - - if (!(*f >= 'A' && *f <= 'Z') && - !(*f >= 'a' && *f <= 'z') && - !(f > s && *f >= '0' && *f <= '9')) { - *(t++) = '_'; - *(t++) = hexchar(*f >> 4); - *(t++) = hexchar(*f); - } else - *(t++) = *f; - } - - *t = 0; - - return r; -} - -char *bus_path_unescape(const char *f) { - char *r, *t; - - assert(f); - - /* Special case for the empty string */ - if (streq(f, "_")) - return strdup(""); - - r = new(char, strlen(f) + 1); - if (!r) - return NULL; - - for (t = r; *f; f++) { - - if (*f == '_') { - int a, b; - - if ((a = unhexchar(f[1])) < 0 || - (b = unhexchar(f[2])) < 0) { - /* Invalid escape code, let's take it literal then */ - *(t++) = '_'; - } else { - *(t++) = (char) ((a << 4) | b); - f += 2; - } - } else - *(t++) = *f; - } - - *t = 0; - - return r; -} - char *ascii_strlower(char *t) { char *p; @@ -2562,7 +2501,7 @@ int getttyname_malloc(int fd, char **r) { assert(r); k = ttyname_r(fd, path, sizeof(path)); - if (k != 0) + if (k > 0) return -k; char_array_0(path); @@ -2597,10 +2536,8 @@ int get_ctty_devnr(pid_t pid, dev_t *d) { char line[LINE_MAX], *p; unsigned long ttynr; const char *fn; - int k; assert(pid >= 0); - assert(d); if (pid == 0) fn = "/proc/self/stat"; @@ -2611,10 +2548,8 @@ int get_ctty_devnr(pid_t pid, dev_t *d) { if (!f) return -errno; - if (!fgets(line, sizeof(line), f)) { - k = feof(f) ? -EIO : -errno; - return k; - } + if (!fgets(line, sizeof(line), f)) + return feof(f) ? -EIO : -errno; p = strrchr(line, ')'); if (!p) @@ -2634,7 +2569,9 @@ int get_ctty_devnr(pid_t pid, dev_t *d) { if (major(ttynr) == 0 && minor(ttynr) == 0) return -ENOENT; - *d = (dev_t) ttynr; + if (d) + *d = (dev_t) ttynr; + return 0; } @@ -5779,56 +5716,6 @@ int search_and_fopen_nulstr(const char *path, const char *mode, const char *sear return search_and_fopen_internal(path, mode, s, _f); } -int create_tmp_dir(char template[], char** dir_name) { - int r = 0; - char *d = NULL, *dt; - - assert(dir_name); - - RUN_WITH_UMASK(0077) { - d = mkdtemp(template); - } - if (!d) { - log_error("Can't create directory %s: %m", template); - return -errno; - } - - dt = strjoin(d, "/tmp", NULL); - if (!dt) { - r = log_oom(); - goto fail3; - } - - RUN_WITH_UMASK(0000) { - r = mkdir(dt, 0777); - } - if (r < 0) { - log_error("Can't create directory %s: %m", dt); - r = -errno; - goto fail2; - } - log_debug("Created temporary directory %s", dt); - - r = chmod(dt, 0777 | S_ISVTX); - if (r < 0) { - log_error("Failed to chmod %s: %m", dt); - r = -errno; - goto fail1; - } - log_debug("Set sticky bit on %s", dt); - - *dir_name = dt; - - return 0; -fail1: - rmdir(dt); -fail2: - free(dt); -fail3: - rmdir(template); - return r; -} - char *strextend(char **x, ...) { va_list ap; size_t f, l; @@ -5904,6 +5791,8 @@ void* greedy_realloc(void **p, size_t *allocated, size_t need) { size_t a; void *q; + assert(allocated); + if (*allocated >= need) return *p; @@ -5917,6 +5806,20 @@ void* greedy_realloc(void **p, size_t *allocated, size_t need) { return q; } +void* greedy_realloc0(void **p, size_t *allocated, size_t need) { + size_t prev = *allocated; + uint8_t *q; + + q = greedy_realloc(p, allocated, need); + if (!q) + return NULL; + + if (*allocated > prev) + memset(&q[prev], 0, *allocated - prev); + + return q; +} + bool id128_is_valid(const char *s) { size_t i, l;