From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/psplit.c | 110 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/psplit.c (limited to 'reference/C/CONTRIB/SNIP/psplit.c') diff --git a/reference/C/CONTRIB/SNIP/psplit.c b/reference/C/CONTRIB/SNIP/psplit.c new file mode 100755 index 0000000..a1837b3 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/psplit.c @@ -0,0 +1,110 @@ +/* +** psplit() - Portable replacement for fnsplit(), _splitpath(), etc. +** +** Splits a full DOS pathname into drive, path, file, and extension +** specifications. Works with forward or back slash path separators and +** network file names, e.g. NET:LOONEY/BIN\WUMPUS.COM, Z:\MYDIR.NEW/NAME.EXT +** +** Arguments: 1 - Full pathname to split +** 2 - Buffer for drive +** 3 - Buffer for path +** 4 - Buffer for name +** 5 - Buffer for extension +** +** Returns: Nothing +** +** public domain by Bob Stout +*/ + +#include +#include + +#define NUL '\0' + +void psplit(char *path, char *drv, char *dir, char *fname, char *ext) +{ + char ch, *ptr, *p; + + /* convert slashes to backslashes for searching */ + + for (ptr = path; *ptr; ++ptr) + { + if ('/' == *ptr) + *ptr = '\\'; + } + + /* look for drive spec */ + + if (NULL != (ptr = strchr(path, ':'))) + { + ++ptr; + if (drv) + { + strncpy(drv, path, ptr - path); + drv[ptr - path] = NUL; + } + path = ptr; + } + else if (drv) + *drv = NUL; + + /* find rightmost backslash or leftmost colon */ + + if (NULL == (ptr = strrchr(path, '\\'))) + ptr = (strchr(path, ':')); + + if (!ptr) + { + ptr = path; /* obviously, no path */ + if (dir) + *dir = NUL; + } + else + { + ++ptr; /* skip the delimiter */ + if (dir) + { + ch = *ptr; + *ptr = NUL; + strcpy(dir, path); + *ptr = ch; + } + } + + if (NULL == (p = strrchr(ptr, '.'))) + { + if (fname) + strcpy(fname, ptr); + if (ext) + *ext = NUL; + } + else + { + *p = NUL; + if (fname) + strcpy(fname, ptr); + *p = '.'; + if (ext) + strcpy(ext, p); + } +} + +#ifdef TEST + +#include + +int main(int argc, char *argv[]) +{ + char drive[10], pathname[FILENAME_MAX], fname[9], ext[5]; + + while (--argc) + { + psplit(*++argv, drive, pathname, fname, ext); + printf("psplit(%s) returns:\n drive = %s\n path = %s\n" + " name = %s\n ext = %s\n", + *argv, drive, pathname, fname, ext); + } + return EXIT_SUCCESS; +} + +#endif -- cgit v1.2.3-54-g00ecf