summaryrefslogtreecommitdiff
path: root/reference/C/EXAMPLES/basename.c
blob: fcecba7e9396e8d61a724d459cf0a6a85b02de35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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);
}