blob: 4cd42652beff4ce5c559d0e9065bfc1490b54f94 [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
Manuel Novoa III cad53642003-03-19 09:13:01 +000020#include "busybox.h"
Rob Landley7a260f02006-06-19 03:20:03 +000021#include <utmp.h>
22#include <time.h>
Eric Andersen00a6a752002-04-26 23:53:10 +000023
Rob Landleye01d7462006-03-12 19:26:01 +000024static const char * idle_string (time_t t)
25{
26 static char str[6];
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000027
Rob Landleye01d7462006-03-12 19:26:01 +000028 time_t s = time(NULL) - t;
29
30 if (s < 60)
31 return ".";
32 if (s < (24 * 60 * 60)) {
Denis Vlasenko92258542006-11-01 10:25:35 +000033 sprintf(str, "%02d:%02d",
Rob Landleye01d7462006-03-12 19:26:01 +000034 (int) (s / (60 * 60)),
35 (int) ((s % (60 * 60)) / 60));
Rob Landley7a260f02006-06-19 03:20:03 +000036 return str;
Rob Landleye01d7462006-03-12 19:26:01 +000037 }
38 return "old";
39}
40
Rob Landleydfba7412006-03-06 20:47:33 +000041int who_main(int argc, char **argv)
Eric Andersen00a6a752002-04-26 23:53:10 +000042{
Rob Landleye01d7462006-03-12 19:26:01 +000043 struct utmp *ut;
44 struct stat st;
45 char *name;
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000046
Rob Landleye01d7462006-03-12 19:26:01 +000047 if (argc > 1) {
48 bb_show_usage();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000049 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000050
Rob Landleye01d7462006-03-12 19:26:01 +000051 setutent();
52 printf("USER TTY IDLE TIME HOST\n");
53 while ((ut = getutent()) != NULL) {
54 if (ut->ut_user[0] && ut->ut_type == USER_PROCESS) {
Rob Landley7a260f02006-06-19 03:20:03 +000055 time_t thyme = ut->ut_tv.tv_sec;
56
Rob Landleye01d7462006-03-12 19:26:01 +000057 /* ut->ut_line is device name of tty - "/dev/" */
58 name = concat_path_file("/dev", ut->ut_line);
59 printf("%-10s %-8s %-8s %-12.12s %s\n", ut->ut_user, ut->ut_line,
60 (stat(name, &st)) ? "?" : idle_string(st.st_atime),
Rob Landley7a260f02006-06-19 03:20:03 +000061 ctime(&thyme) + 4, ut->ut_host);
62 if (ENABLE_FEATURE_CLEAN_UP) free(name);
Rob Landleye01d7462006-03-12 19:26:01 +000063 }
64 }
Rob Landley7a260f02006-06-19 03:20:03 +000065 if (ENABLE_FEATURE_CLEAN_UP) endutent();
Rob Landleye01d7462006-03-12 19:26:01 +000066 return 0;
Eric Andersen00a6a752002-04-26 23:53:10 +000067}