Add one-line GPL boilerplate to numerous (but not all yet) source files.
[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 tarball for details.
16  *
17  *----------------------------------------------------------------------
18  */
19
20 #include "busybox.h"
21 #include <utmp.h>
22 #include <time.h>
23
24 static const char * idle_string (time_t t)
25 {
26         static char str[6];
27         
28         time_t s = time(NULL) - t;
29
30         if (s < 60)
31                 return ".";
32         if (s < (24 * 60 * 60)) {
33                 sprintf (str, "%02d:%02d",
34                                 (int) (s / (60 * 60)),
35                                 (int) ((s % (60 * 60)) / 60));
36                 return str;
37         }
38         return "old";
39 }
40
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                         time_t thyme = ut->ut_tv.tv_sec;
56
57                         /* ut->ut_line is device name of tty - "/dev/" */
58                         name = concat_path_file("/dev", ut->ut_line);
59                         printf("%-10s %-8s %-8s  %-12.12s   %s\n", ut->ut_user, ut->ut_line,
60                                                                         (stat(name, &st)) ?  "?" : idle_string(st.st_atime),
61                                                                         ctime(&thyme) + 4, ut->ut_host);
62                         if (ENABLE_FEATURE_CLEAN_UP) free(name);
63                 }
64         }
65         if (ENABLE_FEATURE_CLEAN_UP) endutent();
66         return 0;
67 }