Line | |
---|
1 | /* $Id: dir.c 219 2024-10-02 20:40:37Z 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__)
|
---|
11 | #include <direct.h>
|
---|
12 | #endif
|
---|
13 | #include <stdlib.h>
|
---|
14 | #include <string.h>
|
---|
15 |
|
---|
16 | int cm_sort(const void* _a, const void* _b) {
|
---|
17 | char* a = *(char**)_a;
|
---|
18 | char* b = *(char**)_b;
|
---|
19 | return strcmp(a, b);
|
---|
20 | }
|
---|
21 |
|
---|
22 | char** cm_scandir(const char* path) {
|
---|
23 | #if defined(_MSC_VER) || defined(__BORLANDC__)
|
---|
24 | return NULL;
|
---|
25 | #else
|
---|
26 | DIR* dir = opendir(path);
|
---|
27 | if(dir != NULL) {
|
---|
28 | char** r = malloc(sizeof(*r));
|
---|
29 | struct dirent* d;
|
---|
30 | r[0] = NULL;
|
---|
31 | while((d = readdir(dir)) != NULL) {
|
---|
32 | if(strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) {
|
---|
33 | struct stat s;
|
---|
34 | char* p = cm_strcat3(path, "/", d->d_name);
|
---|
35 | stat(p, &s);
|
---|
36 | free(p);
|
---|
37 |
|
---|
38 | char** old = r;
|
---|
39 | int i;
|
---|
40 | for(i = 0; old[i] != NULL; i++)
|
---|
41 | ;
|
---|
42 | r = malloc(sizeof(*r) * (i + 2));
|
---|
43 | for(i = 0; old[i] != NULL; i++) r[i] = old[i];
|
---|
44 | r[i] = cm_strcat(d->d_name, S_ISDIR(s.st_mode) ? "/" : "");
|
---|
45 | r[i + 1] = NULL;
|
---|
46 | free(old);
|
---|
47 | }
|
---|
48 | }
|
---|
49 | int len;
|
---|
50 | for(len = 0; r[len] != NULL; len++)
|
---|
51 | ;
|
---|
52 | qsort(r, len, sizeof(char*), cm_sort);
|
---|
53 |
|
---|
54 | char** old = r;
|
---|
55 | int i;
|
---|
56 | for(i = 0; old[i] != NULL; i++)
|
---|
57 | ;
|
---|
58 | r = malloc(sizeof(*r) * (i + 2));
|
---|
59 | for(i = 0; old[i] != NULL; i++) r[i + 1] = old[i];
|
---|
60 | r[0] = cm_strdup("../");
|
---|
61 | r[i + 1] = NULL;
|
---|
62 | free(old);
|
---|
63 |
|
---|
64 | closedir(dir);
|
---|
65 |
|
---|
66 | return r;
|
---|
67 | } else {
|
---|
68 | return NULL;
|
---|
69 | }
|
---|
70 | #endif
|
---|
71 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.