From: k3040e4 AT c210 DOT edvz DOT uni-linz DOT ac DOT at (Oberhumer Markus) Message-Id: <199606041731.TAA13345@c210.edvz.uni-linz.ac.at> Subject: basename() To: djgpp-workers AT delorie DOT com Date: Tue, 4 Jun 1996 19:31:53 -0200 (MET DST) Return-Read-To: markus DOT oberhumer AT jk DOT uni-linz DOT ac DOT at Content-Type: text Content-Length: 3315 =============================================================================== Markus F.X.J. Oberhumer Subject: basename() To: djgpp-workers AT delorie DOT com =============================================================================== This is my suggestion for basename() as found in Linux and many other UNIX variants. Prototype should probably reside in Regards, Markus /* src/libc/dos/dir/basename.c */ /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */ #include #include #include #include /* always returns the same value like fnsplit() */ char * basename (const char *path) { const char *pp, *pe; const char *b; pp = path; if ((isalpha(*pp) || strchr("[\\]^_`", *pp)) && (pp[1] == ':')) pp += 2; if ((pe = strrchr(pp, '\\')) != 0) pp = pe + 1; if ((pe = strrchr(pp, '/')) != 0) pp = pe + 1; b = pp; /* Special case: "c:/path/." or "c:/path/.." These mean FILENAME, not EXTENSION. */ while (*pp == '.') ++pp; pe = strrchr(pp, '.'); if (!pe) pe = strchr(pp, '\0'); if (pp != pe) b = pp; return unconst(b, char *); } #if defined(TEST) || defined(TEST_COMPAT) #include #ifdef TEST_COMPAT #include #endif static int test (const char *n) { #ifdef TEST_COMPAT char name[256], ext[80]; printf("%-50s '%s'\n", n, basename(n)); fnsplit(n,NULL,NULL,name,ext); strcat(name,ext); if (strcmp(basename(n),name) != 0) { printf(" error: fnsplit() returns '%s'\n", name); return 1; } #else printf("%-50s '%s'\n", n, basename(n)); #endif return 0; } int main(int argc, char *argv[]) { int i; int r = 0; if (argc < 2) { r |= test("c:"); r |= test("c:/"); r |= test("c:/."); r |= test("c:/.."); r |= test("c:../"); r |= test("c:../test.dat"); r |= test("c:/dos"); r |= test("c:/dos/"); r |= test("c:/dos.622"); r |= test("c:/dos.622/"); r |= test("c:a.file.with.many.dots"); r |= test("c:/a.file.with.many.dots"); r |= test("c:.a.file.with.many.dots"); r |= test("c:..a.file.with.many.dots"); r |= test("c:/.cshrc"); r |= test("c:/.csh.rc"); } for (i = 1; i < argc; i++) r |= test(argv[i]); return r; } #endif *** e:unistd.h Sun Jun 18 05:48:26 1995 --- unistd.h Sun Jun 02 18:50:18 1996 *************** *** 120,123 **** --- 120,124 ---- #define D_OK 0x10 + char * basename(const char *_path); int brk(void *_heaptop); int __file_exists(const char *_fn); *************** *** 125,135 **** int ftruncate(int, off_t); int getdtablesize(void); ! int gethostname(char *buf, int size); int getpagesize(void); ! char * getwd(char *__buffer); int nice(int _increment); void * sbrk(int _delta); int sync(void); ! int truncate(const char*, off_t); unsigned int usleep(unsigned int _useconds); pid_t vfork(void); --- 126,136 ---- int ftruncate(int, off_t); int getdtablesize(void); ! int gethostname(char *_buf, int _size); int getpagesize(void); ! char * getwd(char *_buffer); int nice(int _increment); void * sbrk(int _delta); int sync(void); ! int truncate(const char *_file, off_t _size); unsigned int usleep(unsigned int _useconds); pid_t vfork(void);