top: correct a small typo in manual page
[procps:procps.git] / uptime.c
1 /*
2  * uptime.c - display system uptime
3  * Copyright (C) 2012 Craig Small <csmall-procps@enc.com.au>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <time.h>
25 #include <sys/time.h>
26
27 #include "c.h"
28 #include "fileutils.h"
29 #include "nls.h"
30 #include "proc/sysinfo.h"
31 #include "proc/whattime.h"
32 #include "proc/version.h"
33
34 static void print_uptime_since()
35 {
36         double now, uptime_secs, idle_secs;
37         time_t up_since_secs;
38         struct tm *up_since;
39         struct timeval tim;
40
41         /* Get the current time and convert it to a double */
42         gettimeofday(&tim, NULL);
43         now = tim.tv_sec + (tim.tv_usec / 1000000.0);
44
45         /* Get the uptime and calculate when that was */
46         uptime(&uptime_secs, &idle_secs);
47         up_since_secs = (time_t) ((now - uptime_secs) + 0.5);
48
49         /* Show this */
50         up_since = localtime(&up_since_secs);
51         printf("%04d-%02d-%02d %02d:%02d:%02d\n",
52                 up_since->tm_year + 1900, up_since->tm_mon + 1, up_since->tm_mday,
53                 up_since->tm_hour, up_since->tm_min, up_since->tm_sec);
54 }
55
56 static void __attribute__ ((__noreturn__)) usage(FILE * out)
57 {
58         fputs(USAGE_HEADER, out);
59         fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
60         fputs(USAGE_OPTIONS, out);
61         fputs(_(" -p, --pretty   show uptime in pretty format\n"), out);
62         fputs(USAGE_HELP, out);
63         fputs(_(" -s, --since    system up since\n"), out);
64         fputs(USAGE_VERSION, out);
65         fprintf(out, USAGE_MAN_TAIL("uptime(1)"));
66
67         exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
68 }
69
70 int main(int argc, char **argv)
71 {
72         int c, p = 0;
73
74         static const struct option longopts[] = {
75                 {"pretty", no_argument, NULL, 'p'},
76                 {"help", no_argument, NULL, 'h'},
77                 {"since", no_argument, NULL, 's'},
78                 {"version", no_argument, NULL, 'V'},
79                 {NULL, 0, NULL, 0}
80         };
81
82 #ifdef HAVE_PROGRAM_INVOCATION_NAME
83         program_invocation_name = program_invocation_short_name;
84 #endif
85         setlocale (LC_ALL, "");
86         bindtextdomain(PACKAGE, LOCALEDIR);
87         textdomain(PACKAGE);
88         atexit(close_stdout);
89
90         while ((c = getopt_long(argc, argv, "phsV", longopts, NULL)) != -1)
91                 switch (c) {
92                 case 'p':
93                         p = 1;
94                         break;
95                 case 'h':
96                         usage(stdout);
97                 case 's':
98                         print_uptime_since();
99                         return EXIT_SUCCESS;
100                 case 'V':
101                         printf(PROCPS_NG_VERSION);
102                         return EXIT_SUCCESS;
103                 default:
104                         usage(stderr);
105                 }
106
107         print_uptime(p);
108         return EXIT_SUCCESS;
109 }