-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhomework-1.c
60 lines (48 loc) · 1.32 KB
/
homework-1.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
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MODE 0600
#define BUFFER_SIZE 2048
char *create_dest_path(char *source, char *dest) {
char *path =
calloc(PATH_MAX, sizeof(char)); // malloc(sizeof(char) * PATH_MAX)
sprintf(path, "%s/%s", dest, basename(source));
return path;
}
void copy(char *source, char *dest) {
int sd, dd, size;
char buffer[BUFFER_SIZE];
if ((sd = open(source, O_RDONLY)) == -1) {
perror(source);
exit(EXIT_FAILURE);
}
char *path = create_dest_path(source, dest);
if ((dd = open(path, O_WRONLY | O_CREAT | O_TRUNC, MODE)) == -1) {
perror(path);
exit(EXIT_FAILURE);
}
do {
if ((size = read(sd, buffer, BUFFER_SIZE)) == -1) {
perror("read");
exit(EXIT_FAILURE);
}
if (write(dd, buffer, size) == -1) {
perror("write");
exit(EXIT_FAILURE);
}
} while (size == BUFFER_SIZE);
free(path);
}
int main(int argc, char **argv) {
if (argc < 3) {
printf("Usage: %s <file-1> [file-2] ... [file-n] <dest-dir>\n",
argv[0]);
exit(EXIT_FAILURE);
}
for (int i = 1; i < argc - 1; i++)
copy(argv[i], argv[argc - 1]);
exit(EXIT_SUCCESS);
}