21char __cwd[MAXNAMLEN + 1] = { 0 };
26#define defaultCWD "host:"
29 SeparatorTypeNotFound,
35#define isnum(c) ((c) >= '0' && (c) <= '9')
39int __get_drive(
const char *dev,
enum SeparatorType *usePOSIXSeparator,
int maxLen)
46 for (d = dev; ((int)(d - dev) < maxLen) && *d ==
' '; d += 1);
49 for (t = d; ((int)(t - dev) < maxLen) && *t && *t !=
':'; t += 1);
52 *usePOSIXSeparator = SeparatorTypeNotFound;
57 for (devname_len = (
int)(t - d); isnum(d[devname_len - 1]); devname_len -= 1);
59 *usePOSIXSeparator = SeparatorTypePOSIX;
64 if ((memcmp(d,
"rom", devname_len) == 0) || (memcmp(d,
"hdd", devname_len) == 0))
65 *usePOSIXSeparator = SeparatorTypeNone;
69 if ((memcmp(d,
"cdrom", devname_len) == 0))
70 *usePOSIXSeparator = SeparatorTypeWindows;
74 if ((memcmp(d,
"usbkbd", devname_len) == 0))
75 *usePOSIXSeparator = SeparatorTypeNone;
90char *getcwd(
char *buf,
size_t size)
107 else if (size < (__cwd_len + 1))
113 snprintf(buf, size,
"%.*s", __cwd_len, __cwd);
119static int __path_normalize(
char *out,
int len,
int posixSeparator)
123 char separator = posixSeparator ?
'/' :
'\\';
124 size_t out_len = strnlen(out, len);
127 if (out_len >= len)
return -10;
130 out[out_len - 1] = separator;
134 for (
size_t i = 0; i < out_len; i++) {
135 if (out[i] ==
'/' || out[i] ==
'\\') {
141 for(i = 0; (i < out_len) && out[i+1]; i++) {
142 if(out[i] == separator && out[i+1] == separator) {
143 for(j=i+1; out[j]; j++)
150 for(i = 0; (i < out_len) && out[i] && out[i+1] && out[i+2]; i++) {
151 if(out[i] == separator && out[i+1] ==
'.' && out[i+2] == separator) {
152 for(j = i+1; out[j]; j++)
161 while((first < out_len) && (next < out_len)) {
163 if(out[next+1] && out[next+1] ==
'.' &&
164 out[next+2] && out[next+2] ==
'.' &&
165 out[next+3] && out[next+3] == separator) {
166 for(j=0; out[first+j+1]; j++)
167 out[first+j+1] = out[next+j+4];
174 for(next= first+1; out[next] && out[next] != separator; next++)
176 if(!out[next])
break;
180 for(i = 1; (i < out_len) && out[i]; i++)
182 if(i > 1 && out[i-1] == separator)
189int __path_absolute(
const char *in,
char *out,
int len)
192 enum SeparatorType separatorType;
198 dr = __get_drive(in, &separatorType, in_len);
199 char separator = separatorType == SeparatorTypePOSIX ?
'/' :
'\\';
201 if((separatorType != SeparatorTypeNotFound) && (separatorType == SeparatorTypeNone || in[dr] == separator)) {
203 out_len = snprintf(out, len,
"%.*s", in_len, in);
204 }
else if((separatorType != SeparatorTypeNotFound) && in[dr - 1] ==
':') {
206 out_len = snprintf(out, len,
"%.*s%c%.*s", dr, in, separator, in_len - dr, in + dr);
207 }
else if(in[0] ==
'\\' || in[0] ==
'/') {
209 out_len = snprintf(out, len,
"%.*s%.*s", __get_drive(__cwd, &separatorType, __cwd_len), __cwd, in_len, in);
212 out_len = snprintf(out, len,
"%.*s%c%.*s", __cwd_len, __cwd, separator, in_len, in);
214 if (out_len >= len)
return -1;
217 dr = __get_drive(out, &separatorType, out_len);
218 return __path_normalize(out + dr, len - dr, separatorType == SeparatorTypePOSIX);
222void __init_cwd(
int argc,
char ** argv)
229 char * p, * s = NULL;
231 for (p = argv[0]; *p; p++) {
232 if ((*p ==
'/') || (*p ==
'\\') || (*p ==
':')) {
241 char backup = *(++s);