diff options
| author | Ramsay Jones <ramsay@ramsay1.demon.co.uk> | 2009-03-20 19:52:50 +0000 |
|---|---|---|
| committer | Shawn O. Pearce <spearce@spearce.org> | 2009-03-20 14:39:03 -0700 |
| commit | 0234c186bacb5144ca13f69f0dc9bfcc8ec7a075 (patch) | |
| tree | c770eeb29c9c95d27b9df2d30b6f47d62e78d07b /src/win32/dir.c | |
| parent | 79ca2edcd4e7a801400fa33c2c705b8f03a5d7f5 (diff) | |
| download | libgit2-0234c186bacb5144ca13f69f0dc9bfcc8ec7a075.tar.gz | |
win32: Add <dirent.h> directory reading routines
Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'src/win32/dir.c')
| -rw-r--r-- | src/win32/dir.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/win32/dir.c b/src/win32/dir.c new file mode 100644 index 000000000..00f43bfc8 --- /dev/null +++ b/src/win32/dir.c @@ -0,0 +1,97 @@ +#include "dir.h" + +static int init_filter(char *filter, size_t n, const char *dir) +{ + int len = strlen(dir); + + if (len+3 >= n) + return 0; + + strcpy(filter, dir); + if (len && dir[len-1] != '/') + strcat(filter, "/"); + strcat(filter, "*"); + + return 1; +} + +DIR *opendir(const char *dir) +{ + char filter[4096]; + DIR *new; + + if (!dir || !init_filter(filter, sizeof(filter), dir)) + return NULL; + + new = git__malloc(sizeof(*new)); + if (!new) + return NULL; + + new->dir = git__malloc(strlen(dir)+1); + if (!new->dir) { + free(new); + return NULL; + } + strcpy(new->dir, dir); + + new->h = FindFirstFile(filter, &new->f); + if (new->h == INVALID_HANDLE_VALUE) { + free(new->dir); + free(new); + return NULL; + } + new->first = 1; + + return new; +} + +struct dirent *readdir(DIR *d) +{ + if (!d || d->h == INVALID_HANDLE_VALUE) + return NULL; + + if (d->first) + d->first = 0; + else { + if (!FindNextFile(d->h, &d->f)) + return NULL; + } + + if (strlen(d->f.cFileName) >= sizeof(d->entry.d_name)) + return NULL; + + d->entry.d_ino = 0; + strcpy(d->entry.d_name, d->f.cFileName); + + return &d->entry; +} + +void rewinddir(DIR *d) +{ + char filter[4096]; + + if (d) { + if (d->h != INVALID_HANDLE_VALUE) + FindClose(d->h); + d->h = INVALID_HANDLE_VALUE; + d->first = 0; + if (init_filter(filter, sizeof(filter), d->dir)) { + d->h = FindFirstFile(filter, &d->f); + if (d->h != INVALID_HANDLE_VALUE) + d->first = 1; + } + } +} + +int closedir(DIR *d) +{ + if (d) { + if (d->h != INVALID_HANDLE_VALUE) + FindClose(d->h); + if (d->dir) + free(d->dir); + free(d); + } + return 0; +} + |
