Files
linux-kernel-module-cheat/userland/linux/open_o_tmpfile.c
Ciro Santilli 六四事件 法轮功 9afe5355e9 userland: add some random filesystem and random stuff
Some moved from C++, some moved from SO, some I just made up.
2019-10-18 00:00:02 +00:00

40 lines
1016 B
C

/* 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;
}