Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicklasring/d8370e8ff05fb55aa44da6f7b071ea17 to your computer and use it in GitHub Desktop.
Save nicklasring/d8370e8ff05fb55aa44da6f7b071ea17 to your computer and use it in GitHub Desktop.
char * get_current_user() {
char user_name[64] = {0};
if( getlogin_r(user_name, sizeof(user_name)-1) != 0 ) {
printf("Error fetching current user");
exit(1);
}
// Allocate memory on heap for user_name and return pointer
char * username_heap_alloc = malloc( strlen(user_name) + 1 ); // Nullterminator + 1
if( username_heap_alloc ) {
memcpy( username_heap_alloc, user_name, strlen(user_name) + 1 );
}
return username_heap_alloc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment