blob: a206ec54b580670a5d00f50339a11d12878da590 [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 */
Bernhard Reutner-Fischere8979882007-11-16 11:52:42 +000019/* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -H, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */
Eric Andersen00a6a752002-04-26 23:53:10 +000020
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000021#include "libbb.h"
Rob Landley7a260f02006-06-19 03:20:03 +000022#include <utmp.h>
23#include <time.h>
Eric Andersen00a6a752002-04-26 23:53:10 +000024
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000025static void idle_string(char *str6, time_t t)
Rob Landleye01d7462006-03-12 19:26:01 +000026{
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000027 t = time(NULL) - t;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000028
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000029 /*if (t < 60) {
30 str6[0] = '.';
31 str6[1] = '\0';
32 return;
33 }*/
34 if (t >= 0 && t < (24 * 60 * 60)) {
35 sprintf(str6, "%02d:%02d",
36 (int) (t / (60 * 60)),
37 (int) ((t % (60 * 60)) / 60));
38 return;
Rob Landleye01d7462006-03-12 19:26:01 +000039 }
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000040 strcpy(str6, "old");
Rob Landleye01d7462006-03-12 19:26:01 +000041}
42
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000043int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +000044int who_main(int argc ATTRIBUTE_UNUSED, char **argv)
Eric Andersen00a6a752002-04-26 23:53:10 +000045{
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000046 char str6[6];
Rob Landleye01d7462006-03-12 19:26:01 +000047 struct utmp *ut;
48 struct stat st;
49 char *name;
Denis Vlasenko01cd9572007-11-16 05:24:43 +000050 unsigned opt;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000051
Denis Vlasenko01cd9572007-11-16 05:24:43 +000052 opt_complementary = "=0";
53 opt = getopt32(argv, "a");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000054
Rob Landleye01d7462006-03-12 19:26:01 +000055 setutent();
Denis Vlasenkoe5569cb2007-11-11 06:35:41 +000056 printf("USER TTY IDLE TIME HOST\n");
Rob Landleye01d7462006-03-12 19:26:01 +000057 while ((ut = getutent()) != NULL) {
Denis Vlasenko01cd9572007-11-16 05:24:43 +000058 if (ut->ut_user[0] && (opt || ut->ut_type == USER_PROCESS)) {
Rob Landleye01d7462006-03-12 19:26:01 +000059 /* ut->ut_line is device name of tty - "/dev/" */
60 name = concat_path_file("/dev", ut->ut_line);
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000061 str6[0] = '?';
62 str6[1] = '\0';
63 if (stat(name, &st) == 0)
64 idle_string(str6, st.st_atime);
Denis Vlasenkoe5569cb2007-11-11 06:35:41 +000065 /* 15 chars for time: Nov 10 19:33:20 */
66 printf("%-10s %-8s %-9s %-15.15s %s\n",
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000067 ut->ut_user, ut->ut_line, str6,
Denis Vlasenko05c8c4f2007-11-13 17:26:21 +000068 ctime(&(ut->ut_tv.tv_sec)) + 4, ut->ut_host);
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000069 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();
Bernhard Reutner-Fischere8979882007-11-16 11:52:42 +000075 return EXIT_SUCCESS;
Eric Andersen00a6a752002-04-26 23:53:10 +000076}