summaryrefslogtreecommitdiff
path: root/src/shared.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared.c')
-rw-r--r--src/shared.c31
1 files changed, 15 insertions, 16 deletions
diff --git a/src/shared.c b/src/shared.c
index 1d5aa7f..e83261c 100644
--- a/src/shared.c
+++ b/src/shared.c
@@ -17,24 +17,23 @@ enum XALLOC_TYPE {
17}; 17};
18 18
19void * xalloc(size_t nmemb, size_t size, enum XALLOC_TYPE actype, void *ptr) { 19void * xalloc(size_t nmemb, size_t size, enum XALLOC_TYPE actype, void *ptr) {
20 if(actype <= XALLOC_INVAL || actype >= XALLOC_2BIG) 20 if(actype <= XALLOC_INVAL || actype >= XALLOC_2BIG) ERRRET(EINVAL, NULL);
21 RETURNWERR(EINVAL, NULL);
22 21
23 void *mem = NULL; 22 void *mem = NULL;
24 switch(actype) { 23 switch(actype) {
25 case XALLOC_MALLOC: 24 case XALLOC_MALLOC:
26 mem = malloc(nmemb * size); 25 mem = malloc(nmemb * size);
27 break; 26 break;
28 27
29 case XALLOC_CALLOC: 28 case XALLOC_CALLOC:
30 mem = calloc(nmemb, size); 29 mem = calloc(nmemb, size);
31 break; 30 break;
32 31
33 case XALLOC_REALLOC: 32 case XALLOC_REALLOC:
34 mem = realloc(ptr, nmemb * size); 33 mem = realloc(ptr, nmemb * size);
35 break; 34 break;
36 35
37 default: 36 default:
38 XALLOC_EXIT("<xalloc> An unknown alloc type was passed, which shouldn't be possible", ); 37 XALLOC_EXIT("<xalloc> An unknown alloc type was passed, which shouldn't be possible", );
39 } 38 }
40 39
@@ -61,7 +60,7 @@ int rwbuf(char **str, unsigned long int initsize, int fd) {
61 // Bytes read == 0, return 0 60 // Bytes read == 0, return 0
62 // Bytes read < 0, free string, return -1; 61 // Bytes read < 0, free string, return -1;
63 // When string hits capacity, double the capacity, and reallocate the string 62 // When string hits capacity, double the capacity, and reallocate the string
64 63 if(!str || initsize < 1) ERRRET(EINVAL, -1);
65 const int ECODE = -100; 64 const int ECODE = -100;
66 65
67 char *lstr = NULL, *tmp = NULL; 66 char *lstr = NULL, *tmp = NULL;
@@ -118,6 +117,8 @@ int rwbuf(char **str, unsigned long int initsize, int fd) {
118 117
119 118
120int wwbuf(int fd, const unsigned char *buf, int len) { 119int wwbuf(int fd, const unsigned char *buf, int len) {
120 if(!buf || len <= 0) ERRRET(EINVAL, -1);
121
121 int total = 0; 122 int total = 0;
122 int left = len; 123 int left = len;
123 int n = -1; 124 int n = -1;
@@ -225,7 +226,7 @@ char * xdirname(const char * const path) {
225 226
226 227
227int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int size) { 228int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int size) {
228 if(!loc || !callbacks || !arguments || size <= 0) {errno = EINVAL; return -1;} 229 if(!loc || !callbacks || !arguments || size <= 0) ERRRET(EINVAL, -1);
229 230
230 loc->callbacks = callbacks; 231 loc->callbacks = callbacks;
231 loc->arguments = arguments; 232 loc->arguments = arguments;
@@ -237,8 +238,8 @@ int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int siz
237 238
238// registers if flag is NOT set 239// registers if flag is NOT set
239int cleanup_register(cleanup *loc, fcallback cb, void *arg) { 240int cleanup_register(cleanup *loc, fcallback cb, void *arg) {
240 if(!loc || !cb) {errno = EINVAL; return -1;} 241 if(!loc || !cb) ERRRET(EINVAL, -1);
241 if(loc->used >= loc->size || loc->used < 0) {errno = ENOMEM; return -1;} 242 if(loc->used >= loc->size || loc->used < 0) ERRRET(ENOMEM, -1);
242 243
243 loc->callbacks[loc->used] = cb; 244 loc->callbacks[loc->used] = cb;
244 loc->arguments[loc->used] = arg; 245 loc->arguments[loc->used] = arg;
@@ -248,20 +249,18 @@ int cleanup_register(cleanup *loc, fcallback cb, void *arg) {
248} 249}
249 250
250int cleanup_cndregister(cleanup *loc, fcallback cb, void *arg, unsigned char flag) { 251int cleanup_cndregister(cleanup *loc, fcallback cb, void *arg, unsigned char flag) {
251 if(flag) 252 if(flag) return 0;
252 return 0;
253 return cleanup_register(loc, cb, arg); 253 return cleanup_register(loc, cb, arg);
254} 254}
255 255
256int cleanup_clear(cleanup *loc) { 256int cleanup_clear(cleanup *loc) {
257 if(!loc) {errno = EINVAL; return -1;} 257 if(!loc) ERRRET(EINVAL, -1);
258
259 loc->used = 0; 258 loc->used = 0;
260 return 0; 259 return 0;
261} 260}
262 261
263int cleanup_fire(cleanup *loc) { 262int cleanup_fire(cleanup *loc) {
264 if(!loc) {errno = EINVAL; return -1;} 263 if(!loc) ERRRET(EINVAL, -1);
265 264
266 for(int i = (loc->used - 1); i >= 0; i--) { 265 for(int i = (loc->used - 1); i >= 0; i--) {
267 if(loc->callbacks[i] == NULL) { 266 if(loc->callbacks[i] == NULL) {