Documentation
Language Guide
C
Tutorials
File Copy

File Copying with WASIX

This sample project demonstrates how file-copying works using a C program compiled with WASIX.

Prerequisites

The project requires the following tools to be installed on your system:

Start a new project

Create a new directory for your project:

$ mkdir wasix-file-copy
$ cd wasix-file-copy

Make a new file called file-copy.c:

$ touch file-copy.c

Link the SysRoot

ℹ️

You need to link the SysRoot to your project. The SysRoot is a directory that contains the WASI libc headers and libraries. The SysRoot is located in the Wasix-libc binary or you can compile it yourself.

Writing the Application

Basic Application Setup

We want to write a program that takes in two arguments from the command line, the source file and the destination file, and copies the contents of the source file to the destination file.

Let's write code for the above:

file-copy.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
 
int main(int argc, char **argv)
{
    /*
     * Declare variables n and m to be of type ssize_t.
     */
    // ssize_t is a signed version of size_t
    ssize_t n, m;
    char buf[BUFSIZ];
 
    /*
     * If the number of arguments is not 3, print an error message and exit.
     */
    if (argc != 3)
    {
        fprintf(stderr, "usage: %s <from> <to>\n", argv[0]);
        exit(1);
    }
 
    /*
     * Open the input file for reading.
     * If the open fails, print an error message and exit.
     */
    int in = open(argv[1], O_RDONLY);
    if (in < 0)
    {
        fprintf(stderr, "error opening input %s: %s\n", argv[1], strerror(errno));
        exit(1);
    }
 
    /*
     * Open the output file for writing.
     * If the open fails, print an error message and exit.
     */
    int out = open(argv[2], O_WRONLY | O_CREAT, 0660);
    if (out < 0)
    {
        fprintf(stderr, "error opening output %s: %s\n", argv[2], strerror(errno));
        exit(1);
    }
 
    /*
     * Read from the input file and write to the output file until the read fails.
     * If the read fails, print an error message and exit.
     */
    while ((n = read(in, buf, BUFSIZ)) > 0) // read the input file into the buffer
    {
        char *ptr = buf; // create a ptr that points to the buffer
        while (n > 0)    // while there is still data to be read
        {
            m = write(out, ptr, (size_t)n); // write the data from the buffer to the output file
 
            /*
             * If the write fails, print an error message and exit.
             */
            if (m < 0)
            {
                fprintf(stderr, "write error: %s\n", strerror(errno));
                exit(1);
            }
            n -= m;   // decrement the number of bytes left to read
            ptr += m; // increment the pointer to the buffer
        }
    }
 
    /*
     * n should be equal to 0 if the read was successful.
     * so if n is less than 0, print an error message and exit.
     */
    if (n < 0)
    {
        fprintf(stderr, "read error: %s\n", strerror(errno));
        exit(1);
    }
 
    return EXIT_SUCCESS;
}

Compiling the Application

Let's compile the application with clang:

$ clang -o file-copy file-copy.c

Running the Application

We can run the application with the following command:

$ ./file-copy
usage: ./file-copy <from> <to>

Let's make two files, input.txt and output.txt:

$ touch input.txt
$ echo "Hello, World!" > input.txt
$ touch output.txt

Now, let's run the application:

$ ./file-copy input.txt output.txt

Let's check the contents of output.txt:

$ cat output.txt
Hello, World!

Let's try to build this example with WASIX.

$ /path/to/wasix-sdk/clang file-copy.c --target="wasm32-wasi" -o file-copy.wasm

It's compiling! Now, let's try to run it:

Don't forget to clean the output file before running the application.

$ wasmer run file-copy.wasm input.txt output.txt
$ cat output.txt
Hello, World!

It works!

Conclusion

In this tutorial we learned:

  • How to copy a file using C
  • Compiling a C program with WASIX
wasix-libc/examples/file-copy.c