Magellan Linux

Annotation of /trunk/mkinitrd-magellan/busybox/coreutils/who.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1123 - (hide annotations) (download)
Wed Aug 18 21:56:57 2010 UTC (13 years, 10 months ago) by niro
File MIME type: text/plain
File size: 2406 byte(s)
-updated to busybox-1.17.1
1 niro 532 /* 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     * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
16     *
17     *----------------------------------------------------------------------
18     */
19 niro 1123 /* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */
20 niro 532
21 niro 816 #include "libbb.h"
22 niro 532 #include <utmp.h>
23    
24 niro 816 static void idle_string(char *str6, time_t t)
25 niro 532 {
26 niro 816 t = time(NULL) - t;
27 niro 532
28 niro 816 /*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;
38 niro 532 }
39 niro 816 strcpy(str6, "old");
40 niro 532 }
41    
42 niro 816 int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
43     int who_main(int argc UNUSED_PARAM, char **argv)
44 niro 532 {
45     struct utmp *ut;
46 niro 816 unsigned opt;
47 niro 532
48 niro 816 opt_complementary = "=0";
49 niro 1123 opt = getopt32(argv, "aH");
50     if (opt & 2) // -H
51     printf("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST\n");
52 niro 532
53     setutent();
54     while ((ut = getutent()) != NULL) {
55 niro 1123 if (ut->ut_user[0]
56     && ((opt & 1) || ut->ut_type == USER_PROCESS)
57     ) {
58     char str6[6];
59     char name[sizeof("/dev/") + sizeof(ut->ut_line) + 1];
60     struct stat st;
61     time_t seconds;
62    
63 niro 816 str6[0] = '?';
64     str6[1] = '\0';
65 niro 1123 strcpy(name, "/dev/");
66     safe_strncpy(ut->ut_line[0] == '/' ? name : name + sizeof("/dev/")-1,
67     ut->ut_line,
68     sizeof(ut->ut_line)+1
69     );
70 niro 816 if (stat(name, &st) == 0)
71     idle_string(str6, st.st_atime);
72     /* manpages say ut_tv.tv_sec *is* time_t,
73     * but some systems have it wrong */
74 niro 1123 seconds = ut->ut_tv.tv_sec;
75     /* How wide time field can be?
76     * "Nov 10 19:33:20": 15 chars
77     * "2010-11-10 19:33": 16 chars
78     */
79     printf("%-15.*s %-15.*s %-7s %-16.16s %.*s\n",
80     (int)sizeof(ut->ut_user), ut->ut_user,
81     (int)sizeof(ut->ut_line), ut->ut_line,
82     str6,
83     ctime(&seconds) + 4,
84     (int)sizeof(ut->ut_host), ut->ut_host
85     );
86 niro 532 }
87     }
88 niro 816 if (ENABLE_FEATURE_CLEAN_UP)
89     endutent();
90     return EXIT_SUCCESS;
91 niro 532 }