用I/O标准库实现who命令
2009-04-28 10:56
288 查看
linux的who命令实际上是能通过读取utmp文件来获得相关的信息
然后按格式输出来,很简单,主要用来了utmp结构,下面是他的描述
struct utmp {
short ut_type; /* type of login */
pid_t ut_pid; /* PID of login process */
char ut_line[UT_LINESIZE]; /* device name of tty - "/dev/" */
char ut_id[4]; /* init id or abbrev. ttyname */
char ut_user[UT_NAMESIZE]; /* user name */
char ut_host[UT_HOSTSIZE]; /* hostname for remote login */
struct exit_status ut_exit; /* The exit status of a process
marked as DEAD_PROCESS */
/* The ut_session and ut_tv fields must be the same size when
compiled 32- and 64-bit. This allows data files and shared
memory to be shared between 32- and 64-bit applications */
#if __WORDSIZE == 64 && defined __WORDSIZE_COMPAT32
int32_t ut_session; /* Session ID, used for windowing */
struct {
int32_t tv_sec; /* Seconds */
int32_t tv_usec; /* Microseconds */
} ut_tv; /* Time entry was made */
#else
long int ut_session; /* Session ID, used for windowing */
struct timeval ut_tv; /* Time entry was made */
#endif
int32_t ut_addr_v6[4]; /* IP address of remote host */
char __unused[20]; /* Reserved for future use */
};
具体的可以查看utmp.h文件,实现就比较简单了,我就贴个源码算了,呵呵
#include <stdio.h>
#include <utmp.h>
#include <time.h>
void showtime(long);
void show_info(struct utmp*);
int
main ( )
{
struct utmp utbuf;
FILE* fd;
int rdnumber;
if ((fd=fopen(UTMP_FILE,"r"))==NULL )
{
perror(UTMP_FILE);
}
while (fread(&utbuf,sizeof(utbuf),1,fd)==1 )
show_info(&utbuf);
fclose(fd);
return 0;
} /* ----- end of function main ----- */
void show_info ( struct utmp* utbufp )
{
if ( utbufp->ut_type!=USER_PROCESS )
return;
printf("% -8.8s",utbufp->ut_name);
printf(" ");
printf("% -8.8s",utbufp->ut_line);
printf(" ");
showtime(utbufp->ut_time);
#ifdef SHOWHOST
if(utbufp->ut_host[0]!='/0')
printf("(%s)",utbufp->ut_host);
#endif
printf("/n");
} /* ----- end of function show_info ----- */
void showtime ( long timeval )
{
char* cp;
cp=ctime(&timeeval);
printf("%12.12s",cp+4);
} /* ----- end of function showtime ----- */
然后按格式输出来,很简单,主要用来了utmp结构,下面是他的描述
struct utmp {
short ut_type; /* type of login */
pid_t ut_pid; /* PID of login process */
char ut_line[UT_LINESIZE]; /* device name of tty - "/dev/" */
char ut_id[4]; /* init id or abbrev. ttyname */
char ut_user[UT_NAMESIZE]; /* user name */
char ut_host[UT_HOSTSIZE]; /* hostname for remote login */
struct exit_status ut_exit; /* The exit status of a process
marked as DEAD_PROCESS */
/* The ut_session and ut_tv fields must be the same size when
compiled 32- and 64-bit. This allows data files and shared
memory to be shared between 32- and 64-bit applications */
#if __WORDSIZE == 64 && defined __WORDSIZE_COMPAT32
int32_t ut_session; /* Session ID, used for windowing */
struct {
int32_t tv_sec; /* Seconds */
int32_t tv_usec; /* Microseconds */
} ut_tv; /* Time entry was made */
#else
long int ut_session; /* Session ID, used for windowing */
struct timeval ut_tv; /* Time entry was made */
#endif
int32_t ut_addr_v6[4]; /* IP address of remote host */
char __unused[20]; /* Reserved for future use */
};
具体的可以查看utmp.h文件,实现就比较简单了,我就贴个源码算了,呵呵
#include <stdio.h>
#include <utmp.h>
#include <time.h>
void showtime(long);
void show_info(struct utmp*);
int
main ( )
{
struct utmp utbuf;
FILE* fd;
int rdnumber;
if ((fd=fopen(UTMP_FILE,"r"))==NULL )
{
perror(UTMP_FILE);
}
while (fread(&utbuf,sizeof(utbuf),1,fd)==1 )
show_info(&utbuf);
fclose(fd);
return 0;
} /* ----- end of function main ----- */
void show_info ( struct utmp* utbufp )
{
if ( utbufp->ut_type!=USER_PROCESS )
return;
printf("% -8.8s",utbufp->ut_name);
printf(" ");
printf("% -8.8s",utbufp->ut_line);
printf(" ");
showtime(utbufp->ut_time);
#ifdef SHOWHOST
if(utbufp->ut_host[0]!='/0')
printf("(%s)",utbufp->ut_host);
#endif
printf("/n");
} /* ----- end of function show_info ----- */
void showtime ( long timeval )
{
char* cp;
cp=ctime(&timeeval);
printf("%12.12s",cp+4);
} /* ----- end of function showtime ----- */
相关文章推荐
- 查看联机帮助与who命令的实现
- Linux命令简单实现 -- whoami
- C语言编程实现Linux命令——who
- 实现Linux的whoami命令
- Linux下who命令的实现
- who命令实现
- #写一个脚本实现如下功能: 输入一个数字,然后运行对应的>一个命令。显示命令如下:*cmd meau** 1—date 2–ls 3–who 4–pwd #当输入1时,会运行date, 输入2
- Linux系统编程第一课: CentOS7下who命令的实现
- linux编程实践3(实现who命令)
- linux命令实现:whoami
- who命令的实现
- Linux C编程的小例子——实现who命令(第二版)
- linux whoami cp mv mesg 命令 c语言实现(部分功能)
- 【C语言】实现Linux下的who命令
- Linux下who命令之C语言实现
- 实现who命令
- who 命令实现
- unix/linux who命令的实现
- ★实验9-3 1. 用不同的方案实现两个命令的连接,例如 “who|sort”、“ps aux | grep init” 源程序: #include <stdio.h> #include <sys
- Linux C编程的一个小例子——实现一个简单的who命令(第一版)