diff options
Diffstat (limited to 'src/shared.c')
| -rw-r--r-- | src/shared.c | 105 |
1 files changed, 35 insertions, 70 deletions
diff --git a/src/shared.c b/src/shared.c index e83261c..c02414b 100644 --- a/src/shared.c +++ b/src/shared.c | |||
| @@ -6,55 +6,6 @@ | |||
| 6 | #include <errno.h> | 6 | #include <errno.h> |
| 7 | #include <error.h> | 7 | #include <error.h> |
| 8 | 8 | ||
| 9 | enum XALLOC_TYPE { | ||
| 10 | XALLOC_INVAL, // Default when unset | ||
| 11 | |||
| 12 | XALLOC_MALLOC, | ||
| 13 | XALLOC_CALLOC, | ||
| 14 | XALLOC_REALLOC, | ||
| 15 | |||
| 16 | XALLOC_2BIG // Out of range | ||
| 17 | }; | ||
| 18 | |||
| 19 | void * xalloc(size_t nmemb, size_t size, enum XALLOC_TYPE actype, void *ptr) { | ||
| 20 | if(actype <= XALLOC_INVAL || actype >= XALLOC_2BIG) ERRRET(EINVAL, NULL); | ||
| 21 | |||
| 22 | void *mem = NULL; | ||
| 23 | switch(actype) { | ||
| 24 | case XALLOC_MALLOC: | ||
| 25 | mem = malloc(nmemb * size); | ||
| 26 | break; | ||
| 27 | |||
| 28 | case XALLOC_CALLOC: | ||
| 29 | mem = calloc(nmemb, size); | ||
| 30 | break; | ||
| 31 | |||
| 32 | case XALLOC_REALLOC: | ||
| 33 | mem = realloc(ptr, nmemb * size); | ||
| 34 | break; | ||
| 35 | |||
| 36 | default: | ||
| 37 | XALLOC_EXIT("<xalloc> An unknown alloc type was passed, which shouldn't be possible", ); | ||
| 38 | } | ||
| 39 | |||
| 40 | if(!mem) | ||
| 41 | XALLOC_EXIT("<xalloc> Could not allocate memory", ); | ||
| 42 | |||
| 43 | return mem; | ||
| 44 | } | ||
| 45 | |||
| 46 | void * xmalloc(size_t size) { | ||
| 47 | return xalloc(size, 1, XALLOC_MALLOC, NULL); | ||
| 48 | } | ||
| 49 | |||
| 50 | void * xcalloc(size_t nmemb, size_t size) { | ||
| 51 | return xalloc(nmemb, size, XALLOC_CALLOC, NULL); | ||
| 52 | } | ||
| 53 | |||
| 54 | void * xreallocarray(void *ptr, size_t nmemb, size_t size) { | ||
| 55 | return xalloc(nmemb, size, XALLOC_REALLOC, ptr); | ||
| 56 | } | ||
| 57 | |||
| 58 | int rwbuf(char **str, unsigned long int initsize, int fd) { | 9 | int rwbuf(char **str, unsigned long int initsize, int fd) { |
| 59 | // Try to read bytes from fd into str | 10 | // Try to read bytes from fd into str |
| 60 | // Bytes read == 0, return 0 | 11 | // Bytes read == 0, return 0 |
| @@ -67,7 +18,10 @@ int rwbuf(char **str, unsigned long int initsize, int fd) { | |||
| 67 | ssize_t bytesread = -1; | 18 | ssize_t bytesread = -1; |
| 68 | int csize = initsize, ccap = initsize; | 19 | int csize = initsize, ccap = initsize; |
| 69 | 20 | ||
| 70 | lstr = xcalloc(initsize, sizeof(char)); | 21 | lstr = calloc(initsize, sizeof(char)); |
| 22 | if(!lstr) | ||
| 23 | return -1; | ||
| 24 | |||
| 71 | while((bytesread = read(fd, lstr + (csize - ccap), ccap)) > 0) { | 25 | while((bytesread = read(fd, lstr + (csize - ccap), ccap)) > 0) { |
| 72 | ccap -= bytesread; | 26 | ccap -= bytesread; |
| 73 | if(ccap <= 0) { | 27 | if(ccap <= 0) { |
| @@ -138,12 +92,14 @@ int wwbuf(int fd, const unsigned char *buf, int len) { | |||
| 138 | // Thanks Beej! | 92 | // Thanks Beej! |
| 139 | 93 | ||
| 140 | // dirname but less retarded hopefully | 94 | // dirname but less retarded hopefully |
| 141 | char * xdirname(const char * const path) { | 95 | char * vxdirname(const char * const path) { |
| 142 | char *tmp = NULL; | 96 | char *tmp = NULL; |
| 143 | if(!path) { // Path being null is a special case which should return super early, before anything else | 97 | if(!path) { // Path being null is a special case which should return early, before anything else (as to avoid null dereference) |
| 144 | tmp = strdup("."); | 98 | tmp = strdup("."); |
| 145 | if(!tmp) | 99 | if(!tmp) { |
| 146 | XALLOC_EXIT("<xdirname> could not strdup \".\" for set path result \"NULL\"", ); | 100 | WARN(errno, "<vxdirname> could not strdup \".\" for set path result \"NULL\"", ); |
| 101 | return NULL; | ||
| 102 | } | ||
| 147 | 103 | ||
| 148 | return tmp; | 104 | return tmp; |
| 149 | } | 105 | } |
| @@ -154,9 +110,11 @@ char * xdirname(const char * const path) { | |||
| 154 | if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;} | 110 | if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;} |
| 155 | 111 | ||
| 156 | if(flag) { | 112 | if(flag) { |
| 157 | if(!tmp) | 113 | if(!tmp) { |
| 158 | XALLOC_EXIT("<xdirname> could not strdup a set path result", ); | 114 | WARN(errno, "<vxdirname> could not strdup a set path result", ); |
| 159 | 115 | return NULL; | |
| 116 | } | ||
| 117 | |||
| 160 | return tmp; | 118 | return tmp; |
| 161 | } | 119 | } |
| 162 | 120 | ||
| @@ -173,8 +131,10 @@ char * xdirname(const char * const path) { | |||
| 173 | 131 | ||
| 174 | // Get a temp copy of the path for manipulation purposes | 132 | // Get a temp copy of the path for manipulation purposes |
| 175 | tmp = strdup(path); | 133 | tmp = strdup(path); |
| 176 | if(!tmp) | 134 | if(!tmp) { |
| 177 | XALLOC_EXIT("<xdirname> could not strdup the given path \"%s\" for internal manipulation", , path); | 135 | WARN(errno, "<vxdirname> could not strdup the given path \"%s\" for internal manipulation", , path); |
| 136 | return NULL; | ||
| 137 | } | ||
| 178 | 138 | ||
| 179 | // If there's a trailing '/', delete it | 139 | // If there's a trailing '/', delete it |
| 180 | size_t pathlen = strlen(path); | 140 | size_t pathlen = strlen(path); |
| @@ -193,21 +153,24 @@ char * xdirname(const char * const path) { | |||
| 193 | count++; | 153 | count++; |
| 194 | } | 154 | } |
| 195 | 155 | ||
| 196 | if(count == 0) { | 156 | if(count == 0 || count == 1) |
| 197 | free(tmp); | 157 | free(tmp); |
| 158 | if(count == 0) { | ||
| 198 | tmp = strdup("."); | 159 | tmp = strdup("."); |
| 199 | if(!tmp) | 160 | if(!tmp) { |
| 200 | XALLOC_EXIT("<xdirname> could not strdup \".\" for set path result", ); | 161 | WARN(errno, "<xdirname> could not strdup \".\" for set path result", ); |
| 162 | return NULL; | ||
| 163 | } | ||
| 201 | return tmp; | 164 | return tmp; |
| 202 | } | 165 | } else if(count == 1) { |
| 203 | if(count == 1) { | ||
| 204 | free(tmp); | ||
| 205 | tmp = strdup("/"); | 166 | tmp = strdup("/"); |
| 206 | if(!tmp) | 167 | if(!tmp) { |
| 207 | XALLOC_EXIT("<xdirname> could not strdup \"/\" for set path result", ); | 168 | WARN(errno, "<xdirname> could not strdup \"/\" for set path result", ); |
| 208 | 169 | return NULL; | |
| 170 | } | ||
| 209 | return tmp; | 171 | return tmp; |
| 210 | } | 172 | } |
| 173 | // This is retarded, fix it | ||
| 211 | 174 | ||
| 212 | for(size_t i = 0, c2 = 0; i < pathlen; i++) { | 175 | for(size_t i = 0, c2 = 0; i < pathlen; i++) { |
| 213 | if(tmp[i] == '/') | 176 | if(tmp[i] == '/') |
| @@ -217,9 +180,11 @@ char * xdirname(const char * const path) { | |||
| 217 | } | 180 | } |
| 218 | 181 | ||
| 219 | char * const actual = strdup(tmp); | 182 | char * const actual = strdup(tmp); |
| 220 | if(!actual) | ||
| 221 | XALLOC_EXIT("<xdirname> could not strdup tmp string to make a shorter end string", ); | ||
| 222 | free(tmp); | 183 | free(tmp); |
| 184 | if(!actual) { | ||
| 185 | WARN(errno, "<xdirname> could not strdup tmp string to make a shorter end string", ); | ||
| 186 | return NULL; | ||
| 187 | } | ||
| 223 | 188 | ||
| 224 | return actual; | 189 | return actual; |
| 225 | } | 190 | } |
