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. |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 14 | *---------------------------------------------------------------------- |
| 15 | */ |
| 16 | |
Rob Landley | 1c60d97 | 2006-03-11 18:22:35 +0000 | [diff] [blame^] | 17 | #include <stdio.h> |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #include <utmp.h> |
| 20 | #include <sys/stat.h> |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 21 | #include <time.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 22 | #include "busybox.h" |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 23 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 24 | int who_main(int argc, char **argv) |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 25 | { |
| 26 | struct utmp *ut; |
| 27 | struct stat st; |
Rob Landley | 1c60d97 | 2006-03-11 18:22:35 +0000 | [diff] [blame^] | 28 | time_t idle; |
| 29 | char *name; |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 30 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 31 | if (argc > 1) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 32 | bb_show_usage(); |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 33 | |
| 34 | setutent(); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 35 | printf("USER TTY IDLE FROM HOST\n"); |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 36 | |
| 37 | while ((ut = getutent()) != NULL) { |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 38 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 39 | if (ut->ut_user[0] && ut->ut_type == USER_PROCESS) { |
Rob Landley | 1c60d97 | 2006-03-11 18:22:35 +0000 | [diff] [blame^] | 40 | /* ut->ut_line is device name of tty - "/dev/" */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 41 | printf("%-10s %-8s ", ut->ut_user, ut->ut_line); |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 42 | |
Rob Landley | 1c60d97 | 2006-03-11 18:22:35 +0000 | [diff] [blame^] | 43 | name = concat_path_file("/dev/", ut->ut_line); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 44 | if (stat(name, &st) == 0) { |
Rob Landley | 1c60d97 | 2006-03-11 18:22:35 +0000 | [diff] [blame^] | 45 | idle = time(NULL) - st.st_atime; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 46 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 47 | 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 Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 60 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 61 | printf("%-12.12s %s\n", ctime((time_t*)&(ut->ut_tv.tv_sec)) + 4, ut->ut_host); |
Rob Landley | 1c60d97 | 2006-03-11 18:22:35 +0000 | [diff] [blame^] | 62 | free(name); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 63 | } |
Eric Andersen | 00a6a75 | 2002-04-26 23:53:10 +0000 | [diff] [blame] | 64 | } |
| 65 | endutent(); |
| 66 | |
| 67 | return 0; |
| 68 | } |