diff options
Diffstat (limited to 'tests/fcntl-open/src.c')
-rw-r--r-- | tests/fcntl-open/src.c | 64 |
1 files changed, 51 insertions, 13 deletions
diff --git a/tests/fcntl-open/src.c b/tests/fcntl-open/src.c index 52d2e7e4..bd52dd3f 100644 --- a/tests/fcntl-open/src.c +++ b/tests/fcntl-open/src.c @@ -1,13 +1,45 @@ -#include <stdio.h> +#include <assert.h> #include <errno.h> -#include <sys/stat.h> #include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> #include <string.h> +#include <unistd.h> +#include <sys/stat.h> -int main() { +char nonexistent_name[] = "noexist-##"; + +void create_file(const char *path, const char *buffer, int mode) { + int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode); + assert(fd >= 0); + + int err = write(fd, buffer, sizeof(char) * strlen(buffer)); + assert(err == (sizeof(char) * strlen(buffer))); + + close(fd); +} + +void setup() { + create_file("test-file", "abcdef", 0777); + mkdir("test-folder", 0777); +} + +void cleanup() { + unlink("test-file"); + rmdir("test-folder"); + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 16; j++) { + nonexistent_name[8] = 'a' + i; + nonexistent_name[9] = 'a' + j; + unlink(nonexistent_name); + } + } + unlink("creat-me"); +} + +void test() { struct stat s; int modes[] = {O_RDONLY, O_WRONLY, O_RDWR}; - char nonexistent_name[] = "/noexist-##"; for (int i = 0; i < 3; i++) { for (int j = 0; j < 16; j++) { @@ -18,25 +50,25 @@ int main() { if (j & 0x8) flags |= O_APPEND; printf("EXISTING FILE %d,%d\n", i, j); - printf("success: %d\n", open("/test-file", flags, 0777) != -1); + printf("success: %d\n", open("test-file", flags, 0777) != -1); printf("errno: %d\n", errno); - stat("/test-file", &s); + stat("test-file", &s); printf("st_mode: 0%o\n", s.st_mode & 037777777000); memset(&s, 0, sizeof s); printf("\n"); errno = 0; printf("EXISTING FOLDER %d,%d\n", i, j); - printf("success: %d\n", open("/test-folder", flags, 0777) != -1); + printf("success: %d\n", open("test-folder", flags, 0777) != -1); printf("errno: %d\n", errno); - stat("/test-folder", &s); + stat("test-folder", &s); printf("st_mode: 0%o\n", s.st_mode & 037777777000); memset(&s, 0, sizeof s); printf("\n"); errno = 0; - nonexistent_name[9] = 'a' + i; - nonexistent_name[10] = 'a' + j; + nonexistent_name[8] = 'a' + i; + nonexistent_name[9] = 'a' + j; printf("NON-EXISTING %d,%d\n", i, j); printf("success: %d\n", open(nonexistent_name, flags, 0777) != -1); printf("errno: %d\n", errno); @@ -49,8 +81,14 @@ int main() { } printf("CREAT\n"); - printf("success: %d\n", creat("/creat-me", 0777) != -1); + printf("success: %d\n", creat("creat-me", 0777) != -1); printf("errno: %d\n", errno); - - return 0; } + +int main() { + atexit(cleanup); + signal(SIGABRT, cleanup); + setup(); + test(); + return EXIT_SUCCESS; +}
\ No newline at end of file |