A summary of functions for finding and writing utmp files in C language _c language

Source: Internet
Author: User

C language Pututline () function: Write utmp record to file
header file:

#include <utmp.h>

To define a function:

void Pututline (struct utmp *ut);

Function Description: Pututline () is used to log the UTMP structure of the parameter UT to the utmp file. This function first uses Getutid () to get the correct write location, and if no matching record is found, it is added to the end of the utmp file.
Additional note: Requires permission to write to/var/run/utmp

Example

#include <utmp.h>
Main ()
{
  struct utmp ut;
  Ut.ut_type = user_process;
  Ut.ut_pid = Getpid ();
  strcpy (Ut.ut_user, "Kids");
  strcpy (Ut.ut_line, "PTS/1");
  strcpy (Ut.ut_host, "www.gnu.org");
  Pututline (&ut);
}

Perform:

After executing the example, observe the
root pts/0 dec9 19:20 kids pts/1 dec12 10:31
(www.gnu.org)
root pts/2 dec12 13:33 with instruction Who-l

C language Getutline () function: File Lookup function (find specific from utmp file)
header file:

#include <utmp.h>

To define a function:

struct utmp * getutline (struct utmp *ut);

Function Description: Getutline () is used to search for user_process or login_process records from the current read-write location of the utmp file, and Ut_line and Ut->ut_line match. Find the matching records and return the data to the UTMP structure.

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

Example

#include <utmp.h>
Main ()
{
  struct utmp ut, *u;
  strcpy (Ut.ut_line, "PTS/1");
  while (U = Getutline (&ut))
  {
    printf ('%d%s%s '%s \ n], U->ut_type, U->ut_user, U->ut_line, u-> ut_host);
  }

Perform:

7 Root PTS/1

C language Getutid () function: Find a specific record from a utmp file
header file:

#include <utmp.h>

To define a function:

Strcut utmp *getutid (strcut utmp *ut);

Function Description:
Getutid () is used to search for parameters UT-specified records from the current read-write location of the utmp file.
1. If Ut->ut_type is Run_lvl, Boot_time, New_time, old_time one of them will find a record that matches the Ut->ut_type;
2. If Ut->ut_type is init_process, login_process, user_process or dead_process one of them, find records that correspond to ut->ut_id. Find the matching records and return the data to the UTMP structure.

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

Example

#include <utmp.h>
Main ()
{
  struct utmp ut, *u;
  UT.UT_TYPE=RUN_LVL;
  while (U = Getutid (&ut))
  {
    printf ("%d%s%s%s\n", U->ut_type, U->ut_user, U->ut_line, U->ut _host);
  }

Perform:

1 RunLevel-

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.