From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/OR_USING_C/dircom.c | 117 ++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 reference/C/CONTRIB/OR_USING_C/dircom.c (limited to 'reference/C/CONTRIB/OR_USING_C/dircom.c') diff --git a/reference/C/CONTRIB/OR_USING_C/dircom.c b/reference/C/CONTRIB/OR_USING_C/dircom.c new file mode 100644 index 0000000..2b2b9ab --- /dev/null +++ b/reference/C/CONTRIB/OR_USING_C/dircom.c @@ -0,0 +1,117 @@ + +/* + * Non-BSD systems only. + */ +#include +#include +#include +#include + +#define DIRSIZE(e) (min(strlen(e->d_name), DIRSIZ)) + +typedef struct { + int d_fd; +} DIR; + +char *malloc(); + +DIR * +opendir(dir) +char *dir; +{ + struct stat stbuf; + DIR *dp = (DIR *) malloc(sizeof *dp); + + if ((dp->d_fd = open(dir, 0)) < 0) + return(0); + + if ((fstat(dp->d_fd, &stbuf) < 0) || + ((stbuf.st_mode & S_IFDIR) == 0)) { + closedir(dp); + return(0); /* this isn't a directory! */ + } + + return(dp); +} + +closedir(dp) +DIR *dp; +{ + (void) close(dp->d_fd); + free((char *) dp); +} + +struct direct * +readdir(dp) +DIR *dp; +{ + static struct direct dir; + + do { + if (read(dp->d_fd, &dir, sizeof(dir)) != sizeof(dir)) + return(0); + } while (dir.d_ino == 0); + + return(&dir); +} + +/* + * Scandir returns the number of entries or -1 if the + * directory cannot be opened or malloc fails. + */ +scandir(dir, nmptr, select, compar) +char *dir; +char ***nmptr; +int (*select)(); +int (*compar)(); +{ + DIR *dirp; + char **array; + char **realloc(); + struct direct *ent; + unsigned int nalloc = 10, nentries = 0; + + if ((dirp = opendir(dir)) == NULL) + return(-1); + + array = (char **) malloc(nalloc * sizeof (char *)); + + if (array == NULL) + return(-1); + + while ((ent = readdir(dirp)) != NULL) { + if (select && ((*select)(ent->d_name) == 0)) + continue; + + if (nentries == nalloc) { + array = realloc(array, (nalloc += 10) * sizeof(char *)); + + if (array == NULL) + return(-1); + } + + array[nentries] = (char *) malloc(DIRSIZE(ent)+1); + strncpy(array[nentries], ent->d_name, DIRSIZE(ent)); + array[nentries][DIRSIZE(ent)] = NULL; + nentries++; + } + + closedir(dirp); + + if ((nentries + 1) != nalloc) + array = realloc(array, ((nentries + 1) * sizeof (char *))); + + if (compar != 0) + qsort(array, nentries, sizeof(char **), compar); + + *nmptr = array; + array[nentries] = 0; /* guaranteed 0 pointer */ + + return(nentries); +} + +alphasort(a, b) +char **a, **b; +{ + return(strcmp(*a, *b)); +} -- cgit v1.2.3-54-g00ecf