diff options
Diffstat (limited to 'reference/C/EXAMPLES/basename.c')
-rw-r--r-- | reference/C/EXAMPLES/basename.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/reference/C/EXAMPLES/basename.c b/reference/C/EXAMPLES/basename.c new file mode 100644 index 0000000..fcecba7 --- /dev/null +++ b/reference/C/EXAMPLES/basename.c @@ -0,0 +1,51 @@ +/************************************************************************ + * + * Purpose: Extract the directory information from a file name. + * + * /etc/local/bin/fdmount --> fdmount + * + * This mimics the unix command 'basename'. + * + * Author: M J Leslie + * Date: 29-Feb-96 + * + ************************************************************************/ + +#include <string.h> + +char *basename(const char *FullName); + +main() +{ + char *FullName = "/usr/local/bin/fdmount"; + + printf("Full name is %s \n", FullName); + + printf("File name is %s \n", basename(FullName)); +} + +/************************************************************************/ + +char *basename(const char *FullName) +{ + static char *File; + + /* ... I guess DOS users will have to change the direction of + ... the slash. */ + + File = strrchr(FullName, '/'); + + /* ... If no slashes have been found, Return the full file name */ + + if (File == NULL) + { + File = FullName; + } + else + { + File++; + } + + return(File); +} + |