bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | #ifndef UTIL_LINUX_STRUTILS |
| 2 | #define UTIL_LINUX_STRUTILS |
| 3 | |
| 4 | #include <inttypes.h> |
| 5 | #include <string.h> |
| 6 | #include <sys/types.h> |
| 7 | |
| 8 | /* default strtoxx_or_err() exit code */ |
| 9 | #ifndef STRTOXX_EXIT_CODE |
| 10 | # define STRTOXX_EXIT_CODE EXIT_FAILURE |
| 11 | #endif |
| 12 | |
| 13 | |
| 14 | extern int strtosize(const char *str, uintmax_t *res); |
| 15 | extern uintmax_t strtosize_or_err(const char *str, const char *errmesg); |
| 16 | |
| 17 | extern int16_t strtos16_or_err(const char *str, const char *errmesg); |
| 18 | extern uint16_t strtou16_or_err(const char *str, const char *errmesg); |
| 19 | |
| 20 | extern int32_t strtos32_or_err(const char *str, const char *errmesg); |
| 21 | extern uint32_t strtou32_or_err(const char *str, const char *errmesg); |
| 22 | |
| 23 | extern int64_t strtos64_or_err(const char *str, const char *errmesg); |
| 24 | extern uint64_t strtou64_or_err(const char *str, const char *errmesg); |
| 25 | |
| 26 | extern double strtod_or_err(const char *str, const char *errmesg); |
| 27 | |
| 28 | extern long strtol_or_err(const char *str, const char *errmesg); |
| 29 | extern unsigned long strtoul_or_err(const char *str, const char *errmesg); |
| 30 | |
| 31 | #ifndef HAVE_MEMPCPY |
| 32 | extern void *mempcpy(void *restrict dest, const void *restrict src, size_t n); |
| 33 | #endif |
| 34 | #ifndef HAVE_STRNLEN |
| 35 | extern size_t strnlen(const char *s, size_t maxlen); |
| 36 | #endif |
| 37 | #ifndef HAVE_STRNDUP |
| 38 | extern char *strndup(const char *s, size_t n); |
| 39 | #endif |
| 40 | #ifndef HAVE_STRNCHR |
| 41 | extern char *strnchr(const char *s, size_t maxlen, int c); |
| 42 | #endif |
| 43 | |
| 44 | /* caller guarantees n > 0 */ |
| 45 | static inline void xstrncpy(char *dest, const char *src, size_t n) |
| 46 | { |
| 47 | strncpy(dest, src, n-1); |
| 48 | dest[n-1] = 0; |
| 49 | } |
| 50 | |
| 51 | extern void strmode(mode_t mode, char *str); |
| 52 | |
| 53 | /* Options for size_to_human_string() */ |
| 54 | enum |
| 55 | { |
| 56 | SIZE_SUFFIX_1LETTER = 0, |
| 57 | SIZE_SUFFIX_3LETTER = 1, |
| 58 | SIZE_SUFFIX_SPACE = 2 |
| 59 | }; |
| 60 | |
| 61 | extern char *size_to_human_string(int options, uint64_t bytes); |
| 62 | |
| 63 | extern int string_to_idarray(const char *list, int ary[], size_t arysz, |
| 64 | int (name2id)(const char *, size_t)); |
| 65 | extern int string_add_to_idarray(const char *list, int ary[], |
| 66 | size_t arysz, int *ary_pos, |
| 67 | int (name2id)(const char *, size_t)); |
| 68 | |
| 69 | extern int string_to_bitarray(const char *list, char *ary, |
| 70 | int (*name2bit)(const char *, size_t)); |
| 71 | |
| 72 | extern int string_to_bitmask(const char *list, |
| 73 | unsigned long *mask, |
| 74 | long (*name2flag)(const char *, size_t)); |
| 75 | extern int parse_range(const char *str, int *lower, int *upper, int def); |
| 76 | |
| 77 | extern int streq_except_trailing_slash(const char *s1, const char *s2); |
| 78 | |
| 79 | #endif |