about summary refs log tree commit diff
path: root/lib/util/filename.c
blob: 18c12e3c2f3c21fd18b542fb8c12089977669e20 (plain) (blame)
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
#include "nstring.h"

#include "filename.h"

const char *
pm_basename(const char * const fileName) {
/*----------------------------------------------------------------------------
   Return the filename portion of a file name, e.g. "foo.ppm" from
   "/home/bryanh/foo.ppm".

   Return it as a malloc'ed string.
-----------------------------------------------------------------------------*/
    unsigned int basenameStart;
    unsigned int i;
    const char * retval;

    basenameStart = 0;  /* initial assumption */

    for (i = 0; fileName[i]; ++i) {
        if (fileName[i] == '/')
            basenameStart = i+1;
    }
    pm_asprintf(&retval, "%s", &fileName[basenameStart]);

    return retval;
}