blob: ec385bf951ec5f4aec0b277c1a85880c0900e8ca [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020015 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000016 *
Eric Andersen00a6a752002-04-26 23:53:10 +000017 *----------------------------------------------------------------------
18 */
Denys Vlasenko37f5bef2010-04-05 03:18:40 +020019/* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */
Eric Andersen00a6a752002-04-26 23:53:10 +000020
Pere Orga34425382011-03-31 14:43:25 +020021//usage:#define who_trivial_usage
22//usage: "[-a]"
23//usage:#define who_full_usage "\n\n"
24//usage: "Show who is logged on\n"
25//usage: "\nOptions:"
26//usage: "\n -a Show all"
27
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Eric Andersen00a6a752002-04-26 23:53:10 +000029
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000030static void idle_string(char *str6, time_t t)
Rob Landleye01d7462006-03-12 19:26:01 +000031{
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000032 t = time(NULL) - t;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000033
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000034 /*if (t < 60) {
35 str6[0] = '.';
36 str6[1] = '\0';
37 return;
38 }*/
39 if (t >= 0 && t < (24 * 60 * 60)) {
40 sprintf(str6, "%02d:%02d",
41 (int) (t / (60 * 60)),
42 (int) ((t % (60 * 60)) / 60));
43 return;
Rob Landleye01d7462006-03-12 19:26:01 +000044 }
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000045 strcpy(str6, "old");
Rob Landleye01d7462006-03-12 19:26:01 +000046}
47
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000048int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000049int who_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen00a6a752002-04-26 23:53:10 +000050{
Rob Landleye01d7462006-03-12 19:26:01 +000051 struct utmp *ut;
Denis Vlasenko01cd9572007-11-16 05:24:43 +000052 unsigned opt;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000053
Denis Vlasenko01cd9572007-11-16 05:24:43 +000054 opt_complementary = "=0";
Denys Vlasenko4c721042010-04-04 23:45:09 +020055 opt = getopt32(argv, "aH");
56 if (opt & 2) // -H
57 printf("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST\n");
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000058
Rob Landleye01d7462006-03-12 19:26:01 +000059 setutent();
Rob Landleye01d7462006-03-12 19:26:01 +000060 while ((ut = getutent()) != NULL) {
Denys Vlasenko4c721042010-04-04 23:45:09 +020061 if (ut->ut_user[0]
62 && ((opt & 1) || ut->ut_type == USER_PROCESS)
63 ) {
64 char str6[6];
65 char name[sizeof("/dev/") + sizeof(ut->ut_line) + 1];
66 struct stat st;
67 time_t seconds;
68
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000069 str6[0] = '?';
70 str6[1] = '\0';
Denys Vlasenko4c721042010-04-04 23:45:09 +020071 strcpy(name, "/dev/");
72 safe_strncpy(ut->ut_line[0] == '/' ? name : name + sizeof("/dev/")-1,
73 ut->ut_line,
74 sizeof(ut->ut_line)+1
75 );
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000076 if (stat(name, &st) == 0)
77 idle_string(str6, st.st_atime);
Denis Vlasenkof62ab2d2008-07-09 09:50:33 +000078 /* manpages say ut_tv.tv_sec *is* time_t,
79 * but some systems have it wrong */
Denys Vlasenko4c721042010-04-04 23:45:09 +020080 seconds = ut->ut_tv.tv_sec;
81 /* How wide time field can be?
82 * "Nov 10 19:33:20": 15 chars
83 * "2010-11-10 19:33": 16 chars
84 */
85 printf("%-15.*s %-15.*s %-7s %-16.16s %.*s\n",
86 (int)sizeof(ut->ut_user), ut->ut_user,
87 (int)sizeof(ut->ut_line), ut->ut_line,
88 str6,
89 ctime(&seconds) + 4,
90 (int)sizeof(ut->ut_host), ut->ut_host
91 );
Rob Landleye01d7462006-03-12 19:26:01 +000092 }
93 }
Denis Vlasenko41cca2b2007-03-07 00:07:42 +000094 if (ENABLE_FEATURE_CLEAN_UP)
95 endutent();
Bernhard Reutner-Fischere8979882007-11-16 11:52:42 +000096 return EXIT_SUCCESS;
Eric Andersen00a6a752002-04-26 23:53:10 +000097}