oga's tools
Révision | 1825d8d45ba8cbc70f94226b06cb2d7a7f960854 (tree) |
---|---|
l'heure | 2023-09-24 15:47:25 |
Auteur | hyperoga <hyperoga@gmai...> |
Commiter | hyperoga |
23/09/24 initial revision
@@ -0,0 +1,269 @@ | ||
1 | +/* | |
2 | + * addimgdate : add file date to ImageXX.jpg file | |
3 | + * | |
4 | + * 2023/07/01 V0.10 by oga | |
5 | + * | |
6 | + */ | |
7 | +#include <stdio.h> | |
8 | +#include <stdlib.h> | |
9 | +#include <string.h> | |
10 | +#include <time.h> | |
11 | +#include <sys/types.h> | |
12 | +#include <sys/stat.h> | |
13 | +#ifdef _WIN32 | |
14 | +#include <windows.h> | |
15 | + | |
16 | +#define strcasecmp stricmp | |
17 | +#define S_IFDIR _S_IFDIR | |
18 | +#else /* Linux */ | |
19 | +#include <dirent.h> | |
20 | +#include <unistd.h> | |
21 | +#include <sys/param.h> | |
22 | +#endif | |
23 | + | |
24 | +#define dprintf if (vf) printf | |
25 | + | |
26 | +/* globals */ | |
27 | +int vf = 0; /* -v verbose */ | |
28 | +int rf = 0; /* -r recursive */ | |
29 | +int tf = 0; /* -t test mode */ | |
30 | +#ifdef _WIN32 | |
31 | +int igncase = 1; /* -i ignore case */ | |
32 | +#else | |
33 | +int igncase = 0; /* -i ignore case */ | |
34 | +#endif | |
35 | + | |
36 | +int compare(const void *arg1, const void *arg2) | |
37 | +{ | |
38 | + return strcmp( *(char**)arg1, *(char**)arg2 ); | |
39 | +} | |
40 | + | |
41 | +#ifdef _WIN32 | |
42 | +/* dirp->direntry */ | |
43 | +struct dirent { | |
44 | + char *d_name; | |
45 | + WIN32_FIND_DATA wfd; | |
46 | +}; | |
47 | + | |
48 | +/* dirp */ | |
49 | +typedef struct _dir { | |
50 | + HANDLE hdir; | |
51 | + char path[1024]; /* search condition for FindFirstFile */ | |
52 | + int firstf; /* first readdir flag */ | |
53 | + struct dirent direntry; | |
54 | +} DIR; | |
55 | + | |
56 | + | |
57 | +DIR *opendir(char *dir) | |
58 | +{ | |
59 | + DIR *dirp; | |
60 | + | |
61 | + dprintf("Start opendir\n"); | |
62 | + | |
63 | + dirp = malloc(sizeof(DIR)); | |
64 | + if (!dirp) { | |
65 | + return NULL; | |
66 | + } | |
67 | + memset(dirp, 0, sizeof(DIR)); | |
68 | + dirp->firstf = 1; | |
69 | + dirp->direntry.d_name = dirp->direntry.wfd.cFileName; | |
70 | + sprintf(dirp->path, "%s\\*", dir); /* find all files */ | |
71 | + return dirp; | |
72 | +} | |
73 | + | |
74 | +struct dirent *readdir(DIR *dirp) | |
75 | +{ | |
76 | + int status = 0; | |
77 | + | |
78 | + dprintf("Start readdir(0x%08x)\n",dirp); | |
79 | + | |
80 | + if (dirp->firstf) { | |
81 | + dirp->hdir = FindFirstFile(dirp->path, &(dirp->direntry.wfd)); | |
82 | + if (dirp->hdir == INVALID_HANDLE_VALUE) { | |
83 | + printf("readdir: FindFirstFile() error\n"); | |
84 | + return NULL; | |
85 | + } | |
86 | + dirp->firstf = 0; | |
87 | + } else { | |
88 | + status = FindNextFile(dirp->hdir, &(dirp->direntry.wfd)); | |
89 | + if (status == FALSE) { | |
90 | + return NULL; /* EOF or Error */ | |
91 | + } | |
92 | + } | |
93 | + return &(dirp->direntry); | |
94 | +} | |
95 | + | |
96 | +int closedir(DIR *dirp) | |
97 | +{ | |
98 | + dprintf("Start closedir\n"); | |
99 | + if (dirp) { | |
100 | + FindClose(dirp->hdir); | |
101 | + free(dirp); | |
102 | + } | |
103 | + return 0; | |
104 | +} | |
105 | +#endif /* _WIN32 */ | |
106 | + | |
107 | +/* | |
108 | + * get current dir files and sort | |
109 | + * | |
110 | + * IN : suffix : NULL : all files | |
111 | + * str : select by suffix str | |
112 | + * IN : maxent : max entry size of files | |
113 | + * OUT : files : filenames store area | |
114 | + * OUT : ret : num of files | |
115 | + */ | |
116 | +int GetFiles(char *files[], char *suffix, int maxent) | |
117 | +{ | |
118 | + DIR *dirp; | |
119 | + struct dirent *dp; | |
120 | + int cnt = 0; | |
121 | + | |
122 | + if ((dirp = opendir(".")) == NULL) { | |
123 | + perror("opendir"); | |
124 | + exit(1); | |
125 | + } | |
126 | + | |
127 | + cnt = 0; | |
128 | + while((dp=readdir(dirp)) !=NULL) { | |
129 | + if (!strcmp(dp->d_name,".") || !strcmp(dp->d_name,"..")) { | |
130 | + continue; | |
131 | + } | |
132 | + if (strlen(dp->d_name) > 99) { | |
133 | + printf("too long filename (%s) skip.\n", dp->d_name); | |
134 | + continue; | |
135 | + } | |
136 | + if (suffix) { | |
137 | + if (igncase) { | |
138 | + if (strcasecmp(&(dp->d_name[strlen(dp->d_name)-strlen(suffix)]), suffix)) { | |
139 | + dprintf("suffix not match (%s)\n", dp->d_name); | |
140 | + continue; | |
141 | + } | |
142 | + } else { | |
143 | + if (strcmp(&(dp->d_name[strlen(dp->d_name)-strlen(suffix)]), suffix)) { | |
144 | + dprintf("suffix not match (%s)\n", dp->d_name); | |
145 | + continue; | |
146 | + } | |
147 | + } | |
148 | + } | |
149 | + if (maxent <= cnt) { | |
150 | + printf("Too many files\n"); | |
151 | + break; | |
152 | + } | |
153 | + dprintf("strcpy(%s)\n", dp->d_name); | |
154 | + files[cnt] = (char *)malloc(strlen(dp->d_name)+1); | |
155 | + strcpy(files[cnt], dp->d_name); | |
156 | + /* printf("%5d file = %s\n", i, dp->d_name); */ | |
157 | + ++cnt; | |
158 | + } | |
159 | + closedir(dirp); | |
160 | + | |
161 | + /* sort */ | |
162 | + qsort(files, cnt, sizeof(char *), compare); | |
163 | + | |
164 | + return cnt; | |
165 | +} | |
166 | + | |
167 | +/* | |
168 | + * rename "Image*" files | |
169 | + * | |
170 | + * IN : curdir : current dir | |
171 | + * IN : arf : recursive flag | |
172 | + */ | |
173 | +void RenameFileR(char *curdir, int arf) | |
174 | +{ | |
175 | + DIR *dirp; | |
176 | + struct dirent *dp; | |
177 | + struct stat stbuf; | |
178 | + struct tm *wtm; | |
179 | + char fullpath[1025]; | |
180 | + char fullpath_dst[1025]; | |
181 | + int wcnt; | |
182 | + | |
183 | + dprintf("curdir: %s\n", curdir); | |
184 | + | |
185 | + if ((dirp = opendir(curdir)) == NULL) { | |
186 | + perror(curdir); | |
187 | + exit(1); | |
188 | + } | |
189 | + | |
190 | + while((dp=readdir(dirp)) !=NULL) { | |
191 | + if (!strcmp(dp->d_name,".") || !strcmp(dp->d_name,"..")) { | |
192 | + continue; | |
193 | + } | |
194 | + if (strlen(dp->d_name) > 99) { | |
195 | + printf("too long filename (%s) skip.\n", dp->d_name); | |
196 | + continue; | |
197 | + } | |
198 | + | |
199 | + sprintf(fullpath, "%s/%s", curdir, dp->d_name); | |
200 | + | |
201 | + if (stat(fullpath, &stbuf) == 0) { | |
202 | + if (stbuf.st_mode & S_IFDIR && arf) { | |
203 | + RenameFileR(fullpath, arf); | |
204 | + } else if ((stbuf.st_mode & S_IFREG) && !strncmp(dp->d_name, "Image", 5)) { | |
205 | + wtm = localtime(&stbuf.st_mtime); | |
206 | + sprintf(fullpath_dst, "%s/%02d%02d%02d_%s", curdir, | |
207 | + wtm->tm_year+1900-2000, | |
208 | + wtm->tm_mon+1, | |
209 | + wtm->tm_mday, | |
210 | + dp->d_name); | |
211 | + wcnt = 1; | |
212 | + while (stat(fullpath_dst, &stbuf) == 0) { | |
213 | + /* if exist dst, try new name */ | |
214 | + sprintf(fullpath_dst, "%s/%02d%02d%02d_%d_%s", curdir, | |
215 | + wtm->tm_year+1900-2000, | |
216 | + wtm->tm_mon+1, | |
217 | + wtm->tm_mday, | |
218 | + wcnt++, | |
219 | + dp->d_name); | |
220 | + } | |
221 | + printf("rename %s => %s\n", fullpath, fullpath_dst); | |
222 | + rename(fullpath, fullpath_dst); | |
223 | + } | |
224 | + } | |
225 | + } | |
226 | + closedir(dirp); | |
227 | +} | |
228 | + | |
229 | +int main(int a, char *b[]) | |
230 | +{ | |
231 | + int i; | |
232 | + FILE *fp; | |
233 | + char *prefix = "Image"; /* arg1 prefix */ | |
234 | + | |
235 | + char indexfile[2048]; | |
236 | + | |
237 | + for (i = 1; i<a; i++) { | |
238 | + if (!strcmp(b[i],"-h")) { | |
239 | + printf("usage: addimgdate [-r] [<target_prefix(%s)>]\n", prefix); | |
240 | + exit(1); | |
241 | + } | |
242 | + if (!strcmp(b[i],"-i")) { | |
243 | + igncase = 1 - igncase; | |
244 | + continue; | |
245 | + } | |
246 | + if (!strcmp(b[i],"-r")) { | |
247 | + rf = 1; | |
248 | + continue; | |
249 | + } | |
250 | + if (!strcmp(b[i],"-t")) { | |
251 | + tf = 1; | |
252 | + continue; | |
253 | + } | |
254 | + if (!strcmp(b[i],"-v")) { | |
255 | + vf = 1; | |
256 | + continue; | |
257 | + } | |
258 | + } | |
259 | + | |
260 | + | |
261 | + RenameFileR(".", rf); | |
262 | + | |
263 | + exit(0); | |
264 | +} | |
265 | + | |
266 | +/* | |
267 | + * vim: ts=4 sw=4 | |
268 | + */ | |
269 | + |