Explorar el Código

Added dirname()

tags/v0.9.3
Michael Brown hace 17 años
padre
commit
182e3ed61d
Se han modificado 2 ficheros con 24 adiciones y 1 borrados
  1. 22
    0
      src/core/basename.c
  2. 2
    1
      src/include/libgen.h

+ 22
- 0
src/core/basename.c Ver fichero

@@ -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 Ver fichero

@@ -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…
Cancelar
Guardar