Skip to content

Instantly share code, notes, and snippets.

@benaryorg
Created May 1, 2015 22:10
Show Gist options
  • Save benaryorg/7882b3941f4740c42b29 to your computer and use it in GitHub Desktop.
Save benaryorg/7882b3941f4740c42b29 to your computer and use it in GitHub Desktop.
mmap cat implementation
#include <stdio.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/mman.h>
int main(int argc,char **argv)
{
assert(argc==2);
int file=open(argv[1],O_RDONLY);
assert(file>0);
struct stat st;
fstat(file,&st);
void *addr=mmap((void *)0xffffffff00000000,st.st_size,PROT_READ,MAP_PRIVATE,file,0);
assert(addr);
fwrite(addr,st.st_size,1,stdout);
munmap(addr,st.st_size);
close(file);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment