-
Notifications
You must be signed in to change notification settings - Fork 2
/
3-cp.c
52 lines (50 loc) · 1.08 KB
/
3-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
#include "main.h"
/**
* main - copies the content of a file to another file
* @argc: number of arguments passed to the program
* @argv: array of arguments
*
* Return: Always 0 (Success)
*/
int main(int argc, char *argv[])
{
int fd_r, fd_w, x, m, n;
char buf[BUFSIZ];
if (argc != 3)
{
dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n");
exit(97);
}
fd_r = open(argv[1], O_RDONLY);
if (fd_r < 0)
{
dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]);
exit(98);
}
fd_w = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0664);
while ((x = read(fd_r, buf, BUFSIZ)) > 0)
{
if (fd_w < 0 || write(fd_w, buf, x) != x)
{
dprintf(STDERR_FILENO, "Error: Can't write to %s\n", argv[2]);
close(fd_r);
exit(99);
}
}
if (x < 0)
{
dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]);
exit(98);
}
m = close(fd_r);
n = close(fd_w);
if (m < 0 || n < 0)
{
if (m < 0)
dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", fd_r);
if (n < 0)
dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", fd_w);
exit(100);
}
return (0);
}