Skip to content

Instantly share code, notes, and snippets.

@sergioperez
Created April 9, 2018 14:15
Show Gist options
  • Save sergioperez/919238b7da1825ee239a0985d672036b to your computer and use it in GitHub Desktop.
Save sergioperez/919238b7da1825ee239a0985d672036b to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
int main()
{
register struct passwd *pw;
register uid_t uid;
uid = geteuid(); //Get userID
pw = getpwuid(uid); //Get userID data
if(pw)
{
//Print username
printf("Executed as {%s}\n.", pw->pw_name);
exit(EXIT_SUCCESS);
}
fprintf(stderr, "cannot find username for UID %u/n", (unsigned)uid);
exit(EXIT_FAILURE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment