15 #include <sys/param.h>
22 char __cwd[MAXNAMLEN + 1] = { 0 };
24 extern char __cwd[MAXNAMLEN + 1];
31 extern size_t __cwd_len;
34 #define defaultCWD "host:"
37 SeparatorTypeNotFound,
43 #define isnum(c) ((c) >= '0' && (c) <= '9')
48 int __get_drive(
const char *dev,
enum SeparatorType *usePOSIXSeparator,
int maxLen)
55 for (d = dev; ((int)(d - dev) < maxLen) && *d ==
' '; d += 1);
58 for (t = d; ((int)(t - dev) < maxLen) && *t && *t !=
':'; t += 1);
61 *usePOSIXSeparator = SeparatorTypeNotFound;
66 for (devname_len = (
int)(t - d); isnum(d[devname_len - 1]); devname_len -= 1);
68 *usePOSIXSeparator = SeparatorTypePOSIX;
73 if ((memcmp(d,
"rom", devname_len) == 0) || (memcmp(d,
"hdd", devname_len) == 0))
74 *usePOSIXSeparator = SeparatorTypeNone;
78 if ((memcmp(d,
"cdrom", devname_len) == 0))
79 *usePOSIXSeparator = SeparatorTypeWindows;
83 if ((memcmp(d,
"usbkbd", devname_len) == 0))
84 *usePOSIXSeparator = SeparatorTypeNone;
99 int __get_drive(
const char *dev,
enum SeparatorType *usePOSIXSeparator,
int maxLen);
103 char *getcwd(
char *buf,
size_t size)
107 size = __cwd_len + 1;
120 else if (size < (__cwd_len + 1))
126 snprintf(buf, size,
"%.*s", __cwd_len, __cwd);
131 #ifdef F___path_absolute
134 static int __path_normalize(
char *out,
int len,
int posixSeparator)
138 char separator = posixSeparator ?
'/' :
'\\';
139 size_t out_len = strnlen(out, len);
142 if (out_len >= len)
return -10;
145 out[out_len - 1] = separator;
149 for (
size_t i = 0; i < out_len; i++) {
150 if (out[i] ==
'/' || out[i] ==
'\\') {
156 for(i = 0; (i < out_len) && out[i+1]; i++) {
157 if(out[i] == separator && out[i+1] == separator) {
158 for(j=i+1; out[j]; j++)
165 for(i = 0; (i < out_len) && out[i] && out[i+1] && out[i+2]; i++) {
166 if(out[i] == separator && out[i+1] ==
'.' && out[i+2] == separator) {
167 for(j = i+1; out[j]; j++)
176 while((first < out_len) && (next < out_len)) {
178 if(out[next+1] && out[next+1] ==
'.' &&
179 out[next+2] && out[next+2] ==
'.' &&
180 out[next+3] && out[next+3] == separator) {
181 for(j=0; out[first+j+1]; j++)
182 out[first+j+1] = out[next+j+4];
189 for(next= first+1; out[next] && out[next] != separator; next++)
191 if(!out[next])
break;
195 for(i = 1; (i < out_len) && out[i]; i++)
197 if(i > 1 && out[i-1] == separator)
204 int __path_absolute(
const char *in,
char *out,
int len)
207 enum SeparatorType separatorType;
213 dr = __get_drive(in, &separatorType, in_len);
214 char separator = separatorType == SeparatorTypePOSIX ?
'/' :
'\\';
216 if((separatorType != SeparatorTypeNotFound) && (separatorType == SeparatorTypeNone || in[dr] == separator)) {
218 out_len = snprintf(out, len,
"%.*s", in_len, in);
219 }
else if((separatorType != SeparatorTypeNotFound) && in[dr - 1] ==
':') {
221 out_len = snprintf(out, len,
"%.*s%c%.*s", dr, in, separator, in_len - dr, in + dr);
222 }
else if(in[0] ==
'\\' || in[0] ==
'/') {
224 out_len = snprintf(out, len,
"%.*s%.*s", __get_drive(__cwd, &separatorType, __cwd_len), __cwd, in_len, in);
227 out_len = snprintf(out, len,
"%.*s%c%.*s", __cwd_len, __cwd, separator, in_len, in);
229 if (out_len >= len)
return -1;
232 dr = __get_drive(out, &separatorType, out_len);
233 return __path_normalize(out + dr, len - dr, separatorType == SeparatorTypePOSIX);
239 void __init_cwd(
int argc,
char ** argv)
246 char * p, * s = NULL;
248 for (p = argv[0]; *p; p++) {
249 if ((*p ==
'/') || (*p ==
'\\') || (*p ==
':')) {
258 char backup = *(++s);