-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpcat.c
62 lines (56 loc) · 1.35 KB
/
cpcat.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char* argv[]) {
int inp, out, stBajtov;
char vhod[1000];
if(argc == 1 || argv[1][0] == '-'){
inp = 0;
}else {
inp = open(argv[1], O_RDONLY);
if(inp == -1){
int koda = errno;
perror("Napaka pri odpiranju vhodne datoteke");
exit(koda);
}
}
if(argc == 3){
out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC);
if(out == -1){
int koda = errno;
perror("Napaka pri odpiranju izhodne datoteke");
exit(koda);
}
}else{
out = 1;
}
while((stBajtov = read(inp, vhod, 1000)) > 0){
if(write(out, vhod, stBajtov) != stBajtov){
int koda = errno;
perror("Napaka pri kopiranju");
exit(koda);
}
}
if(stBajtov == -1){
int koda = errno;
perror("Napaka pri branju");
exit(koda);
}
if(inp != 0){
if(close(inp) == -1){
int koda = errno;
perror("Napaka pri zapiranju vhodne datoteke");
exit(koda);
}
}
if(out != 1){
if(close(out) == -1){
int koda = errno;
perror("Napaka pri zapiranju izhodne datoteke");
exit(koda);
}
}
return 0;
}