uptime.c
author Myhailo Danylenko <isbear@ukrpost.net>
Sun, 04 Apr 2010 15:45:42 +0300
changeset 0 3f69962cbbf4
child 1 2b0115cae8b2
permissions -rw-r--r--
Initial commit
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     1
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     2
/* Copyright 2010 Myhailo Danylenko
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     3
 *
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     4
 * This file is part of mcabber-uptime
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     5
 *
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     6
 * mcabber-uptime is free software: you can redistribute it and/or modify
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     8
 * the Free Software Foundation, either version 2 of the License, or
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     9
 * (at your option) any later version.
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    10
 *
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    14
 * GNU General Public License for more details.
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    15
 *
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. */
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    18
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    19
#include <glib.h>
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    20
#include <gmodule.h>
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    21
#include <time.h>
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    22
#include <stdio.h>
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    23
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    24
#include <mcabber/modules.h>
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    25
#include <mcabber/logprint.h>
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    26
#include <mcabber/commands.h>
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    27
#include <mcabber/settings.h>
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    28
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    29
#include "config.h"
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    30
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    31
#define DESCRIPTION ( "Shows mcabber uptime\nRecognizes option uptime_hz (at load time)." )
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    32
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    33
module_info_t info_uptime_experimental = {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    34
	.branch      = "experimental",
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    35
	.api         = 12,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    36
	.version     = PROJECT_VERSION,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    37
	.description = DESCRIPTION,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    38
	.requires    = NULL,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    39
	.init        = NULL,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    40
	.uninit      = NULL,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    41
	.next        = NULL,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    42
};
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    43
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    44
module_info_t info_uptime = {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    45
	.branch      = "dev",
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    46
	.api         = 11,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    47
	.version     = PROJECT_VERSION,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    48
	.description = DESCRIPTION,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    49
	.requires    = NULL,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    50
	.init        = NULL,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    51
	.uninit      = NULL,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    52
	.next        = &info_uptime_experimental,
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    53
};
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    54
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    55
time_t starttime = 0;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    56
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    57
void do_uptime (char *arg)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    58
{
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    59
	GString *line = g_string_new (NULL);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    60
	guint seconds = time (NULL) - starttime;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    61
	guint minutes = seconds / 60;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    62
	guint hours   = minutes / 60;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    63
	guint days    = hours   / 24;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    64
	seconds %= 60;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    65
	minutes %= 60;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    66
	hours   %= 24;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    67
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    68
	g_string_truncate (line, 0);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    69
	if (days)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    70
		g_string_append_printf (line, " %u days", days);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    71
	if (hours)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    72
		g_string_append_printf (line, " %u hours", hours);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    73
	if (minutes)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    74
		g_string_append_printf (line, " %u minutes", minutes);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    75
	if (seconds)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
		g_string_append_printf (line, " %u seconds", seconds);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    78
	scr_log_print (LPRINT_NORMAL, "Uptime: %s.", line -> len ? line -> str : " 0 seconds");
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    79
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    80
	g_string_free (line, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    81
}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    82
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    83
gchar *g_module_check_init (GModule *module)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    84
{
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    85
	if (settings_opt_get_int ("uptime_use_proc")) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    86
		long long unsigned int mstime = 0;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    87
		long long unsigned int kbtime = 0;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    88
		GError     *error   = NULL;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    89
		GString    *line;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    90
		GIOChannel *channel = g_io_channel_new_file ("/proc/self/stat", "r", &error);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    91
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    92
		if (!channel)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    93
			return "Cannot open own stats for reading";
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    94
		g_io_channel_set_close_on_unref (channel, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    95
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    96
		line = g_string_new (NULL);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    97
		if (g_io_channel_read_line_string (channel, line, NULL, &error) != G_IO_STATUS_NORMAL) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    98
			if (error) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    99
				scr_log_print (LPRINT_DEBUG, "uptime: Own stats reading error: %s.", error -> message);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   100
				g_error_free (error);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   101
			}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   102
			g_io_channel_unref (channel);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   103
			g_string_free (line, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   104
			return "Error reading own stats";
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   105
		}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   106
		g_io_channel_unref (channel);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   107
		gchar *p = strrchr (line -> str, ')'); // end of command
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   108
		if (!p) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   109
			g_string_free (line, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   110
			return "Missing ) in own stats"
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   111
		}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   112
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   113
		while (*p && isalpha (*p)) p++; // state (%c)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   114
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   115
		while (*p && isdigit (*p)) p++; // ppid (this and next are %d)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   116
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   117
		while (*p && isdigit (*p)) p++; // pgrp
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   118
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   119
		while (*p && isdigit (*p)) p++; // session
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   120
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   121
		while (*p && isdigit (*p)) p++; // tty_nr
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   122
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   123
		while (*p && isdigit (*p)) p++; // tpgid
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   124
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   125
		while (*p && isdigit (*p)) p++; // flags (this and all next - %u)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   126
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   127
		while (*p && isdigit (*p)) p++; // minflt
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   128
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   129
		while (*p && isdigit (*p)) p++; // cminflt
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   130
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   131
		while (*p && isdigit (*p)) p++; // majflt
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   132
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   133
		while (*p && isdigit (*p)) p++; // cmajflt
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   134
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   135
		while (*p && isdigit (*p)) p++; // utime
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   136
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   137
		while (*p && isdigit (*p)) p++; // stime
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   138
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   139
		while (*p && isdigit (*p)) p++; // cutime (this and next next are %d)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   140
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   141
		while (*p && isdigit (*p)) p++; // cstime
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   142
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   143
		while (*p && isdigit (*p)) p++; // priority
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   144
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   145
		while (*p && isdigit (*p)) p++; // nice
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   146
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   147
		while (*p && isdigit (*p)) p++; // num_threads
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   148
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   149
		while (*p && isdigit (*p)) p++; // itrealvalue
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   150
		while (*p && isspace (*p)) p++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   151
		char *u = p;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   152
		while (*u && isdigit (*u)) u++;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   153
		*u = '\0';
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   154
		if (!sscanf (p, "%llu", &mstime)) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   155
			scr_log_print (LPRINT_LOGNORM, "uptime: now at %u/%u, remaining string: %s.", p - line -> str, line -> len, p);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   156
			g_string_free (line, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   157
			return "Malformed own start time.";
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   158
		}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   159
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   160
		channel = g_io_channel_new_file ("/proc/stat", "r", &error);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   161
		if (!channel) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   162
			g_string_free (line, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   163
			return "Cannot open system stats for reading";
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   164
		}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   165
		g_io_channel_set_close_on_unref (channel, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   166
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   167
		while (TRUE) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   168
			GIOStatus ret = g_io_channel_read_line_string (channel, line, NULL, &error);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   169
			if (ret != G_IO_STATUS_NORMAL) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   170
				if (error) {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   171
					scr_log_print (LPRINT_DEBUG, "uptime: System stats reading error: %s.", error -> message);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   172
					g_error_free (error);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   173
				}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   174
				g_io_channel_unref (channel);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   175
				g_string_free (line, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   176
				return "Error reading system stats.";
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   177
			}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   178
			if (sscanf (line -> str, "btime %llu", &kbtime))
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   179
				break;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   180
		}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   181
		g_io_channel_unref (channel);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   182
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   183
		g_string_free (line, TRUE);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   184
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   185
		guint hz = settings_opt_get_int ("uptime_hz");
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   186
		if (!hz)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   187
			hz = 250;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   188
		starttime = kbtime + (mstime / hz);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   189
	} else {
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   190
		starttime = time (NULL);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   191
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   192
	cmd_add ("uptime", "", 0, 0, do_uptime, NULL);
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   193
	return NULL;
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   194
}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   195
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   196
void g_module_unload (GModule *module)
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   197
{
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   198
	cmd_del ("uptime");
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   199
}
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   200
3f69962cbbf4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   201
/* The End */