2 Copyright (C) 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by jla; revised by djm */
27 /* The name this program was run with. */
30 /* If nonzero, display usage information and exit. */
33 /* If nonzero, print the version on standard output and exit. */
34 static int show_version
;
36 static struct option
const longopts
[] =
38 {"help", no_argument
, &show_help
, 1},
39 {"version", no_argument
, &show_version
, 1},
44 print_uptime (int n
, const STRUCT_UTMP
*this)
46 register int entries
= 0;
56 #ifdef HAVE_PROC_UPTIME
60 fp
= fopen ("/proc/uptime", "r");
65 fgets (buf
, BUFSIZ
, fp
);
66 res
= sscanf (buf
, "%lf", &upsecs
);
68 uptime
= (time_t) upsecs
;
71 #endif /* HAVE_PROC_UPTIME */
72 /* Loop through all the utmp entries we just read and count up the valid
73 ones, also in the process possibly gleaning boottime. */
78 && this->ut_type
== USER_PROCESS
84 /* If BOOT_MSG is defined, we can get boottime from utmp. This avoids
85 possibly needing special privs to read /dev/kmem. */
89 # endif /* HAVE_PROC_UPTIME */
90 if (!strcmp (this->ut_line
, BOOT_MSG
))
91 boot_time
= UT_TIME_MEMBER (this);
96 #if defined HAVE_PROC_UPTIME
101 error (1, errno
, _("couldn't get boot time"));
102 uptime
= time_now
- boot_time
;
104 updays
= uptime
/ 86400;
105 uphours
= (uptime
- (updays
* 86400)) / 3600;
106 upmins
= (uptime
- (updays
* 86400) - (uphours
* 3600)) / 60;
107 tmn
= localtime (&time_now
);
108 printf (_(" %2d:%02d%s up "), ((tmn
->tm_hour
% 12) == 0
109 ? 12 : tmn
->tm_hour
% 12),
110 /* FIXME: use strftime, not am, pm. Uli reports that
111 the german translation is meaningless. */
112 tmn
->tm_min
, (tmn
->tm_hour
< 12 ? _("am") : _("pm")));
114 printf ("%d %s,", updays
, (updays
== 1 ? _("day") : _("days")));
115 printf (" %2d:%02d, %d %s", uphours
, upmins
, entries
,
116 (entries
== 1) ? _("user") : _("users"));
118 #if defined (HAVE_GETLOADAVG) || defined (C_GETLOADAVG)
119 loads
= getloadavg (avg
, 3);
129 printf (_(", load average: %.2f"), avg
[0]);
131 printf (", %.2f", avg
[1]);
133 printf (", %.2f", avg
[2]);
139 /* Display the system uptime and the number of users on the system,
140 according to utmp file FILENAME. */
143 uptime (const char *filename
)
146 STRUCT_UTMP
*utmp_buf
;
147 int fail
= read_utmp (filename
, &n_users
, &utmp_buf
);
150 error (1, errno
, "%s", filename
);
152 print_uptime (n_users
, utmp_buf
);
159 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
163 printf (_("Usage: %s [OPTION]... [ FILE ]\n"), program_name
);
165 Output who is currently logged in according to FILE.\n\
166 If FILE is not specified, use %s. %s as FILE is common.\n\
168 --help display this help and exit\n\
169 --version output version information and exit\n"),
170 UTMP_FILE
, WTMP_FILE
);
171 puts (_("\nReport bugs to <sh-utils-bugs@gnu.org>."));
177 main (int argc
, char **argv
)
180 program_name
= argv
[0];
181 setlocale (LC_ALL
, "");
182 bindtextdomain (PACKAGE
, LOCALEDIR
);
183 textdomain (PACKAGE
);
185 while ((optc
= getopt_long (argc
, argv
, "", longopts
, &longind
)) != -1)
199 printf ("uptime (%s) %s\n", GNU_PACKAGE
, VERSION
);
206 switch (argc
- optind
)
212 case 1: /* uptime <utmp file> */
213 uptime (argv
[optind
]);
217 error (0, 0, _("too many arguments"));