-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.c
37 lines (35 loc) · 946 Bytes
/
copy.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char** argv)
{
if(argc != 3)
{
printf("Usage: ./copy \"source\" \"destination\"\n");
}
else
{
FILE* file_to_read = fopen(argv[1], "r");
FILE* file_to_write = fopen(argv[2], "w");
char* line = malloc(4096 * sizeof(char));
int fd_to_read;
int fd_to_write;
size_t buffer = 0;
size_t size;
if(file_to_read != NULL)
{
fd_to_read = fileno(file_to_read);
fd_to_write = fileno(file_to_write);
while(0 != (size = read(fd_to_read, line, 4096)))
{
printf("writing thing of size %lu\n", size);
printf("line = %s\n", line);
write(fd_to_write, line, size);
}
}
free(line);
fclose(file_to_read);
fclose(file_to_write);
}
return 0;
}