Browse Source

Added dirname()

tags/v0.9.3
Michael Brown 17 years ago
parent
commit
182e3ed61d
2 changed files with 24 additions and 1 deletions
  1. 22
    0
      src/core/basename.c
  2. 2
    1
      src/include/libgen.h

+ 22
- 0
src/core/basename.c View File

@@ -38,3 +38,25 @@ char * basename ( char *path ) {
38 38
 	basename = strrchr ( path, '/' );
39 39
 	return ( basename ? ( basename + 1 ) : path );
40 40
 }
41
+
42
+/**
43
+ * Return directory name from path
44
+ *
45
+ * @v path		Full path
46
+ * @ret dirname		Directory name
47
+ *
48
+ * Note that this function may modify its argument.
49
+ */
50
+char * dirname ( char *path ) {
51
+	char *separator;
52
+
53
+	separator = strrchr ( path, '/' );
54
+	if ( separator == path ) {
55
+		return "/";
56
+	} else if ( separator ) {
57
+		*separator = 0;
58
+		return path;
59
+	} else {
60
+		return ".";
61
+	}
62
+}

+ 2
- 1
src/include/libgen.h View File

@@ -1,6 +1,7 @@
1 1
 #ifndef _LIBGEN_H
2 2
 #define _LIBGEN_H
3 3
 
4
-char * basename ( char *path );
4
+extern char * basename ( char *path );
5
+extern char * dirname ( char *path );
5 6
 
6 7
 #endif /* _LIBGEN_H */

Loading…
Cancel
Save