adduser: prefer to call addgroup --gid, not non-std addgroup -g
[platform/upstream/busybox.git] / coreutils / who.c
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  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
16  *
17  *----------------------------------------------------------------------
18  */
19 /* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'.  */
20
21 //usage:#define who_trivial_usage
22 //usage:       "[-a]"
23 //usage:#define who_full_usage "\n\n"
24 //usage:       "Show who is logged on\n"
25 //usage:     "\nOptions:"
26 //usage:     "\n        -a      Show all"
27
28 #include "libbb.h"
29
30 static void idle_string(char *str6, time_t t)
31 {
32         t = time(NULL) - t;
33
34         /*if (t < 60) {
35                 str6[0] = '.';
36                 str6[1] = '\0';
37                 return;
38         }*/
39         if (t >= 0 && t < (24 * 60 * 60)) {
40                 sprintf(str6, "%02d:%02d",
41                                 (int) (t / (60 * 60)),
42                                 (int) ((t % (60 * 60)) / 60));
43                 return;
44         }
45         strcpy(str6, "old");
46 }
47
48 int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
49 int who_main(int argc UNUSED_PARAM, char **argv)
50 {
51         struct utmp *ut;
52         unsigned opt;
53
54         opt_complementary = "=0";
55         opt = getopt32(argv, "aH");
56         if (opt & 2) // -H
57                 printf("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST\n");
58
59         setutent();
60         while ((ut = getutent()) != NULL) {
61                 if (ut->ut_user[0]
62                  && ((opt & 1) || ut->ut_type == USER_PROCESS)
63                 ) {
64                         char str6[6];
65                         char name[sizeof("/dev/") + sizeof(ut->ut_line) + 1];
66                         struct stat st;
67                         time_t seconds;
68
69                         str6[0] = '?';
70                         str6[1] = '\0';
71                         strcpy(name, "/dev/");
72                         safe_strncpy(ut->ut_line[0] == '/' ? name : name + sizeof("/dev/")-1,
73                                 ut->ut_line,
74                                 sizeof(ut->ut_line)+1
75                         );
76                         if (stat(name, &st) == 0)
77                                 idle_string(str6, st.st_atime);
78                         /* manpages say ut_tv.tv_sec *is* time_t,
79                          * but some systems have it wrong */
80                         seconds = ut->ut_tv.tv_sec;
81                         /* How wide time field can be?
82                          * "Nov 10 19:33:20": 15 chars
83                          * "2010-11-10 19:33": 16 chars
84                          */
85                         printf("%-15.*s %-15.*s %-7s %-16.16s %.*s\n",
86                                         (int)sizeof(ut->ut_user), ut->ut_user,
87                                         (int)sizeof(ut->ut_line), ut->ut_line,
88                                         str6,
89                                         ctime(&seconds) + 4,
90                                         (int)sizeof(ut->ut_host), ut->ut_host
91                         );
92                 }
93         }
94         if (ENABLE_FEATURE_CLEAN_UP)
95                 endutent();
96         return EXIT_SUCCESS;
97 }