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