chiark / gitweb /
cgroups: drop debug log msg
[elogind.git] / util.c
diff --git a/util.c b/util.c
index e9f7813b8b378d13af2b7e322a5aaf9a3f612362..9d99fefe78b8ce5e373e52e91084988d8c938893 100644 (file)
--- a/util.c
+++ b/util.c
@@ -114,6 +114,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,6 +132,9 @@ bool startswith(const char *s, const char *prefix) {
         sl = strlen(s);
         pl = strlen(prefix);
 
+        if (pl == 0)
+                return true;
+
         if (sl < pl)
                 return false;
 
@@ -147,11 +153,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) {
@@ -1420,10 +1429,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) {
@@ -1776,6 +1789,69 @@ int path_is_mount_point(const char *t) {
         return a.st_dev != b.st_dev;
 }
 
+int parse_usec(const char *t, usec_t *usec) {
+        static const struct {
+                const char *suffix;
+                usec_t usec;
+        } table[] = {
+                { "sec", USEC_PER_SEC },
+                { "s", USEC_PER_SEC },
+                { "min", USEC_PER_MINUTE },
+                { "hr", USEC_PER_HOUR },
+                { "h", USEC_PER_HOUR },
+                { "d", USEC_PER_DAY },
+                { "w", USEC_PER_WEEK },
+                { "msec", USEC_PER_MSEC },
+                { "ms", USEC_PER_MSEC },
+                { "m", USEC_PER_MINUTE },
+                { "usec", 1ULL },
+                { "us", 1ULL },
+                { "", USEC_PER_SEC },
+        };
+
+        const char *p;
+        usec_t r = 0;
+
+        assert(t);
+        assert(usec);
+
+        p = t;
+        do {
+                long long l;
+                char *e;
+                unsigned i;
+
+                errno = 0;
+                l = strtoll(p, &e, 10);
+
+                if (errno != 0)
+                        return -errno;
+
+                if (l < 0)
+                        return -ERANGE;
+
+                if (e == p)
+                        return -EINVAL;
+
+                e += strspn(e, WHITESPACE);
+
+                for (i = 0; i < ELEMENTSOF(table); i++)
+                        if (startswith(e, table[i].suffix)) {
+                                r += (usec_t) l * table[i].usec;
+                                p = e + strlen(table[i].suffix);
+                                break;
+                        }
+
+                if (i >= ELEMENTSOF(table))
+                        return -EINVAL;
+
+        } while (*p != 0);
+
+        *usec = r;
+
+        return 0;
+}
+
 static const char *const ioprio_class_table[] = {
         [IOPRIO_CLASS_NONE] = "none",
         [IOPRIO_CLASS_RT] = "realtime",