X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Futil.h;h=b979b0e89fe9c0208955aa3cfc24f323d6f9935d;hb=0901758558506273c0b7553dc3cae587f2b94290;hp=e1d4735ee38dec94278118343fd89d6b0cba6e69;hpb=aa408e7799cf01f048efedf434916544b4badc77;p=elogind.git diff --git a/src/shared/util.h b/src/shared/util.h index e1d4735ee..b979b0e89 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -79,7 +79,7 @@ union dirent_storage { #define QUOTES "\"\'" #define COMMENTS "#;\n" -#define FORMAT_TIMESTAMP_MAX 64 +#define FORMAT_TIMESTAMP_MAX (5+11+9+4+1) #define FORMAT_TIMESTAMP_PRETTY_MAX 256 #define FORMAT_TIMESPAN_MAX 64 #define FORMAT_BYTES_MAX 8 @@ -141,9 +141,9 @@ static inline bool isempty(const char *p) { return !p || !p[0]; } -bool endswith(const char *s, const char *postfix); -bool startswith(const char *s, const char *prefix); -bool startswith_no_case(const char *s, const char *prefix); +char *endswith(const char *s, const char *postfix); +char *startswith(const char *s, const char *prefix); +char *startswith_no_case(const char *s, const char *prefix); bool first_word(const char *s, const char *word); @@ -291,6 +291,7 @@ int make_console_stdio(void); unsigned long long random_ull(void); +/* For basic lookup tables with strictly enumerated entries */ #define __DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \ scope const char *name##_to_string(type i) { \ if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \ @@ -299,15 +300,11 @@ unsigned long long random_ull(void); } \ scope type name##_from_string(const char *s) { \ type i; \ - unsigned u = 0; \ assert(s); \ for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \ if (name##_table[i] && \ streq(name##_table[i], s)) \ return i; \ - if (safe_atou(s, &u) >= 0 && \ - u < ELEMENTSOF(name##_table)) \ - return (type) u; \ return (type) -1; \ } \ struct __useless_struct_to_allow_trailing_semicolon__ @@ -315,6 +312,39 @@ unsigned long long random_ull(void); #define DEFINE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,) #define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,static) +/* For string conversions where numbers are also acceptable */ +#define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \ + int name##_to_string_alloc(type i, char **str) { \ + char *s; \ + int r; \ + if (i < 0 || i > max) \ + return -ERANGE; \ + if (i < (type) ELEMENTSOF(name##_table)) { \ + s = strdup(name##_table[i]); \ + if (!s) \ + return log_oom(); \ + } else { \ + r = asprintf(&s, "%u", i); \ + if (r < 0) \ + return log_oom(); \ + } \ + *str = s; \ + return 0; \ + } \ + type name##_from_string(const char *s) { \ + type i; \ + unsigned u = 0; \ + assert(s); \ + for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \ + if (name##_table[i] && \ + streq(name##_table[i], s)) \ + return i; \ + if (safe_atou(s, &u) >= 0 && u <= max) \ + return (type) u; \ + return (type) -1; \ + } \ + struct __useless_struct_to_allow_trailing_semicolon__ + int fd_nonblock(int fd, bool nonblock); int fd_cloexec(int fd, bool cloexec); @@ -384,10 +414,11 @@ int status_welcome(void); int fd_columns(int fd); unsigned columns(void); -unsigned columns_uncached(void); - int fd_lines(int fd); unsigned lines(void); +void columns_lines_cache_reset(int _unused_ signum); + +bool on_tty(void); int running_in_chroot(void); @@ -477,25 +508,25 @@ int strdup_or_null(const char *a, char **b); #define NULSTR_FOREACH_PAIR(i, j, l) \ for ((i) = (l), (j) = strchr((i), 0)+1; (i) && *(i); (i) = strchr((j), 0)+1, (j) = *(i) ? strchr((i), 0)+1 : (i)) -const char *ioprio_class_to_string(int i); +int ioprio_class_to_string_alloc(int i, char **s); int ioprio_class_from_string(const char *s); const char *sigchld_code_to_string(int i); int sigchld_code_from_string(const char *s); -const char *log_facility_unshifted_to_string(int i); +int log_facility_unshifted_to_string_alloc(int i, char **s); int log_facility_unshifted_from_string(const char *s); -const char *log_level_to_string(int i); +int log_level_to_string_alloc(int i, char **s); int log_level_from_string(const char *s); -const char *sched_policy_to_string(int i); +int sched_policy_to_string_alloc(int i, char **s); int sched_policy_from_string(const char *s); const char *rlimit_to_string(int i); int rlimit_from_string(const char *s); -const char *ip_tos_to_string(int i); +int ip_tos_to_string_alloc(int i, char **s); int ip_tos_from_string(const char *s); const char *signal_to_string(int i); @@ -528,6 +559,7 @@ int setrlimit_closest(int resource, const struct rlimit *rlim); int getenv_for_pid(pid_t pid, const char *field, char **_value); int can_sleep(const char *type); +int can_sleep_disk(const char *type); bool is_valid_documentation_url(const char *url); @@ -557,3 +589,14 @@ _malloc_ static inline void *memdup_multiply(const void *p, size_t a, size_t b) return memdup(p, a * b); } + +bool filename_is_safe(const char *p); +bool string_is_safe(const char *p); + +int parse_timestamp(const char *t, usec_t *usec); + +void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, + int (*compar) (const void *, const void *, void *), + void *arg); + +bool is_locale_utf8(void);