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

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

void run_uptime_command(const char *command) {
  char buf[128];

  FILE *p = popen(command, "r");
  if (!p) {
    fprintf(stderr, "起動時間コマンドを実効に失敗: %s", command);
    return;
  }

  while (fgets(buf, sizeof(buf), p) != NULL) {
    buf[strcspn(buf, "\n")] = '\0';
    printf("%s", buf);
  }

  pclose(p);
}

void display_days() {
  run_uptime_command("uptime | awk '{print $3}' && echo \" days\"");
}

void display_time() {
  /* run_uptime_command("uptime | awk '{print $3}' | sed 's/,//' | " */
  /*                    "sed 's/:/ hours, /' && echo \" mins\""); */
  run_uptime_command("uptime | awk '{print $5}' | sed 's/,//' | "
                     "sed 's/:/ hours, /' && echo \" mins\"");
}