finish OSLib/Generic.c

This commit is contained in:
Ash Wolf 2022-10-13 03:03:13 +01:00
parent 945f737516
commit bdf2608f1d
1 changed files with 181 additions and 2 deletions

View File

@ -230,27 +230,177 @@ char *OS_CompactPaths(char *buf, const char *p, const char *n, int size) {
int nlen;
char *ptr;
int bidx;
int wat;
plen = p ? strlen(p) : 0;
nlen = n ? strlen(n) : 0;
if (plen + nlen + 1 <= size) {
sprintf(buf, "%s%s", p ? p : "", n ? n : "");
return buf;
}
ptr = buf;
bidx = 0;
wat = plen + nlen - size;
while (plen > 0 && nlen > 0) {
if (plen > 0) {
*(ptr++) = *(p++);
plen--;
} else if (nlen > 0) {
*(ptr++) = *(n++);
nlen--;
}
if (++bidx == (size / 2) - 2) {
ptr[0] = '.';
ptr[1] = '.';
ptr[2] = '.';
ptr += 3;
bidx += 3;
if (plen > 0) {
plen -= wat / 2;
if (plen < 0) {
n -= plen;
nlen += plen;
plen = 0;
}
} else {
n += wat / 2;
nlen -= wat / 2;
}
}
}
*ptr = 0;
return buf;
}
char *OS_SpecToStringRelative(const OSSpec *spec, const OSPathSpec *cwdspec, char *path, int size) {
char fullbuf[256];
char *full;
char cwfbuf[256];
char cwdbuf[256];
char *cwd;
OSPathSpec mycwdspec;
char *pptr;
full = fullbuf;
cwd = cwdbuf;
OS_SpecToString(spec, full, 256);
if (!size)
size = 256;
if (!path) {
path = malloc(size);
if (!path)
return 0;
}
if (!cwdspec) {
OS_GetCWD(&mycwdspec);
cwdspec = &mycwdspec;
}
if (!OS_PathSpecToString(cwdspec, cwdbuf, 256)) {
memcpy(path, fullbuf, size - 1);
path[size - 1] = 0;
return path;
}
while (*cwd && *full == *cwd) {
full++;
cwd++;
}
if ((cwd - cwdbuf) < (strlen(fullbuf) / 2) && (strlen(cwd) > (strlen(fullbuf) / 2))) {
memcpy(path, fullbuf, size - 1);
path[size - 1] = 0;
return path;
}
while (cwd > cwdbuf) {
cwd--;
full--;
if (*cwd == '/')
break;
}
if (cwd == cwdbuf) {
strncpy(path, full, size - 1);
path[size - 1] = 0;
} else {
if (*(++cwd)) {
pptr = path;
while (*cwd) {
if (*cwd == '/')
pptr += sprintf(pptr, "../");
++cwd;
}
strcpy(pptr, full + 1);
} else {
strncpy(path, full + 1, size - 1);
path[size - 1] = 0;
}
}
return path;
}
int OS_FindFileInPath(const char *filename, const char *plist, OSSpec *spec) {
char *next;
const char *next;
char path[256];
int err;
while (plist && *plist) {
next = strchr(plist, ':');
if (!next)
next = strpbrk(plist, ":;");
if (!next)
next = plist + strlen(plist);
strcpyn(path, plist, next - plist, 255);
if (!OS_MakeSpec2(path, filename, spec)) {
if (!OS_Status(spec))
return 0;
}
if (*next)
plist = next + 1;
else
plist = 0;
}
err = OS_MakeFileSpec(filename, spec);
if (!err) {
err = OS_Status(spec);
if (!err)
err = 0;
}
return err;
}
int OS_FindProgram(const char *filename, OSSpec *spec) {
char *plist;
int err;
char temp[256];
strncpy(temp, filename, 256);
temp[255] = 0;
if (!strchr(temp, '/')) {
plist = getenv("PATH");
err = OS_FindFileInPath(temp, plist, spec);
if (!err)
return 0;
}
err = OS_MakeFileSpec(temp, spec);
if (!err)
err = OS_Status(spec);
return err;
}
int OS_CopyHandle(OSHandle *hand, OSHandle *copy) {
@ -258,10 +408,39 @@ int OS_CopyHandle(OSHandle *hand, OSHandle *copy) {
UInt32 sz;
void *f;
void *t;
err = OS_GetHandleSize(hand, &sz);
if (!err) {
err = OS_NewHandle(sz, copy);
if (!err) {
f = OS_LockHandle(hand);
t = OS_LockHandle(copy);
memcpy(t, f, sz);
OS_UnlockHandle(hand);
OS_UnlockHandle(copy);
return 0;
}
}
OS_FreeHandle(copy);
return err;
}
int OS_AppendHandle(OSHandle *hand, const void *data, UInt32 len) {
int err;
UInt32 sz;
void *buffer;
err = OS_GetHandleSize(hand, &sz);
if (!err) {
err = OS_ResizeHandle(hand, sz + len);
if (!err) {
buffer = OS_LockHandle(hand);
if (buffer) {
memcpy((unsigned char *) buffer + sz, data, len);
OS_UnlockHandle(hand);
return 0;
}
}
}
return err;
}