From 256f011d2f256c8c2c05c37a518401199fa423bf Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 1 Nov 2023 05:09:40 +0100 Subject: Merging upstream version 4.2+20231026. Signed-off-by: Daniel Baumann --- lib.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 79 insertions(+), 16 deletions(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index fe5c8d2..7ab5998 100644 --- a/lib.c +++ b/lib.c @@ -27,6 +27,24 @@ #include #include +/** + * is_string_lq() - Check if string length with NULL byte is lower or equal to requested. + * @str: string to check. + * @max_len: max length. + * + * @str length must be bigger than 0 and be lower or equal @max_len, including termination byte. + */ +bool is_string_lq(const char * const str, size_t max_len) +{ + assert(str); + + size_t _len = strnlen(str, max_len); + + if (_len > 0 && _len < max_len) + return true; + return false; +} + bool is_dev_alive(char *path) { if (!path) @@ -465,24 +483,50 @@ void print_quoted(char *str) putchar(q); } -void print_escape(char *str) +/** + * is_alphanum() - Check if sign is letter or digit. + * @c: char to analyze. + * + * Similar to isalnum() but additional locales are excluded. + * + * Return: %true on success, %false otherwise. + */ +bool is_alphanum(const char c) { - /* print str, but change space and tab to '_' - * as is suitable for device names - */ - for (; *str; str++) { - switch (*str) { - case ' ': - case '\t': - putchar('_'); - break; - case '/': - putchar('-'); - break; - default: - putchar(*str); - } + if (isupper(c) || islower(c) || isdigit(c) != 0) + return true; + return false; +} + +/** + * is_name_posix_compatible() - Check if name is POSIX compatible. + * @name: name to check. + * + * POSIX portable file name character set contains ASCII letters, + * digits, '_', '.', and '-'. Also forbid leading '-'. + * The length of the name cannot exceed NAME_MAX - 1 (ensure NULL ending). + * + * Return: %true on success, %false otherwise. + */ +bool is_name_posix_compatible(const char * const name) +{ + assert(name); + + char allowed_symbols[] = "-_."; + const char *n = name; + + if (!is_string_lq(name, NAME_MAX)) + return false; + + if (*n == '-') + return false; + + while (*n != '\0') { + if (!is_alphanum(*n) && !strchr(allowed_symbols, *n)) + return false; + n++; } + return true; } int check_env(char *name) @@ -585,3 +629,22 @@ int parse_num(int *dest, const char *num) *dest = temp; return 0; } + +/** + * s_gethostname() - secure get hostname. Assure null-terminated string. + * + * @buf: buffer for hostname. + * @buf_len: buffer length. + * + * Return: gethostname() result. + */ +int s_gethostname(char *buf, int buf_len) +{ + assert(buf); + + int ret = gethostname(buf, buf_len); + + buf[buf_len - 1] = 0; + + return ret; +} -- cgit v1.2.3