annotate toys/other/uptime.c @ 1322:b91284c2e569 draft

Make "losetup /dev/loop0 filename" work. Sigh. Implement the complex cases and you screw up the simple cases you already tested...
author Rob Landley <rob@landley.net>
date Thu, 29 May 2014 06:29:12 -0500
parents 960462460639
children 685a0da6ca59
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
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
13 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
14 usage: uptime
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
15
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
16 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
17 averages for the past 1, 5 and 15 minutes.
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
18 */
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 #include "toys.h"
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
21
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
22 void uptime_main(void)
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
23 {
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
24 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
25 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
26 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
27 unsigned int days, hours, minutes;
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
28 struct utmpx *entry;
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
29 int users = 0;
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
30
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
31 // 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
32 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
33 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
34 now = localtime(&tmptime);
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
35 // Obtain info about logged on users
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
36 setutxent();
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 while ((entry = getutxent())) if (entry->ut_type == USER_PROCESS) users++;
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
38 endutxent();
466
9f1e089262cb Adding free and uptime
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
39
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
40 // 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
41 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
42 // 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
43 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
44 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
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 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
47 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
48 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
49 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
50 else printf("%d min, ", minutes);
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
51 printf(" %d user%s, ", users, (users!=1) ? "s" : "");
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
52 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
53 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
54 }