Skip to content

Commit 7cf8f25

Browse files
committed
Copying Content
1 parent e05904c commit 7cf8f25

File tree

2 files changed

+40
-81
lines changed

2 files changed

+40
-81
lines changed

0x15-file_io/3-cp.c

Lines changed: 40 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,69 @@
11
#include "main.h"
2-
3-
char *allocate_buffer(char *filename);
4-
void closer(int fd);
5-
62
/**
7-
* allocate_buffer - Entry point
8-
* Description: Allocates bytes to be used by the buffer.
9-
* @filename: Name of the file
10-
*
11-
* Return: Buffer allocated
3+
* error_file - checks if files can be opened.
4+
* @file_from: file_from.
5+
* @file_to: file_to.
6+
* @argv: arguments vector.
7+
* Return: no return.
128
*/
13-
14-
char *allocate_buffer(char *filename)
9+
void error_file(int file_from, int file_to, char *argv[])
1510
{
16-
char *scrap;
17-
18-
/* Allocate 1024 bytes of memory for buffer */
19-
scrap = malloc(sizeof(char) * 1024);
20-
21-
if (scrap == NULL)
11+
if (file_from == -1)
2212
{
23-
/* Prints error mesage if buffer allocation failes */
24-
dprintf(STDERR_FILENO, "Error: Can't write to %s\n", filename);
25-
exit(99);
13+
dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]);
14+
exit(98);
2615
}
27-
return (scrap);
28-
}
29-
30-
/**
31-
* closer - Closes file
32-
* @fd: File description file
33-
*/
34-
35-
void closer(int fd)
36-
{
37-
int result;
38-
39-
/* Close file descriptor */
40-
result = close(fd);
41-
if (result == -1)
16+
if (file_to == -1)
4217
{
43-
/* Print error message if file descriptor closing fails */
44-
dprintf(STDERR_FILENO, "Error: Can't close fd%d\n", fd);
45-
exit(100);
18+
dprintf(STDERR_FILENO, "Error: Can't write to %s\n", argv[2]);
19+
exit(99);
4620
}
4721
}
4822

4923
/**
50-
* main - Entry point
51-
* Description: Copies content within file to another file
52-
* @argc: Number of arguments
53-
* @argv: Array of strings
54-
*
55-
* Return: 0, if successful
56-
* exit code 97 if arguement count is incorrect
57-
* exit code 98 if file_from cant be read or doesn't exist
58-
* exit code 99 if file_to cannot be written or created
59-
* exit code 100 if file_to or file_from cannot be closed
24+
* main - check the code for Holberton School students.
25+
* @argc: number of arguments.
26+
* @argv: arguments vector.
27+
* Return: Always 0.
6028
*/
61-
6229
int main(int argc, char *argv[])
6330
{
64-
int file_from, file_to, my_read, my_write, alpha, beta;
65-
char buffer[BUFFER_SIZE];
31+
int file_from, file_to, err_close;
32+
ssize_t nchars, nwr;
33+
char buf[1024];
6634

67-
/* Print error message if argument count is incorrect */
6835
if (argc != 3)
6936
{
70-
dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n");
37+
dprintf(STDERR_FILENO, "%s\n", "Usage: cp file_from file_to");
7138
exit(97);
7239
}
73-
/* Open source file for reading */
40+
7441
file_from = open(argv[1], O_RDONLY);
75-
/* Read 1024 bytes from source file into buffer */
76-
my_read = read(file_from, buffer, BUFFER_SIZE);
77-
/* Open destination file for writing -rw-r--r-, with those permissions */
78-
file_to = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
42+
file_to = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC | O_APPEND, 0664);
43+
error_file(file_from, file_to, argv);
7944

80-
if (file_from < 0)
81-
{
82-
dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]);
83-
exit(98);
84-
}
85-
my_write = write(file_to, buffer, my_read);
86-
while (my_read > 0)
45+
nchars = 1024;
46+
while (nchars == 1024)
8747
{
88-
if (file_to || my_write != my_read)
89-
{
90-
dprintf(STDERR_FILENO, "Error: Can't write to %s\n", argv[2]);
91-
close(file_from);
92-
exit(99);
93-
}
48+
nchars = read(file_from, buf, 1024);
49+
if (nchars == -1)
50+
error_file(-1, 0, argv);
51+
nwr = write(file_to, buf, nchars);
52+
if (nwr == -1)
53+
error_file(0, -1, argv);
9454
}
95-
if (my_read < 0)
55+
56+
err_close = close(file_from);
57+
if (err_close == -1)
9658
{
97-
dprintf(STDERR_FILENO, "Error: Can't read from file %s\n", argv[1]);
98-
exit(98);
59+
dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from);
60+
exit(100);
9961
}
100-
alpha = close(file_from);
101-
beta = close(file_to);
102-
if (alpha < 0 || beta < 0)
62+
63+
err_close = close(file_to);
64+
if (err_close == -1)
10365
{
104-
if (alpha < 0)
105-
dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from);
106-
if (beta < 0)
107-
dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_to);
66+
dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", file_from);
10867
exit(100);
10968
}
11069
return (0);

0x15-file_io/cp

-80 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)