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]))
74 *usePOSIXSeparator = SeparatorTypePOSIX;
79 if ((memcmp(d,
"rom", devname_len) == 0) || (memcmp(d,
"hdd", devname_len) == 0))
80 *usePOSIXSeparator = SeparatorTypeNone;
84 if ((memcmp(d,
"cdrom", devname_len) == 0))
85 *usePOSIXSeparator = SeparatorTypeWindows;
89 if ((memcmp(d,
"usbkbd", devname_len) == 0))
90 *usePOSIXSeparator = SeparatorTypeNone;
102 return (tail - dev) + 1;
105int __get_drive(
const char *dev,
enum SeparatorType *usePOSIXSeparator);
109char *getcwd(
char *buf,
size_t size)
116 if(__cwd_len >= size) {
121 memcpy(buf, __cwd, __cwd_len);
122 buf[__cwd_len] =
'\x00';
127#ifdef F___path_absolute
130static int __path_normalize(
char *out,
int len,
int posixSeparator)
134 char separator = posixSeparator ?
'/' :
'\\';
135 size_t out_len = strnlen(out, len);
138 if (out_len >= len)
return -10;
141 out[out_len - 1] = separator;
145 for (
size_t i = 0; i < out_len; i++) {
146 if (out[i] ==
'/' || out[i] ==
'\\') {
152 for(i = 0; (i < out_len) && out[i+1]; i++) {
153 if(out[i] == separator && out[i+1] == separator) {
154 for(j=i+1; out[j]; j++)
161 for(i = 0; (i < out_len) && out[i] && out[i+1] && out[i+2]; i++) {
162 if(out[i] == separator && out[i+1] ==
'.' && out[i+2] == separator) {
163 for(j = i+1; out[j]; j++)
172 while((first < out_len) && (next < out_len)) {
174 if(out[next+1] && out[next+1] ==
'.' &&
175 out[next+2] && out[next+2] ==
'.' &&
176 out[next+3] && out[next+3] == separator) {
177 for(j=0; out[first+j+1]; j++)
178 out[first+j+1] = out[next+j+4];
185 for(next= first+1; out[next] && out[next] != separator; next++)
187 if(!out[next])
break;
191 for(i = 1; (i < out_len) && out[i]; i++)
193 if(i > 1 && out[i-1] == separator)
200int __path_absolute(
const char *in,
char *out,
int len)
203 enum SeparatorType separatorType;
208 dr = __get_drive(in, &separatorType);
209 char separator = separatorType == SeparatorTypePOSIX ?
'/' :
'\\';
211 if(dr > 0 && (separatorType == SeparatorTypeNone || in[dr] == separator)) {
213 if (in_len >= len)
return -1;
214 strncpy(out, in, len);
215 }
else if(dr > 0 && in[dr - 1] ==
':') {
217 if (in_len + 1 >= len)
return -2;
218 strncpy(out, in, dr);
220 strncpy(out + dr + 1, in + dr, len - dr - 1);
221 }
else if(in[0] ==
'\\' || in[0] ==
'/') {
223 if(__cwd_len + in_len >= len)
return -3;
224 memcpy(out, __cwd, __cwd_len);
225 out[__cwd_len] =
'\x00';
226 dr = __get_drive(out, &separatorType);
229 strncat(out, in, len);
232 if(__cwd_len + 1 + in_len >= len)
return -5;
233 memcpy(out, __cwd, __cwd_len);
234 out[__cwd_len] = separator;
235 out[__cwd_len + 1] =
'\x00';
236 strncat(out, in, len);
240 dr = __get_drive(out, &separatorType);
242 return __path_normalize(out + dr, len - dr, separatorType == SeparatorTypePOSIX);
248void __init_cwd(
int argc,
char ** argv)
255 char * p, * s = NULL;
257 for (p = argv[0]; *p; p++) {
258 if ((*p ==
'/') || (*p ==
'\\') || (*p ==
':')) {
267 char backup = *(++s);