Mercurial > repos > blastem
comparison paths.c @ 2262:bc68560b4a04
Fix bug when loading cue sheet without leading path
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 23 Dec 2022 06:23:41 -0800 |
parents | dda7479f3bbb |
children | 2972a8e16ed2 |
comparison
equal
deleted
inserted
replaced
2261:86dfcf3f418a | 2262:bc68560b4a04 |
---|---|
1 #include <string.h> | 1 #include <string.h> |
2 #include <stdlib.h> | 2 #include <stdlib.h> |
3 #include "blastem.h" | 3 #include "blastem.h" |
4 #include "util.h" | 4 #include "util.h" |
5 #ifdef _WIN32 | |
6 #include <windows.h> | |
7 #else | |
8 #include <unistd.h> | |
9 #include <errno.h> | |
10 #endif | |
5 | 11 |
6 static char **current_path; | 12 static char **current_path; |
7 | 13 |
8 static void persist_path(void) | 14 static void persist_path(void) |
9 { | 15 { |
138 char const *pieces[] = {base, PATH_SEP, suffix}; | 144 char const *pieces[] = {base, PATH_SEP, suffix}; |
139 return alloc_concat_m(3, pieces); | 145 return alloc_concat_m(3, pieces); |
140 } | 146 } |
141 } | 147 } |
142 } | 148 } |
149 | |
150 char *path_current_dir(void) | |
151 { | |
152 size_t size = 128; | |
153 char *res = malloc(size); | |
154 for (;;) { | |
155 #ifdef _WIN32 | |
156 DWORD actual = GetCurrentDirectoryA(size, res); | |
157 if (actual > size) { | |
158 res = realloc(res, actual); | |
159 size = actual; | |
160 } else { | |
161 return res; | |
162 } | |
163 #else | |
164 errno = 0; | |
165 char *tmp = getcwd(res, size); | |
166 if (!tmp) { | |
167 if (errno == ERANGE) { | |
168 size *= 2; | |
169 res = realloc(res, size); | |
170 } else { | |
171 free(res); | |
172 return NULL; | |
173 } | |
174 } else { | |
175 return res; | |
176 } | |
177 #endif | |
178 } | |
179 } |