C language Pututline () function: Write utmp record to file
header file:
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:
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:
C language Getutid () function: Find a specific record from a utmp file
header file:
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: