oga's tools
Revision | 44e9b62c8b7673f8c9b2895b8390502ae2cea081 (tree) |
---|---|
Zeit | 2014-02-11 19:08:45 |
Autor | oga <hyperoga@gmai...> |
Commiter | oga |
find2.c 2013/12/12 V0.13 support X68K & Linux
@@ -26,7 +26,7 @@ TARGETS=$(VP)/age $(VP)/cal2 $(VP)/cgrep $(VP)/cmem $(VP)/cpkt $(VP)/cpuid \ | ||
26 | 26 | $(VP)/peke2 $(VP)/peke $(VP)/perfan $(VP)/strings2 $(VP)/t64an \ |
27 | 27 | $(VP)/tolower $(VP)/txt2vnt $(VP)/upload $(VP)/wavana $(VP)/wavcut2 \ |
28 | 28 | $(VP)/wavcut $(VP)/welcome $(VP)/whichx $(VP)/wwstat $(VP)/xalloc \ |
29 | - $(VP)/zusa $(VP)/rand2 $(VP)/usleep | |
29 | + $(VP)/zusa $(VP)/rand2 $(VP)/usleep $(VP)/find2 | |
30 | 30 | |
31 | 31 | TARGET_X=$(VP)/qix $(VP)/xmem $(VP)/xpkt $(VP)/xpstat |
32 | 32 |
@@ -45,7 +45,7 @@ $(VP)/base64 : $(TOOL)/base64.c | ||
45 | 45 | ${CC} $? -o $@ -O |
46 | 46 | |
47 | 47 | $(VP)/cal2 : $(TOOL)/cal.c |
48 | - ${CC} $? -o $@ -O | |
48 | + ${CC} $? -o $@ -O -DLINUX | |
49 | 49 | |
50 | 50 | $(VP)/cgrep : $(TOOL)/cgrep.c |
51 | 51 | ${CC} $? -o $@ -O |
@@ -100,6 +100,9 @@ $(VP)/enews : $(TOOL)/enews.c | ||
100 | 100 | $(VP)/filer : $(TOOL)/filer.c |
101 | 101 | ${CC} $? -o $@ -O -DLINUX -I/usr/include/ncursesw -L/usr/lib/arm-linux-gnueabihf -lncursesw |
102 | 102 | |
103 | +$(VP)/find2 : $(TOOL)/find2.c | |
104 | + ${CC} $? -o $@ -O -DLINUX | |
105 | + | |
103 | 106 | $(VP)/galaxian : $(TOOL)/galaxian.c |
104 | 107 | ${CC} $? -o $@ -O -DX3050RX -I/usr/include/ncursesw -L/usr/lib/arm-linux-gnueabihf -lncursesw |
105 | 108 |
@@ -2,19 +2,30 @@ | ||
2 | 2 | * find2 : ファイル/ディレクトリの検索 (日本語対応版) |
3 | 3 | * for WIN Only |
4 | 4 | * |
5 | - * 表示形式 | |
6 | - * | |
7 | 5 | * 2003/05/16 V0.10 by oga. |
8 | 6 | * 2003/05/21 V0.11 fix find2 c:\ => c:\/xxx |
9 | 7 | * 2009/04/04 V0.12 support -mtime |
8 | + * 2013/12/12 V0.13 support X68K & Linux | |
9 | + * | |
10 | + * CFLAG | |
11 | + * Linux: -DX68K -DLINUX | |
12 | + * | |
13 | + * | |
10 | 14 | */ |
15 | +#ifdef _WIN32 | |
11 | 16 | #include <windows.h> |
17 | +#else /* X68K, LINUX */ | |
12 | 18 | #include <stdio.h> |
19 | +#include <string.h> | |
20 | +#include <stdlib.h> | |
21 | +#include <dirent.h> | |
22 | +#endif /* X68K, LINUX */ | |
13 | 23 | #include <sys/types.h> |
14 | 24 | #include <sys/stat.h> |
25 | +#include <errno.h> | |
15 | 26 | |
16 | 27 | /* macros */ |
17 | -#define VER "0.12" | |
28 | +#define VER "0.13" | |
18 | 29 | #define IS_DOT(str) (!strcmp(str,".") || !strcmp(str,"..")) |
19 | 30 | |
20 | 31 | #define dprintf if (vf) printf |
@@ -32,6 +43,163 @@ int vf = 0; /* -v */ | ||
32 | 43 | int depth = 0; |
33 | 44 | time_t cur_tt = 0; /* current time_t V0.12-A */ |
34 | 45 | |
46 | +#ifdef LINUX | |
47 | +#define stricmp strcasecmp | |
48 | +#endif | |
49 | + | |
50 | +#if defined X68K || defined LINUX | |
51 | +int errno2 = 0; | |
52 | + | |
53 | +#define INVALID_HANDLE_VALUE NULL | |
54 | +#define MAX_PATH PATH_MAX | |
55 | +#define TRUE 1 | |
56 | +#define FALSE 0 | |
57 | + | |
58 | +/* #define PDELM "\\" */ | |
59 | + | |
60 | +typedef int DWORD; | |
61 | +typedef int BOOL; | |
62 | +typedef char CHAR; | |
63 | + | |
64 | +typedef struct _DIR2 { | |
65 | + DIR *dirp; | |
66 | + char path[MAX_PATH]; | |
67 | +} DIR2, *HANDLE; | |
68 | + | |
69 | +typedef struct _WIN32_FIND_DATA { | |
70 | + DWORD dwFileAttributes; | |
71 | + DWORD nFileSizeHigh; | |
72 | + DWORD nFileSizeLow; | |
73 | + CHAR cFileName[ MAX_PATH ]; | |
74 | + | |
75 | + /* FILETIME ftCreationTime; */ | |
76 | + /* FILETIME ftLastAccessTime; */ | |
77 | + /* FILETIME ftLastWriteTime; */ | |
78 | + /* DWORD dwReserved0; */ | |
79 | + /* DWORD dwReserved1; */ | |
80 | + /* CHAR cAlternateFileName[ 16 ]; */ | |
81 | +} WIN32_FIND_DATA; | |
82 | + | |
83 | +/* available attr */ | |
84 | +#define FILE_ATTRIBUTE_DIRECTORY 0x00000010 /* ディレクトリ */ | |
85 | +#define FILE_ATTRIBUTE_NORMAL 0x00000080 /* 通常ファイル */ | |
86 | + | |
87 | +/* not support attr */ | |
88 | +#define FILE_ATTRIBUTE_READONLY 0x00000001 /* 読み取り専用属性 */ | |
89 | +#define FILE_ATTRIBUTE_HIDDEN 0x00000002 /* 隠しファイル属性 */ | |
90 | +#define FILE_ATTRIBUTE_ARCHIVE 0x00000020 /* アーカイブ属性 */ | |
91 | +#define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400 /* 関連リパースポイントがある */ | |
92 | +#define FILE_ATTRIBUTE_COMPRESSED 0x00000800 /* 圧縮属性。 */ | |
93 | +#define FILE_ATTRIBUTE_ENCRYPTED 0x00004000 /* 暗号化属性。 */ | |
94 | +#define FILE_ATTRIBUTE_OFFLINE 0x00001000 /* 利用可能ではない */ | |
95 | + | |
96 | +/* FindNextFile error reason */ | |
97 | +#define ERROR_NO_MORE_FILES ENOENT; | |
98 | + | |
99 | + | |
100 | +HANDLE FindFirstFile(char *path, WIN32_FIND_DATA *pwfd) | |
101 | +{ | |
102 | + DIR *dirp; | |
103 | + DIR2 *dir2p; /* DIR with path */ | |
104 | + struct dirent *dp; | |
105 | + struct stat stbuf; | |
106 | + char path2[MAX_PATH]; | |
107 | + | |
108 | + /* delete wild card */ | |
109 | + strcpy(path2, path); | |
110 | + if (path2[strlen(path2)-1] == '*') { | |
111 | + path2[strlen(path2)-1] = '\0'; | |
112 | + } | |
113 | + if (path2[strlen(path2)-1] == '/') { | |
114 | + if (strlen(path2) >= 2 && path2[strlen(path2)-2] == ':') { | |
115 | + /* if "a:/" ... don't delete "/" */ | |
116 | + } else { | |
117 | + /* delete "/" */ | |
118 | + path2[strlen(path2)-1] = '\0'; | |
119 | + } | |
120 | + } | |
121 | + | |
122 | + memset(pwfd, 0, sizeof(WIN32_FIND_DATA)); | |
123 | + | |
124 | + if (vf) printf("opendir = [%s]\n", path2); | |
125 | + dirp = opendir(path2); | |
126 | + if (!dirp) { | |
127 | + errno2 = errno; | |
128 | + perror("opendir"); | |
129 | + printf("path = [%s]\n", path); | |
130 | + return INVALID_HANDLE_VALUE; | |
131 | + } | |
132 | + | |
133 | + dir2p = (DIR2 *)malloc(sizeof(DIR2)); | |
134 | + if (dir2p == NULL) { | |
135 | + errno2 = errno; | |
136 | + perror("malloc"); | |
137 | + return INVALID_HANDLE_VALUE; | |
138 | + } | |
139 | + | |
140 | + dir2p->dirp = dirp; | |
141 | + strcpy(dir2p->path, path2); | |
142 | + | |
143 | + if (FindNextFile(dir2p, pwfd) == FALSE) { | |
144 | + /* EOF or Error */ | |
145 | + return dir2p; | |
146 | + } | |
147 | + return dir2p; | |
148 | +} | |
149 | + | |
150 | +BOOL FindNextFile(HANDLE dir2p, WIN32_FIND_DATA *pwfd) | |
151 | +{ | |
152 | + struct dirent *dp; | |
153 | + struct stat stbuf; | |
154 | + char wkpath[MAX_PATH]; | |
155 | + | |
156 | + if (vf) printf("FindNextFile: start readdir(%s)\n", dir2p->path); /* debug */ | |
157 | + | |
158 | + memset(pwfd, 0, sizeof(WIN32_FIND_DATA)); | |
159 | + | |
160 | + if (vf) printf("FindNextFile: ----\n"); /* debug */ | |
161 | + | |
162 | + dp = readdir(dir2p->dirp); | |
163 | + if (!dp) { | |
164 | + /* end of entry */ | |
165 | + errno2 = ERROR_NO_MORE_FILES; | |
166 | + return FALSE; | |
167 | + } | |
168 | + strcpy(pwfd->cFileName, dp->d_name); | |
169 | + strcpy(wkpath, dir2p->path); | |
170 | + strcat(wkpath, "/"); | |
171 | + strcat(wkpath, dp->d_name); | |
172 | + if (vf) printf("FindNextFile: start stat(%s)\n", wkpath); /* debug */ | |
173 | + if (stat(wkpath, &stbuf) == 0) { | |
174 | + if (stbuf.st_mode & S_IFDIR) { | |
175 | + pwfd->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY; | |
176 | + if (vf >= 2) printf("# %s is dir. 0x%08x\n", wkpath, pwfd->dwFileAttributes); | |
177 | + } else { | |
178 | + /* とりあえずディレクトリ以外はNORMALにたおす */ | |
179 | + pwfd->dwFileAttributes |= FILE_ATTRIBUTE_NORMAL; | |
180 | + if (vf >= 2) printf("# %s is file. 0x%08x\n", wkpath, pwfd->dwFileAttributes); | |
181 | + } | |
182 | + pwfd->nFileSizeHigh = 0; | |
183 | + pwfd->nFileSizeLow = stbuf.st_size; | |
184 | + } | |
185 | + return TRUE; | |
186 | +} | |
187 | + | |
188 | +BOOL FindClose(HANDLE dir2p) | |
189 | +{ | |
190 | + closedir(dir2p->dirp); /* void */ | |
191 | + free(dir2p); | |
192 | + | |
193 | + return TRUE; | |
194 | +} | |
195 | + | |
196 | +int GetLastError() | |
197 | +{ | |
198 | + return errno2; | |
199 | +} | |
200 | + | |
201 | +#endif /* X68K || LINUX */ | |
202 | + | |
35 | 203 | void Usage() |
36 | 204 | { |
37 | 205 | printf("find Verson %s by oga.\n",VER); |
@@ -40,7 +208,7 @@ void Usage() | ||
40 | 208 | printf(" -type {d|f} : dir or file only\n"); |
41 | 209 | printf(" -name <name> : name match (ignore case)\n"); |
42 | 210 | printf(" -mtime <n> : file's data was last modified n*24 hours ago.\n"); |
43 | - //printf(" -mmin <n> : file's data was last modified n*24 hours ago.\n"); | |
211 | + /*printf(" -mmin <n> : file's data was last modified n*24 hours ago.\n"); */ | |
44 | 212 | } |
45 | 213 | |
46 | 214 | /* |
@@ -95,6 +263,7 @@ void Find(char *path) | ||
95 | 263 | char *pt; |
96 | 264 | |
97 | 265 | depth++; |
266 | + if (vf) printf("[%d]Find start.\n", depth); /* debug */ | |
98 | 267 | |
99 | 268 | /* ディレクトリ表示 */ |
100 | 269 | if ((ftype & F_TYPE_FILE) == 0) { |
@@ -201,6 +370,7 @@ void Find(char *path) | ||
201 | 370 | if (strlen(wk) && wk[strlen(wk)-1] != '/') strcat(wk,"/"); |
202 | 371 | strcat(wk,wfd.cFileName); /* make new path */ |
203 | 372 | Find(wk); |
373 | + if (vf) printf("[%d]Find return.\n", depth); /* debug */ | |
204 | 374 | } else { |
205 | 375 | /* ファイルの場合すぐ表示(-type d指定のない場合) */ |
206 | 376 | if ((ftype & F_TYPE_DIR) == 0) { |
@@ -208,10 +378,14 @@ void Find(char *path) | ||
208 | 378 | } |
209 | 379 | } |
210 | 380 | } |
381 | + if (vf) printf("[%d]Find end.\n", depth); /* debug */ | |
211 | 382 | FindClose(fh); |
383 | + if (vf) printf("[%d]Find end. (FindClose end)\n", depth); /* debug */ | |
212 | 384 | |
213 | 385 | depth--; |
214 | 386 | |
387 | + return; | |
388 | + | |
215 | 389 | } /* End Find() */ |
216 | 390 | |
217 | 391 | int main(int a, char *b[]) |
@@ -272,5 +446,7 @@ int main(int a, char *b[]) | ||
272 | 446 | return 0; |
273 | 447 | } |
274 | 448 | |
449 | +/* vim:ts=8:sw=4: | |
450 | + */ | |
275 | 451 | |
276 | 452 |