21char __cwd[MAXNAMLEN + 1] = { 0 };
23extern char __cwd[MAXNAMLEN + 1];
30extern size_t __cwd_len;
33#define defaultCWD "host:"
41#define isnum(c) ((c) >= '0' && (c) <= '9')
46int __get_drive(
const char *dev,
enum SeparatorType *usePOSIXSeparator)
60 tail = strchr(d,
':');
66 devname_len = (int)(tail - d);
69 while (isnum(d[devname_len - 1]))
75 if (devname_len == 5 && (memcmp(d,
"cdrom", 5) == 0))
77 *usePOSIXSeparator = SeparatorTypeWindows;
80 else if (devname_len == 3 && ((memcmp(d,
"rom", 3) == 0) || (memcmp(d,
"hdd", 3) == 0)))
82 *usePOSIXSeparator = SeparatorTypeNone;
86 *usePOSIXSeparator = SeparatorTypePOSIX;
95 return (tail - dev) + 1;
98int __get_drive(
const char *dev,
enum SeparatorType *usePOSIXSeparator);
102char *getcwd(
char *buf,
size_t size)
109 if(__cwd_len >= size) {
114 memcpy(buf, __cwd, __cwd_len);
115 buf[__cwd_len] =
'\x00';
120#ifdef F___path_absolute
123static int __path_normalize(
char *out,
int len,
int posixSeparator)
127 char separator = posixSeparator ?
'/' :
'\\';
128 size_t out_len = strnlen(out, len);
131 if (out_len >= len)
return -10;
134 out[out_len - 1] = separator;
138 for (
size_t i = 0; i < out_len; i++) {
139 if (out[i] ==
'/' || out[i] ==
'\\') {
145 for(i = 0; (i < out_len) && out[i+1]; i++) {
146 if(out[i] == separator && out[i+1] == separator) {
147 for(j=i+1; out[j]; j++)
154 for(i = 0; (i < out_len) && out[i] && out[i+1] && out[i+2]; i++) {
155 if(out[i] == separator && out[i+1] ==
'.' && out[i+2] == separator) {
156 for(j = i+1; out[j]; j++)
165 while((first < out_len) && (next < out_len)) {
167 if(out[next+1] && out[next+1] ==
'.' &&
168 out[next+2] && out[next+2] ==
'.' &&
169 out[next+3] && out[next+3] == separator) {
170 for(j=0; out[first+j+1]; j++)
171 out[first+j+1] = out[next+j+4];
178 for(next= first+1; out[next] && out[next] != separator; next++)
180 if(!out[next])
break;
184 for(i = 1; (i < out_len) && out[i]; i++)
186 if(i > 1 && out[i-1] == separator)
193int __path_absolute(
const char *in,
char *out,
int len)
196 enum SeparatorType separatorType;
201 dr = __get_drive(in, &separatorType);
202 char separator = separatorType == SeparatorTypePOSIX ?
'/' :
'\\';
204 if(dr > 0 && (separatorType == SeparatorTypeNone || in[dr] == separator)) {
206 if (in_len >= len)
return -1;
207 strncpy(out, in, len);
208 }
else if(dr > 0 && in[dr - 1] ==
':') {
210 if (in_len + 1 >= len)
return -2;
211 strncpy(out, in, dr);
213 strncpy(out + dr + 1, in + dr, len - dr - 1);
214 }
else if(in[0] ==
'\\' || in[0] ==
'/') {
216 if(__cwd_len + in_len >= len)
return -3;
217 memcpy(out, __cwd, __cwd_len);
218 out[__cwd_len] =
'\x00';
219 dr = __get_drive(out, &separatorType);
222 strncat(out, in, len);
225 if(__cwd_len + 1 + in_len >= len)
return -5;
226 memcpy(out, __cwd, __cwd_len);
227 out[__cwd_len] = separator;
228 out[__cwd_len + 1] =
'\x00';
229 strncat(out, in, len);
233 dr = __get_drive(out, &separatorType);
235 return __path_normalize(out + dr, len - dr, separatorType == SeparatorTypePOSIX);
241void __init_cwd(
int argc,
char ** argv)
248 char * p, * s = NULL;
250 for (p = argv[0]; *p; p++) {
251 if ((*p ==
'/') || (*p ==
'\\') || (*p ==
':')) {
260 char backup = *(++s);