Linux c get user information and host information

Source: Internet
Author: User

/*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-&GT;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-&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.