linux编写who命令代码
-
编写一个简单的Linux的who命令代码,可以使用以下步骤:
1. 导入所需的头文件和库:
“`c
#include
#include
#include
#include
#include
“`2. 定义一个函数来格式化时间:
“`c
void formatTime(long int sec, char* buffer){
struct tm *timeinfo;
timeinfo = localtime(&sec);
strftime(buffer, 20, “%Y-%m-%d %H:%M”, timeinfo);
}
“`3. 主函数中实现who命令的功能:
“`c
int main(){
struct utmp currentRecord;
int utmpfd;
int reclen = sizeof(currentRecord);
char buffer[20];if ((utmpfd = open(UTMP_FILE, O_RDONLY)) == -1){
perror(UTMP_FILE);
exit(1);
}while (read(utmpfd, ¤tRecord, reclen) == reclen){
if (currentRecord.ut_type == USER_PROCESS){
printf(“%-8.8s”, currentRecord.ut_user);
printf(” “);
printf(“%-8.8s”, currentRecord.ut_line);
printf(” “);
formatTime(currentRecord.ut_tv.tv_sec, buffer);
printf(“%s”, buffer);
printf(“\n”);
}
}close(utmpfd);
return 0;
}
“`上述代码中,首先通过open函数打开utmp文件,然后通过循环读取utmp文件中的记录,筛选出用户进程(ut_type为USER_PROCESS),然后分别输出用户名、终端名和登录时间。关闭utmp文件后,程序结束。
你可以把以上代码保存为一个名为`who.c`的文件,然后使用gcc编译器编译:
“`bash
gcc -o who who.c
“`然后运行该可执行文件:
“`bash
./who
“`程序将输出类似于`who`命令的结果,显示当前连接到系统的用户信息。
2年前 -
在Linux系统中,可以使用C语言编写一个简单的”who”命令的代码。下面是一个参考的实现:
“`c
#include
#include
#include
#include#define SHOWHOST
void show_info(struct utmp *utbufp);
int main()
{
struct utmp current_record;
int utmpfd;
int reclen = sizeof(current_record);if ((utmpfd = open(UTMP_FILE, O_RDONLY)) == -1)
{
perror(UTMP_FILE);
exit(1);
}while (read(utmpfd, ¤t_record, reclen) == reclen)
{
show_info(¤t_record);
}close(utmpfd);
return 0;
}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(” “);
printf(“%12.12s”, ctime(&utbufp->ut_time) + 4);
printf(” “);
#ifdef SHOWHOST
printf(“(%s)”, utbufp->ut_host);
#endif
printf(“\n”);
}
“`这段代码使用了`
`头文件提供的`struct utmp`结构体,以及相关的方法,来读取utmp文件并显示用户登录信息。 可以将这段代码保存为`mywho.c`文件,然后在终端中使用以下命令编译和运行:
“`
gcc -o mywho mywho.c
./mywho
“`这样就可以执行自己编写的”who”命令了。
这段代码通过打开`/var/run/utmp`文件,并循环读取其中的记录。然后,通过`show_info`函数,显示每个用户的用户ID、终端设备、登录时间以及登录的主机名。
注意:在某些Linux发行版中,utmp文件的路径可能会有所不同。你可以使用`man utmp`命令来查看utmp文件在你的系统上的具体位置。另外,编译时需要链接`-lutmp`标志,如:`gcc -o mywho mywho.c -lutmp`。
2年前 -
编写一个简单的who命令代码,可以使用C语言在Linux环境下进行编写。下面是一个示例代码:
“`c
#include
#include
#include
#include
#includevoid showInfo(struct utmp *utbuf)
{
printf(“%-8.8s”, utbuf->ut_user);
printf(” “);
printf(“%-8.8s”, utbuf->ut_line);
printf(” “);
printf(“%12.12s”, ctime(&(utbuf->ut_tv.tv_sec)));
printf(” “);
#ifdef SHOWHOST
printf(“(%s)”, utbuf->ut_host);
#endif
printf(“\n”);
}int main()
{
struct utmp *utbufp;
struct utmp current_record;
int utmpfd;if ((utmpfd = open(UTMP_FILE, O_RDONLY)) == -1)
{
perror(UTMP_FILE);
exit(1);
}while (read(utmpfd, ¤t_record, sizeof(current_record)) == sizeof(current_record))
{
showInfo(¤t_record);
}close(utmpfd);
return 0;
}
“`这个代码使用了utmp.h头文件中定义的结构体和函数。首先,我们打开UTMP_FILE(通常是/var/run/utmp)文件来获取utmp记录。然后使用read函数读取记录,每次读取一个记录的大小。最后通过showInfo函数打印每个记录的信息。
编译和运行该代码需要在Linux环境下进行。可以使用以下命令来编译:
“`
gcc who.c -o who
“`然后使用以下命令运行:
“`
./who
“`这样就可以看到类似于who命令输出的信息了。
2年前