X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=util.c;h=b4d6eefbd265f160eddfe38e0f07f88954aafc4d;hb=70123e68a04016efca9266fc22dd05a0a7fd2d51;hp=03c60af98daa527222c87214cb14d60edc6c1a9b;hpb=2f357920ffd58e7b56940c64ab8d14a83a27b9f1;p=elogind.git diff --git a/util.c b/util.c index 03c60af98..b4d6eefbd 100644 --- a/util.c +++ b/util.c @@ -412,7 +412,7 @@ finish: int read_one_line_file(const char *fn, char **line) { FILE *f; int r; - char t[64], *c; + char t[2048], *c; assert(fn); assert(line); @@ -438,6 +438,33 @@ finish: return r; } +char *truncate_nl(char *s) { + assert(s); + + s[strcspn(s, NEWLINE)] = 0; + return s; +} + +int get_process_name(pid_t pid, char **name) { + char *p; + int r; + + assert(pid >= 1); + assert(name); + + if (asprintf(&p, "/proc/%llu/comm", (unsigned long long) pid) < 0) + return -ENOMEM; + + r = read_one_line_file(p, name); + free(p); + + if (r < 0) + return r; + + truncate_nl(*name); + return 0; +} + char *strappend(const char *s, const char *suffix) { size_t a, b; char *r; @@ -611,6 +638,23 @@ char *strstrip(char *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++) { + if (strchr(bad, *f)) + continue; + + *(t++) = *f; + } + + *t = 0; + + return s; +} + char *file_in_same_dir(const char *path, const char *filename) { char *e, *r; size_t k; @@ -1133,7 +1177,7 @@ int close_all_fds(const int except[], unsigned n_except) { return -errno; while ((de = readdir(d))) { - int fd; + int fd = -1; if (de->d_name[0] == '.') continue; @@ -1176,6 +1220,17 @@ finish: return r; } +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; +} + static const char *const ioprio_class_table[] = { [IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_RT] = "realtime",