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 | */ |
Bernhard Reutner-Fischer | e897988 | 2007-11-16 11:52:42 +0000 | [diff] [blame] | 19 | /* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -H, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */ |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 21 | #include "libbb.h" |
Rob Landley | 7a260f0 | 2006-06-19 03:20:03 +0000 | [diff] [blame] | 22 | #include <utmp.h> |
| 23 | #include <time.h> |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 25 | static void idle_string(char *str6, time_t t) |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 26 | { |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 27 | t = time(NULL) - t; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 28 | |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 29 | /*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 Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 39 | } |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 40 | strcpy(str6, "old"); |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 43 | int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 44 | int who_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 45 | { |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 46 | char str6[6]; |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 47 | struct utmp *ut; |
| 48 | struct stat st; |
| 49 | char *name; |
Denis Vlasenko | 01cd957 | 2007-11-16 05:24:43 +0000 | [diff] [blame] | 50 | unsigned opt; |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | 01cd957 | 2007-11-16 05:24:43 +0000 | [diff] [blame] | 52 | opt_complementary = "=0"; |
| 53 | opt = getopt32(argv, "a"); |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 54 | |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 55 | setutent(); |
Denis Vlasenko | e5569cb | 2007-11-11 06:35:41 +0000 | [diff] [blame] | 56 | printf("USER TTY IDLE TIME HOST\n"); |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 57 | while ((ut = getutent()) != NULL) { |
Denis Vlasenko | 01cd957 | 2007-11-16 05:24:43 +0000 | [diff] [blame] | 58 | if (ut->ut_user[0] && (opt || ut->ut_type == USER_PROCESS)) { |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 59 | time_t tmp; |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 60 | /* ut->ut_line is device name of tty - "/dev/" */ |
| 61 | name = concat_path_file("/dev", ut->ut_line); |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 62 | str6[0] = '?'; |
| 63 | str6[1] = '\0'; |
| 64 | if (stat(name, &st) == 0) |
| 65 | idle_string(str6, st.st_atime); |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 66 | /* manpages say ut_tv.tv_sec *is* time_t, |
| 67 | * but some systems have it wrong */ |
| 68 | tmp = ut->ut_tv.tv_sec; |
Denis Vlasenko | e5569cb | 2007-11-11 06:35:41 +0000 | [diff] [blame] | 69 | /* 15 chars for time: Nov 10 19:33:20 */ |
| 70 | printf("%-10s %-8s %-9s %-15.15s %s\n", |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 71 | ut->ut_user, ut->ut_line, str6, |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 72 | ctime(&tmp) + 4, ut->ut_host); |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 73 | if (ENABLE_FEATURE_CLEAN_UP) |
| 74 | free(name); |
Rob Landley | e01d746 | 2006-03-12 19:26:01 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
Denis Vlasenko | 41cca2b | 2007-03-07 00:07:42 +0000 | [diff] [blame] | 77 | if (ENABLE_FEATURE_CLEAN_UP) |
| 78 | endutent(); |
Bernhard Reutner-Fischer | e897988 | 2007-11-16 11:52:42 +0000 | [diff] [blame] | 79 | return EXIT_SUCCESS; |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 80 | } |