2 Copyright (C) 1992-1999 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 /* Created by hacking who.c by Kaveh Ghazi ghazi@caip.rutgers.edu. */
25 #include "long-options.h"
29 /* The official name of this program (e.g., no `g' prefix). */
30 #define PROGRAM_NAME "uptime"
32 #define AUTHORS "Joseph Arceneaux, David MacKenzie, and Kaveh Ghazi"
36 /* The name this program was run with. */
39 static struct option
const longopts
[] =
45 print_uptime (int n
, const STRUCT_UTMP
*this)
47 register int entries
= 0;
57 #ifdef HAVE_PROC_UPTIME
61 fp
= fopen ("/proc/uptime", "r");
66 fgets (buf
, BUFSIZ
, fp
);
68 /* The following sscanf must use the C locale. */
69 setlocale (LC_NUMERIC
, "C");
70 res
= sscanf (buf
, "%lf", &upsecs
);
71 setlocale (LC_NUMERIC
, "");
73 uptime
= (time_t) upsecs
;
77 #endif /* HAVE_PROC_UPTIME */
78 /* Loop through all the utmp entries we just read and count up the valid
79 ones, also in the process possibly gleaning boottime. */
84 && this->ut_type
== USER_PROCESS
90 /* If BOOT_MSG is defined, we can get boottime from utmp. This avoids
91 possibly needing special privs to read /dev/kmem. */
95 # endif /* HAVE_PROC_UPTIME */
96 if (!strcmp (this->ut_line
, BOOT_MSG
))
97 boot_time
= UT_TIME_MEMBER (this);
102 #if defined HAVE_PROC_UPTIME
107 error (1, errno
, _("couldn't get boot time"));
108 uptime
= time_now
- boot_time
;
110 updays
= uptime
/ 86400;
111 uphours
= (uptime
- (updays
* 86400)) / 3600;
112 upmins
= (uptime
- (updays
* 86400) - (uphours
* 3600)) / 60;
113 tmn
= localtime (&time_now
);
114 printf (_(" %2d:%02d%s up "), ((tmn
->tm_hour
% 12) == 0
115 ? 12 : tmn
->tm_hour
% 12),
116 /* FIXME: use strftime, not am, pm. Uli reports that
117 the german translation is meaningless. */
118 tmn
->tm_min
, (tmn
->tm_hour
< 12 ? _("am") : _("pm")));
120 printf ("%d %s,", updays
, (updays
== 1 ? _("day") : _("days")));
121 printf (" %2d:%02d, %d %s", uphours
, upmins
, entries
,
122 (entries
== 1) ? _("user") : _("users"));
124 #if defined (HAVE_GETLOADAVG) || defined (C_GETLOADAVG)
125 loads
= getloadavg (avg
, 3);
135 printf (_(", load average: %.2f"), avg
[0]);
137 printf (", %.2f", avg
[1]);
139 printf (", %.2f", avg
[2]);
145 /* Display the system uptime and the number of users on the system,
146 according to utmp file FILENAME. */
149 uptime (const char *filename
)
152 STRUCT_UTMP
*utmp_buf
;
153 int fail
= read_utmp (filename
, &n_users
, &utmp_buf
);
156 error (1, errno
, "%s", filename
);
158 print_uptime (n_users
, utmp_buf
);
165 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
169 printf (_("Usage: %s [OPTION]... [ FILE ]\n"), program_name
);
171 Print the current time, the length of time the system has been up,\n\
172 the number of users on the system, and the average number of jobs\n\
173 in the run queue over the last 1, 5 and 15 minutes.\n\
174 If FILE is not specified, use %s. %s as FILE is common.\n\
176 --help display this help and exit\n\
177 --version output version information and exit\n"),
178 UTMP_FILE
, WTMP_FILE
);
179 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
185 main (int argc
, char **argv
)
188 program_name
= argv
[0];
189 setlocale (LC_ALL
, "");
190 bindtextdomain (PACKAGE
, LOCALEDIR
);
191 textdomain (PACKAGE
);
193 parse_long_options (argc
, argv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
196 while ((optc
= getopt_long (argc
, argv
, "", longopts
, &longind
)) != -1)
208 switch (argc
- optind
)
214 case 1: /* uptime <utmp file> */
215 uptime (argv
[optind
]);
219 error (0, 0, _("too many arguments"));