The function summary of obtaining data from password file in C language programming _c language

Source: Internet
Author: User

C language Getpw () function: Gets the password file data of the specified user
header file:

#include <pwd.h>  #include <sys/types.h>

To define a function:

int GETPW (uid_t uid, char *buf);

Function Description: GETPW () will find the user account data specified in the parameter UID from/ETC/PASSWD, and return 1 if the relevant data is not found.

The returned BUF string is formatted as follows:
Account: Password: User ID (UID): Group identification Code (GID): Full name: root: Shell

Return value: A return of 0 indicates success and returns-1 if an error occurs.

Additional Instructions
1. GETPW () may have potential security problems, please try to replace it with a different function.
2. The system using shadow has extracted the user's password/etc/passwd, so the password obtained using GETPW () will be "X".

Example

#include <pwd.h>
#include <sys/types.h>
main ()
{
  char buffer[80];
  GETPW (0, buffer);
  printf ("%s\n", buffer);
}

Perform:

Root:x:0:0:root:/root:/bin/bash


C language Getpwnam () function: Obtain the specified account data from the password file
header file:

#include <pwd.h>  #include <sys/types.h>

To define a function:

struct passwd * Getpwnam (const char * name);

Function Description: Getpwnam () is used to search the account name specified by the parameter name, and the user's data is returned in passwd structure when found. PASSWD structure please refer to getpwent ().

Return value: Returns the PASSWD structure data, if returned null indicates no data, or an error occurred.

Example

/* Get the ID and root directory of root account */
#include <pwd.h>
#include <sys/types.h>
main ()
{
  struct passwd *user;
  user = Getpwnam ("root");
  printf ("name:%s\n", user->pw_name);
  printf ("uid:%d\n", user->pw_uid);
  printf ("home:%s\n", User->pw_dir);
}

Perform:

Name:root
uid:0
home:/root

C language Getpwuid () function: Get the specified UID data from the password file
header file:

#include <pwd.h>  #include <sys/types.h>

To define a function:

struct passwd * GETPWUID (uid_t uid);

Function Description: Getpwuid () is used to search the parameter uid to specify the user identification code, when found, the user's data to structure return structure please refer to the user's data passwd structure return. PASSWD structure please refer to getpwent ().

Return value: Returns the PASSWD structure data, if returned null indicates no data, or an error occurred.

Example

#include <pwd.h>
#include <sys/types.h>
main ()
{
  struct passwd *user;
  User= Getpwuid (6);
  printf ("name:%s\n", user->pw_name);
  printf ("uid:%d\n", user->pw_uid);
  printf ("home:%s\n", User->pw_dir);
}

Perform:

Name:shutdown
uid:6
home:/sbin

Related Article

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.