summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author@syxhe <https://t.me/syxhe>2025-01-26 23:32:25 -0600
committer@syxhe <https://t.me/syxhe>2025-01-26 23:32:25 -0600
commit47a1a3a803de74b2521720966318a8be23490e7c (patch)
tree0b3aaf37f02a3fa55112f9b993b1779e8b100073 /src
parent1a590a5859207d5adb4f7a72d6b9ed1ab301e8c4 (diff)
Add error macros for xdirname()
Diffstat (limited to 'src')
-rw-r--r--src/shared.c72
1 files changed, 59 insertions, 13 deletions
diff --git a/src/shared.c b/src/shared.c
index c46b001..d67e362 100644
--- a/src/shared.c
+++ b/src/shared.c
@@ -127,8 +127,16 @@ char *xdirname(const char * const path) {
127 char *tmp = NULL; 127 char *tmp = NULL;
128 if(!path) { // Path being null is a special case which should return super early, before anything else 128 if(!path) { // Path being null is a special case which should return super early, before anything else
129 tmp = strdup("."); 129 tmp = strdup(".");
130 if(!tmp) 130 if(!tmp) {
131 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
132 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
133 error(1, errno, "<xdirname> could not strdup \".\" for set path result \"NULL\"");
134 #else
135 exit(EXIT_FAILURE);
136 #endif
137 #endif
131 abort(); 138 abort();
139 }
132 140
133 return tmp; 141 return tmp;
134 } 142 }
@@ -139,9 +147,16 @@ char *xdirname(const char * const path) {
139 if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;} 147 if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;}
140 148
141 if(flag) { 149 if(flag) {
142 if(!tmp) 150 if(!tmp) {
143 abort(); 151 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
144 152 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
153 error(1, errno, "<xdirname> could not strdup a set path result");
154 #else
155 exit(EXIT_FAILURE);
156 #endif
157 #endif
158 abort();
159 }
145 return tmp; 160 return tmp;
146 } 161 }
147 162
@@ -158,8 +173,16 @@ char *xdirname(const char * const path) {
158 173
159 // Get a temp copy of the path for manipulation purposes 174 // Get a temp copy of the path for manipulation purposes
160 tmp = strdup(path); 175 tmp = strdup(path);
161 if(!tmp) 176 if(!tmp) {
162 abort(); 177 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
178 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
179 error(1, errno, "<xdirname> could not strdup the given path \"%s\" for internal manipulation", path);
180 #else
181 exit(EXIT_FAILURE);
182 #endif
183 #endif
184 abort();
185 }
163 186
164 // If there's a trailing '/', delete it 187 // If there's a trailing '/', delete it
165 size_t pathlen = strlen(path); 188 size_t pathlen = strlen(path);
@@ -181,16 +204,31 @@ char *xdirname(const char * const path) {
181 if(count == 0) { 204 if(count == 0) {
182 free(tmp); 205 free(tmp);
183 tmp = strdup("."); 206 tmp = strdup(".");
184 if(!tmp) 207 if(!tmp) {
185 abort(); 208 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
186 209 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
210 error(1, errno, "<xdirname> could not strdup \".\" for set path result");
211 #else
212 exit(EXIT_FAILURE);
213 #endif
214 #endif
215 abort();
216 }
187 return tmp; 217 return tmp;
188 } 218 }
189 if(count == 1) { 219 if(count == 1) {
190 free(tmp); 220 free(tmp);
191 tmp = strdup("/"); 221 tmp = strdup("/");
192 if(!tmp) 222 if(!tmp) {
193 abort(); 223 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
224 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
225 error(1, errno, "<xdirname> could not strdup \"/\" for set path result");
226 #else
227 exit(EXIT_FAILURE);
228 #endif
229 #endif
230 abort();
231 }
194 232
195 return tmp; 233 return tmp;
196 } 234 }
@@ -203,8 +241,16 @@ char *xdirname(const char * const path) {
203 } 241 }
204 242
205 char * const actual = strdup(tmp); 243 char * const actual = strdup(tmp);
206 if(!actual) 244 if(!actual) {
207 abort(); 245 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
246 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
247 error(1, errno, "<xdirname> could not strdup tmp string to make a shorter end string");
248 #else
249 exit(EXIT_FAILURE);
250 #endif
251 #endif
252 abort();
253 }
208 free(tmp); 254 free(tmp);
209 255
210 return actual; 256 return actual;