Native API - Linux
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
// Get the user ID of the calling process
uid_t uid = getuid();
// Retrieve the passwd struct for the given user ID
struct passwd *pw = getpwuid(uid);
// Check if the passwd struct is valid
if (pw == NULL) {
perror("getpwuid");
exit(EXIT_FAILURE);
}
// Print the user information
printf("User information:\n");
printf("Username: %s\n", pw->pw_name);
printf("User ID: %u\n", pw->pw_uid);
printf("Group ID: %u\n", pw->pw_gid);
printf("Home directory: %s\n", pw->pw_dir);
printf("Shell: %s\n", pw->pw_shell);
return 0;
}
Last updated