fixpath.c
来自「俄罗斯高人Mamaich的Pocket gcc编译器(运行在PocketPC上)」· C语言 代码 · 共 109 行
C
109 行
#include <stdlib.h>#include <string.h>#include <sys/wcetrace.h>#include <sys/wcebase.h>#include <sys/wcefile.h>#include <sys/fixpath.h>voidfixpath(const char *pathin, char *pathout){ char buf[MAX_PATH]; int len, idx; char *cp, *pip; if (pathin == NULL || pathout == NULL) { return; } /* Strip off leading/trailing quotes */ pip = pathin; if (pip[0] == '"') { pip++; idx = strlen(pip)-1; if (pip[idx] == '"') pip[idx] = '\0'; } /* Preprocessing: strip off drive letter if any */ if (pip[0] != '/' && pip[0] != '\\' && pip[1] == ':' && (pip[2] == '/' || pip[2] == '\\')) { pip += 2; WCETRACE(WCE_IO, "fixpath: drive letter stripped, pip \"%s\"", pip); } /* Path is not absolute - insert cwd */ WCETRACE(WCE_IO, "DEBUG: pip \"%s\"", pip); if ((pip[0] != '\\' && pip[0] != '/') || pip[0] == '.') { if (strncmp(pip, "./", 2) == 0) { pip += 2; } strcpy(pathout, getcwd(buf, MAX_PATH)); /* If cwd is not the root directory (/) append "/" */ if (!(strlen(buf) == 1 && buf[0] == '/')) strcat(pathout, "/"); strcat(pathout, pip); } else { strcpy(pathout, pip); } WCETRACE(WCE_IO, "fixpath(1): pathout \"%s\"", pathout); for (cp = pathout; *cp; cp++) { if (*cp == '/') { *cp = '\\'; } } /* disallow slash at end of directory name */ if (cp[-1] == '\\' && cp != pathout + 1) cp[-1] = 0; /* now remove . and .. */ _canonicalizepath(pathout); WCETRACE(WCE_IO, "fixpath(2): pathout \"%s\"", pathout); return pathout;}#define IS_DIRECTORY_SEP(X) (X == '\\')#define IS_ANY_SEP(X) (X == '\\')/* This is from emacs... */static char *_canonicalizepath(char *target){ char *p = target; char *o = target; while (*p) { if (!IS_DIRECTORY_SEP(*p)) { *o++ = *p++; } else if (IS_DIRECTORY_SEP(p[0]) && p[1] == '.' && (IS_DIRECTORY_SEP(p[2]) || p[2] == 0)) { /* If "/." is the entire filename, keep the "/". Otherwise just delete the whole "/.". */ if (o == target && p[2] == '\0') *o++ = *p; p += 2; } else if (IS_DIRECTORY_SEP(p[0]) && p[1] == '.' && p[2] == '.' /* `/../' is the "superroot" on certain file systems. */ && o != target && (IS_DIRECTORY_SEP(p[3]) || p[3] == 0)) { while (o != target && (--o) && !IS_DIRECTORY_SEP(*o)); /* Keep initial / only if this is the whole name. */ if (o == target && IS_ANY_SEP(*o) && p[3] == 0) ++o; p += 3; } else { *o++ = *p++; } } *o = 0; return target;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?