Skip to content

Instantly share code, notes, and snippets.

@worthless443
Created September 2, 2022 01:22
Show Gist options
  • Save worthless443/642331ef12e1906ae879f08c62fe5c3b to your computer and use it in GitHub Desktop.
Save worthless443/642331ef12e1906ae879f08c62fe5c3b to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
#include<sys/mman.h>
#include<string.h>
#include<assert.h>
#define GLOBAL_FILE 0x4000000
void start_file(const char *fname) {
void *fp = mmap((void*)GLOBAL_FILE, sizeof(FILE), PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,0,0);
FILE *f = fopen(fname, "w");
assert(*(int*)fp!=-1);
memcpy(fp, f, sizeof(FILE));
}
void write_file(const char *what) {
FILE *f = (FILE*)(void*)GLOBAL_FILE;
fwrite(what,1,strlen(what),f);
}
int main() {
start_file("file.txt");
write_file("content");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment