I find some answers but they all work on Linux only. But how about MacOS? My code is okay on ubuntu so I needn't paste them on. Thank you! ———————————————————————————————————————————— Revise and paste my code.
void unix_error(char* msg)
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(0);
}
void* Mmap(void* start, size_t length, int prot, int flags, int fd, off_t offset)
{
void* ptr;
if((ptr = mmap(start, length, prot, flags , fd, offset)) == ((void*)-1)){
unix_error("mmap");
}
return ptr;
}
int main
{
char *homepath = getenv("HOME");
char *file = "/Desktop/main.c";
strcat(homepath, file);
printf("%s\n", homepath);
int fd = open_file(homepath);
printf("%d\n", fd);
char *ptr = Mmap(NULL, filesize, PROT_READ, MAP_PRIVATE, fd, 0);
write(1, ptr, filesize);
}
get error:
/Users/<username>/Desktop/main.c
3
mmap: Cannot allocate memory