chiark / gitweb /
mount: don't negate errno when passing it to strerror()
[elogind.git] / util.c
diff --git a/util.c b/util.c
index 83e819a0cb769decb718148ab02af4a3bb9b8f61..f8b3f44ad15d665aa4a25a48cf44fb62f087a665 100644 (file)
--- a/util.c
+++ b/util.c
@@ -42,6 +42,7 @@
 #include <sys/inotify.h>
 #include <sys/poll.h>
 #include <libgen.h>
+#include <ctype.h>
 
 #include "macro.h"
 #include "util.h"
@@ -114,6 +115,9 @@ bool endswith(const char *s, const char *postfix) {
         sl = strlen(s);
         pl = strlen(postfix);
 
+        if (pl == 0)
+                return true;
+
         if (sl < pl)
                 return false;
 
@@ -129,12 +133,39 @@ bool startswith(const char *s, const char *prefix) {
         sl = strlen(s);
         pl = strlen(prefix);
 
+        if (pl == 0)
+                return true;
+
         if (sl < pl)
                 return false;
 
         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;
 
@@ -147,11 +178,14 @@ bool first_word(const char *s, const char *word) {
         if (sl < wl)
                 return false;
 
+        if (wl == 0)
+                return true;
+
         if (memcmp(s, word, wl) != 0)
                 return false;
 
-        return (s[wl] == 0 ||
-                strchr(WHITESPACE, s[wl]));
+        return s[wl] == 0 ||
+                strchr(WHITESPACE, s[wl]);
 }
 
 int close_nointr(int fd) {
@@ -1190,6 +1224,7 @@ bool ignore_file(const char *filename) {
 
         return
                 filename[0] == '.' ||
+                streq(filename, "lost+found") ||
                 endswith(filename, "~") ||
                 endswith(filename, ".rpmnew") ||
                 endswith(filename, ".rpmsave") ||
@@ -1420,10 +1455,14 @@ int ask(char *ret, const char *replies, const char *text, ...) {
                 int r;
                 bool need_nl = true;
 
+                fputs("\x1B[1m", stdout);
+
                 va_start(ap, text);
                 vprintf(text, ap);
                 va_end(ap);
 
+                fputs("\x1B[0m", stdout);
+
                 fflush(stdout);
 
                 if ((r = read_one_char(stdin, &c, &need_nl)) < 0) {
@@ -1558,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);
 
@@ -1588,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
@@ -1664,13 +1704,24 @@ fail:
 
 int release_terminal(void) {
         int r = 0, fd;
+        struct sigaction sa_old, sa_new;
 
-        if ((fd = open("/dev/tty", O_RDWR)) < 0)
+        if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY)) < 0)
                 return -errno;
 
+        /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
+         * by our own TIOCNOTTY */
+
+        zero(sa_new);
+        sa_new.sa_handler = SIG_IGN;
+        sa_new.sa_flags = SA_RESTART;
+        assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
+
         if (ioctl(fd, TIOCNOTTY) < 0)
                 r = -errno;
 
+        assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
+
         close_nointr_nofail(fd);
         return r;
 }