-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path3-cp.c
executable file
·47 lines (46 loc) · 1.15 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
#include "main.h"
/**
* main - copy a file.
* @argc: the number of args
* @argv: the Args.
* Description: copy a file
* Return: 0 in success.
*/
int main(int argc, char *argv[])
{
int src_fd, dest_fd;
int _read, _close_src, _close_dest;
char buffer[1024];
if (argc > 3 || argc < 3)
{
dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n");
exit(97);
}
src_fd = open(*(argv + 1), O_RDONLY);
if (src_fd == -1)
{
dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", *(argv + 1));
exit(98);
}
dest_fd = open(*(argv + 2), O_TRUNC | O_CREAT | O_WRONLY, 0664);
while ((_read = read(src_fd, buffer, 1024)) > 0)
{
if (dest_fd == -1 || (write(dest_fd, buffer, _read) != _read))
{
dprintf(STDERR_FILENO, "Error: Can't write to %s\n", *(argv + 2));
exit(99);
}
}
if (_read < 0)
{
dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", *(argv + 1));
exit(98);
}
_close_src = close(src_fd);
if (_close_src < 0)
dprintf(STDERR_FILENO, "Error: Can't close fd %i\n", src_fd), exit(100);
_close_dest = close(dest_fd);
if (_close_dest < 0)
dprintf(STDERR_FILENO, "Error: Can't close fd %i\n", dest_fd), exit(100);
return (0);
}