Documentation
Language Guide
C
Tutorials
Readdir

ReadDir with WASIX

This sample project demonstrates how to read directories in 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-readdir
$ cd wasix-readdir

Make a new file called readdir.c:

$ touch readdir.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 simple C program that demonstrates how to use the opendir and readdir functions to read the contents of a directory. It takes a single command-line argument, which is the path to the directory to read.

Let's write code for the above:

readdir.c
#include <stdio.h>
#include <string.h>
#include <sys/random.h>
#include <unistd.h>
#include <dirent.h> // header file for functions for accessing directories
 
/**
 * Recursive function to print the directory entries
 */
int print_dir(char *name)
{
 
    /**
     * Return here or else the program will go into infinite recursion
     */
    if (strcmp(name, "..") == 0 || strcmp(name, ".") == 0 || name == NULL)
    {
        return 1;
    }
    DIR *d = opendir(name); // open the directory and return a pointer to the directory stream
    for (;;)
    {
        struct dirent *ent = readdir(d); // read the next directory entry from the directory stream
        if (ent == NULL)
        {
            break;
        }
        if (strcmp(ent->d_name, "..") == 0 || strcmp(ent->d_name, ".") == 0)
        {
            continue;
        }
        printf("dir entry name - '%s'\n", ent->d_name);
 
        /**
         * If the entry is a directory then recursively call the function
         */
        print_dir(ent->d_name);
    }
    return 0;
}
 
int main(int argc, char **argv)
{
    /**
     * If the length of arguments is not equal to 2 then print the usage and return
     */
    if (argc != 2)
    {
        fprintf(stderr, "usage: %s <dir>\n", argv[0]);
        return 1;
    }
 
    char *name = argv[1]; // name of the directory
    printf("main: just before opening dir '%s'\n", name);
    fflush(stdout);
    // call the recursive function
    print_dir(name);
    return 0;
}

The main function takes the directory name as a command-line argument and calls the print_dir function to print the contents of the directory.

Compiling the Application

Let's compile the application with WASIX:

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

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

$ wasmer run readdir.wasm /
main: just before opening dir '/'
dir entry name - '.app'
dir entry name - '.private'
dir entry name - 'bin'
dir entry name - 'dev'
dir entry name - 'etc'
dir entry name - 'tmp'

Yaay, It works!

Conclusion

In this tutorial we learned:

  • How to write a simple C program that uses the opendir and readdir functions to read the contents of a directory
  • Compiling a C program with WASIX
wasix-libc/examples/readdir_tree.c