~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to plugin_uptime.c

  • Committer: Reinhard Tartler
  • Date: 2011-04-27 17:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 750.
  • Revision ID: siretart@tauware.de-20110427172415-6n4aptmvmz0eztvm
Tags: upstream-0.11.0~svn1143
ImportĀ upstreamĀ versionĀ 0.11.0~svn1143

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: plugin_uptime.c 840 2007-09-09 12:17:42Z michael $
 
2
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/plugin_uptime.c $
 
3
 *
 
4
 * plugin for uptime
 
5
 *
 
6
 * Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at>
 
7
 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 
8
 *
 
9
 * This file is part of LCD4Linux.
 
10
 *
 
11
 * LCD4Linux is free software; you can redistribute it and/or modify
 
12
 * it under the terms of the GNU General Public License as published by
 
13
 * the Free Software Foundation; either version 2, or (at your option)
 
14
 * any later version.
 
15
 *
 
16
 * LCD4Linux is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 * GNU General Public License for more details.
 
20
 *
 
21
 * You should have received a copy of the GNU General Public License
 
22
 * along with this program; if not, write to the Free Software
 
23
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
24
 *
 
25
 */
 
26
 
 
27
/* 
 
28
 * exported functions:
 
29
 *
 
30
 * int plugin_init_uptime (void)
 
31
 *  adds functions for uptime
 
32
 *
 
33
 */
 
34
 
 
35
 
 
36
#include "config.h"
 
37
 
 
38
#include <stdlib.h>
 
39
#include <stdio.h>
 
40
#include <string.h>
 
41
#include <errno.h>
 
42
#include <unistd.h>
 
43
#include <sys/types.h>
 
44
#include <sys/stat.h>
 
45
#include <sys/time.h>
 
46
#include <fcntl.h>
 
47
 
 
48
#include "debug.h"
 
49
#include "plugin.h"
 
50
 
 
51
static int fd = -2;
 
52
 
 
53
 
 
54
static char *itoa(char *buffer, const size_t size, unsigned int value)
 
55
{
 
56
    char *p;
 
57
 
 
58
    /* sanity checks */
 
59
    if (buffer == NULL || size < 2)
 
60
        return (NULL);
 
61
 
 
62
    /* p points to last char */
 
63
    p = buffer + size - 1;
 
64
 
 
65
    /* set terminating zero */
 
66
    *p = '\0';
 
67
 
 
68
    do {
 
69
        *--p = value % 10 + '0';
 
70
        value = value / 10;
 
71
    } while (value != 0 && p > buffer);
 
72
 
 
73
    return p;
 
74
}
 
75
 
 
76
 
 
77
char *struptime(const unsigned int uptime, const char *format)
 
