复制代码
1
复制代码
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <malloc.h> #include <string.h> #include <dirent.h> #define DEBUG_LOG #if defined(DEBUG_LOG) #define DEBUG(fmt, arg...) printf(fmt "rn", ##arg) #else #define DEBUG(fmt, arg...) #endif #define ERROR_LOG #if defined(ERROR_LOG) #define ERROR(fmt, arg...) printf("ERROR" fmt "rn", ##arg) #else #define ERROR(fmt, arg...) #endif #define FNAME_MAXLEN 10000 #define UNDERSCORE 1 #define SLASH 2 int isdir(char *path) { struct stat buf; if (lstat(path, &buf) < 0) DEBUG("Error: isdir lstat error!n"); if (S_ISDIR(buf.st_mode)) { //DEBUG("Is a direcotry!n"); return 1; } else return 0; } int isreg(char *path) { struct stat buf; if (lstat(path, &buf) < 0) DEBUG("Error: isreg lstat error!n"); if (S_ISREG(buf.st_mode)) { //DEBUG("Is a direcotry!n"); return 1; } else return 0; } int connect_symbol(char *first, char *sec, char val) { char temp[3] ; if (val == 1) temp[0] = '_'; else if (val == 2) temp[0] = '/'; else printf("connect_symbol: Error!n"); temp[1] = ''; strcat(first, temp); strcat(first, sec); return 0; } int contain_dot(char *path, char *before_dot) { char *p; char flag = 0, i = 0; char after_dot[5]; p = path; while(*p) { if (*p == '.') { p++; flag = 1; break; } *before_dot = *p; before_dot++; p++; i++; } *before_dot = ''; before_dot -= i; i = 0; while(*p) { after_dot[i] = *p; p++; i++; } after_dot[i] = ''; if(flag) { connect_symbol(before_dot, after_dot, UNDERSCORE); } return flag; } int cp_file(char *old_path, char *new_path) { int fd1, fd2; char *buf; off_t currpos; DEBUG("cp_file: old_path = %sn", old_path); DEBUG("cp_file: new_path = %sn", new_path); fd1 = open(old_path, O_RDONLY); if (fd1 < 0) { DEBUG("Open original file failed!n"); return 0; } currpos = lseek(fd1, 0, SEEK_END); fd1 = open(old_path, O_RDONLY); buf = (char *)malloc(currpos); if (strlen(buf) < 0) DEBUG("Assign temporary buffer failed!n"); fd2 = creat(new_path, 0777); if (fd2 < 0) { DEBUG("Creat new file failed!n"); return 0; } if (read(fd1, buf, currpos) < 0) { DEBUG("Read original file failed!n"); return 0; } if (write(fd2, buf, currpos) < 0) { DEBUG("Write new file failed!n"); return 0; } free(buf); return 1; } int cp_log(char *old_path, char *new_path) { char ret; DEBUG("cp_log: old_path = %sn", old_path); DEBUG("cp_log: new_path = %sn", new_path); if (isreg(old_path)) { if(ret = cp_file(old_path, new_path)) DEBUG("cp_log: cp file success!n"); else { DEBUG("cp_log: copy failed!n"); return 0; } } else if(isdir(old_path)) { DEBUG("cp_log: Is a dircotry!n"); } else DEBUG("cp_log: Object file is not a file or directory!n"); return 1; } int do_copy(char *source, char *dest_filename) { char add_log[10] = ".log"; char currname[FNAME_MAXLEN]; char ret; if (contain_dot(source, currname)) // ie: input "test/test.c" currname = "test/test_c" { DEBUG("do_copy: source = %sn", source); strcat(dest_filename, currname); // add prefix and save the finnal name in "dest_filename" strcat(dest_filename, add_log); DEBUG("do_copy: currdir = %sn", dest_filename); if(ret = cp_log(source, dest_filename)) DEBUG("do_copy: cp to log success!n"); else DEBUG("do_copy: copy failed!n"); } return 1; } /* cd a directory and ergodic it, save all the files in *rtdir_file[FNAME_MAXLEN]*/ int ergodic_dir(char *dir, char *rtdir_file[FNAME_MAXLEN]) { DIR *dp; unsigned int i = 0; struct dirent *dirp; //char rtdir_file[200][200]; if ((dp = opendir(dir)) == NULL) printf("ergodic_dir: can't open %sn", rtdir_file[1]); while((dirp = readdir(dp)) != NULL) { printf("ergodic_dir: dirp->d_name = %sn", dirp->d_name); if ((strcmp("..", dirp->d_name) == 0) || (strcmp(".", dirp->d_name) == 0)) ; else { //printf("ergodic_dir: dir = %s, strlen(dir) = %dn", dir, strlen(dir)); //printf("ergodic_dir: dirp->d_name = %s, strlen(dirp->d_name) = %dn", dirp->d_name, strlen(dirp->d_name)); connect_symbol(dir, dirp->d_name, SLASH); printf("ergodic_dir: dir = %sn", dir); strcpy(rtdir_file[i++], dir); } } closedir(dp); while(*rtdir_file[i]) printf("ergodic_dir: rtdir_file[%d] = %sn", i, rtdir_file[i++]); return 0; } int copy(char *object, char *dest_filename) { char dest_lenth; char rtdir_file[500][FNAME_MAXLEN]; dest_lenth = strlen(dest_filename); if (isreg(object)) { DEBUG("copy: argv[i] = %sn", object); DEBUG("copy: currdir = %sn", dest_filename); if(do_copy(object, dest_filename)) printf("copy: cp to log success!n"); else printf("copy: copy failed!n"); } else if(isdir(object)) { strcat(dest_filename, object); // 1. create the same-name directory if ((mkdir(dest_filename, 0755)) < 0) DEBUG("copy: mkdir %s failed !n", dest_filename); //2. enter this directory, call cp_log recursively ergodic_dir(object, rtdir_file[FNAME_MAXLEN]); //copy(object, dest_filename); printf("Is a dircotry!n"); } dest_filename[dest_lenth] = ''; return 0; } int main(int argc, char *argv[]) { char i = 0; char dest_filename[FNAME_MAXLEN] = "./test_dir/"; copy(argv[1], dest_filename); return 0; }
最后
以上就是儒雅镜子最近收集整理的关于cp_log的全部内容,更多相关cp_log内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复