1 #ifndef _NO_DIRNAME 2 3 /* Copyright 2005 Shaun Jackman 4 * Permission to use, copy, modify, and distribute this software 5 * is freely granted, provided that this notice is preserved. 6 */ 7 8 #include <libgen.h> 9 #include <string.h> 10 11 char * dirname(char * path)12dirname (char *path) 13 { 14 char *p; 15 if( path == NULL || *path == '\0' ) 16 return "."; 17 p = path + strlen(path) - 1; 18 while( *p == '/' ) { 19 if( p == path ) 20 return path; 21 *p-- = '\0'; 22 } 23 while( p >= path && *p != '/' ) 24 p--; 25 while( p > path && p[-1] == '/' ) 26 p--; 27 return 28 p < path ? "." : 29 p == path ? "/" : 30 (*p = '\0', path); 31 } 32 33 #endif /* !_NO_DIRNAME */ 34