source: Main/trunk/Common/dir.c@ 257

Last change on this file since 257 was 257, checked in by Nishi, on Oct 4, 2024 at 2:34:27 PM

[release 2.04] can read mime.types from apache

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