forked from jhpy1024/CProgrammingLanguageExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_8-1.c
44 lines (39 loc) · 841 Bytes
/
exercise_8-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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void copy(int from, int to)
{
char current_char;
while ((read(from, ¤t_char, 1)) == 1)
{
write(to, ¤t_char, 1);
}
}
int main(int argc, char* argv[])
{
if (argc == 1)
{
/* Copy input to output. */
copy(0, 1);
}
else
{
int file_descriptor;
while (--argc > 0)
{
if ((file_descriptor = open(*++argv, O_RDONLY, 0)) == -1)
{
printf("cat: can't open %s\n", *argv);
return EXIT_FAILURE;
}
else
{
/* Copy file to output. */
copy(file_descriptor, 1);
close(file_descriptor);
}
}
}
return EXIT_SUCCESS;
}