nckernel  0.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
basename.c
Go to the documentation of this file.
1 #include <sys/types.h>
2 #include <stdio.h>
3 #include <stddef.h>
4 #include <string.h>
5 #include <limits.h>
6 #include <libgen.h>
7 
8 char *basename(char *s)
9 {
10  static char ret[PATH_MAX];
11  int i;
12  enum {
13  INIT,
14  NAME,
15  SEP,
16  } state;
17 
18  i = strlen(s) - 1;
19  state = INIT;
20  while (i >= 0) {
21  switch (s[i]) {
22  case '\0':
23  case '/':
24  if (state == NAME) {
25  int j;
26  while (s[i] && s[i] == '/') {
27  i++;
28  }
29 
30  j = 0;
31  while (s[i] && s[i] != '/') {
32  ret[j++] = s[i++];
33  }
34 
35  if (j) {
36  ret[j] = '\0';
37  } else {
38  strcpy(ret, s);
39  }
40 
41  return ret;
42  }
43  state = SEP;
44  break;
45  default:
46  state = NAME;
47  break;
48  }
49 
50  i--;
51  }
52 
53  strcpy(ret, s);
54  return ret;
55 }
56 
57 /* End of a file */