1 | /* $Id: dir.c 261 2024-10-04 23:19:59Z nishi $ */
|
---|
2 |
|
---|
3 | #include "cm_dir.h"
|
---|
4 |
|
---|
5 | #include "cm_string.h"
|
---|
6 |
|
---|
7 | #include <sys/stat.h>
|
---|
8 | #if !defined(_MSC_VER) && !defined(__WATCOMC__)
|
---|
9 | #include <dirent.h>
|
---|
10 | #elif defined(__WATCOMC__) || defined(_MSC_VER)
|
---|
11 | #include <direct.h>
|
---|
12 | #endif
|
---|
13 | #ifdef _MSC_VER
|
---|
14 | #include <windows.h>
|
---|
15 | #endif
|
---|
16 | #include <stdlib.h>
|
---|
17 | #include <string.h>
|
---|
18 |
|
---|
19 | int cm_sort(const void* _a, const void* _b) {
|
---|
20 | char* a = *(char**)_a;
|
---|
21 | char* b = *(char**)_b;
|
---|
22 | return strcmp(a, b);
|
---|
23 | }
|
---|
24 |
|
---|
25 | char** cm_scandir(const char* path) {
|
---|
26 | #if defined(_MSC_VER) || defined(__BORLANDC__)
|
---|
27 | WIN32_FIND_DATA ffd;
|
---|
28 | HANDLE hfind;
|
---|
29 | char** r = malloc(sizeof(*r));
|
---|
30 | int len;
|
---|
31 | char** old;
|
---|
32 | int i;
|
---|
33 | char* p;
|
---|
34 | r[0] = NULL;
|
---|
35 |
|
---|
36 | p = cm_strcat(path, "/*");
|
---|
37 | hfind = FindFirstFile(p, &ffd);
|
---|
38 | if(INVALID_HANDLE_VALUE == hfind) {
|
---|
39 | return NULL;
|
---|
40 | }
|
---|
41 | do {
|
---|
42 | if(strcmp(ffd.cFileName, ".") != 0 && strcmp(ffd.cFileName, "..") != 0) {
|
---|
43 | old = r;
|
---|
44 | for(i = 0; old[i] != NULL; i++)
|
---|
45 | ;
|
---|
46 | r = malloc(sizeof(*r) * (i + 2));
|
---|
47 | for(i = 0; old[i] != NULL; i++) r[i] = old[i];
|
---|
48 | r[i] = cm_strcat(ffd.cFileName, (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? "/" : "");
|
---|
49 | r[i + 1] = NULL;
|
---|
50 | free(old);
|
---|
51 | }
|
---|
52 | } while(FindNextFile(hfind, &ffd) != 0);
|
---|
53 | FindClose(hfind);
|
---|
54 | free(p);
|
---|
55 | for(len = 0; r[len] != NULL; len++)
|
---|
56 | ;
|
---|
57 | qsort(r, len, sizeof(char*), cm_sort);
|
---|
58 |
|
---|
59 | old = r;
|
---|
60 | for(i = 0; old[i] != NULL; i++)
|
---|
61 | ;
|
---|
62 | r = malloc(sizeof(*r) * (i + 2));
|
---|
63 | for(i = 0; old[i] != NULL; i++) r[i + 1] = old[i];
|
---|
64 | r[0] = cm_strdup("../");
|
---|
65 | r[i + 1] = NULL;
|
---|
66 | free(old);
|
---|
67 |
|
---|
68 | return r;
|
---|
69 | #else
|
---|
70 | DIR* dir = opendir(path);
|
---|
71 | if(dir != NULL) {
|
---|
72 | char** r = malloc(sizeof(*r));
|
---|
73 | struct dirent* d;
|
---|
74 | r[0] = NULL;
|
---|
75 | while((d = readdir(dir)) != NULL) {
|
---|
76 | if(strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) {
|
---|
77 | struct stat s;
|
---|
78 | char* p = cm_strcat3(path, "/", d->d_name);
|
---|
79 | stat(p, &s);
|
---|
80 | free(p);
|
---|
81 |
|
---|
82 | char** old = r;
|
---|
83 | int i;
|
---|
84 | for(i = 0; old[i] != NULL; i++)
|
---|
85 | ;
|
---|
86 | r = malloc(sizeof(*r) * (i + 2));
|
---|
87 | for(i = 0; old[i] != NULL; i++) r[i] = old[i];
|
---|
88 | r[i] = cm_strcat(d->d_name, S_ISDIR(s.st_mode) ? "/" : "");
|
---|
89 | r[i + 1] = NULL;
|
---|
90 | free(old);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | int len;
|
---|
94 | for(len = 0; r[len] != NULL; len++)
|
---|
95 | ;
|
---|
96 | qsort(r, len, sizeof(char*), cm_sort);
|
---|
97 |
|
---|
98 | char** old = r;
|
---|
99 | int i;
|
---|
100 | for(i = 0; old[i] != NULL; i++)
|
---|
101 | ;
|
---|
102 | r = malloc(sizeof(*r) * (i + 2));
|
---|
103 | for(i = 0; old[i] != NULL; i++) r[i + 1] = old[i];
|
---|
104 | r[0] = cm_strdup("../");
|
---|
105 | r[i + 1] = NULL;
|
---|
106 | free(old);
|
---|
107 |
|
---|
108 | closedir(dir);
|
---|
109 |
|
---|
110 | return r;
|
---|
111 | } else {
|
---|
112 | return NULL;
|
---|
113 | }
|
---|
114 | #endif
|
---|
115 | }
|
---|