SystemCall run(block) can now exit the run if it returns false
[io/quag.git] / libs / basekit / source / _new / Directory.c
bloba77f513479d78ae2f54eb393061b183cabb3793b
3 #include <stdio.h>
5 /* ----------------------------------------------------- */
7 #include <sys/stat.h>
9 #ifndef _WIN32
10 #include <sys/types.h>
11 #include <dirent.h>
13 /*#ifdef __CYGWIN__*/
14 #include <sys/file.h>
15 /*#endif*/
16 #else
17 #include <windows.h>
18 #define S_IRGRP 0
19 #define S_IXGRP 0
20 #define S_IROTH 0
21 #define S_IXOTH 0
22 #define S_IRWXU 0
24 #define DT_DIR 0x01
26 struct dirent {
27 char d_name[MAX_PATH];
28 unsigned char d_type;
31 typedef struct {
32 WIN32_FIND_DATA wfd;
33 HANDLE hFind;
34 struct dirent de;
35 unsigned char valid;
36 } DIR;
38 static DIR *opendir(char *pSpec)
40 DIR *pDir = malloc(sizeof *pDir);
41 char *longer_string = malloc((strlen(pSpec) + 3) * sizeof *longer_string);
43 strcpy(longer_string, pSpec);
44 strcat(longer_string, "/*");
45 pDir->hFind = FindFirstFile(longer_string, &pDir->wfd);
46 free(longer_string);
47 pDir->valid = pDir->hFind != INVALID_HANDLE_VALUE;
48 return pDir;
51 static void closedir(DIR * pDir)
53 if (pDir->hFind != INVALID_HANDLE_VALUE)
54 FindClose(pDir->hFind);
55 free(pDir);
58 static struct dirent *readdir(DIR *pDir)
60 if (pDir->valid)
62 strcpy(pDir->de.d_name, pDir->wfd.cFileName);
63 pDir->de.d_type = (pDir->wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 ? DT_DIR : 0;
64 pDir->valid = FindNextFile(pDir->hFind, &pDir->wfd);
65 return &pDir->de;
67 return NULL;
70 typedef int mode_t;
72 int mkdir(const char *path, mode_t mode)
74 /* returns zero on sucess */
75 LPCTSTR lpPathName = path;
76 LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL;
77 return (CreateDirectory(lpPathName, lpSecurityAttributes) != 0);
80 #endif
82 #if defined(__CYGWIN__) || defined(sun) || defined(__sun)
83 int isDirectory(struct dirent *dp, char *path);
85 struct stat st;
86 /*fstat( dp->d_fd, &st );*/
87 stat(pathString, &st);
88 return ( (st.st_mode & S_IFMT) == S_IFDIR );
90 #else
91 int isDirectory(struct dirent *dp, char *path)
93 return (dp->d_type == DT_DIR);
95 #endif
97 /* ----------------------------------------------------- */
99 typedef struct
101 char *path;
102 DIR *dir;
103 char *next;
104 } DirEnum;
106 DirEnum *DirEnum_new(void)
108 DirEnum *self = calloc(1, sizeof(DirEnum));
109 return self;
112 void DirEnum_free(DirEnum *self)
114 if (self->path) free(self->path);
115 free(self);
118 void DirEnum_setPath_(DirEnum *self, char *s)
120 self->path = strcpy(realloc(self->path, strlen(s) + 1), s);
121 self->dir = opendir(s);
124 char *DirEnum_setNext_(DirEnum *self, char *s)
126 self->next = strcpy(realloc(self->next, strlen(s) + 1), s);
127 return self->next;
130 char *DirEnum_next(DirEnum *self)
132 struct direct *dp = readdir(dirp);
133 if (dp)
135 char *name = dp.d_name;