#define _DEFAULT_SOURCE
#define _BSD_SOURCE
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>

void
dir_recurse (DIR *parent, int level)
{
    struct dirent *ent;
    DIR *child;
    int fd;

    while ((ent = readdir(parent)) != NULL) {
        if ((strcmp(ent->d_name, ".") == 0) ||
            (strcmp(ent->d_name, "..") == 0)) {
            continue;
        }
        if (ent->d_type == DT_DIR) {
            printf("%*s%s/\n", level, "", ent->d_name);
            fd = openat(dirfd(parent), ent->d_name, O_RDONLY | O_DIRECTORY);
            if (fd != -1) {
                child = fdopendir(fd);
                dir_recurse(child, level + 1);
                closedir(child);
            } else {
                perror("open");
            }
        } else {
            printf("%*s%s\n", level, "", ent->d_name);
        }
    }
}

int
main (int argc, char *argv)
{
    DIR *root;

    root = opendir("..");
    dir_recurse(root, 0);
    closedir(root);

    return 0;
}