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

Last change on this file since 214 was 212, checked in by Nishi, on Oct 3, 2024 at 2:44:55 AM

compiles on vc6

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