annotate toys/other/uptime.c @ 1727:c0ef9b7976f0 draft

Use xsignal() instead of signal().
author Rob Landley <rob@landley.net>
date Tue, 10 Mar 2015 11:07:28 -0500
parents a3500bd8b322
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
1 /* uptime.c - Tell how long the system has been running.
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
2 *
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
3 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
1095
960462460639 I noticed the user count was missing and added this, borrowing a bit of code from toys/posix/who.c.
Jeroen van Rijn <jvrnix@gmail.com>
parents: 694
diff changeset
4 * Copyright 2012 Luis Felipe Strano Moraes <lfelipe@profusion.mobi>
960462460639 I noticed the user count was missing and added this, borrowing a bit of code from toys/posix/who.c.
Jeroen van Rijn <jvrnix@gmail.com>
parents: 694
diff changeset
5 * Copyright 2013 Jeroen van Rijn <jvrnix@gmail.com>
960462460639 I noticed the user count was missing and added this, borrowing a bit of code from toys/posix/who.c.
Jeroen van Rijn <jvrnix@gmail.com>
parents: 694
diff changeset
6
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
7
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
8 USE_UPTIME(NEWTOY(uptime, NULL, TOYFLAG_USR|TOYFLAG_BIN))
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
9
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
10 config UPTIME
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
11 bool "uptime"
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
12 default y
1565
a3500bd8b322 Fixups for the android/bionic build probes patch.
Rob Landley <rob@landley.net>
parents: 1564
diff changeset
13 depends on TOYBOX_UTMPX
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
14 help
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
15 usage: uptime
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
16
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
17 Tell how long the system has been running and the system load
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
18 averages for the past 1, 5 and 15 minutes.
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
19 */
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
20
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
21 #include "toys.h"
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
22
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
23 void uptime_main(void)
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
24 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
25 struct sysinfo info;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
26 time_t tmptime;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
27 struct tm * now;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
28 unsigned int days, hours, minutes;
1565
a3500bd8b322 Fixups for the android/bionic build probes patch.
Rob Landley <rob@landley.net>
parents: 1564
diff changeset
29 struct utmpx *entry;
a3500bd8b322 Fixups for the android/bionic build probes patch.
Rob Landley <rob@landley.net>
parents: 1564
diff changeset
30 int users = 0;
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
31
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
32 // Obtain the data we need.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
33 sysinfo(&info);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
34 time(&tmptime);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
35 now = localtime(&tmptime);
1564
685a0da6ca59 probe for getspnam(), forkpty(), utmpx, replace sethostname()
Isaac Dunham <ibid.ag@gmail.com>
parents: 1095
diff changeset
36
1095
960462460639 I noticed the user count was missing and added this, borrowing a bit of code from toys/posix/who.c.
Jeroen van Rijn <jvrnix@gmail.com>
parents: 694
diff changeset
37 // Obtain info about logged on users
1565
a3500bd8b322 Fixups for the android/bionic build probes patch.
Rob Landley <rob@landley.net>
parents: 1564
diff changeset
38 setutxent();
a3500bd8b322 Fixups for the android/bionic build probes patch.
Rob Landley <rob@landley.net>
parents: 1564
diff changeset
39 while ((entry = getutxent())) if (entry->ut_type == USER_PROCESS) users++;
a3500bd8b322 Fixups for the android/bionic build probes patch.
Rob Landley <rob@landley.net>
parents: 1564
diff changeset
40 endutxent();
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
41
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
42 // Time
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
43 xprintf(" %02d:%02d:%02d up ", now->tm_hour, now->tm_min, now->tm_sec);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
44 // Uptime
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
45 info.uptime /= 60;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
46 minutes = info.uptime%60;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
47 info.uptime /= 60;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
48 hours = info.uptime%24;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
49 days = info.uptime/24;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
50 if (days) xprintf("%d day%s, ", days, (days!=1)?"s":"");
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
51 if (hours) xprintf("%2d:%02d, ", hours, minutes);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
52 else printf("%d min, ", minutes);
1565
a3500bd8b322 Fixups for the android/bionic build probes patch.
Rob Landley <rob@landley.net>
parents: 1564
diff changeset
53 printf(" %d user%s, ", users, (users!=1) ? "s" : "");
1095
960462460639 I noticed the user count was missing and added this, borrowing a bit of code from toys/posix/who.c.
Jeroen van Rijn <jvrnix@gmail.com>
parents: 694
diff changeset
54 printf(" load average: %.02f, %.02f, %.02f\n", info.loads[0]/65536.0,
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
55 info.loads[1]/65536.0, info.loads[2]/65536.0);
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
56 }