瀏覽代碼

Added dirname()

tags/v0.9.3
Michael Brown 17 年之前
父節點
當前提交
182e3ed61d
共有 2 個檔案被更改,包括 24 行新增1 行删除
  1. 22
    0
      src/core/basename.c
  2. 2
    1
      src/include/libgen.h

+ 22
- 0
src/core/basename.c 查看文件

@@ -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 查看文件

@@ -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…
取消
儲存