From 8a1c3461bb820bf875a50be56c7652177cf50645 Mon Sep 17 00:00:00 2001 From: axtloss Date: Tue, 16 Jul 2024 02:28:06 +0200 Subject: fix formatting --- src/constraint_handler.c | 55 +++++++++--------- src/extlib.c | 90 +++++++++++++++--------------- src/extlib.h | 15 ++--- src/extstring.c | 141 +++++++++++++++++++++++++---------------------- src/memset_s.c | 38 ++++++------- 5 files changed, 175 insertions(+), 164 deletions(-) (limited to 'src') diff --git a/src/constraint_handler.c b/src/constraint_handler.c index 450632e..5506ab0 100644 --- a/src/constraint_handler.c +++ b/src/constraint_handler.c @@ -11,41 +11,44 @@ static constraint_handler_t *_cnstrnt_handler = NULL; constraint_handler_t set_constraint_handler_s (constraint_handler_t handler) { - constraint_handler_t *new, *old, ret; - - new = malloc (sizeof (constraint_handler_t)); - if (!new) return NULL; - *new = handler; - old = _cnstrnt_handler; - _cnstrnt_handler = new; - if (!old) - ret = NULL; - else { - ret = *old; - free (old); - } - return ret; + constraint_handler_t *new, *old, ret; + + new = malloc (sizeof (constraint_handler_t)); + if (!new) + return NULL; + *new = handler; + old = _cnstrnt_handler; + _cnstrnt_handler = new; + if (!old) + ret = NULL; + else + { + ret = *old; + free (old); + } + return ret; } void -throw_constraint_handler_s (const char * restrict msg, errno_t error) +throw_constraint_handler_s (const char *restrict msg, errno_t error) { - constraint_handler_t ch; - ch = _cnstrnt_handler != NULL ? *_cnstrnt_handler : NULL; - if (ch != NULL) - ch (msg, NULL, error); + constraint_handler_t ch; + ch = _cnstrnt_handler != NULL ? *_cnstrnt_handler : NULL; + if (ch != NULL) + ch (msg, NULL, error); } void -abort_handler_s (const char *restrict msg, void * restrict ptr, errno_t error) +abort_handler_s (const char *restrict msg, void *restrict ptr, errno_t error) { - static const char abrt_msg[] = "abort_handler: "; + static const char abrt_msg[] = "abort_handler: "; - fprintf (stderr, "%s", abrt_msg); - fprintf (stderr, "%s\n", msg); - abort (); + fprintf (stderr, "%s", abrt_msg); + fprintf (stderr, "%s\n", msg); + abort (); } void -ignore_handler_s (const char * restrict msg, void * restrict ptr, errno_t error) -{} +ignore_handler_s (const char *restrict msg, void *restrict ptr, errno_t error) +{ +} diff --git a/src/extlib.c b/src/extlib.c index 2ee4225..e1fdb14 100644 --- a/src/extlib.c +++ b/src/extlib.c @@ -1,15 +1,16 @@ -/* extlib.c +/* + * extlib.c * * Copyright 2024 axtlos * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, version 3. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . @@ -32,69 +33,66 @@ #undef malloc void -free_secure(void **__ptr, size_t ptrlen) +free_secure (void **__ptr, size_t ptrlen) { - memset_s (*__ptr, ptrlen+1, 0, ptrlen); - free (*__ptr); - *__ptr = NULL; - return; + memset_s (*__ptr, ptrlen + 1, 0, ptrlen); + free (*__ptr); + *__ptr = NULL; + return; } void * malloc_secure (size_t len) { - void *mem = calloc (1, len); - return mem; + void *mem = calloc (1, len); + return mem; } int -memvcmp (void *str, - char val, - size_t n) +memvcmp (void *str, char val, size_t n) { - char str2[n]; - memset (str2, val, n); - return memcmp (str, str2, n); + char str2[n]; + memset (str2, val, n); + return memcmp (str, str2, n); } size_t fcopy (FILE *src, FILE *dst) { - char buffer[BUFSIZ]; - size_t n, copied = 0; + char buffer[BUFSIZ]; + size_t n, copied = 0; - if (src == NULL || dst == NULL) - return -1; + if (src == NULL || dst == NULL) + return -1; - while ((n = fread (buffer, sizeof (char), sizeof (buffer), src)) > 0) - { - if (fwrite (buffer, sizeof (char), n, dst) != n) { - fprintf (stderr, "Failed to copy data"); - return -1; + while ((n = fread (buffer, sizeof (char), sizeof (buffer), src)) > 0) + { + if (fwrite (buffer, sizeof (char), n, dst) != n) + { + fprintf (stderr, "Failed to copy data"); + return -1; + } + fflush (dst); + copied += n; } - fflush (dst); - copied += n; - } - return copied; + return copied; } int -rmfile(const char *fpath, - const struct stat *sb, - int typeflag, - struct FTW *ftwbuf) +rmfile (const char *fpath, + const struct stat *sb, int typeflag, struct FTW *ftwbuf) { - int err = remove (fpath); - if (err < 0) - return err; - return 0; + int err = remove (fpath); + if (err < 0) + return err; + return 0; } int -rrmdir(char *pathname) +rrmdir (char *pathname) { - int err = nftw (pathname, rmfile, 10, FTW_DEPTH|FTW_MOUNT|FTW_PHYS); - if (err < 0) - return err; - return 0; + int err = nftw (pathname, rmfile, 10, FTW_DEPTH | FTW_MOUNT | FTW_PHYS); + if (err < 0) + return err; + return 0; } diff --git a/src/extlib.h b/src/extlib.h index b12ddbf..c62c374 100644 --- a/src/extlib.h +++ b/src/extlib.h @@ -25,7 +25,8 @@ void *malloc_secure (size_t len); typedef int errno_t; typedef size_t rsize_t; -typedef void (*constraint_handler_t) (const char *__restrict, void *__restrict, errno_t); +typedef void (*constraint_handler_t) (const char *__restrict, + void *__restrict, errno_t); /// Copy the value of c (converted to an unsigned char) into each of the first n /// characters of the object pointed to by s. /// Conforms to ISO/IEC 9899:2011 K.3.7.4.1 @@ -33,19 +34,19 @@ errno_t memset_s (void *s, rsize_t max, int c, rsize_t n); constraint_handler_t set_constraint_handler_s (constraint_handler_t handler); void throw_constraint_handler_s (const char *restrict msg, errno_t error); -_Noreturn void abort_handler_s (const char * __restrict, void * __restrict, - errno_t); +_Noreturn void abort_handler_s (const char *__restrict, void *__restrict, + errno_t); void ignore_handler_s (const char *__restrict, void *__restrict, errno_t); #endif /// Copy the data of one filestream to another /// Returns the amount of bytes copied. -size_t fcopy(FILE *src, FILE *dst); +size_t fcopy (FILE * src, FILE * dst); #if (_XOPEN_SOURCE == 500) /// Recursively remove a directory pathname. -int rrmdir(char *pathname); +int rrmdir (char *pathname); #endif /// Compare the first n bytes of a memory area str to the value val. @@ -53,10 +54,10 @@ int rrmdir(char *pathname); int memvcmp (void *str, char val, size_t n); /// Convert a string to lowercase -char *strlwr(char *s); +char *strlwr (char *s); /// Convert a string to uppercase -char *strupr(char *s); +char *strupr (char *s); /// Trim spaces from a string char *trim (char *s, int *rem_front, int *rem_back); diff --git a/src/extstring.c b/src/extstring.c index fc3cb45..044e368 100644 --- a/src/extstring.c +++ b/src/extstring.c @@ -11,102 +11,111 @@ char * strlwr (char *s) { - unsigned char *p = (unsigned char *) s; + unsigned char *p = (unsigned char *) s; - while (*p) { - *p = tolower ((unsigned char)*p); - p++; - } + while (*p) + { + *p = tolower ((unsigned char) *p); + p++; + } - return s; + return s; } char * strupr (char *s) { - unsigned char *p = (unsigned char *) s; + unsigned char *p = (unsigned char *) s; - while (*p) { - *p = toupper ((unsigned char)*p); - p++; - } + while (*p) + { + *p = toupper ((unsigned char) *p); + p++; + } - return s; + return s; } char * trim (char *s, int *rem_front, int *rem_back) { - char *result = strdup (s); - char *end; - - while (isspace ((unsigned char)*result)) { - result++; - if (rem_front) *rem_front += 1; - } + char *result = strdup (s); + char *end; + + while (isspace ((unsigned char) *result)) + { + result++; + if (rem_front) + *rem_front += 1; + } - if (*result == 0) - return result; + if (*result == 0) + return result; - end = result + strlen (result) - 1; - while (end > result && isspace ((unsigned char)*end)) { - end--; - if (rem_back) *rem_back +=1; - } + end = result + strlen (result) - 1; + while (end > result && isspace ((unsigned char) *end)) + { + end--; + if (rem_back) + *rem_back += 1; + } - end[1] = '\0'; + end[1] = '\0'; - return result; + return result; } char * -replace_str (char *s, - char *old, - char *replace) +replace_str (char *s, char *old, char *replace) { - char* result; - int i, cnt = 0; - size_t new_size = strlen (replace); - size_t old_size = strlen (old); - - for (i = 0; s[i] != '\0'; i++) { - if (strstr (&s[i], old) == &s[i]) { - cnt++; - i += old_size - 1; + char *result; + int i, cnt = 0; + size_t new_size = strlen (replace); + size_t old_size = strlen (old); + + for (i = 0; s[i] != '\0'; i++) + { + if (strstr (&s[i], old) == &s[i]) + { + cnt++; + i += old_size - 1; + } } - } - - result = (char*)malloc_secure (i + cnt * (new_size - old_size) + 1); - i = 0; - while (*s) { - if (strstr (s, old) == s) { - strcpy (&result[i], replace); - i += new_size; - s += old_size; + result = (char *) malloc_secure (i + cnt * (new_size - old_size) + 1); + + i = 0; + while (*s) + { + if (strstr (s, old) == s) + { + strcpy (&result[i], replace); + i += new_size; + s += old_size; + } + else + result[i++] = *s++; } - else - result[i++] = *s++; - } - result[i] = '\0'; - return result; + result[i] = '\0'; + return result; }; char * join_str (char **s, size_t len, char delim) { - char *ret = strdup (s[0]); - size_t ret_len = sizeof (char) * len + strlen (ret); - - for (int i = 1; i < len; i++) { - char *ret_tmp = realloc (ret, ret_len + strlen (s[i]) + 1); - ret_len += strlen (s[i]) + 1; - if (ret_tmp != NULL) - ret = ret_tmp; - sprintf (ret, "%s%c%s", ret, delim, s[i]); - } - - return ret; + char *ret = strdup (s[0]); + size_t ret_len = sizeof (char) * len + strlen (ret); + + for (int i = 1; i < len; i++) + { + char *ret_tmp = realloc (ret, ret_len + strlen (s[i]) + 1); + ret_len += strlen (s[i]) + 1; + if (ret_tmp != NULL) + ret = ret_tmp; + sprintf (ret, "%s%c%s", ret, delim, s[i]); + } + + return ret; } diff --git a/src/memset_s.c b/src/memset_s.c index 8b4d342..74c0025 100644 --- a/src/memset_s.c +++ b/src/memset_s.c @@ -7,25 +7,25 @@ #include errno_t -memset_s(void *s, rsize_t smax, int c, rsize_t n) +memset_s (void *s, rsize_t smax, int c, rsize_t n) { - volatile unsigned char *dest = (unsigned char *) s; - errno_t ret = EINVAL; - rsize_t limit = n < smax ? n : smax; + volatile unsigned char *dest = (unsigned char *) s; + errno_t ret = EINVAL; + rsize_t limit = n < smax ? n : smax; - if (!s) - throw_constraint_handler_s("memset_s: s = NULL", ret); - else if (n > RSIZE_MAX) - throw_constraint_handler_s("memset_s: n > RSIZE_MAX", ret); - else if (smax > RSIZE_MAX) - throw_constraint_handler_s("memset_s: smax > RSIZE_MAX", ret); - else if (n > smax) - throw_constraint_handler_s("memset_s: n > smax", ret); - else { - while (limit > 0) - dest[--limit] = (unsigned char)c; - ret = 0; - } - return ret; + if (!s) + throw_constraint_handler_s ("memset_s: s = NULL", ret); + else if (n > RSIZE_MAX) + throw_constraint_handler_s ("memset_s: n > RSIZE_MAX", ret); + else if (smax > RSIZE_MAX) + throw_constraint_handler_s ("memset_s: smax > RSIZE_MAX", ret); + else if (n > smax) + throw_constraint_handler_s ("memset_s: n > smax", ret); + else + { + while (limit > 0) + dest[--limit] = (unsigned char) c; + ret = 0; + } + return ret; } - -- cgit v1.2.3