From 859efb84a8160e694b35a90fd60fcb3606ca8ef9 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Mon, 3 Mar 2003 17:16:15 +0000 Subject: Ajout de Sys.readdir git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@5415 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02 --- byterun/unix.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'byterun/unix.c') diff --git a/byterun/unix.c b/byterun/unix.c index 65c960a3b..c47544217 100644 --- a/byterun/unix.c +++ b/byterun/unix.c @@ -32,6 +32,11 @@ #ifdef HAS_UNISTD #include #endif +#ifdef HAS_DIRENT +#include +#else +#include +#endif #include "memory.h" #include "misc.h" #include "osdeps.h" @@ -322,6 +327,34 @@ void aligned_munmap (char * addr, asize_t size) #endif +/* Add to [contents] the (short) names of the files contained in + the directory named [dirname]. No entries are added for [.] and [..]. + Return 0 on success, -1 on error; set errno in the case of error. */ + +int caml_read_directory(char * dirname, struct ext_table * contents) +{ + DIR * d; +#ifdef HAS_DIRENT + struct dirent * e; +#else + struct direct * e; +#endif + char * p; + + d = opendir(dirname); + if (d == NULL) return -1; + while (1) { + e = readdir(d); + if (e == NULL) break; + if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) continue; + p = stat_alloc(strlen(e->d_name) + 1); + strcpy(p, e->d_name); + ext_table_add(contents, p); + } + closedir(d); + return 0; +} + /* Recover executable name from /proc/self/exe if possible */ #ifdef __linux__ -- cgit v1.2.3-70-g09d2