-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.c
58 lines (43 loc) · 1.12 KB
/
util.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
#include "util.h"
/*
* copy file from src_path to dst_path
*
* returns 0 if sucessfull
*/
int copy_file(char* dst_path, char* src_path) {
int src_fd, dst_fd;
char *buf;
int n_read;
struct stat source_stat;
if((src_fd = open(src_path,O_RDONLY)) < 0) {
printf("unable to open source file\n");
return -1;
}
fstat(src_fd,&source_stat);
/* if((dst_fd = open(dst_path,O_CREAT|O_WRONLY,S_IRUSR|S_IWUSR)) < 0) {*/
if((dst_fd = open(dst_path,O_CREAT|O_WRONLY,source_stat.st_mode)) < 0) {
printf("unable to open destination file\n");
return -1;
}
buf = malloc(sizeof(char) * BUF_LENGTH);
while((n_read = read(src_fd,buf,BUF_LENGTH)) > 0) {
// printf("read %d bytes:%s\n",n_read,buf);
write(dst_fd,buf,n_read);
}
futimes(src_fd,NULL);
close(src_fd);
close(dst_fd);
free(buf);
return 0;
}
/*
int main() {
char* source = "/tmp/test/test";
char* destination = "/tmp/test/test_copy";
printf("will copy %s to %s\n",source,destination);
if(copy_file(destination,source))
printf("copy was not sucessful\n");
printf("copy was sucessful\n");
return 0;
}
*/