userland: add some random filesystem and random stuff

Some moved from C++, some moved from SO, some I just made up.
This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-10-18 00:00:02 +00:00
parent f6123ac9f1
commit 9afe5355e9
6 changed files with 130 additions and 1 deletions

View File

@@ -0,0 +1,39 @@
/* https://cirosantilli.com/linux-kernel-module-cheat#c */
#define _GNU_SOURCE
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(void) {
char buf[] = { 'a', 'b', 'c', 'd' };
char buf2[] = { 'e', 'f', 'g', 'h' };
int f, ret;
size_t off;
/* Create the temporary file and write to it. */
f = open(".", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
ret = write(f, buf, sizeof(buf));
/* Interactivelly check if anything changed on directory. It hasn't. */
/*puts("hit enter to continue");*/
/*getchar();*/
/* Read from the temporary file, and assert that
* we read the same as we wrote. */
lseek(f, 0, SEEK_SET);
off = 0;
while ((ret = read(f, buf2 + off, sizeof(buf) - off))) {
off += ret;
}
close(f);
assert(!memcmp(buf, buf2, sizeof(buf)));
/* Assert that the file is gone now that we removed it. */
return EXIT_SUCCESS;
}