0% found this document useful (0 votes)
10 views2 pages

Assignment 1

Uploaded by

unknown Me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Assignment 1

Uploaded by

unknown Me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#define BUF_SIZE 4096

// Function to copy contents from source file descriptor to destination file


descriptor
int my_copy(int source_fd, int dest_fd) {
char buffer[BUF_SIZE];
ssize_t bytesRead, bytesWritten;

// Copy loop
while ((bytesRead = read(source_fd, buffer, BUF_SIZE)) > 0) {
bytesWritten = write(dest_fd, buffer, bytesRead);

// Check for write errors


if (bytesWritten != bytesRead) {
perror("Write error");
return -1;
}
}

// Check for read errors


if (bytesRead < 0) {
perror("Read error");
return -1;
}

return 0;
}

int main(int argc, char *argv[]) {


// Check for the correct number of arguments
if (argc < 3) {
fprintf(stderr, "Insufficient arguments\n");
fprintf(stderr, "Usage: %s [option] source_file dest_file\n", argv[0]);
exit(EXIT_FAILURE);
}

// Parse command line arguments


int preserve_permissions = 0;
if (argc == 4 && strcmp(argv[1], "-p") == 0) {
preserve_permissions = 1;
}

char *source_file = argv[argc - 2];


char *dest_file = argv[argc - 1];

// Open source file for reading


int source_fd = open(source_file, O_RDONLY);
if (source_fd == -1) {
perror("Error opening source file");
exit(EXIT_FAILURE);
}
// Check if destination file exists
int dest_fd;
struct stat dest_stat;
if (stat(dest_file, &dest_stat) == 0) {
// Destination file exists

// Ask user for confirmation to overwrite


printf("File \"%s\" is already exists. Do you want to overwrite (Y/n)? ",
dest_file);
char response;
scanf(" %c", &response);

if (response != 'Y' && response != 'y') {


// User chose not to overwrite
close(source_fd);
exit(EXIT_SUCCESS);
}

// Open destination file for writing, truncating it to 0 size


dest_fd = open(dest_file, O_WRONLY | O_TRUNC);
} else {
// Destination file does not exist, create a new one
dest_fd = open(dest_file, O_WRONLY | O_CREAT, 0666);
}

if (dest_fd == -1) {
perror("Error opening destination file");
close(source_fd);
exit(EXIT_FAILURE);
}

// Copy contents from source to destination


if (my_copy(source_fd, dest_fd) == -1) {
// Error during copy
close(source_fd);
close(dest_fd);
exit(EXIT_FAILURE);
}

// Preserve permissions if the -p option is specified


if (preserve_permissions) {
if (fchmod(dest_fd, dest_stat.st_mode) == -1) {
perror("Error preserving permissions");
close(source_fd);
close(dest_fd);
exit(EXIT_FAILURE);
}
}

// Close file descriptors


close(source_fd);
close(dest_fd);

printf("File copied successfully\n");

return 0;
}

You might also like