-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcp.c
79 lines (48 loc) · 1.17 KB
/
cp.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define K 1024
/*
*
* This main function copies files specified on the command line to the directory destination at
* the end of the command line with open()/read()/write() system calls
*
*/
int main (int argc, char *argv[]) {
char buf[K];
if (argc > 3 ) {
printf ("Usage: cp src dst\n");
exit(0);
}
char *src = argv[1];
char *dst = argv[2];
int infd = open (src, O_RDONLY);
if (infd < 0 ) {
fprintf (stderr, "cp: Unable to open %s for copying.\n", src);
exit(1);
}
char *new_path = (char *) malloc (1 + strlen(src) + strlen(dst) );
if (new_path == NULL ) {
fprintf (stderr, "Unable to allocate new memory. \n");
exit(1);
}
strcpy (new_path, dst);
strcat (new_path, "/");
strcat (new_path, src);
int outfd = open (new_path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (outfd < 0) {
fprintf (stderr, "cp: Unable to create %s to copy to\n", dst);
exit(1);
}
int reading;
while ((reading = read(infd, buf, K)) > 0) {
write(outfd, buf, reading);
}
close(infd);
close(outfd);
return 0;
}