blob: 6e7710c2d4ec8e68356743422483cc5bcef302d2 [file] [log] [blame]
Eric Andersen00a6a752002-04-26 23:53:10 +00001/* vi: set sw=4 ts=4: */
2/*----------------------------------------------------------------------
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003 * Mini who is used to display user name, login time,
Eric Andersen00a6a752002-04-26 23:53:10 +00004 * idle time and host name.
5 *
6 * Author: Da Chen <dchen@ayrnetworks.com>
7 *
8 * This is a free document; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation:
11 * http://www.gnu.org/copyleft/gpl.html
12 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000013 * Copyright (c) 2002 AYR Networks, Inc.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000014 *
15 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
16 *
Eric Andersen00a6a752002-04-26 23:53:10 +000017 *----------------------------------------------------------------------
18 */
19
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000020#include "libbb.h"
Rob Landley7a260f02006-06-19 03:20:03 +000021#include <utmp.h>
22#include <time.h>
Eric Andersen00a6a752002-04-26 23:53:10 +000023
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000024static void idle_string(char *str6, time_t t)
Rob Landleye01d7462006-03-12 19:26:01 +000025{
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000026 t = time(NULL) - t;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000027
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000028 /*if (t < 60) {
29 str6[0] = '.';
30 str6[1] = '\0';
31 return;
32 }*/
33 if (t >= 0 && t < (24 * 60 * 60)) {
34 sprintf(str6, "%02d:%02d",
35 (int) (t / (60 * 60)),
36 (int) ((t % (60 * 60)) / 60));
37 return;
Rob Landleye01d7462006-03-12 19:26:01 +000038 }
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000039 strcpy(str6, "old");
Rob Landleye01d7462006-03-12 19:26:01 +000040}
41
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000042int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000043int who_main(int argc, char **argv)
Eric Andersen00a6a752002-04-26 23:53:10 +000044{
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000045 char str6[6];
Rob Landleye01d7462006-03-12 19:26:01 +000046 struct utmp *ut;
47 struct stat st;
48 char *name;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000049
Rob Landleye01d7462006-03-12 19:26:01 +000050 if (argc > 1) {
51 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000052 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000053
Rob Landleye01d7462006-03-12 19:26:01 +000054 setutent();
55 printf("USER TTY IDLE TIME HOST\n");
56 while ((ut = getutent()) != NULL) {
57 if (ut->ut_user[0] && ut->ut_type == USER_PROCESS) {
Rob Landley7a260f02006-06-19 03:20:03 +000058 time_t thyme = ut->ut_tv.tv_sec;
59
Rob Landleye01d7462006-03-12 19:26:01 +000060 /* ut->ut_line is device name of tty - "/dev/" */
61 name = concat_path_file("/dev", ut->ut_line);
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000062 str6[0] = '?';
63 str6[1] = '\0';
64 if (stat(name, &st) == 0)
65 idle_string(str6, st.st_atime);
66 printf("%-10s %-8s %-9s %-14.14s %s\n",
67 ut->ut_user, ut->ut_line, str6,
68 ctime(&thyme) + 4, ut->ut_host);
69 if (ENABLE_FEATURE_CLEAN_UP)
70 free(name);
Rob Landleye01d7462006-03-12 19:26:01 +000071 }
72 }
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000073 if (ENABLE_FEATURE_CLEAN_UP)
74 endutent();
Rob Landleye01d7462006-03-12 19:26:01 +000075 return 0;
Eric Andersen00a6a752002-04-26 23:53:10 +000076}