[21] | 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>
|
---|
[219] | 8 | #if !defined(_MSC_VER) && !defined(__WATCOMC__)
|
---|
[21] | 9 | #include <dirent.h>
|
---|
[261] | 10 | #elif defined(__WATCOMC__) || defined(_MSC_VER)
|
---|
[219] | 11 | #include <direct.h>
|
---|
[212] | 12 | #endif
|
---|
[261] | 13 | #ifdef _MSC_VER
|
---|
| 14 | #include <windows.h>
|
---|
| 15 | #endif
|
---|
[21] | 16 | #include <stdlib.h>
|
---|
| 17 | #include <string.h>
|
---|
| 18 |
|
---|
[22] | 19 | int cm_sort(const void* _a, const void* _b) {
|
---|
[21] | 20 | char* a = *(char**)_a;
|
---|
| 21 | char* b = *(char**)_b;
|
---|
| 22 | return strcmp(a, b);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
[22] | 25 | char** cm_scandir(const char* path) {
|
---|
[215] | 26 | #if defined(_MSC_VER) || defined(__BORLANDC__)
|
---|
[255] | 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) {
|
---|
[257] | 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);
|
---|
[255] | 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;
|
---|
[212] | 69 | #else
|
---|
[21] | 70 | DIR* dir = opendir(path);
|
---|
[22] | 71 | if(dir != NULL) {
|
---|
[21] | 72 | char** r = malloc(sizeof(*r));
|
---|
[212] | 73 | struct dirent* d;
|
---|
[21] | 74 | r[0] = NULL;
|
---|
[22] | 75 | while((d = readdir(dir)) != NULL) {
|
---|
| 76 | if(strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) {
|
---|
[21] | 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;
|
---|
[22] | 84 | for(i = 0; old[i] != NULL; i++)
|
---|
| 85 | ;
|
---|
[21] | 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;
|
---|
[22] | 94 | for(len = 0; r[len] != NULL; len++)
|
---|
| 95 | ;
|
---|
[21] | 96 | qsort(r, len, sizeof(char*), cm_sort);
|
---|
[22] | 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 |
|
---|
[150] | 108 | closedir(dir);
|
---|
| 109 |
|
---|
[21] | 110 | return r;
|
---|
[22] | 111 | } else {
|
---|
[21] | 112 | return NULL;
|
---|
| 113 | }
|
---|
[212] | 114 | #endif
|
---|
[21] | 115 | }
|
---|