Scripting‎ > ‎C examples‎ > ‎

Universial linux copy example in c

Thank you for visiting this page, this page has been update in another link A universal Linux copy example in C
Here is an example of linux file read/write example, an simplfied cp or dd. Shows you how use system calls(open(), clock(), read(), write() etc..) in c on linux.
Linux treats all devices as 'file', so this small version program can also be used for other purposes, for example: use some special device like /dev/zero or /dev/tty.

Here is the code first, then will show some examples.
# cat ucp.c
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>

#ifndef BUF_SIZE
#define BUF_SIZE 1024
#endif
#ifndef COUNT
#define COUNT 104857600
#endif

main(int argc, char *argv[])
{
    int inputFd, outputFd, openFlags;
    long int cnt,t_size;
    clock_t t1,t2;
    mode_t filePerms;
    ssize_t numRead;
    char buf[BUF_SIZE];

    t1=clock();
    if (argc != 3 || strcmp(argv[1], "--help") == 0) {
        printf("%s old-file new-file\n",argv[0]);
        return 1;
    }

    /* Open input and output files */

    inputFd = open(argv[1],O_RDONLY);
    if (inputFd == -1) {
        printf("opening file %s",argv[1]);
        return 1 ;
    }

    openFlags = O_CREAT | O_WRONLY |O_TRUNC;
    filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
            S_IROTH | S_IWOTH; /*rw-rw-rw-*/
    outputFd = open(argv[2],openFlags, filePerms);
    if (outputFd == -1) {
        printf("opening file %s", argv[2]);
        return 1 ;
    }
   
    /*transfer data */
    cnt=0;
    t2=clock();
    while ((numRead = read(inputFd, buf, BUF_SIZE)) >0 )
    {
        if(write(outputFd,buf,numRead) != numRead)
        {
            perror("couldn't write whole buffer");
            return 1;
        }
        if(++cnt >= COUNT) {
            break;
        }
    }
    if(numRead == -1) {
        perror("read");
        return 1 ;
    }

    if(close(inputFd) == -1) {
        perror("close input");
        return 1 ;
    }
    if(close(outputFd) == -1) {
        perror("close output");
        return 1 ;
    }

    t2=clock();
    float t_diff = ((float)t2 - (float)t1) / 1000000.0F ;
    t_size = (cnt * (long int)BUF_SIZE) ;
    float speed = (t_size / t_diff )/1000000.0F;

    printf("record size %d, records %d\n",BUF_SIZE,cnt);
    printf("%ld bytes copied, %0.2f, %0.2f MB/sec\n",t_size,t_diff,speed);

    return 0;
}

In the code, I have a file size cap setting for particular device like /dev/zero, of course you can make a small change to use arguments to pass these values. Now the value is set to 100GB. Also, I have buffer size setting 1024, you can also change it.

Compile

cc ucp.c -o ucp

Copy a small file

# ./ucp ./ucp.c testfile
record size 1024, records 2
2048 bytes copied, inf, 0.00 MB/sec

Copy a big file from /dev/zero

# ./copy /dev/zero /lun/testfile
record size 1024, records 104857600, time 253.01sec
107374182400 bytes copied, 424.39 MB/sec

Copy to special device /dev/tty

it is your current terminal, it will print the ucp.c code to your screen.
$./ucp ./ucp.c /dev/tty
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>

#ifndef BUF_SIZE
#define BUF_SIZE 1024
#endif
#ifndef COUNT
#define COUNT 104857600
#endif
...




Comments