blob: 2773e1a8b1613b26b43c4c6baee80f703d776b6d [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.
Eric Andersen00a6a752002-04-26 23:53:10 +000014 *----------------------------------------------------------------------
15 */
16
Rob Landley1c60d972006-03-11 18:22:35 +000017#include <stdio.h>
Eric Andersen00a6a752002-04-26 23:53:10 +000018#include <stdlib.h>
19#include <utmp.h>
20#include <sys/stat.h>
Eric Andersen00a6a752002-04-26 23:53:10 +000021#include <time.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000022#include "busybox.h"
Eric Andersen00a6a752002-04-26 23:53:10 +000023
Rob Landleydfba7412006-03-06 20:47:33 +000024int who_main(int argc, char **argv)
Eric Andersen00a6a752002-04-26 23:53:10 +000025{
26 struct utmp *ut;
27 struct stat st;
Rob Landley1c60d972006-03-11 18:22:35 +000028 time_t idle;
29 char *name;
Eric Andersen00a6a752002-04-26 23:53:10 +000030
Eric Andersenc7bda1c2004-03-15 08:29:22 +000031 if (argc > 1)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000032 bb_show_usage();
Eric Andersen00a6a752002-04-26 23:53:10 +000033
34 setutent();
Eric Andersenc7bda1c2004-03-15 08:29:22 +000035 printf("USER TTY IDLE FROM HOST\n");
Eric Andersen00a6a752002-04-26 23:53:10 +000036
37 while ((ut = getutent()) != NULL) {
Eric Andersen00a6a752002-04-26 23:53:10 +000038
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000039 if (ut->ut_user[0] && ut->ut_type == USER_PROCESS) {
Rob Landley1c60d972006-03-11 18:22:35 +000040 /* ut->ut_line is device name of tty - "/dev/" */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000041 printf("%-10s %-8s ", ut->ut_user, ut->ut_line);
Eric Andersen00a6a752002-04-26 23:53:10 +000042
Rob Landley1c60d972006-03-11 18:22:35 +000043 name = concat_path_file("/dev/", ut->ut_line);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000044 if (stat(name, &st) == 0) {
Rob Landley1c60d972006-03-11 18:22:35 +000045 idle = time(NULL) - st.st_atime;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000046
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000047 if (idle < 60)
48 printf("00:00m ");
49 else if (idle < (60 * 60))
50 printf("00:%02dm ", (int)(idle / 60));
51 else if (idle < (24 * 60 * 60))
52 printf("%02d:%02dm ", (int)(idle / (60 * 60)),
53 (int)(idle % (60 * 60)) / 60);
54 else if (idle < (24 * 60 * 60 * 365))
55 printf("%03ddays ", (int)(idle / (24 * 60 * 60)));
56 else
57 printf("%02dyears ", (int) (idle / (24 * 60 * 60 * 365)));
58 } else
59 printf("%-8s ", "?");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000060
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000061 printf("%-12.12s %s\n", ctime((time_t*)&(ut->ut_tv.tv_sec)) + 4, ut->ut_host);
Rob Landley1c60d972006-03-11 18:22:35 +000062 free(name);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000063 }
Eric Andersen00a6a752002-04-26 23:53:10 +000064 }
65 endutent();
66
67 return 0;
68}