source: MondoRescue/branches/2.2.5/mindi-busybox/coreutils/who.c@ 1765

Last change on this file since 1765 was 1765, checked in by Bruno Cornec, 16 years ago

Update to busybox 1.7.2

File size: 1.8 KB
RevLine 
[821]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.
[1765]14 *
15 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
16 *
[821]17 *----------------------------------------------------------------------
18 */
19
[1765]20#include "libbb.h"
[821]21#include <utmp.h>
22#include <time.h>
23
[1765]24static void idle_string(char *str6, time_t t)
[821]25{
[1765]26 t = time(NULL) - t;
[821]27
[1765]28 /*if (t < 60) {
29 str6[0] = '.';
30 str6[1] = '\0';
31 return;
32 }*/
33 if (t >= 0 && t < (24 * 60 * 60)) {
34 sprintf(str6, "%02d:%02d",
35 (int) (t / (60 * 60)),
36 (int) ((t % (60 * 60)) / 60));
37 return;
[821]38 }
[1765]39 strcpy(str6, "old");
[821]40}
41
[1765]42int who_main(int argc, char **argv);
[821]43int who_main(int argc, char **argv)
44{
[1765]45 char str6[6];
[821]46 struct utmp *ut;
47 struct stat st;
48 char *name;
[1765]49
[821]50 if (argc > 1) {
51 bb_show_usage();
52 }
[1765]53
[821]54 setutent();
55 printf("USER TTY IDLE TIME HOST\n");
56 while ((ut = getutent()) != NULL) {
57 if (ut->ut_user[0] && ut->ut_type == USER_PROCESS) {
58 time_t thyme = ut->ut_tv.tv_sec;
59
60 /* ut->ut_line is device name of tty - "/dev/" */
61 name = concat_path_file("/dev", ut->ut_line);
[1765]62 str6[0] = '?';
63 str6[1] = '\0';
64 if (stat(name, &st) == 0)
65 idle_string(str6, st.st_atime);
66 printf("%-10s %-8s %-9s %-14.14s %s\n",
67 ut->ut_user, ut->ut_line, str6,
68 ctime(&thyme) + 4, ut->ut_host);
69 if (ENABLE_FEATURE_CLEAN_UP)
70 free(name);
[821]71 }
72 }
[1765]73 if (ENABLE_FEATURE_CLEAN_UP)
74 endutent();
[821]75 return 0;
76}
Note: See TracBrowser for help on using the repository browser.