-
Notifications
You must be signed in to change notification settings - Fork 3
/
cpyenv.c
53 lines (51 loc) · 1.07 KB
/
cpyenv.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
#include "holberton.h"
#include <stdio.h>
#define LIM 1024
/**
* _readandcpy - function that reads from fr and copy to to
* @fr: file origin.
* @to: file destiny.
* @buf: buffer to store the characters.
* Return: nothing.
*/
int _readandcpy(char *fr, char *to, char *buf)
{
int f, t, n_read = LIM, n_write, a, b, total = 0;
if (!fr)
{
error98:
dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", fr);
exit(98);
}
f = open(fr, O_RDONLY, 0400);
t = open(to, O_CREAT | O_WRONLY | O_TRUNC | O_APPEND, 0777);
if (f == -1)
goto error98;
if (t == -1)
{
error99:
dprintf(STDERR_FILENO, "Error: Can't write to %s\n", to);
exit(99);
}
while (n_read == LIM)
{
n_read = read(f, buf, LIM);
if (n_read == -1)
goto error98;
total += n_read;
n_write = write(t, buf, n_read);
if (n_write != n_read)
goto error99;
}
a = close(f);
b = close(t);
if (a == -1 || b == -1)
{
if (a == -1)
dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", f);
if (b == -1)
dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", t);
exit(100);
}
return (total);
}