comparison util.c @ 1008:51885857c019

Removed assumptions that path separators are Unix style outside of Unix-only verions of functions
author Michael Pavone <pavone@retrodev.com>
date Sun, 01 May 2016 21:39:43 -0700
parents fd7702bcc034
children 4a92889e2889
comparison
equal deleted inserted replaced
1007:5165537244e2 1008:51885857c019
92 } 92 }
93 *text = 0; 93 *text = 0;
94 return text+1; 94 return text+1;
95 } 95 }
96 96
97 char is_path_sep(char c)
98 {
99 #ifdef _WIN32
100 if (c == '\\') {
101 return 1;
102 }
103 #endif
104 return c == '/';
105 }
106
107 char is_absolute_path(char *path)
108 {
109 #ifdef _WIN32
110 if (path[1] == ':' && is_path_sep(path[2]) && isalpha(path[0])) {
111 return 1;
112 }
113 #endif
114 return is_path_sep(path[0]);
115 }
116
97 char * basename_no_extension(char *path) 117 char * basename_no_extension(char *path)
98 { 118 {
99 char *lastdot = NULL; 119 char *lastdot = NULL;
100 char *lastslash = NULL; 120 char *lastslash = NULL;
101 char *cur; 121 char *cur;
102 for (cur = path; *cur; cur++) 122 for (cur = path; *cur; cur++)
103 { 123 {
104 if (*cur == '.') { 124 if (*cur == '.') {
105 lastdot = cur; 125 lastdot = cur;
106 } else if (*cur == '/') { 126 } else if (is_path_sep(*cur)) {
107 lastslash = cur + 1; 127 lastslash = cur + 1;
108 } 128 }
109 } 129 }
110 if (!lastdot) { 130 if (!lastdot) {
111 lastdot = cur; 131 lastdot = cur;
312 if (GetLastError() != ERROR_PATH_NOT_FOUND) { 332 if (GetLastError() != ERROR_PATH_NOT_FOUND) {
313 warning("CreateDirectory failed with unexpected error code %X\n", GetLastError()); 333 warning("CreateDirectory failed with unexpected error code %X\n", GetLastError());
314 return 0; 334 return 0;
315 } 335 }
316 char *parent = strdup(path); 336 char *parent = strdup(path);
317 char *sep = strrchr(parent, '/'); 337 //Windows technically supports both native and Unix-style path separators
338 //so search for both
339 char *sep = strrchr(parent, '\\');
340 char *osep = strrchr(parent, '/');
341 if (osep && (!sep || osep < sep)) {
342 sep = osep;
343 }
318 if (!sep || sep == parent) { 344 if (!sep || sep == parent) {
319 //relative path, but for some reason we failed 345 //relative path, but for some reason we failed
320 return 0; 346 return 0;
321 } 347 }
322 *sep = 0; 348 *sep = 0;
370 goto fallback; 396 goto fallback;
371 } 397 }
372 int linksize = strlen(linktext); 398 int linksize = strlen(linktext);
373 for(cur = linktext + linksize - 1; cur != linktext; cur--) 399 for(cur = linktext + linksize - 1; cur != linktext; cur--)
374 { 400 {
375 if (*cur == '/') { 401 if (is_path_sep(*cur)) {
376 *cur = 0; 402 *cur = 0;
377 break; 403 break;
378 } 404 }
379 } 405 }
380 if (cur == linktext) { 406 if (cur == linktext) {
385 fputs("/proc/self/exe is not available and set_exe_str was not called!", stderr); 411 fputs("/proc/self/exe is not available and set_exe_str was not called!", stderr);
386 } 412 }
387 int pathsize = strlen(exe_str); 413 int pathsize = strlen(exe_str);
388 for(cur = exe_str + pathsize - 1; cur != exe_str; cur--) 414 for(cur = exe_str + pathsize - 1; cur != exe_str; cur--)
389 { 415 {
390 if (*cur == '/') { 416 if (is_path_sep(*cur)) {
391 exe_dir = malloc(cur-exe_str+1); 417 exe_dir = malloc(cur-exe_str+1);
392 memcpy(exe_dir, exe_str, cur-exe_str); 418 memcpy(exe_dir, exe_str, cur-exe_str);
393 exe_dir[cur-exe_str] = 0; 419 exe_dir[cur-exe_str] = 0;
394 break; 420 break;
395 } 421 }
531 if (sizeret) { 557 if (sizeret) {
532 *sizeret = -1; 558 *sizeret = -1;
533 } 559 }
534 return NULL; 560 return NULL;
535 } 561 }
536 char const *pieces[] = {exe_dir, "/", name}; 562 char const *pieces[] = {exe_dir, PATH_SEP, name};
537 char *path = alloc_concat_m(3, pieces); 563 char *path = alloc_concat_m(3, pieces);
538 FILE *f = fopen(path, "rb"); 564 FILE *f = fopen(path, "rb");
539 free(path); 565 free(path);
540 if (!f) { 566 if (!f) {
541 if (sizeret) { 567 if (sizeret) {