diff --git a/main.c b/main.c index bf5b61e..d06846b 100644 --- a/main.c +++ b/main.c @@ -73,6 +73,7 @@ size_t logosize = 11; }; #endif + const char *reset = RESET; size_t ls = logosize <= MIN_SIZE ? MIN_SIZE : logosize; if (issmall) { size_t ne = sizeof(LOGO_SMALL) / sizeof(LOGO_SMALL[0]); @@ -83,45 +84,41 @@ size_t logosize = 11; } printf("%s ", LOGO[lc]); - printf("%s", titlecolor); - display_user_name(); - printf(RESET); - printf("@"); - printf("%s", titlecolor); - display_user_host(); - printf(RESET); - lc++; + if (display_user_name() || display_user_host()) { + printf( + "%s%s%s@%s%s%s\n", + titlecolor, display_user_name(), reset, + titlecolor, display_user_host(), reset + ); + lc++; + } + printf("%s ", LOGO[lc]); printf("------------------\n"); lc++; printf("%s ", LOGO[lc]); - printf("%s%s%s"RESET, color, "OS", ": "); - display_os_name(); - printf(" "); - display_os_vers(); - printf(" "); - display_os_arch(); - printf("\n"); - lc++; + if (display_os()) { + printf("%sOS%s: %s\n", color, reset, display_os()); + lc++; + } #if defined(__linux__) - if (display_distro() != NULL) { - printf("%s ", LOGO[lc]); - printf("%s%s%s"RESET, color, "Distro", ": "); - printf("%s\n", display_distro()); + printf("%s ", LOGO[lc]); + if (display_distro()) { + printf("%sDistro%s: %s\n", color, reset, display_distro()); lc++; } #endif printf("%s ", LOGO[lc]); - printf("%s%s%s"RESET, color, "Host", ": "); + printf("%s%s%s%s", color, "Host", reset, ": "); display_host_model(); printf("\n"); lc++; printf("%s ", LOGO[lc]); - printf("%s%s%s"RESET, color, "Uptime", ": "); + printf("%s%s%s%s", color, "Uptime", reset, ": "); display_days(); printf(", "); display_time(); @@ -130,43 +127,46 @@ size_t logosize = 11; #if defined(__OpenBSD__) printf("%s ", LOGO[lc]); - printf("%s%s%s"RESET, color, "Recording", ": "); - printf("audio = "); - display_recording_audio(); - printf(", video = "); - display_recording_video(); - printf("\n"); - lc++; + if (display_recording_audio() || display_recording_video()) { + printf("%sRecording%s: ", color, reset); + if (display_recording_audio()) { + printf("audio = %s", display_recording_audio()); + if (display_recording_video()) printf(", "); + } + if (display_recording_video()) { + printf("video = %s", display_recording_video()); + } + printf("\n"); + lc++; + } #endif printf("%s ", LOGO[lc]); - printf("%s%s%s"RESET, color, "Packages", ": "); - display_packages(); - printf("\n"); - lc++; + if (display_packages()) { + printf("%sPackages%s: %s\n", color, reset, display_packages()); + lc++; + } + printf("%s ", LOGO[lc]); if (display_resolution()) { - printf("%s ", LOGO[lc]); - printf("%s%s%s"RESET, color, "Resolution", ": "); - printf("%s\n", display_resolution()); + printf("%sResolution%s: %s\n", color, reset, display_resolution()); lc++; } printf("%s ", LOGO[lc]); - printf("%s%s%s"RESET, color, "CPU", ": "); - display_cpu(); - printf("\n"); - lc++; + if (display_cpu()) { + printf("%sCPU%s: %s\n", color, reset, display_cpu()); + lc++; + } + printf("%s ", LOGO[lc]); if (display_gpu()) { - printf("%s ", LOGO[lc]); - printf("%s%s%s"RESET, color, "GPU", ": "); - printf("%s\n", display_gpu()); + printf("%sGPU%s: %s\n", color, reset, display_gpu()); lc++; } printf("%s ", LOGO[lc]); - printf("%s%s%s"RESET, color, "Memory", ": "); + printf("%s%s%s%s", color, "Memory", reset, ": "); display_memory(); printf("\n"); lc++; 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 +#include +#include + +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 -#include -#include - -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 -#include -#include +#if !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__FreeBSD__) &&\ + !defined(__linux__) && !defined(__DragonFly__) #include - -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 - -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 -#include -#include - -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 -#include - -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 -#include -#include -#include +#include "common.h" #if defined(__linux__) -#include "distro.h" +#include -/*const char *distroname;*/ +#include "distro.h" #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 -#include -#include - -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); +const char *display_recording_audio() { + return run_command_s("sysctl -n kern.audio.record"); } -void display_recording_audio() { - run_rec_command("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 -#include -#include -#include - -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 -#include -#include - -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 -#include - -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 +const char *display_user_name() { + return run_command_s("whoami"); } -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); -} - -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