/*gino -/7/Ten +: the: -*/#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <pwd.h>#include <sys/utsname.h>intMain () {/* The GetLogin function returns the logon name associated with the current user. The Getuid function returns the UID associated with the program, which is usually the UID of the user who initiates the program. */ printf("Login user is %s\ n",GetLogin());printf("uid=%d, gid=%d\ n", Getuid (), Getgid ());/ * System files/etc/PASSWD contains a database of user accounts. It consists of rows with one user per line, including: User name, encrypted password, user identifier (UID), group identifier (GID), full name, home directory, and default shell. Data structure of the programming interface: Strcut passwd {char *pw_name; Char *pw_passwd; uid_t Pw_uid; gid_t Pw_gid; Char *pw_gecos; Char *pw_dir; Char *pw_shell; } */struct passwd*PW;/ * Gets the current user information and returns a pointer to the passwd structure. Error returns NULL, and set errno*/PW =Getpwuid(Getuid ());printf("Name=%s, pass=%s, uid=%d, gid=%d, gecos=%s, dir=%s, Shell =%s\ n ", Pw->pw_name, PW->PW_PASSWD, Pw->pw_uid, Pw->pw_gid, Pw->pw_gecos, Pw->pw_dir, PW-&G T;pw_shell);/ * Gets the specified user information and returns a pointer to the passwd structure. Error returns NULL, and set errno*/PW =Getpwnam("Gino");printf("Name=%s, pass=%s, uid=%d, gid=%d, gecos=%s, dir=%s, Shell =%s\ n ", Pw->pw_name, PW->PW_PASSWD, Pw->pw_uid, Pw->pw_gid, Pw->pw_gecos, Pw->pw_dir, PW-&G T;pw_shell); Char computer[ the];/* Get the Local host's standard host name int gethostname (char *name, size_t len); Name: A pointer to the buffer that holds the host name Len: the length of the buffer successfully returns 0, and the failure returns-1. */GetHostName (Computer, the);printf("hostname=%s\ n", computer);/ * struct Utsname {char sysname[]; //Current operating system name Char nodename[]; //Name on the network Char release[]; //Current release level char version[]; //Current release version char machine[]; //Current hardware system type #ifdef _gnu_source char domainname[]; //current domain #endif}; */struct Utsname UTS;/ * Get current kernel name and other information int uname (struct utsname *buf); Successful return 0, Failure returns-1. */Uname (&uts);printf("sysname=%s\ n", uts.sysname);printf("nodename=%s\ n", uts.nodename);printf("release=%s\ n", uts.release);printf("version=%s\ n", uts.version);printf("machine=%s\ n", Uts.machine);return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux c get user information and host information