summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author諏訪子 <suwako@076.moe>2024-06-19 23:52:56 +0900
committer諏訪子 <suwako@076.moe>2024-06-19 23:52:56 +0900
commitb6e3f6cb5d1051777c35988f201e6730525e3389 (patch)
tree98679f1995f7b103f3314ce54fdf0d1c76df6dbd /src
parent8f378429801162d690dc68c0f15b483c1d291838 (diff)
掃除
Diffstat (limited to 'src')
-rw-r--r--src/common.c60
-rw-r--r--src/common.h7
-rw-r--r--src/cpu.c36
-rw-r--r--src/cpu.h2
-rw-r--r--src/gpu.c52
-rw-r--r--src/host.c7
-rw-r--r--src/memory.c49
-rw-r--r--src/os.c53
-rw-r--r--src/os.h4
-rw-r--r--src/packages.c57
-rw-r--r--src/packages.h2
-rw-r--r--src/recording.c30
-rw-r--r--src/recording.h4
-rw-r--r--src/resolution.c51
-rw-r--r--src/uptime.c28
-rw-r--r--src/user.c62
-rw-r--r--src/user.h4
17 files changed, 141 insertions, 367 deletions
diff --git a/src/common.c b/src/common.c
new file mode 100644
index 0000000..acb0a45
--- /dev/null
+++ b/src/common.c
@@ -0,0 +1,60 @@
+#include "common.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+long long int run_command_lld(const char *command) {
+ char buf[128];
+ long long int res = 0;
+
+ FILE *p = popen(command, "r");
+ if (!p) {
+ fprintf(stderr, "コマンドを実効に失敗: %s", command);
+ return 0;
+ }
+
+ while (fgets(buf, sizeof(buf), p) != NULL) {
+ buf[strcspn(buf, "\n")] = '\0';
+ }
+
+ res = atoll(buf);
+ pclose(p);
+
+ return res;
+}
+
+const char *run_command_s(const char *command) {
+ char buf[128];
+ char *out = NULL;
+ size_t outsize = 0;
+
+ FILE *p = popen(command, "r");
+ if (!p) {
+ fprintf(stderr, "コマンドを実効に失敗: %s", command);
+ return NULL;
+ }
+
+ while (fgets(buf, sizeof(buf), p) != NULL) {
+ buf[strcspn(buf, "\n")] = '\0';
+
+ size_t len = strlen(buf);
+ char *nout = realloc(out, outsize + len + 1);
+ if (nout == NULL) {
+ perror("メモリの役割に失敗");
+ free(out);
+ pclose(p);
+ return NULL;
+ }
+
+ out = nout;
+
+ memccpy(out + outsize, buf, sizeof(buf), len);
+ outsize += len;
+ out[outsize] = '\0';
+ }
+
+ pclose(p);
+
+ return out;
+}
diff --git a/src/common.h b/src/common.h
new file mode 100644
index 0000000..7955ffe
--- /dev/null
+++ b/src/common.h
@@ -0,0 +1,7 @@
+#ifndef COMMON_H
+#define COMMON_H
+
+long long int run_command_lld(const char *command);
+const char *run_command_s(const char *command);
+
+#endif
diff --git a/src/cpu.c b/src/cpu.c
index c6c7a6b..bf02346 100644
--- a/src/cpu.c
+++ b/src/cpu.c
@@ -1,37 +1,17 @@
#include "cpu.h"
+#include "common.h"
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-
-void run_cpu_command(const char *command) {
- char buf[128];
-
- FILE *p = popen(command, "r");
- if (!p) {
- fprintf(stderr, "CPUコマンドを実効に失敗: %s", command);
- return;
- }
-
- while (fgets(buf, sizeof(buf), p) != NULL) {
- buf[strcspn(buf, "\n")] = '\0';
- printf("%s", buf);
- }
-
- pclose(p);
-}
-
-void display_cpu() {
+const char *display_cpu() {
#if defined(__NetBSD__)
- run_cpu_command("sysctl -n machdep.cpu_brand | sed 's/(R)//' | "
+ return run_command_s("sysctl -n machdep.cpu_brand | sed 's/(R)//' | "
"sed 's/(TM)//' | sed 's/CPU //' | sed 's/Processor//'");
- run_cpu_command("echo \" (\" && sysctl -n hw.ncpu && echo \" core)\"");
+ return run_command_s("echo \" (\" && sysctl -n hw.ncpu && echo \" core)\"");
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
- run_cpu_command("sysctl -n hw.model | sed 's/(R)//' | "
+ return run_command_s("sysctl -n hw.model | sed 's/(R)//' | "
"sed 's/(TM)//' | sed 's/CPU //' | sed 's/Processor//'");
- run_cpu_command("echo \" (\" && sysctl -n hw.ncpu && echo \" core)\"");
+ return run_command_s("echo \" (\" && sysctl -n hw.ncpu && echo \" core)\"");
#elif defined(__linux__)
- run_cpu_command("cat /proc/cpuinfo | awk -F '\\\\s*: | @' "
+ return run_command_s("cat /proc/cpuinfo | awk -F '\\\\s*: | @' "
"'/model name|Hardware|Processor|^cpu model|chip type|^cpu type/ { "
"cpu=$2; if ($1 == \"Hardware\") exit } END { print cpu }' | "
"sed 's/(R)//' | sed 's/(TM)//' | sed 's/CPU //' | sed 's/ Processor//' | "
@@ -62,6 +42,6 @@ void display_cpu() {
}
fclose(fp);
- run_cpu_command("echo \"GHz (\" && nproc && echo \" core)\"");
+ return run_command_s("echo \"GHz (\" && nproc && echo \" core)\"");
#endif
}
diff --git a/src/cpu.h b/src/cpu.h
index 89a810a..055764e 100644
--- a/src/cpu.h
+++ b/src/cpu.h
@@ -1,6 +1,6 @@
#ifndef CPU_H
#define CPU_H
-void display_cpu();
+const char *display_cpu();
#endif
diff --git a/src/gpu.c b/src/gpu.c
index 5873dd5..f2b641b 100644
--- a/src/gpu.c
+++ b/src/gpu.c
@@ -1,59 +1,25 @@
#include "gpu.h"
+#include "common.h"
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
+#if !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__FreeBSD__) &&\
+ !defined(__linux__) && !defined(__DragonFly__)
#include <unistd.h>
-
-const char *run_gpu_command(const char *command) {
- char buf[128];
- char *out = NULL;
- size_t outsize = 0;
-
- FILE *p = popen(command, "r");
- if (!p) {
- fprintf(stderr, "GPUコマンドを実効に失敗: %s", command);
- return NULL;
- }
-
- while (fgets(buf, sizeof(buf), p) != NULL) {
- buf[strcspn(buf, "\n")] = '\0';
-
- size_t len = strlen(buf);
- char *nout = realloc(out, outsize + len + 1);
- if (nout == NULL) {
- perror("メモリの役割に失敗");
- free(out);
- pclose(p);
- return NULL;
- }
-
- out = nout;
-
- memccpy(out + outsize, buf, sizeof(buf), len);
- outsize += len;
- out[outsize] = '\0';
- }
-
- pclose(p);
-
- return out;
-}
+#endif
const char *display_gpu() {
#if defined(__OpenBSD__)
- return run_gpu_command("dmesg | grep -i graphics | sed 's/^.* \"//' | "
+ return run_command_s("dmesg | grep -i graphics | sed 's/^.* \"//' | "
"sed 's/\".*$//'");
#elif defined(__NetBSD__)
- return run_gpu_command("dmesg | grep -i graphics | sed 's/^.*: //' | "
+ return run_command_s("dmesg | grep -i graphics | sed 's/^.*: //' | "
"sed 's/ (.*$//'");
#elif defined(__FreeBSD__) || defined(__DragonFly__)
- return run_gpu_command("pciconf -lv | grep -B 4 -F \"VGA\" | "
+ return run_command_s("pciconf -lv | grep -B 4 -F \"VGA\" | "
"grep -F \"device\" | sed 's/^.* device//' | "
"sed \"s/^.* '//\" | sed \"s/'//\" | tail -1 | "
"sed 's/ Core Processor Integrated Graphics Controller//'");
#elif defined(__linux__)
- return run_gpu_command("lspci | grep VGA | sed 's/^.*: //' | "
+ return run_command_s("lspci | grep VGA | sed 's/^.*: //' | "
"sed 's/Corporation //' | sed 's/ (.*$//' | "
"sed 's/Advanced Micro Devices//' | "
"sed 's/, Inc. //' | sed 's/Navi [0-9]* //' | "
@@ -67,7 +33,7 @@ const char *display_gpu() {
access("/usr/X11R7/bin/glxinfo", F_OK) == -1 &&
access("/usr/pkg/bin/glxinfo", F_OK) == -1
) return NULL;
- return run_gpu_command("glxinfo -B | grep -F 'OpenGL renderer string' | "
+ return run_command_s("glxinfo -B | grep -F 'OpenGL renderer string' | "
"sed 's/OpenGL renderer string: //' | sed 's/Mesa //' | "
"sed 's/DRI //' | sed 's/(R)//' | sed 's/(.*$//'");
#endif
diff --git a/src/host.c b/src/host.c
index 03b2f2c..ee88778 100644
--- a/src/host.c
+++ b/src/host.c
@@ -6,8 +6,6 @@
#if defined(__linux__)
#include <unistd.h>
-
-int skip = 0;
#endif
const char *run_host_command(const char *command) {
@@ -38,8 +36,7 @@ const char *run_host_command(const char *command) {
strstr(buf, "INVALID") != NULL ||
strstr(buf, "All Series") != NULL
) {
- skip = 1;
- break;
+ return NULL;
}
#endif
@@ -113,7 +110,7 @@ void display_host_model() {
if (cmd2) {
const char *model = run_host_command(cmd2);
- if (!skip) printf(" %s", model);
+ if (model) printf(" %s", model);
}
#elif defined(__APPLE__)
printf("%s", run_host_command("sysctl -n hw.model"));
diff --git a/src/memory.c b/src/memory.c
index 7fa55be..879fef7 100644
--- a/src/memory.c
+++ b/src/memory.c
@@ -1,60 +1,39 @@
#include "memory.h"
+#include "common.h"
#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-
-long long int run_mem_command(const char *command) {
- char buf[128];
- long long int res = 0;
-
- FILE *p = popen(command, "r");
- if (!p) {
- fprintf(stderr, "メモリコマンドを実効に失敗: %s", command);
- return 0;
- }
-
- while (fgets(buf, sizeof(buf), p) != NULL) {
- buf[strcspn(buf, "\n")] = '\0';
- }
-
- res = atoll(buf);
- pclose(p);
-
- return res;
-}
void display_memory() {
long long int memused = 0;
long long int memtotal = 0;
#if defined(__OpenBSD__)
- memused = run_mem_command("vmstat | awk 'END {printf $3}' | sed 's/M//'");
- memtotal = run_mem_command("sysctl -n hw.physmem") / 1024LL / 1024LL;
+ memused = run_command_lld("vmstat | awk 'END {printf $3}' | sed 's/M//'");
+ memtotal = run_command_lld("sysctl -n hw.physmem") / 1024LL / 1024LL;
#elif defined(__FreeBSD__) || defined(__DragonFly__)
// 13M Active, 200M Inact, 184M Laundry,
- long long int used1 = run_mem_command("top | grep \"Mem:\" | awk '{print $2}' | "
+ long long int used1 = run_command_lld("top | grep \"Mem:\" | awk '{print $2}' | "
"sed 's/M//'");
- long long int used2 = run_mem_command("top | grep \"Mem:\" | awk '{print $4}' | "
+ long long int used2 = run_command_lld("top | grep \"Mem:\" | awk '{print $4}' | "
"sed 's/M//'");
- long long int used3 = run_mem_command("top | grep \"Mem:\" | awk '{print $6}' | "
+ long long int used3 = run_command_lld("top | grep \"Mem:\" | awk '{print $6}' | "
"sed 's/M//'");
- long long int used4 = run_mem_command("top | grep \"ARC:\" | awk '{print $6}' | "
+ long long int used4 = run_command_lld("top | grep \"ARC:\" | awk '{print $6}' | "
"sed 's/M//'");
memused = used1 + used2 + used3 + used4;
- memtotal = run_mem_command("sysctl -n hw.physmem") / 1024LL / 1024LL;
+ memtotal = run_command_lld("sysctl -n hw.physmem") / 1024LL / 1024LL;
#elif defined(__NetBSD__)
- memused = run_mem_command("top | grep \"Memory:\" | awk '{print $2}' | "
+ memused = run_command_lld("top | grep \"Memory:\" | awk '{print $2}' | "
"sed 's/M//'");
- memtotal = run_mem_command("sysctl -n hw.physmem64") / 1024LL / 1024LL;
+ memtotal = run_command_lld("sysctl -n hw.physmem64") / 1024LL / 1024LL;
#elif defined(__minix)
- memtotal = run_mem_command("sysctl -n hw.physmem") / 1024LL / 1024LL;
+ memtotal = run_command_lld("sysctl -n hw.physmem") / 1024LL / 1024LL;
#elif defined(__linux__)
- long long int memaval = run_mem_command("cat /proc/meminfo | grep MemAvailable | "
+ long long int memaval = run_command_lld("cat /proc/meminfo | grep MemAvailable | "
"awk '{print $2}'") / 1024LL;
- memtotal = run_mem_command("cat /proc/meminfo | grep MemTotal | "
+ memtotal = run_command_lld("cat /proc/meminfo | grep MemTotal | "
"awk '{print $2}'") / 1024LL;
memused = memtotal - memaval;
- /* memused = run_mem_command("cat /proc/meminfo | grep MemFree | "
+ /* memused = run_command_lld("cat /proc/meminfo | grep MemFree | "
"awk '{print $2}'") / 1024LL; */
#endif
diff --git a/src/os.c b/src/os.c
index 4662acd..e1c15f4 100644
--- a/src/os.c
+++ b/src/os.c
@@ -1,52 +1,7 @@
#include "os.h"
+#include "common.h"
-#include <stdio.h>
-#include <string.h>
-
-void display_os_name() {
- char buf[64];
- FILE *p = popen("uname -s", "r");
- if (!p) {
- fprintf(stderr, "「uname」コマンドを実効に失敗");
- return;
- }
-
- while (fgets(buf, sizeof(buf), p) != NULL) {
- buf[strcspn(buf, "\n")] = '\0';
- printf("%s", buf);
- }
-
- pclose(p);
-}
-
-void display_os_vers() {
- char buf[16];
- FILE *p = popen("uname -r", "r");
- if (!p) {
- fprintf(stderr, "「uname」コマンドを実効に失敗");
- return;
- }
-
- while (fgets(buf, sizeof(buf), p) != NULL) {
- buf[strcspn(buf, "\n")] = '\0';
- printf("%s", buf);
- }
-
- pclose(p);
-}
-
-void display_os_arch() {
- char buf[16];
- FILE *p = popen("uname -m", "r");
- if (!p) {
- fprintf(stderr, "「uname」コマンドを実効に失敗");
- return;
- }
-
- while (fgets(buf, sizeof(buf), p) != NULL) {
- buf[strcspn(buf, "\n")] = '\0';
- printf("%s", buf);
- }
-
- pclose(p);
+const char *display_os() {
+ return run_command_s("uname -s && echo \" \" && uname -r && "
+ "echo \" \" && uname -m");
}
diff --git a/src/os.h b/src/os.h
index 690ed47..4943036 100644
--- a/src/os.h
+++ b/src/os.h
@@ -1,8 +1,6 @@
#ifndef OS_H
#define OS_H
-void display_os_name();
-void display_os_vers();
-void display_os_arch();
+const char *display_os();
#endif
diff --git a/src/packages.c b/src/packages.c
index e89eb82..3505815 100644
--- a/src/packages.c
+++ b/src/packages.c
@@ -1,65 +1,22 @@
#include "packages.h"
+#include "common.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#if defined(__linux__)
#include <unistd.h>
-#if defined(__linux__)
#include "distro.h"
-
-/*const char *distroname;*/
#endif
-const char *run_package_command(const char *command) {
- char buf[64];
- char *out = NULL;
- size_t outsize = 0;
-
- FILE *p = popen(command, "r");
- if (!p) {
- fprintf(stderr, "パッケージコマンドを実効に失敗: %s", command);
- return NULL;
- }
-
- while (fgets(buf, sizeof(buf), p) != NULL) {
- buf[strcspn(buf, "\n")] = '\0';
-
- size_t len = strlen(buf);
- char *nout = realloc(out, outsize + len + 1);
- if (nout == NULL) {
- perror("メモリの役割に失敗");
- free(out);
- pclose(p);
- return NULL;
- }
-
- out = nout;
-
- memccpy(out + outsize, buf, sizeof(buf), len);
- outsize += len;
- out[outsize] = '\0';
- }
-
- pclose(p);
-
- return out;
-}
-
-void display_packages() {
+const char *display_packages() {
#if defined(__OpenBSD__) || defined(__NetBSD__)
- printf("%s (pkg_info)", run_package_command("pkg_info -a | wc -l | "
- "sed \"s/ //g\""));
+ return run_command_s("pkg_info -a | wc -l | sed \"s/ //g\"");
#elif defined(__FreeBSD__) || defined(__DragonFly__)
- printf("%s (pkg)", run_package_command("pkg info -a | wc -l | "
- "sed \"s/ //g\""));
+ return run_command_s("pkg info -a | wc -l | sed \"s/ //g\"");
#elif defined(__linux__)
if (access("/bin/xbps-query", F_OK) != -1) {
- printf("%s (xbps-query)", run_package_command("xbps-query -l | wc -l | "
- "sed \"s/ //g\""));
+ return run_command_s("xbps-query -l | wc -l | sed \"s/ //g\"");
} else if (access("/usr/bin/dpkg-query", F_OK) != -1) {
- printf("%s (dpkg)", run_package_command("dpkg-query -f '.\n' -W | wc -l | "
- "sed \"s/ //g\""));
+ return run_command_s("dpkg-query -f '.\n' -W | wc -l | sed \"s/ //g\"");
}
#endif
}
diff --git a/src/packages.h b/src/packages.h
index 0733f34..5a1a29f 100644
--- a/src/packages.h
+++ b/src/packages.h
@@ -1,6 +1,6 @@
#ifndef PACKAGES_H
#define PACKAGES_H
-void display_packages();
+const char *display_packages();
#endif
diff --git a/src/recording.c b/src/recording.c
index 8ed3d98..459b1f7 100644
--- a/src/recording.c
+++ b/src/recording.c
@@ -1,32 +1,12 @@
#if defined(__OpenBSD__)
#include "recording.h"
+#include "common.h"
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-
-void run_rec_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", strncmp(buf, "0", strlen(buf)) ? "off" : "on");
- }
-
- pclose(p);
-}
-
-void display_recording_audio() {
- run_rec_command("sysctl -n kern.audio.record");
+const char *display_recording_audio() {
+ return run_command_s("sysctl -n kern.audio.record");
}
-void display_recording_video() {
- run_rec_command("sysctl -n kern.video.record");
+const char *display_recording_video() {
+ return run_command_s("sysctl -n kern.video.record");
}
#endif
diff --git a/src/recording.h b/src/recording.h
index bf68106..99fe7e7 100644
--- a/src/recording.h
+++ b/src/recording.h
@@ -2,8 +2,8 @@
#ifndef RECORDING_H
#define RECORDING_H
-void display_recording_audio();
-void display_recording_video();
+const char *display_recording_audio();
+const char *display_recording_video();
#endif
#endif
diff --git a/src/resolution.c b/src/resolution.c
index 1d9fde3..f089fd2 100644
--- a/src/resolution.c
+++ b/src/resolution.c
@@ -1,49 +1,10 @@
#include "resolution.h"
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-const char *run_res_command(const char *command) {
- char buf[128];
- char *out = NULL;
- size_t outsize = 0;
-
- FILE *p = popen(command, "r");
- if (!p) {
- fprintf(stderr, "解像度コマンドを実効に失敗: %s", command);
- return NULL;
- }
-
- while (fgets(buf, sizeof(buf), p) != NULL) {
- buf[strcspn(buf, "\n")] = '\0';
-
- size_t len = strlen(buf);
- char *nout = realloc(out, outsize + len + 1);
- if (nout == NULL) {
- perror("メモリの役割に失敗");
- free(out);
- pclose(p);
- return NULL;
- }
-
- out = nout;
-
- memccpy(out + outsize, buf, sizeof(buf), len);
- outsize += len;
- out[outsize] = '\0';
- }
-
- pclose(p);
-
- return out;
-}
+#include "common.h"
const char *display_resolution() {
- return run_res_command("xrandr --nograb --current | "
- "awk -F 'connected |\\\\+|\\\\(' '/ "
- "connected.*[0-9]+x[0-9]+\\+/ && $2 {printf $2 "
- "\", \"}' | sed 's/primary //' | "
- "sed 's/,\\([^,]*\\)$/\\1/'");
+ return run_command_s("xrandr --nograb --current | "
+ "awk -F 'connected |\\\\+|\\\\(' '/ "
+ "connected.*[0-9]+x[0-9]+\\+/ && $2 {printf $2 "
+ "\", \"}' | sed 's/primary //' | "
+ "sed 's/,\\([^,]*\\)$/\\1/'");
}
diff --git a/src/uptime.c b/src/uptime.c
index 22b8356..56ea6b7 100644
--- a/src/uptime.c
+++ b/src/uptime.c
@@ -1,33 +1,13 @@
#include "uptime.h"
+#include "common.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\"");
+ printf("%s", run_command_s("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\"");
+ printf("%s", run_command_s("uptime | awk '{print $5}' | sed 's/,//' | "
+ "sed 's/:/ hours, /' && echo \" mins\""));
}
diff --git a/src/user.c b/src/user.c
index a3805d5..0f11cec 100644
--- a/src/user.c
+++ b/src/user.c
@@ -1,64 +1,18 @@
#include "user.h"
+#include "common.h"
-#include <stdio.h>
-#include <string.h>
-
-void run_user_command(const char *command) {
- char buf[64];
-#if defined(__NetBSD__) || defined(__FreeBSD__)
- FILE *p = popen(command, "r");
- if (!p) {
- snprintf(buf, sizeof(buf), "「%s」コマンドを見つけられません。", command);
- perror(buf);
- return;
- }
-
- while (fgets(buf, sizeof(buf), p) != NULL) {
- printf("%s", buf);
- }
-
- pclose(p);
- //
-#else
- FILE *f = fopen(command, "r");
- if (!f) {
- snprintf(buf, sizeof(buf), "「%s」ファイルを見つけられません。", command);
- perror(buf);
- return;
- }
-
- while (fgets(buf, sizeof(buf), f) != NULL) {
- printf("%s", buf);
- }
-
- fclose(f);
-#endif
-}
-
-void display_user_name() {
- char buf[128];
- FILE *p = popen("whoami", "r");
- if (!p) {
- fprintf(stderr, "「whoami」コマンドを実効に失敗");
- return;
- }
-
- while (fgets(buf, sizeof(buf), p) != NULL) {
- buf[strcspn(buf, "\n")] = '\0';
- printf("%s", buf);
- }
-
- pclose(p);
+const char *display_user_name() {
+ return run_command_s("whoami");
}
-void display_user_host() {
+const char *display_user_host() {
#if defined(__OpenBSD__)
- run_user_command("/etc/myname");
+ return run_command_s("cat /etc/myname");
#elif defined(__NetBSD__)
- run_user_command("hostname");
+ return run_command_s("hostname");
#elif defined(__FreeBSD__)
- run_user_command("sysctl -n kern.hostname");
+ return run_command_s("sysctl -n kern.hostname");
#else
- run_user_command("/etc/hostname");
+ return run_command_s("cat /etc/hostname");
#endif
}
diff --git a/src/user.h b/src/user.h
index fc63853..a9f4d97 100644
--- a/src/user.h
+++ b/src/user.h
@@ -1,7 +1,7 @@
#ifndef USER_H
#define USER_H
-void display_user_name();
-void display_user_host();
+const char *display_user_name();
+const char *display_user_host();
#endif