diff options
author | axtloss <axtlos@getcryst.al> | 2024-07-08 13:03:03 +0200 |
---|---|---|
committer | axtloss <axtlos@getcryst.al> | 2024-07-08 13:03:03 +0200 |
commit | 5a0598e0b8d42bc3800cf0d0a49572d13f7b0afa (patch) | |
tree | 996744adacd2ff590597aab9e92bea23f46db07e | |
parent | a084dfa02d28a9372a7e406c9b101a43308a4937 (diff) | |
download | extlib-5a0598e0b8d42bc3800cf0d0a49572d13f7b0afa.tar.gz extlib-5a0598e0b8d42bc3800cf0d0a49572d13f7b0afa.tar.bz2 |
add new function rremove
-rw-r--r-- | src/extlib.c | 26 | ||||
-rw-r--r-- | src/extlib.h | 12 | ||||
-rw-r--r-- | src/extstring.c | 11 |
3 files changed, 43 insertions, 6 deletions
diff --git a/src/extlib.c b/src/extlib.c index 151a74f..13d7f50 100644 --- a/src/extlib.c +++ b/src/extlib.c @@ -17,12 +17,17 @@ * SPDX-License-Identifier: LGPL-3.0-only */ -#include "extlib.h" +#define _XOPEN_SOURCE 500 +#define USE_SECURE_MEM +#include <ftw.h> + +#include "extlib.h" #include <stdlib.h> #include <stdio.h> #include <string.h> + #undef free #undef malloc @@ -68,3 +73,22 @@ fcopy(FILE *f1, FILE *f2) fflush (f2); } } + +int +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 +rrmdir(char *pathname) +{ + 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 15903c3..a292707 100644 --- a/src/extlib.h +++ b/src/extlib.h @@ -21,6 +21,7 @@ #include <stdio.h> #include <unistd.h> +#ifdef USE_SECURE_MEM #define free(x) error - use free_secure #define malloc(x) error - use malloc_secure @@ -29,10 +30,16 @@ void free_secure (void **__ptr, size_t ptrlen); /// Automatically initialise the allocated memory with zeros void *malloc_secure (size_t len); +#endif -/// Copy the data of one filestream to another +/// Copy the data of one filestream to another */ void fcopy(FILE *f1, FILE *f2); +#if (_XOPEN_SOURCE == 500) +/// Recursively remove a directory pathname. +int rrmdir(char *pathname); +#endif + /// Compare the first n bytes of a memory area str to the value val. /// Returns 0 if the values the same, any other value if they are not the same. int memvcmp (void *str, char val, size_t n); @@ -44,7 +51,8 @@ char *strlwr(char *s); char *strupr(char *s); /// Trim spaces from a string -char *trim (char *s); +char *trim (char *s, int *rem_front, int *rem_back); /// Match string old and replace it with string replace char *replace_str (char *s, char *old, char *replace); + diff --git a/src/extstring.c b/src/extstring.c index 5867fc5..477f5f7 100644 --- a/src/extstring.c +++ b/src/extstring.c @@ -17,6 +17,7 @@ * SPDX-License-Identifier: LGPL-3.0-only */ +#define USE_SECURE_MEM #include <stdlib.h> #include <string.h> #include <ctype.h> @@ -50,20 +51,24 @@ strupr (char *s) } char * -trim (char *s) +trim (char *s, int *rem_front, int *rem_back) { char *result = strdup (s); char *end; - while (isspace ((unsigned char)*result)) + while (isspace ((unsigned char)*result)) { result++; + if (rem_front) *rem_front += 1; + } if (*result == 0) return result; end = result + strlen (result) - 1; - while (end > result && isspace ((unsigned char)*end)) + while (end > result && isspace ((unsigned char)*end)) { end--; + if (rem_back) *rem_back +=1; + } end[1] = '\0'; |