Bump to procps-ng 3.3.16
[platform/upstream/procps-ng.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         if (gettimeofday(&tim, NULL) != 0)
43                 xerr(EXIT_FAILURE, "gettimeofday");
44         now = tim.tv_sec + (tim.tv_usec / 1000000.0);
45
46         /* Get the uptime and calculate when that was */
47         if (uptime(&uptime_secs, &idle_secs) == 0)
48                 xerrx(EXIT_FAILURE, "uptime");
49         up_since_secs = (time_t) ((now - uptime_secs) + 0.5);
50
51         /* Show this */
52         if ((up_since = localtime(&up_since_secs)) == NULL)
53                 xerrx(EXIT_FAILURE, "localtime");
54         printf("%04d-%02d-%02d %02d:%02d:%02d\n",
55                 up_since->tm_year + 1900, up_since->tm_mon + 1, up_since->tm_mday,
56                 up_since->tm_hour, up_since->tm_min, up_since->tm_sec);
57 }
58
59 static void __attribute__ ((__noreturn__)) usage(FILE * out)
60 {
61         fputs(USAGE_HEADER, out);
62         fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
63         fputs(USAGE_OPTIONS, out);
64         fputs(_(" -p, --pretty   show uptime in pretty format\n"), out);
65         fputs(USAGE_HELP, out);
66         fputs(_(" -s, --since    system up since\n"), out);
67         fputs(USAGE_VERSION, out);
68         fprintf(out, USAGE_MAN_TAIL("uptime(1)"));
69
70         exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
71 }
72
73 int main(int argc, char **argv)
74 {
75         int c, p = 0;
76
77         static const struct option longopts[] = {
78                 {"pretty", no_argument, NULL, 'p'},
79                 {"help", no_argument, NULL, 'h'},
80                 {"since", no_argument, NULL, 's'},
81                 {"version", no_argument, NULL, 'V'},
82                 {NULL, 0, NULL, 0}
83         };
84
85 #ifdef HAVE_PROGRAM_INVOCATION_NAME
86         program_invocation_name = program_invocation_short_name;
87 #endif
88         setlocale (LC_ALL, "");
89         bindtextdomain(PACKAGE, LOCALEDIR);
90         textdomain(PACKAGE);
91         atexit(close_stdout);
92
93         while ((c = getopt_long(argc, argv, "phsV", longopts, NULL)) != -1)
94                 switch (c) {
95                 case 'p':
96                         p = 1;
97                         break;
98                 case 'h':
99                         usage(stdout);
100                 case 's':
101                         print_uptime_since();
102                         return EXIT_SUCCESS;
103                 case 'V':
104                         printf(PROCPS_NG_VERSION);
105                         return EXIT_SUCCESS;
106                 default:
107                         usage(stderr);
108                 }
109
110         print_uptime(p);
111         return EXIT_SUCCESS;
112 }