uptime.c 2.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * uptime.c - display system uptime
 * Copyright (C) 2012 Craig Small <csmall-procps@enc.com.au>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

20 21 22
#include <errno.h>
#include <getopt.h>
#include <stdlib.h>
23
#include <stdio.h>
Frank's avatar
Frank committed
24 25
#include <time.h>
#include <sys/time.h>
26 27

#include "c.h"
28
#include "fileutils.h"
29
#include "nls.h"
Frank's avatar
Frank committed
30
#include "proc/sysinfo.h"
csmall's avatar
csmall committed
31 32 33
#include "proc/whattime.h"
#include "proc/version.h"

Frank's avatar
Frank committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
static void print_uptime_since()
{
	double now, uptime_secs, idle_secs;
	time_t up_since_secs;
	struct tm *up_since;
	struct timeval tim;

	/* Get the current time and convert it to a double */
	gettimeofday(&tim, NULL);
	now = tim.tv_sec + (tim.tv_usec / 1000000.0);

	/* Get the uptime and calculate when that was */
	uptime(&uptime_secs, &idle_secs);
	up_since_secs = (time_t) ((now - uptime_secs) + 0.5);

	/* Show this */
	up_since = localtime(&up_since_secs);
	printf("%04d-%02d-%02d %02d:%02d:%02d\n",
		up_since->tm_year + 1900, up_since->tm_mon + 1, up_since->tm_mday,
		up_since->tm_hour, up_since->tm_min, up_since->tm_sec);
}

56
static void __attribute__ ((__noreturn__)) usage(FILE * out)
57
{
58 59 60
	fputs(USAGE_HEADER, out);
	fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
	fputs(USAGE_OPTIONS, out);
61
	fputs(_(" -p, --pretty   show uptime in pretty format\n"), out);
62
	fputs(USAGE_HELP, out);
Frank's avatar
Frank committed
63
	fputs(_(" -s, --since    system up since\n"), out);
64 65
	fputs(USAGE_VERSION, out);
	fprintf(out, USAGE_MAN_TAIL("uptime(1)"));
66 67 68 69 70 71

	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}

int main(int argc, char **argv)
{
72
	int c, p = 0;
73 74

	static const struct option longopts[] = {
75
		{"pretty", no_argument, NULL, 'p'},
76
		{"help", no_argument, NULL, 'h'},
Frank's avatar
Frank committed
77
		{"since", no_argument, NULL, 's'},
78 79 80 81
		{"version", no_argument, NULL, 'V'},
		{NULL, 0, NULL, 0}
	};

82
#ifdef HAVE_PROGRAM_INVOCATION_NAME
83
	program_invocation_name = program_invocation_short_name;
84
#endif
85 86 87
	setlocale (LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
88
	atexit(close_stdout);
89

90
	while ((c = getopt_long(argc, argv, "phsV", longopts, NULL)) != -1)
91
		switch (c) {
92 93 94
		case 'p':
			p = 1;
			break;
95 96
		case 'h':
			usage(stdout);
Frank's avatar
Frank committed
97 98 99
		case 's':
			print_uptime_since();
			return EXIT_SUCCESS;
100
		case 'V':
101
			printf(PROCPS_NG_VERSION);
102 103 104 105 106
			return EXIT_SUCCESS;
		default:
			usage(stderr);
		}

107
	print_uptime(p);
108
	return EXIT_SUCCESS;
csmall's avatar
csmall committed
109
}