Add a basic test for opendir, readdir, and closedir. (#266)

pull/267/head
Dan Gohman 2 years ago committed by GitHub
parent 36553bb62d
commit cecfcf7f91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,72 @@
#include <fcntl.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <sys/stat.h>
#include <unistd.h>
#define perror_and_exit(message) \
do { \
perror(message); \
return EXIT_FAILURE; \
} while (0)
#define OFFSET 10726
#define LENGTH 143
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "usage: %s <dir>\n", argv[0]);
return EXIT_FAILURE;
}
DIR *dir = opendir(argv[1]);
if (dir == NULL) {
perror_and_exit("opendir");
}
int count = 0;
int zeros = 0;
errno = 0;
for (;; count += 1) {
struct dirent *ent = readdir(dir);
if (ent == NULL) {
if (errno == 0) {
break;
}
perror_and_exit("readdir");
}
if (strcmp(ent->d_name, "file.md") == 0) {
assert(ent->d_type == DT_REG);
} else if (strcmp(ent->d_name, "dir") == 0) {
assert(ent->d_type == DT_DIR);
} else if (strcmp(ent->d_name, "file-symlink") == 0) {
assert(ent->d_type == DT_LNK);
} else if (strcmp(ent->d_name, "dir-symlink") == 0) {
assert(ent->d_type == DT_LNK);
} else if (strcmp(ent->d_name, ".") == 0) {
assert(ent->d_type == DT_DIR);
} else if (strcmp(ent->d_name, "..") == 0) {
assert(ent->d_type == DT_DIR);
} else {
assert(false);
}
if (ent->d_ino == 0) {
zeros += 1;
}
}
assert(count == 6);
assert(zeros <= 1);
if (closedir(dir) != 0)
perror_and_exit("closedir");
return EXIT_SUCCESS;
}

@ -0,0 +1 @@
This is a file in a directory.

@ -0,0 +1 @@
# This is a top-level file

@ -10,7 +10,7 @@ set -ueo pipefail
# location. Alternatively, exporting $CC and $CXX allow more flexibility. e.g:
#
# export CXX="<wasi-sdk>/bin/clang++ --sysroot <wasi-sdk>/share/wasi-sysroot"
# export CCC="<wasi-sdk>/bin/clang --sysroot <wasi-sdk>/share/wasi-sysroot"
# export CC="<wasi-sdk>/bin/clang --sysroot <wasi-sdk>/share/wasi-sysroot"
#
# Determine the wasm runtime to use, if one is provided.

Loading…
Cancel
Save