MWCC/command_line/CmdLine/Src/OSLib/Generic.c

268 lines
5.8 KiB
C
Raw Normal View History

#include "oslib.h"
2022-10-12 17:07:57 +00:00
#include <errno.h>
static char wildname[63];
static char wilddir[255];
static OSOpenedDir wilddirref;
static OSSpec wildmatch;
char STSbuf[256];
2022-10-12 17:07:57 +00:00
inline char dummyfunc(int ch) {
return ch;
}
int WildCardMatch(char *wild, char *name) {
char next;
2022-10-12 17:07:57 +00:00
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) {
char filename[64];
Boolean isfile;
OSSpec spec;
const char *nptr;
2022-10-12 17:07:57 +00:00
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;
2022-10-12 17:07:57 +00:00
ptr = strrchr(path, '/');
return !ptr ? path : (ptr + 1);
}
char *OS_GetDirName(const OSPathSpec *spec, char *buf, int size) {
char *path;
char *pptr;
2022-10-12 17:07:57 +00:00
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) {
char bpath[256];
char *eptr;
int pthlen;
int fnlen;
2022-10-12 17:07:57 +00:00
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) {
Boolean relpath;
char buf[256];
char *mptr;
char *eptr;
2022-10-12 17:07:57 +00:00
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;
2022-10-12 17:07:57 +00:00
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;
2022-10-12 17:07:57 +00:00
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) {
int plen;
int nlen;
char *ptr;
int bidx;
}
char *OS_SpecToStringRelative(const OSSpec *spec, const OSPathSpec *cwdspec, char *path, int size) {
char fullbuf[256];
char *full;
char cwfbuf[256];
char *cwd;
OSPathSpec mycwdspec;
char *pptr;
}
int OS_FindFileInPath(const char *filename, const char *plist, OSSpec *spec) {
char *next;
char path[256];
int err;
}
int OS_FindProgram(const char *filename, OSSpec *spec) {
char *plist;
int err;
char temp[256];
}
int OS_CopyHandle(OSHandle *hand, OSHandle *copy) {
int err;
UInt32 sz;
void *f;
void *t;
}
int OS_AppendHandle(OSHandle *hand, const void *data, UInt32 len) {
int err;
UInt32 sz;
void *buffer;
}