78
{
 
79
    static char string[256];
 
80
    const char *src;
 
81
    char *dst;
 
82
    int len, size;
 
83
 
 
84
    src = format;
 
85
    dst = string;
 
86
    len = 0;
 
87
 
 
88
    /* leave room for terminating zero  */
 
89
    size = sizeof(string) - 1;
 
90
 
 
91
    while (len < size) {
 
92
 
 
93
        if (*src == '%') {
 
94
            src++;
 
95
 
 
96
            if (strchr("sSmMhHd", *src) != NULL) {
 
97
                char buffer[12], *s;
 
98
                unsigned int value = 0;
 
99
                int leading_zero = 0;
 
100
                switch (*src++) {
 
101
                case 's':
 
102
                    value = uptime;
 
103
                    break;
 
104
                case 'S':
 
105
                    value = uptime % 60;
 
106
                    leading_zero = 1;
 
107
                    break;
 
108
                case 'm':
 
109
                    value = uptime / 60;
 
110
                    break;
 
111
                case 'M':
 
112
                    value = (uptime / 60) % 60;
 
113
                    leading_zero = 1;
 
114
                    break;
 
115
                case 'h':
 
116
                    value = uptime / 60 / 60;
 
117
                    break;
 
118
                case 'H':
 
119
                    value = (uptime / 60 / 60) % 24;
 
120
                    leading_zero = 1;
 
121
                    break;
 
122
                case 'd':
 
123
                    value = uptime / 60 / 60 / 24;
 
124
                    break;
 
125
                }
 
126
 
 
127
                if (leading_zero && value < 10) {
 
128
                    len++;
 
129
                    *dst++ = '0';
 
130
                }
 
131
 
 
132
                s = itoa(buffer, sizeof(buffer), value);
 
133
                while (len < size && *s != '\0') {
 
134
                    len++;
 
135
                    *dst++ = *s++;
 
136
                }
 
137
 
 
138
            } else if (*src == '%') {
 
139
                len++;
 
140
                *dst++ = '%';
 
141
 
 
142
            } else {
 
143
                len += 2;
 
144
                *dst++ = '%';
 
145
                *dst++ = *src++;
 
146
            }
 
147
 
 
148
        } else {
 
149
            len++;
 
150
            *dst++ = *src;
 
151
            if (*src++ == '\0')
 
152
                break;
 
153
        }
 
154
    }
 
155
 
 
156
    /* enforce terminating zero */
 
157
    if (len >= size && *(dst - 1) != '\0') {
 
158
        len++;
 
159
        *dst = '\0';
 
160
    }
 
161
 
 
162
    return string;
 
163
}
 
164
 
 
165
 
 
166
double getuptime(void)
 
167
{
 
168
    char buffer[36];
 
169
    int i;
 
170
 
 
171
    if (fd == -2)
 
172
        fd = open("/proc/uptime", O_RDONLY);
 
173
    if (fd < 0)
 
174
        return -1;
 
175
 
 
176
    lseek(fd, 0, SEEK_SET);
 
177
 
 
178
    i = read(fd, buffer, sizeof(buffer) - 1);
 
179
    if (i < 0)
 
180
        return -1;
 
181
 
 
182
    buffer[i - 1] = '\0';
 
183
 
 
184
    /* ignore the 2nd value from /proc/uptime */
 
185
    return strtod(buffer, NULL);
 
186
}
 
187
 
 
188
 
 
189
static void my_uptime(RESULT * result, const int argc, RESULT * argv[])
 
190
{
 
191
    int age;
 
192
    static double uptime = 0.0;
 
193
    static struct timeval last_value;
 
194
    struct timeval now;
 
195
 
 
196
    if (argc > 1) {
 
197
        error("uptime(): wrong number of parameters");
 
198
        SetResult(&result, R_STRING, "");
 
199
        return;
 
200
    }
 
201
 
 
202
    gettimeofday(&now, NULL);
 
203
 
 
204
    age = (now.tv_sec - last_value.tv_sec) * 1000 + (now.tv_usec - last_value.tv_usec) / 1000;
 
205
    /* reread every 100 msec only */
 
206
    if (fd == -2 || age == 0 || age > 100) {
 
207
        uptime = getuptime();
 
208
        if (uptime < 0.0) {
 
209
            error("parse(/proc/uptime) failed!");
 
210
            SetResult(&result, R_STRING, "");
 
211
            return;
 
212
        }
 
213
 
 
214
        last_value = now;
 
215
    }
 
216
 
 
217
    if (argc == 0) {
 
218
        SetResult(&result, R_NUMBER, &uptime);
 
219
    } else {
 
220
        SetResult(&result, R_STRING, struptime(uptime, R2S(argv[0])));
 
221
    }
 
222
 
 
223
    return;
 
224
 
 
225
}
 
226
 
 
227
int plugin_init_uptime(void)
 
228
{
 
229
    AddFunction("uptime", -1, my_uptime);
 
230
    return 0;
 
231
}
 
232
 
 
233
void plugin_exit_uptime(void)
 
234
{
 
235
    if (fd > 0)
 
236
        close(fd);
 
237
    fd = -2;
 
238
}