summaryrefslogtreecommitdiff
path: root/lib/basename.c
blob: 0fd981f3032e09dbd35c276e6ef6224dcb5d40ce (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* basename.c -- return the last element in a path */

#if HAVE_CONFIG_H
# include <config.h>
#endif

#ifndef FILESYSTEM_PREFIX_LEN
# define FILESYSTEM_PREFIX_LEN(f) 0
#endif

#ifndef ISSLASH
# define ISSLASH(c) ((c) == '/')
#endif

/* In general, we can't use the builtin `basename' function if available,
   since it has different meanings in different environments.
   In some environments the builtin `basename' modifies its argument.  */

char *
base_name (name)
     char const *name;
{
  char const *base = name += FILESYSTEM_PREFIX_LEN (name);

  for (; *name; name++)
    if (ISSLASH (*name))
      base = name + 1;

  return (char *) base;
}

#ifdef STDC_HEADERS
# include <stdlib.h>
#else
char *malloc ();
#endif

char *
base_name_strip_trailing_slashes (name)
     char const *name;
{
  char const *end_p = name += FILESYSTEM_PREFIX_LEN (name);
  char const *first, *p;
  char *base;
  int length;

  /* Make END_P point to the byte after the last non-slash character
     in NAME if one exists.  */
  for (p = name; *p; p++)
    if (!ISSLASH (*p))
      end_p = p + 1;

  if (end_p == name)
    {
      first = end_p;
    }
  else
    {
      first = end_p - 1;
      while (first > name && !ISSLASH (*(first - 1)))
	--first;
    }

  length = end_p - first;
  base = (char *) malloc (length + 1);
  if (base == 0)
    return 0;

  memcpy (base, first, length);
  base[length] = '\0';

  return base;
}

#ifdef TEST
# include <assert.h>
# include <stdlib.h>

# define CHECK(a,b) assert (strcmp (base_name_strip_trailing_slashes(a), b) \
			    == 0)

int
main ()
{
  CHECK ("a", "a");
  CHECK ("ab", "ab");
  CHECK ("ab/c", "c");
  CHECK ("/ab/c", "c");
  CHECK ("/ab/c/", "c");
  CHECK ("/ab/c////", "c");
  CHECK ("/", "");
  CHECK ("////", "");
  CHECK ("////a", "a");
  CHECK ("//a//", "a");
  CHECK ("/a", "a");
  exit (0);
}
#endif