Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Implementing the cp command using read/write system calls

I am trying to implement the cp command only using read/write system calls.
Here is my code:

/**
 * cp file1 file 2
 */

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    int errsv;
    char contents[1024];
    int fd_read, fd_write;
    fd_read = open(argv[1], O_RDONLY);
    if (fd_read == -1)
    {
        errsv = errno;
        printf("Error occured: %d\n", errsv);
    }
    read(fd_read, contents, sizeof(contents));
    fd_write = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0744);
    if (fd_write == -1)
    {
        errsv = errno;
        printf("Error occured: %d\n", errsv);
    }
    write(fd_write, contents, sizeof(contents));
    close(fd_read);
    close(fd_write);
    return 0;
}

I tested the code using the commands:

cc test.c 
./a.out file1 file2

Here is my file1:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

dummy text
dummy text

After running the code, although file2 contains the text from file1, it also has some gibberish characters. [not keeping this here.]
Why is this so?

>Solution :

You need to call read() and write() in a loop to copy the entire file. read() returns 0 when you reach EOF, or a negative result if there’s an error, then you can end the loop.

read() returns the number of bytes that were read, which may be less than the size of the buffer. You need to use that number when calling write(), otherwise you’ll write extra characters to the output file. These will be unitialized characters on the first iteration, and on other iterations they’ll be left over characters from previous iterations.

int main(int argc, char *argv[])
{
    char contents[1024];
    int fd_read, fd_write;
    fd_read = open(argv[1], O_RDONLY);

    if (fd_read == -1)
    {
        perror("open input file");
        exit(1);
    }
    fd_write = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0744);
    if (fd_write == -1)
    {
        perror("open output file");
        exit(1)
    }

    int n_read;
    while ((n_read = read(fd_read, contents, sizeof(contents))) > 0) {
        write(fd_write, contents, n_read);
    }

    close(fd_read);
    close(fd_write);
    return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading