forked from haripradhan/UnixProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mywhoami.c
44 lines (42 loc) · 1.09 KB
/
mywhoami.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
///Name: Hari Pradhan
///Date: 06/10/2013
///Purpose: This program displays the following information.
/// 1. Full Name (assume the comment field of the password file contains the full name)
/// 2. Login Name
/// 3. UID
/// 4. GID
/// 5. Home Directory
/// 6. Default Shell
/// 7. Host Name
/// 8. Machine
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/utsname.h>
int main(int argc, char* argv[])
{
struct passwd* userInfo;
struct utsname sysInfo;
char hostname[20];
uid_t userid = getuid();
userInfo = getpwuid(userid);
if(uname(&sysInfo) == -1){
perror("uname\n");
exit(1);
}
if(gethostname(hostname, sizeof(hostname)) == -1){
perror("gethostname\n");
exit(1);
}
printf("Full Name: %s\n",userInfo->pw_gecos);
printf("Login Name: %s\n",userInfo->pw_name);
printf("UID: %d\n",userid);
printf("GID: %d\n",userInfo->pw_gid);
printf("Home Directory: %s\n",userInfo->pw_dir);
printf("Default Shell: %s\n",userInfo->pw_shell);
printf("Host Name: %s\n",hostname);
printf("Machine: %s\n\n",sysInfo.machine);
exit(0);
}