-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-cp.c
70 lines (62 loc) · 1.7 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "main.h"
/**
* __exit - prints error messages and exits with exit value
* @error: num is either exit value or file descriptor
* @s: str is a name, either of the two filenames
* @fd: file descriptor
* Return: 0 on success
**/
int __exit(int error, char *s, int fd)
{
switch (error)
{
case 97:
dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n");
exit(error);
case 98:
dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", s);
exit(error);
case 99:
dprintf(STDERR_FILENO, "Error: Can't write to %s\n", s);
exit(error);
case 100:
dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", fd);
exit(error);
default:
return (0);
}
}
/**
* main - copies one file to another
* @argc: should be 3 (./a.out copyfromfile copytofile)
* @argv: first is file to copy from (fd_1), second is file to copy to (fd_2)
* Return: 0 (success), 97-100 (exit value errors)
*/
int main(int argc, char *argv[])
{
int fd_1, fd_2, n_read, n_wrote;
char *buffer[1024];
if (argc != 3)
__exit(97, NULL, 0);
/*sets file descriptor for copy-to file*/
fd_2 = open(argv[2], O_CREAT | O_TRUNC | O_WRONLY, 0664);
if (fd_2 == -1)
__exit(99, argv[2], 0);
/*sets file descriptor for copy-from file*/
fd_1 = open(argv[1], O_RDONLY);
if (fd_1 == -1)
__exit(98, argv[1], 0);
/*reads original file as long as there's more than 0 to read*/
/*copies/writes contents into new file */
while ((n_read = read(fd_1, buffer, 1024)) != 0)
{
if (n_read == -1)
__exit(98, argv[1], 0);
n_wrote = write(fd_2, buffer, n_read);
if (n_wrote == -1)
__exit(99, argv[2], 0);
}
close(fd_2) == -1 ? (__exit(100, NULL, fd_2)) : close(fd_2);
close(fd_1) == -1 ? (__exit(100, NULL, fd_1)) : close(fd_1);
return (0);
}