Last change
on this file since 49 was
49,
checked in by Sam Hocevar, 15 years ago
|
- Imported original public domain release, for future reference.
|
-
Property svn:keywords set to
Id
|
File size:
2.0 KB
|
Line | |
---|
1 | #include "dirent.h" |
---|
2 | |
---|
3 | #include <stdlib.h> |
---|
4 | #include <string.h> |
---|
5 | #include "jmalloc.hpp" |
---|
6 | #include <unistd.h> |
---|
7 | |
---|
8 | char MacName[512]; |
---|
9 | |
---|
10 | extern char *macify_name(char *s); |
---|
11 | char *macify_name(char *s) |
---|
12 | { |
---|
13 | char *p = s; |
---|
14 | int macify = 0; |
---|
15 | int premacify = 1; |
---|
16 | int slashed = 0; |
---|
17 | |
---|
18 | while (*p) { |
---|
19 | if (*p=='/') |
---|
20 | macify = 1; |
---|
21 | if (*p==':') |
---|
22 | premacify = 0; |
---|
23 | p++; |
---|
24 | } |
---|
25 | |
---|
26 | if (!macify) |
---|
27 | return s; |
---|
28 | |
---|
29 | p = MacName; |
---|
30 | |
---|
31 | if (s[0] != '.' ) |
---|
32 | { |
---|
33 | if (premacify) |
---|
34 | *p++ = ':'; |
---|
35 | } |
---|
36 | else |
---|
37 | s++; |
---|
38 | while (*p = *s++) { |
---|
39 | if (*p=='/') |
---|
40 | *p = ':'; |
---|
41 | if (*p != ':') |
---|
42 | { |
---|
43 | slashed = 0; |
---|
44 | p++; |
---|
45 | } |
---|
46 | else |
---|
47 | if (!slashed) |
---|
48 | { |
---|
49 | slashed = 1; |
---|
50 | p++; |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | return MacName; |
---|
55 | /* |
---|
56 | p = s; |
---|
57 | while (*p) { |
---|
58 | if (*p=='/') |
---|
59 | *p = '_'; |
---|
60 | p++; |
---|
61 | } |
---|
62 | return s; |
---|
63 | */ |
---|
64 | } |
---|
65 | |
---|
66 | void get_directory(char *path, char **&files, int &tfiles, char **&dirs, int &tdirs) |
---|
67 | { |
---|
68 | |
---|
69 | struct dirent *de; |
---|
70 | files=NULL; |
---|
71 | dirs=NULL; |
---|
72 | tfiles=0; |
---|
73 | tdirs=0; |
---|
74 | DIR *d=opendir(macify_name(path)); |
---|
75 | if (!d) return ; |
---|
76 | |
---|
77 | char **tlist=NULL; |
---|
78 | int t=0; |
---|
79 | char curdir[200]; |
---|
80 | getcwd(curdir,200); |
---|
81 | chdir(macify_name(path)); |
---|
82 | |
---|
83 | do |
---|
84 | { |
---|
85 | de=readdir(d); |
---|
86 | if (de) |
---|
87 | { |
---|
88 | t++; |
---|
89 | tlist=(char **)jrealloc(tlist,sizeof(char *)*t,"tmp file list"); |
---|
90 | tlist[t-1]=strcpy((char *)jmalloc(strlen((char*)de->d_name)+1,"tmp file name"),(char*)de->d_name); |
---|
91 | } |
---|
92 | } while (de); |
---|
93 | closedir(d); |
---|
94 | |
---|
95 | for (int i=0;i<t;i++) |
---|
96 | { |
---|
97 | d=opendir(tlist[i]); |
---|
98 | if (d) |
---|
99 | { |
---|
100 | tdirs++; |
---|
101 | dirs=(char **)jrealloc(dirs,sizeof(char *)*tdirs,"dir list"); |
---|
102 | dirs[tdirs-1]=strcpy((char *)jmalloc(strlen(tlist[i])+1,"tmp file name"),tlist[i]); |
---|
103 | closedir(d); |
---|
104 | } else |
---|
105 | { |
---|
106 | tfiles++; |
---|
107 | files=(char **)jrealloc(files,sizeof(char *)*tfiles,"dir list"); |
---|
108 | files[tfiles-1]=strcpy((char *)jmalloc(strlen(tlist[i])+1,"tmp file name"),tlist[i]); |
---|
109 | } |
---|
110 | jfree(tlist[i]); |
---|
111 | } |
---|
112 | if (t) |
---|
113 | jfree(tlist); |
---|
114 | chdir(curdir); |
---|
115 | |
---|
116 | // get files in directory in path into files & tfiles |
---|
117 | // get dirs in directory in path into dirs & tdirs |
---|
118 | } |
---|
119 | |
---|
Note: See
TracBrowser
for help on using the repository browser.