Skip to content

Instantly share code, notes, and snippets.

@alsamitech
Last active April 30, 2021 22:54
Show Gist options
  • Select an option

  • Save alsamitech/1a9bda214d7dfe5d7147d64dbc8b4662 to your computer and use it in GitHub Desktop.

Select an option

Save alsamitech/1a9bda214d7dfe5d7147d64dbc8b4662 to your computer and use it in GitHub Desktop.
Requires <unistd.h>, <fcntl.h>. It is recommended that you contain the result as an arr_t or use a wrapper for that.
char* unix_read_file(char* filenm, long unsigned int* len){
int fd=open(filenm, O_RDONLY);
char* buf=0x0;
if(fd>0){
struct stat stats;
fstat(fd, &stats);
buf=malloc(stats.st_size+1);
if(buf){
read(fd, buf, stats.st_size);
buf[stats.st_size]=0x0;
}
close(fd);
}
return buf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment