diff options
Diffstat (limited to '')
-rw-r--r-- | command_line/CmdLine/Src/OSLib/Generic.c | 178 |
1 files changed, 176 insertions, 2 deletions
diff --git a/command_line/CmdLine/Src/OSLib/Generic.c b/command_line/CmdLine/Src/OSLib/Generic.c index 4430790..e95ebd5 100644 --- a/command_line/CmdLine/Src/OSLib/Generic.c +++ b/command_line/CmdLine/Src/OSLib/Generic.c @@ -1,4 +1,5 @@ #include "oslib.h" +#include <errno.h> static char wildname[63]; static char wilddir[255]; @@ -6,9 +7,48 @@ static OSOpenedDir wilddirref; static OSSpec wildmatch; char STSbuf[256]; -int WildCardMatch(const char *wild, const char *name) { +inline char dummyfunc(int ch) { + return ch; +} + +int WildCardMatch(char *wild, char *name) { char next; - const char *prev; + char *prev; + + if (!name[0]) + return 0; + + while (*wild) { + if (*wild == '*') { + wild++; + next = *wild; + prev = 0; + + while (*name) { + if (dummyfunc(*name) == dummyfunc(next)) + prev = name; + ++name; + } + + if (prev) + name = prev; + + if (dummyfunc(*name) != dummyfunc(next)) + return 0; + } else if (*wild == '?' && *name) { + wild++; + name++; + if (!*wild && *name) + return 0; + } else if (dummyfunc(*wild) == dummyfunc(*name)) { + wild++; + name++; + } else { + return 0; + } + } + + return !name[0] || (name[0] == '/' && !name[1]); } OSSpec *OS_MatchPath(const char *path) { @@ -16,15 +56,63 @@ OSSpec *OS_MatchPath(const char *path) { Boolean isfile; OSSpec spec; const char *nptr; + + if (path) { + nptr = strrchr(path, '/'); + if (!nptr) { + nptr = path; + strcpyn(wilddir, ".", -1, 256); + } else { + nptr = nptr + 1; + strcpyn(wilddir, path, nptr - path, 256); + } + + if (OS_MakePathSpec(0, wilddir, &spec.path)) + return 0; + + strcpyn(wildname, nptr, -1, 64); + if (OS_MakeNameSpec(wildname, &spec.name)) + return 0; + + if (OS_OpenDir(&spec.path, &wilddirref)) + return 0; + } + + while (!OS_ReadDir(&wilddirref, &wildmatch, filename, &isfile)) { + if (isfile && WildCardMatch(wildname, filename)) + return &wildmatch; + } + + OS_CloseDir(&wilddirref); + return 0; } char *OS_GetFileNamePtr(char *path) { char *ptr; + ptr = strrchr(path, '/'); + return !ptr ? path : (ptr + 1); } char *OS_GetDirName(const OSPathSpec *spec, char *buf, int size) { char *path; char *pptr; + + if (!spec || !buf || size <= 0) + return 0; + + path = OS_PathSpecToString(spec, STSbuf, 256); + pptr = path + strlen(path) - 1; + if (pptr > path && *pptr == '/') { + *pptr = 0; + --pptr; + } + + while (pptr >= path && *pptr != '/') + pptr--; + + strncpy(buf, pptr, size - 1); + buf[size - 1] = 0; + return buf; } int OS_MakeSpec2(const char *path, const char *filename, OSSpec *spec) { @@ -32,6 +120,24 @@ int OS_MakeSpec2(const char *path, const char *filename, OSSpec *spec) { char *eptr; int pthlen; int fnlen; + + if (!path) + path = ""; + if (!filename) + filename = ""; + + fnlen = strlen(filename); + pthlen = strlen(path); + if (fnlen + pthlen + 1 > 255) + return ENAMETOOLONG; + + strncpy(bpath, path, pthlen); + eptr = bpath + pthlen; + if (eptr[-1] != '/') + *(eptr++) = '/'; + strcpy(eptr, filename); + + return OS_MakeSpec(bpath, spec, 0); } int OS_MakeSpecWithPath(OSPathSpec *path, const char *filename, Boolean noRelative, OSSpec *spec) { @@ -39,16 +145,84 @@ int OS_MakeSpecWithPath(OSPathSpec *path, const char *filename, Boolean noRelati char buf[256]; char *mptr; char *eptr; + + relpath = filename && strpbrk(filename, "/\\:"); + + if (!filename) { + if (path) + spec->path = *path; + else + OS_GetCWD(&spec->path); + return OS_MakeNameSpec("", &spec->name); + } else { + if ((!noRelative || !relpath) && !OS_IsFullPath(filename)) { + if (path) + OS_PathSpecToString(path, buf, 256); + else + buf[0] = 0; + + mptr = &buf[255] - strlen(filename); + eptr = &buf[strlen(buf)]; + strcpy((eptr > mptr) ? mptr : eptr, filename); + return OS_MakeSpec(buf, spec, 0); + } else { + return OS_MakeSpec(filename, spec, 0); + } + } } int OS_NameSpecChangeExtension(OSNameSpec *spec, const char *ext, Boolean append) { char tmp[64]; char *per; + + OS_NameSpecToString(spec, tmp, 256); + if (!append) { + per = strrchr(tmp, '.'); + if (!per) + per = tmp + strlen(tmp); + } else { + per = tmp + strlen(tmp); + if (ext[0] != '.') { + per[0] = '.'; + per[1] = 0; + per += 1; + } + } + + if (strlen(tmp) + strlen(ext) > 64) + per = tmp + 63 - strlen(ext); + + strcpy(per, ext); + return OS_MakeNameSpec(tmp, spec); } int OS_NameSpecSetExtension(OSNameSpec *spec, const char *ext) { char tmp[64]; char *per; + + OS_NameSpecToString(spec, tmp, 256); + if (ext[0] != '.') { + per = strrchr(tmp, '.'); + if (!per) + per = tmp + strlen(tmp); + + if (ext[0]) { + if (strlen(tmp) + 1 >= 64) { + per[-1] = '.'; + } else { + per[0] = '.'; + per++; + } + } + } else { + per = tmp + strlen(tmp); + } + + if (strlen(tmp) + strlen(ext) > 64) + per = tmp + 63 - strlen(ext); + + strcpy(per, ext); + return OS_MakeNameSpec(tmp, spec); } char *OS_CompactPaths(char *buf, const char *p, const char *n, int size) { |