diff options
| author | Rob Landley <rob@landley.net> | 2006-03-12 19:26:01 +0000 |
|---|---|---|
| committer | Rob Landley <rob@landley.net> | 2006-03-12 19:26:01 +0000 |
| commit | e01d746fe962e1c3de57f8b909abc9801ecceff1 (patch) | |
| tree | d33d03da8572bbce56bdcf2e77f42ad45f1c6379 /coreutils | |
| parent | 1c60d9762e150ed9f3bd4584a639ff2d6996699c (diff) | |
| download | busybox-w32-e01d746fe962e1c3de57f8b909abc9801ecceff1.tar.gz busybox-w32-e01d746fe962e1c3de57f8b909abc9801ecceff1.tar.bz2 busybox-w32-e01d746fe962e1c3de57f8b909abc9801ecceff1.zip | |
Cleanup patch from tito.
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/who.c | 77 |
1 files changed, 37 insertions, 40 deletions
diff --git a/coreutils/who.c b/coreutils/who.c index 2773e1a8b..0cb74313e 100644 --- a/coreutils/who.c +++ b/coreutils/who.c | |||
| @@ -21,48 +21,45 @@ | |||
| 21 | #include <time.h> | 21 | #include <time.h> |
| 22 | #include "busybox.h" | 22 | #include "busybox.h" |
| 23 | 23 | ||
| 24 | int who_main(int argc, char **argv) | 24 | static const char * idle_string (time_t t) |
| 25 | { | 25 | { |
| 26 | struct utmp *ut; | 26 | static char str[6]; |
| 27 | struct stat st; | 27 | |
| 28 | time_t idle; | 28 | time_t s = time(NULL) - t; |
| 29 | char *name; | ||
| 30 | |||
| 31 | if (argc > 1) | ||
| 32 | bb_show_usage(); | ||
| 33 | |||
| 34 | setutent(); | ||
| 35 | printf("USER TTY IDLE FROM HOST\n"); | ||
| 36 | |||
| 37 | while ((ut = getutent()) != NULL) { | ||
| 38 | |||
| 39 | if (ut->ut_user[0] && ut->ut_type == USER_PROCESS) { | ||
| 40 | /* ut->ut_line is device name of tty - "/dev/" */ | ||
| 41 | printf("%-10s %-8s ", ut->ut_user, ut->ut_line); | ||
| 42 | |||
| 43 | name = concat_path_file("/dev/", ut->ut_line); | ||
| 44 | if (stat(name, &st) == 0) { | ||
| 45 | idle = time(NULL) - st.st_atime; | ||
| 46 | 29 | ||
| 47 | if (idle < 60) | 30 | if (s < 60) |
| 48 | printf("00:00m "); | 31 | return "."; |
| 49 | else if (idle < (60 * 60)) | 32 | if (s < (24 * 60 * 60)) { |
| 50 | printf("00:%02dm ", (int)(idle / 60)); | 33 | sprintf (str, "%02d:%02d", |
| 51 | else if (idle < (24 * 60 * 60)) | 34 | (int) (s / (60 * 60)), |
| 52 | printf("%02d:%02dm ", (int)(idle / (60 * 60)), | 35 | (int) ((s % (60 * 60)) / 60)); |
| 53 | (int)(idle % (60 * 60)) / 60); | 36 | return (const char *) str; |
| 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 ", "?"); | ||
| 60 | |||
| 61 | printf("%-12.12s %s\n", ctime((time_t*)&(ut->ut_tv.tv_sec)) + 4, ut->ut_host); | ||
| 62 | free(name); | ||
| 63 | } | 37 | } |
| 64 | } | 38 | return "old"; |
| 65 | endutent(); | 39 | } |
| 66 | 40 | ||
| 67 | return 0; | 41 | int who_main(int argc, char **argv) |
| 42 | { | ||
| 43 | struct utmp *ut; | ||
| 44 | struct stat st; | ||
| 45 | char *name; | ||
| 46 | |||
| 47 | if (argc > 1) { | ||
| 48 | bb_show_usage(); | ||
| 49 | } | ||
| 50 | |||
| 51 | setutent(); | ||
| 52 | printf("USER TTY IDLE TIME HOST\n"); | ||
| 53 | while ((ut = getutent()) != NULL) { | ||
| 54 | if (ut->ut_user[0] && ut->ut_type == USER_PROCESS) { | ||
| 55 | /* ut->ut_line is device name of tty - "/dev/" */ | ||
| 56 | name = concat_path_file("/dev", ut->ut_line); | ||
| 57 | printf("%-10s %-8s %-8s %-12.12s %s\n", ut->ut_user, ut->ut_line, | ||
| 58 | (stat(name, &st)) ? "?" : idle_string(st.st_atime), | ||
| 59 | ctime((time_t*)&(ut->ut_tv.tv_sec)) + 4, ut->ut_host); | ||
| 60 | free(name); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | endutent(); | ||
| 64 | return 0; | ||
| 68 | } | 65 | } |
