#include <sys/inotify.h>
#include <sys/poll.h>
#include <libgen.h>
+#include <ctype.h>
#include "macro.h"
#include "util.h"
sl = strlen(s);
pl = strlen(postfix);
+ if (pl == 0)
+ return true;
+
if (sl < pl)
return false;
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;
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) {
return
filename[0] == '.' ||
+ streq(filename, "lost+found") ||
endswith(filename, "~") ||
endswith(filename, ".rpmnew") ||
endswith(filename, ".rpmsave") ||
"smbfs",
"ncpfs",
"nfs",
- "nfs4"
+ "nfs4",
+ "gfs",
+ "gfs2"
};
unsigned i;
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) {
}
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);
}
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
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;
}
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;
+}
+
+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;
+}
+
+bool is_clean_exit(int code, int status) {
+
+ if (code == CLD_EXITED)
+ return status == 0;
+
+ /* If a daemon does not implement handlers for some of the
+ * signals that's not considered an unclean shutdown */
+ if (code == CLD_KILLED)
+ return
+ status == SIGHUP ||
+ status == SIGINT ||
+ status == SIGTERM ||
+ status == SIGPIPE;
+
+ return false;
+}
+
+bool is_device_path(const char *path) {
+
+ /* Returns true on paths that refer to a device, either in
+ * sysfs or in /dev */
+
+ return
+ path_startswith(path, "/dev/") ||
+ path_startswith(path, "/sys/");
+}
+
static const char *const ioprio_class_table[] = {
[IOPRIO_CLASS_NONE] = "none",
[IOPRIO_CLASS_RT] = "realtime",