-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include "main.h" | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
char *create_buffer(char *file); | ||
void close_file(int fd); | ||
|
||
/** | ||
* create_buffer - Allocates 1024 bytes for a buffer. | ||
* @file: The name of the file buffer is storing chars for. | ||
* | ||
* Return: A pointer to the newly-allocated buffer. | ||
*/ | ||
|
||
char *create_buffer(char *file) | ||
{ | ||
char *buffer; | ||
|
||
buffer = malloc(sizeof(char) * 1024); | ||
|
||
if (buffer == NULL) | ||
{ | ||
dprintf(STDERR_FILENO, | ||
"Error: Can't write to %s\n", file); | ||
exit(99); | ||
} | ||
|
||
return (buffer); | ||
} | ||
|
||
/** | ||
* close_file - Closes file descriptors. | ||
* @fd: The file descriptor to be closed. | ||
*/ | ||
void close_file(int fd) | ||
{ | ||
int c; | ||
|
||
c = close(fd); | ||
|
||
if (c == -1) | ||
{ | ||
dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", fd); | ||
exit(100); | ||
} | ||
} | ||
|
||
/** | ||
* main - Copies the contents of a file to another file. | ||
* @argc: The number of arguments supplied to the program. | ||
* @argv: An array of pointers to the arguments. | ||
* | ||
* Return: 0 on success. | ||
* | ||
* Description: If the argument count is incorrect - exit code 97. | ||
* If file_from does not exist or cannot be read - exit code 98. | ||
* If file_to cannot be created or written to - exit code 99. | ||
* If file_to or file_from cannot be closed - exit code 100. | ||
*/ | ||
int main(int argc, char *argv[]) | ||
{ | ||
int from, to, r, w; | ||
char *buffer; | ||
|
||
if (argc != 3) | ||
{ | ||
dprintf(STDERR_FILENO, "Usage: cp file_from file_to\n"); | ||
exit(97); | ||
} | ||
|
||
buffer = create_buffer(argv[2]); | ||
from = open(argv[1], O_RDONLY); | ||
r = read(from, buffer, 1024); | ||
to = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0664); | ||
|
||
do { | ||
if (from == -1 || r == -1) | ||
{ | ||
dprintf(STDERR_FILENO, | ||
"Error: Can't read from file %s\n", argv[1]); | ||
free(buffer); | ||
exit(98); | ||
} | ||
|
||
w = write(to, buffer, r); | ||
if (to == -1 || w == -1) | ||
{ | ||
dprintf(STDERR_FILENO, | ||
"Error: Can't write to %s\n", argv[2]); | ||
free(buffer); | ||
exit(99); | ||
} | ||
|
||
r = read(from, buffer, 1024); | ||
to = open(argv[2], O_WRONLY | O_APPEND); | ||
|
||
} while (r > 0); | ||
|
||
free(buffer); | ||
close_file(from); | ||
close_file(to); | ||
|
||
return (0); | ||
} |