summaryrefslogtreecommitdiff
path: root/src/uptime.c
blob: 269cdda290f5e3661cfad3edc4ebffbc762bd422 (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
#include "uptime.h"
#include "common.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char *display_days() {
  const char *days = run_command_s("uptime | awk '{for (i=1; i<=NF; i++) "
                                   "if ($i ~ /day/) print $(i-1), $i}'");

  if (days == NULL || strlen(days) == 0) {
    return strndup("0 days,", 7);
  }

  return days;
}

const char *display_time() {
  const char *uptime = run_command_s("uptime");
  if (uptime == NULL) {
    return NULL;
  }

  char *days = strstr(uptime, "day");
  int nodays = 0;
  if (days == NULL) {
    nodays = 1;
    days = strstr(uptime, "days");
  }

  free((void *)uptime);

  if (nodays) {
    return run_command_s("uptime | awk '{print $3}' | sed 's/,//' | "
                         "sed 's/:/ hours, /' && echo \" mins\"");
  }

  return run_command_s("uptime | awk '{print $5}' | sed 's/,//' | "
                         "sed 's/:/ hours, /' && echo \" mins\"");
}