| 1 | /* vi: set sw=4 ts=4: */
|
|---|
| 2 | /*----------------------------------------------------------------------
|
|---|
| 3 | * Mini who is used to display user name, login time,
|
|---|
| 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 | *
|
|---|
| 13 | * Copyright (c) 2002 AYR Networks, Inc.
|
|---|
| 14 | *----------------------------------------------------------------------
|
|---|
| 15 | */
|
|---|
| 16 |
|
|---|
| 17 | #include "busybox.h"
|
|---|
| 18 | #include <utmp.h>
|
|---|
| 19 | #include <time.h>
|
|---|
| 20 |
|
|---|
| 21 | static const char * idle_string (time_t t)
|
|---|
| 22 | {
|
|---|
| 23 | static char str[6];
|
|---|
| 24 |
|
|---|
| 25 | time_t s = time(NULL) - t;
|
|---|
| 26 |
|
|---|
| 27 | if (s < 60)
|
|---|
| 28 | return ".";
|
|---|
| 29 | if (s < (24 * 60 * 60)) {
|
|---|
| 30 | sprintf (str, "%02d:%02d",
|
|---|
| 31 | (int) (s / (60 * 60)),
|
|---|
| 32 | (int) ((s % (60 * 60)) / 60));
|
|---|
| 33 | return str;
|
|---|
| 34 | }
|
|---|
| 35 | return "old";
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | int who_main(int argc, char **argv)
|
|---|
| 39 | {
|
|---|
| 40 | struct utmp *ut;
|
|---|
| 41 | struct stat st;
|
|---|
| 42 | char *name;
|
|---|
| 43 |
|
|---|
| 44 | if (argc > 1) {
|
|---|
| 45 | bb_show_usage();
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | setutent();
|
|---|
| 49 | printf("USER TTY IDLE TIME HOST\n");
|
|---|
| 50 | while ((ut = getutent()) != NULL) {
|
|---|
| 51 | if (ut->ut_user[0] && ut->ut_type == USER_PROCESS) {
|
|---|
| 52 | time_t thyme = ut->ut_tv.tv_sec;
|
|---|
| 53 |
|
|---|
| 54 | /* ut->ut_line is device name of tty - "/dev/" */
|
|---|
| 55 | name = concat_path_file("/dev", ut->ut_line);
|
|---|
| 56 | printf("%-10s %-8s %-8s %-12.12s %s\n", ut->ut_user, ut->ut_line,
|
|---|
| 57 | (stat(name, &st)) ? "?" : idle_string(st.st_atime),
|
|---|
| 58 | ctime(&thyme) + 4, ut->ut_host);
|
|---|
| 59 | if (ENABLE_FEATURE_CLEAN_UP) free(name);
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 | if (ENABLE_FEATURE_CLEAN_UP) endutent();
|
|---|
| 63 | return 0;
|
|---|
| 64 | }
|
|---|