X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=vrlugburz;a=blobdiff_plain;f=src%2Ffs.c;fp=src%2Ffs.c;h=099e890f7b2be41570cd85b990e05a6ecb28fa20;hp=0000000000000000000000000000000000000000;hb=6066118fc6a58b379f52b9aaaf45200b136812b9;hpb=95e4bd8e387c3cb9fc325ae922052bf7bc7ae8ea diff --git a/src/fs.c b/src/fs.c new file mode 100644 index 0000000..099e890 --- /dev/null +++ b/src/fs.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include "fs.h" + +char *path_dir(const char *path, char *buf) +{ + char *sep; + + if(!buf) buf = (char*)path; + if(path != buf) { + strcpy(buf, path); + } + + if((sep = strrchr(buf, '/')) && sep > buf) { + *sep = 0; + } + return buf; +} + +char *path_file(const char *path, char *buf) +{ + int len; + char *sep; + + if(!buf) buf = (char*)path; + if(path != buf) { + strcpy(buf, path); + } + + if((sep = strrchr(buf, '/'))) { + len = strlen(sep + 1); + memmove(buf, sep + 1, len + 1); + } + return buf; +} + +char *combine_path(const char *dirname, const char *fname, char *buf) +{ + char *dest; + + if(!buf) return 0; + + if(!dirname || !*dirname) { + strcpy(buf, fname); + return buf; + } + + dest = buf; + while(*dirname) *dest++ = *dirname++; + + if(dest[-1] != '/') *dest++ = '/'; + + strcpy(dest, fname); + return buf; +} + +FILE *fopenat(const char *dirname, const char *fname, const char *attr) +{ + char *buf; + + if(!dirname || !*dirname) { + return fopen(fname, attr); + } + + buf = alloca(strlen(dirname) + strlen(fname) + 2); + combine_path(dirname, fname, buf); + return fopen(buf, attr); +}