Question

Write a C program to Simulate Implementation of Linux *cp* command. Copying Files and directories

Write a C program to Simulate Implementation of Linux *cp* command.
Copying Files and directories
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find my implemetation:

Here is the code for you:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char *argv[])
{
char fileName[50];
if(argc != 3)
{
printf("Error. Usage: mycp source_file target_file\n");
return 0;
}
strcpy(fileName, argv[1]);
//Attempts to open the file with the name specified by the user (using the open system call) in read only mode.
int fdIn = open((const char *)fileName, O_RDONLY);
//Displays an error message and quits the program if file open is unsuccessful.
if(fdIn == -1)
{
printf("Error: Source file doesn't exist.\n");
return 0;
}
strcpy(fileName, argv[2]);
int fdOut = open((const char *)fileName, O_RDONLY);
if(fdOut != -1)
{
printf("Error: Destination file readily exist.\n");
return 0;
}
close(fdOut);
fdOut = open((const char *)fileName, O_CREAT);
fdOut = open((const char *)fileName, O_WRONLY);
char buffer;
int byteCount = 0;
while(read(fdIn, &buffer, 1))
{
write(fdOut, &buffer, 1);
byteCount++;
}   
close(fdIn);
close(fdOut);
printf("Copied %d bytes\n", byteCount);
}

Add a comment
Know the answer?
Add Answer to:
Write a C program to Simulate Implementation of Linux *cp* command. Copying Files and directories
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT