The cp command copies the source file specified by the Source File parameter to the destination file specified by the Destination File parameter. Write a C program that mimics the cp command using open() system call to open source.txt file in read-only mode and copy the contents of it to destination.txt using read() and write() system calls.

Respuesta :

The cp command copies the source file specified by the Source File parameter to the destination file specified by the Destination File parameter. It is explained in the explanation part.

Explanation:

void *scanFile( s_request *request )

{

   //TODO Print out all struct variables.

   pthread_mutex_lock(&mutex);

    int readFileReference;

   int writeFileReference;

   int bufferSize = request->bufferSize;

   int numberOfBytesRead = bufferSize;

   int *buffer[bufferSize];

    if ((readFileReference = open(request->file, O_RDONLY)) == -1)

   {

   printf("Failed to open file. Aborting.\n");

   exit(EXIT_FAILURE);

   }

    if ((writeFileReference = open("newfile.txt", O_CREAT | O_WRONLY, 0777)) == -1)

   {

   printf("Failed to open write file. Aborting.\n");

   exit(EXIT_FAILURE);

   }

    while ((read(readFileReference, buffer, numberOfBytesRead)) != 0)

   {

   write(writeFileReference, buffer, bufferSize);

   bzero(buffer, bufferSize);

   }

   close(writeFileReference);

   close(readFileReference);

    pthread_mutex_unlock(&mutex);

}