-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
93 lines (68 loc) · 1.87 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* ---------------------------------------------------------------------
* #############################################
* Copyright (c) 2015-2035 kuramal All rights reserved.
* #############################################
*
* Name: main.c
* Date: 2015-04-28 21:47
* Author: kuramal
* Desc:
*
* ---------------------------------------------------------------------
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
int copy_file(char *src_dir, char *dst_dir, char *file_name)
{
FILE *dst_ptr;
char src_file[256] = {'\0',};
char dst_file[256] = {'\0',};
snprintf(src_file, sizeof(src_file), "%s/%s", src_dir, file_name);
snprintf(dst_file, sizeof(dst_file), "%s/%s", dst_dir, file_name);
dst_ptr = fopen(dst_file, "w");
fclose(dst_ptr);
return 0;
}
int file_porter(char *src_dir, char *dst_dir)
{
int ret;
DIR *cur_dir;
char new_src_dir[256] = {'\0',};
char new_dst_dir[256] = {'\0',};
struct dirent *dir_ptr = NULL;
if (access(dst_dir, F_OK) != 0){
ret = mkdir(dst_dir, 0);
if (ret){
printf("mkdir %s ERR\n", dst_dir);
return ret;
}
}
cur_dir = opendir(src_dir);
while( (dir_ptr = (readdir(cur_dir))) != NULL){
if (strncmp(dir_ptr->d_name, ".", 1) == 0 || strcmp(dir_ptr->d_name, "wakeup") == 0){
continue;
}
if (dir_ptr->d_type == DT_DIR){
snprintf(new_src_dir, sizeof(new_src_dir), "%s/%s", src_dir, dir_ptr->d_name);
snprintf(new_dst_dir, sizeof(new_dst_dir), "%s/%s", dst_dir, dir_ptr->d_name);
file_porter(new_src_dir, new_dst_dir);
}else if (dir_ptr->d_type == DT_REG){
copy_file(src_dir, dst_dir, dir_ptr->d_name);
}
}
closedir(cur_dir);
return 0;
}
int main(int argc, char **argv)
{
char *src_dir = "./";
char *encrypt_dir = "./wakeup";
file_porter(src_dir, encrypt_dir);
return 0;
}