Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /*---------------------------------------------------------------------- |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3 | * Mini who is used to display user name, login time, |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 4 | * 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 Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 13 | * Copyright (c) 2002 AYR Networks, Inc. |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 14 | * |
| 15 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 16 | * |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 17 | *---------------------------------------------------------------------- |
| 18 | */ |
| 19 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 20 | #include "busybox.h" |
Rob Landley | 7a260f0 | 2006-06-19 03:20:03 +0000 | [diff] [blame] | 21 | #include <utmp.h> |
| 22 | #include <time.h> |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 23 | |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 24 | static const char * idle_string (time_t t) |
| 25 | { |
| 26 | static char str[6]; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 27 | |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 28 | time_t s = time(NULL) - t; |
| 29 | |
| 30 | if (s < 60) |
| 31 | return "."; |
| 32 | if (s < (24 * 60 * 60)) { |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 33 | sprintf(str, "%02d:%02d", |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 34 | (int) (s / (60 * 60)), |
| 35 | (int) ((s % (60 * 60)) / 60)); |
Rob Landley | 7a260f0 | 2006-06-19 03:20:03 +0000 | [diff] [blame] | 36 | return str; |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 37 | } |
| 38 | return "old"; |
| 39 | } |
| 40 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 41 | int who_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 42 | int who_main(int argc, char **argv) |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 43 | { |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 44 | struct utmp *ut; |
| 45 | struct stat st; |
| 46 | char *name; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 47 | |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 48 | if (argc > 1) { |
| 49 | bb_show_usage(); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 50 | } |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 51 | |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 52 | setutent(); |
| 53 | printf("USER TTY IDLE TIME HOST\n"); |
| 54 | while ((ut = getutent()) != NULL) { |
| 55 | if (ut->ut_user[0] && ut->ut_type == USER_PROCESS) { |
Rob Landley | 7a260f0 | 2006-06-19 03:20:03 +0000 | [diff] [blame] | 56 | time_t thyme = ut->ut_tv.tv_sec; |
| 57 | |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 58 | /* ut->ut_line is device name of tty - "/dev/" */ |
| 59 | name = concat_path_file("/dev", ut->ut_line); |
| 60 | printf("%-10s %-8s %-8s %-12.12s %s\n", ut->ut_user, ut->ut_line, |
| 61 | (stat(name, &st)) ? "?" : idle_string(st.st_atime), |
Rob Landley | 7a260f0 | 2006-06-19 03:20:03 +0000 | [diff] [blame] | 62 | ctime(&thyme) + 4, ut->ut_host); |
| 63 | if (ENABLE_FEATURE_CLEAN_UP) free(name); |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
Rob Landley | 7a260f0 | 2006-06-19 03:20:03 +0000 | [diff] [blame] | 66 | if (ENABLE_FEATURE_CLEAN_UP) endutent(); |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 67 | return 0; |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 68 | } |