X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=util.c;h=5df811d038df60414a221b9d2a2f9d40a1545cca;hb=4f4a1dbf2171aa62da04d2e3b6945e8992139d14;hp=eed9aa7f84e598a5d92ed8fac733fd030ec3b695;hpb=6c78be3c3c63b59f18311b2d2b0e8d745f6ba131;p=elogind.git diff --git a/util.c b/util.c index eed9aa7f8..5df811d03 100644 --- a/util.c +++ b/util.c @@ -42,6 +42,7 @@ #include #include #include +#include #include "macro.h" #include "util.h" @@ -141,6 +142,30 @@ bool startswith(const char *s, const char *prefix) { return memcmp(s, prefix, pl) == 0; } +bool startswith_no_case(const char *s, const char *prefix) { + size_t sl, pl; + unsigned i; + + assert(s); + assert(prefix); + + sl = strlen(s); + pl = strlen(prefix); + + if (pl == 0) + return true; + + if (sl < pl) + return false; + + for(i = 0; i < pl; ++i) { + if (tolower(s[i]) != tolower(prefix[i])) + return false; + } + + return true; +} + bool first_word(const char *s, const char *word) { size_t sl, wl; @@ -1572,7 +1597,7 @@ int flush_fd(int fd) { } int acquire_terminal(const char *name, bool fail, bool force) { - int fd = -1, notify = -1, r, wd; + int fd = -1, notify = -1, r, wd = -1; assert(name); @@ -1602,8 +1627,9 @@ int acquire_terminal(const char *name, bool fail, bool force) { } for (;;) { - if ((r = flush_fd(notify)) < 0) - goto fail; + if (notify >= 0) + if ((r = flush_fd(notify)) < 0) + goto fail; /* We pass here O_NOCTTY only so that we can check the return * value TIOCSCTTY and have a reliable way to figure out if we @@ -1864,6 +1890,24 @@ int parse_usec(const char *t, usec_t *usec) { return 0; } +int make_stdio(int fd) { + int r, s, t; + + assert(fd >= 0); + + r = dup2(fd, STDIN_FILENO); + s = dup2(fd, STDOUT_FILENO); + t = dup2(fd, STDERR_FILENO); + + if (fd >= 3) + close_nointr_nofail(fd); + + if (r < 0 || s < 0 || t < 0) + return -errno; + + return 0; +} + static const char *const ioprio_class_table[] = { [IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_RT] = "realtime",