System Programming: Basic Copy program in C using low-level IO

In this article, we are going to start learning system programming by first of all creating a copy program like cp using low-level IO functions and not the standard IO functions. Now first let’s see what is system programming and what is low-level IO functions in C.

System Programming: Basic Copy program in C using low-level IO

System Programming:

System programming aims at producing software that is used by other software instead of directly used by users. In system programming, mostly low-level languages like C are used. System programmers are supposed to have an in-depth understanding of kernel and hardware.

Low-level IO functions in C:

When you generally write your program in C. You end up using functions like fopen, fclose, etc. These functions are wrapper over low-level IO functions that are open, close, read and write. In this article, we are going to use these functions to write our copy program.

Lets first see the difference between the two. If you using a Linux system you can see their usage by using the below commands.

For open function: man 2 open, you will see something like below

       #include <sys/types.h>
       #include <sys/stat.h>
       #include <fcntl.h>

       int open(const char *pathname, int flags);
       int open(const char *pathname, int flags, mode_t mode);

       int creat(const char *pathname, mode_t mode);

       int openat(int dirfd, const char *pathname, int flags);
       int openat(int dirfd, const char *pathname, int flags, mode_t mode);

You can see here usage of this command as open(char “pathname”, flags), these flags are the ones that define which mode to open it in. Similarly, there are man pages for read, close and write. You can also search for their details online. I have provided the link on the name of the calls.

Now let’s write code to accomplish our task.

For fopen function: man fopen and you will see the syntax to use it. But remember it is a wrapper over open and not a low-level call.

#include <fcntl.h>
#include <stdlib.h>
#define BUFFERSIZE 10000

void main(){
    int filein, fileout;
    char buffer[BUFFERSIZE];
    int count;

    if ((filein == open("source_file", O_RDONLY)) < 0){
        perror("source_file");
        exit(1);
    }

    if ((fileout = open("destination_file", O_WRONLY| O_CREAT, 0644)) < 0){
        perror("destination_file");
        exit(2);
    }

    while ((count = read(filein, buffer, BUFFERSIZE)) > 0)
        write(fileout, buffer, count);
    close(filein);
    close(fileout);
}

Now you have to compile this code using GCC. Use the command below.

gcc -o copy copy.c 

Now you can execute this using below

./copy

Code walkthrough

Let’s take a look at the code now. First of all, we included the libraries that are required. Next, we defined the buffer size(BUFFERSIZE) in which we will read the data. We will read 1000 bytes in each iteration. Then starts our main program.

Next, we defined two variables: filein and fileout to keep the file descriptors for input and output file. Then a buffer variable to keep the data. Then we try to open the file and get the file descriptor with 0_RDONLY which means read-only, we check if the value of file descriptor is not a negative integer. If it is a negative integer it means it failed to get the file descriptor.

Next, we do the same for the file descriptor for output file with O_WRONLY and O_CREAT which means write-only and create if not exists. Once these two descriptor is there. You can now start reading the file in the buffer and writing it to the destination file. Count variable writes count bytes from buffer. The whole look will break once it gets 0, which is when the file is read completely.

Now you can see the content of your destination file to compare.

This was a small article on how to write a basic Copy program in C using low-level function calls.

If you like the article please share.


Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.