X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=util.c;h=52ca5e25631a9e56f8fd5e27df7c63d777b6d50a;hp=b4d6eefbd265f160eddfe38e0f07f88954aafc4d;hb=013b87c09199926434583e8bb54ac6cb4b835eb5;hpb=7072ced8274274cd1b0fa085dd4118462e03884e diff --git a/util.c b/util.c index b4d6eefbd..52ca5e256 100644 --- a/util.c +++ b/util.c @@ -42,6 +42,19 @@ #include "log.h" #include "strv.h" +bool streq_ptr(const char *a, const char *b) { + + /* Like streq(), but tries to make sense of NULL pointers */ + + if (a && b) + return streq(a, b); + + if (!a && !b) + return true; + + return false; +} + usec_t now(clockid_t clock_id) { struct timespec ts; @@ -715,6 +728,20 @@ int mkdir_parents(const char *path, mode_t mode) { } } +int mkdir_p(const char *path, mode_t mode) { + int r; + + /* Like mkdir -p */ + + if ((r = mkdir_parents(path, mode)) < 0) + return r; + + if (mkdir(path, mode) < 0) + return -errno; + + return 0; +} + char hexchar(int x) { static const char table[16] = "0123456789abcdef"; @@ -1231,6 +1258,24 @@ bool chars_intersect(const char *a, const char *b) { return false; } +char *format_timestamp(char *buf, size_t l, usec_t t) { + struct tm tm; + time_t sec; + + assert(buf); + assert(l > 0); + + if (t <= 0) + return NULL; + + sec = (time_t) t / USEC_PER_SEC; + + if (strftime(buf, l, "%a, %d %b %Y %H:%M:%S %z", localtime_r(&sec, &tm)) <= 0) + return NULL; + + return buf; +} + static const char *const ioprio_class_table[] = { [IOPRIO_CLASS_NONE] = "none", [IOPRIO_CLASS_RT] = "realtime",