summaryrefslogtreecommitdiff
path: root/example/mock/uptime/uptime.c
blob: 77776a781f97b9e1d54af34e1f0902abbbe12d0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright 2018 Andreas Scheider <asn@cryptomilk.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

#include "proc_uptime.h"

static char *calc_uptime(void)
{
    uint32_t up_minutes, up_hours, up_days, up_weeks, up_years;
    ssize_t pos = 0;
    size_t comma = 0;
    double uptime_secs, idle_secs;
    char buf[1024] = {0};
    int up;

    up = uptime("/proc/uptime", &uptime_secs, &idle_secs);
    if (up == 0) {
        return NULL;
    }

    up_years = ((uint32_t)uptime_secs / (60 * 60 * 24 * 365)) % 10;
    up_weeks = ((uint32_t)uptime_secs / (60 * 60 * 24 * 7)) % 52;
    up_days = ((uint32_t)uptime_secs / (60 * 60 * 24)) % 7;

    pos += snprintf(buf + pos, sizeof(buf) - pos, "up ");

    up_minutes = (uint32_t)uptime_secs / 60;
    up_hours = up_minutes / 60;
    up_hours = up_hours % 24;
    up_minutes = up_minutes % 60;

    if (up_years > 0) {
        pos += snprintf(buf + pos, sizeof(buf) - pos,
                        "%u %s",
                        up_years,
                        up_years > 1 ? "years" : "year");
        comma++;
    }

    if (up_weeks > 0) {
        pos += snprintf(buf + pos, sizeof(buf) - pos,
                        "%s%u %s",
                        comma > 0 ? ", " : "",
                        up_weeks,
                        up_weeks > 1 ? "weeks" : "week");
        comma++;
    }

    if (up_days > 0) {
        pos += snprintf(buf + pos, sizeof(buf) - pos,
                        "%s%u %s",
                        comma > 0 ? ", " : "",
                        up_days,
                        up_days > 1 ? "days" : "day");
        comma++;
    }

    if (up_hours > 0) {
        pos += snprintf(buf + pos, sizeof(buf) - pos,
                        "%s%u %s",
                        comma > 0 ? ", " : "",
                        up_hours,
                        up_hours > 1 ? "hours" : "hour");
        comma++;
    }

    if (up_minutes > 0 || (up_minutes == 0 && uptime_secs < 60)) {
        pos += snprintf(buf + pos, sizeof(buf) - pos,
                        "%s%u %s",
                        comma > 0 ? ", " : "",
                        up_minutes,
                        up_minutes != 1 ? "minutes" : "minute");
        comma++;
    }

    return strdup(buf);
}

#ifndef UNIT_TESTING
int main(void)
{
    char *uptime_str = NULL;

    uptime_str = calc_uptime();
    if (uptime_str == NULL) {
        fprintf(stderr, "Failed to read uptime\n");
        return 1;
    }

    printf("%s\n", uptime_str);

    free(uptime_str);

    return 0;
}
#endif /* UNIT_TESTING */