回答編集履歴

1

追加

2016/08/29 02:35

投稿

A.Ichi
A.Ichi

スコア4070

test CHANGED
@@ -15,3 +15,407 @@
15
15
 
16
16
 
17
17
  プログラムを修正する場合、こんな感じでやっております。
18
+
19
+
20
+
21
+ サンプルですのでご参考としてください。
22
+
23
+ ```c
24
+
25
+ #include <stdio.h>
26
+
27
+ #include <stdlib.h>
28
+
29
+ #include <string.h>
30
+
31
+ #include <unistd.h>
32
+
33
+ #include <dirent.h>
34
+
35
+ #include <sys/types.h>
36
+
37
+ #include <sys/stat.h>
38
+
39
+ #include <errno.h>
40
+
41
+
42
+
43
+ struct strbuf {
44
+
45
+ char *ptr;
46
+
47
+ size_t len;
48
+
49
+ };
50
+
51
+
52
+
53
+ void traverse(struct strbuf *buf);
54
+
55
+ void traverseSub(struct strbuf *buf, int first);
56
+
57
+ struct strbuf *strbuf_new(void);
58
+
59
+ void strbuf_realloc(struct strbuf *buf, size_t size);
60
+
61
+
62
+
63
+ void print_error(char *s){
64
+
65
+ fprintf(stderr, "%s: %s\n", s, strerror(errno));
66
+
67
+ }
68
+
69
+
70
+
71
+ void die (const char *s){
72
+
73
+ perror(s);
74
+
75
+ exit(1);
76
+
77
+ }
78
+
79
+
80
+
81
+ char dir1[256];
82
+
83
+ char dir2[256];
84
+
85
+
86
+
87
+ int ccp(char *frfile,char *tofile)
88
+
89
+ {
90
+
91
+ FILE* fpSrc;
92
+
93
+ FILE* fpDest;
94
+
95
+ char c;
96
+
97
+
98
+
99
+ fpSrc = fopen( frfile, "rb" );
100
+
101
+ if( fpSrc == NULL ){
102
+
103
+ exit( EXIT_FAILURE );
104
+
105
+ }
106
+
107
+
108
+
109
+ fpDest = fopen( tofile, "wb" );
110
+
111
+ if( fpDest == NULL ){
112
+
113
+ exit( EXIT_FAILURE );
114
+
115
+ }
116
+
117
+
118
+
119
+ while( 1 ){
120
+
121
+ /* バイナリデータとして 1Byteずつ読み込み、1Byteずつ書き込む */
122
+
123
+ fread( &c, sizeof(c), 1, fpSrc );
124
+
125
+ if( feof( fpSrc ) ){
126
+
127
+ break;
128
+
129
+ }
130
+
131
+ if( ferror( fpSrc ) ){
132
+
133
+ exit( EXIT_FAILURE );
134
+
135
+ }
136
+
137
+ fwrite( &c, sizeof(c), 1, fpDest );
138
+
139
+ }
140
+
141
+
142
+
143
+ if( fclose( fpDest ) == EOF ){
144
+
145
+ fputs( "ファイルクローズに失敗しました。\n", stderr );
146
+
147
+ exit( EXIT_FAILURE );
148
+
149
+ }
150
+
151
+ if( fclose( fpSrc ) == EOF ){
152
+
153
+ fputs( "ファイルクローズに失敗しました。\n", stderr );
154
+
155
+ exit( EXIT_FAILURE );
156
+
157
+ }
158
+
159
+ return 0;
160
+
161
+ }
162
+
163
+
164
+
165
+ int main(int argc, char *argv[]){
166
+
167
+ struct strbuf *pathbuf;
168
+
169
+
170
+
171
+ if(argc != 3){
172
+
173
+ fprintf(stderr, "Usage:cpdir dir1 dir2\n");
174
+
175
+ exit(1);
176
+
177
+ }
178
+
179
+ pathbuf = strbuf_new();
180
+
181
+ strbuf_realloc(pathbuf, strlen(argv[1]));
182
+
183
+ strcpy(pathbuf->ptr, argv[1]);
184
+
185
+ strcpy(dir1, argv[1]);
186
+
187
+ strcpy(dir2, argv[2]);
188
+
189
+ traverse(pathbuf);
190
+
191
+ exit(0);
192
+
193
+ }
194
+
195
+
196
+
197
+ void traverse(struct strbuf *pathbuf){
198
+
199
+ traverseSub(pathbuf, 1);
200
+
201
+ }
202
+
203
+
204
+
205
+ void traverseSub(struct strbuf *pathbuf, int first){
206
+
207
+ DIR *d;
208
+
209
+ struct dirent *ent;
210
+
211
+ struct stat st;
212
+
213
+ char to_name[2048];
214
+
215
+
216
+
217
+ // printf("traverseSub S\n");
218
+
219
+ if (first){
220
+
221
+ strcpy(to_name,dir2);
222
+
223
+ }else{
224
+
225
+ strcpy(to_name,dir2);
226
+
227
+ strcpy(to_name+strlen(dir2),pathbuf->ptr+strlen(dir1));
228
+
229
+ }
230
+
231
+
232
+
233
+ d = opendir(pathbuf->ptr);
234
+
235
+ if (!d) {
236
+
237
+ switch (errno){
238
+
239
+ case ENOTDIR:
240
+
241
+ return;
242
+
243
+ case ENOENT:
244
+
245
+ if(first){
246
+
247
+ die(pathbuf->ptr);
248
+
249
+ } else {
250
+
251
+ return;
252
+
253
+ }
254
+
255
+ case EACCES:
256
+
257
+ puts(pathbuf->ptr);
258
+
259
+ print_error(pathbuf->ptr);
260
+
261
+ return;
262
+
263
+ default:
264
+
265
+ perror(pathbuf->ptr);
266
+
267
+ exit(1);
268
+
269
+ }
270
+
271
+ }
272
+
273
+ //printf("Dir:%s \n", pathbuf->ptr);
274
+
275
+ //printf("Dir2:%s \n", to_name);
276
+
277
+ puts(pathbuf->ptr);
278
+
279
+ if (mkdir(to_name,0755) == 0){
280
+
281
+ printf("%s mkdir\n", to_name);
282
+
283
+ }
284
+
285
+ while(ent = readdir(d)){
286
+
287
+ if(strcmp(ent->d_name, ".") == 0)
288
+
289
+ continue;
290
+
291
+ if(strcmp(ent->d_name, "..") == 0)
292
+
293
+ continue;
294
+
295
+ strbuf_realloc(pathbuf, pathbuf->len + 1 + strlen(ent->d_name) + 1);
296
+
297
+ // special handling for the root directory
298
+
299
+ if(strcmp(pathbuf->ptr, "/") != 0){
300
+
301
+ strcat(pathbuf->ptr, "/");
302
+
303
+ }
304
+
305
+ strcat(pathbuf->ptr, ent->d_name);
306
+
307
+ if(lstat(pathbuf->ptr, &st) < 0){
308
+
309
+ switch (errno){
310
+
311
+ case ENOENT:
312
+
313
+ break;
314
+
315
+ case EACCES:
316
+
317
+ print_error(pathbuf->ptr);
318
+
319
+ break;
320
+
321
+ default:
322
+
323
+ die(pathbuf->ptr);
324
+
325
+ }
326
+
327
+ } else {
328
+
329
+ if (S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode)) {
330
+
331
+ traverseSub(pathbuf, 0);
332
+
333
+ } else {
334
+
335
+ puts(pathbuf->ptr);
336
+
337
+ strcpy(to_name,dir2);
338
+
339
+ strcpy(to_name+strlen(dir2),pathbuf->ptr+strlen(dir1));
340
+
341
+ if (ccp(pathbuf->ptr, to_name)){
342
+
343
+ printf("File:%s\n",to_name);
344
+
345
+ }
346
+
347
+ }
348
+
349
+ }
350
+
351
+ *strrchr(pathbuf->ptr, '/') = '\0';
352
+
353
+ }
354
+
355
+ closedir(d);
356
+
357
+ //printf("traverseSub E\n");
358
+
359
+ }
360
+
361
+ #define INITLEN 1024
362
+
363
+
364
+
365
+ struct strbuf * strbuf_new(void){
366
+
367
+ struct strbuf *buf;
368
+
369
+
370
+
371
+ buf = (struct strbuf*)malloc(sizeof(struct strbuf));
372
+
373
+ if (!buf) {
374
+
375
+ die("malloc(3)");
376
+
377
+ }
378
+
379
+ buf->ptr = malloc(INITLEN);
380
+
381
+ if(!buf->ptr){
382
+
383
+ die("malloc(3)");
384
+
385
+ }
386
+
387
+ buf->len = INITLEN;
388
+
389
+ return buf;
390
+
391
+ }
392
+
393
+
394
+
395
+ void strbuf_realloc(struct strbuf *buf, size_t len){
396
+
397
+ char *tmp;
398
+
399
+
400
+
401
+ if(buf->len > len)
402
+
403
+ return;
404
+
405
+ tmp = realloc(buf->ptr, len);
406
+
407
+ if (!tmp) {
408
+
409
+ die("realloc(3)");
410
+
411
+ }
412
+
413
+ buf->ptr = tmp;
414
+
415
+ buf->len = len;
416
+
417
+ }
418
+
419
+ ```
420
+
421
+