From b48f40f863c80c9f8394cfd5e8c3716191611888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Tue, 18 Jun 2024 19:12:24 +0900 Subject: =?UTF-8?q?=E3=83=91=E3=83=83=E3=82=B1=E3=83=BC=E3=82=B8=E6=83=85?= =?UTF-8?q?=E5=A0=B1=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 7 +++++++ src/distro.c | 9 ++++++++- src/host.c | 2 +- src/logo/openbsd.h | 2 ++ src/packages.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/packages.h | 6 ++++++ 6 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/packages.c create mode 100644 src/packages.h diff --git a/main.c b/main.c index 69a62fe..6a13b72 100644 --- a/main.c +++ b/main.c @@ -12,6 +12,7 @@ #if defined(__OpenBSD__) #include "src/recording.h" #endif +#include "src/packages.h" #include "src/cpu.h" #include "src/memory.h" @@ -113,6 +114,12 @@ int main(int argc, char *argv[]) { lc++; #endif + printf("%s ", LOGO[lc]); + printf(COLOR"%s%s"RESET, "Packages", ": "); + display_packages(); + printf("\n"); + lc++; + printf("%s ", LOGO[lc]); printf(COLOR"%s%s"RESET, "CPU", ": "); display_cpu(); diff --git a/src/distro.c b/src/distro.c index 845f85e..f39d1ea 100644 --- a/src/distro.c +++ b/src/distro.c @@ -51,9 +51,16 @@ void display_distro() { while (fgets(buf, sizeof(buf), p) != NULL) { buf[strcspn(buf, "\n")] = '\0'; printf("%s", buf); - distroname = buf; } + if (strstr(buf, "Devuan") != NULL) distroname = "devuan"; + else if (strstr(buf, "Void Linux") != NULL) distroname = "void"; + else if (strstr(buf, "Debian") != NULL) distroname = "debian"; + else if (strstr(buf, "Arch Linux") != NULL) distroname = "arch"; + else if (strstr(buf, "Artix Linux") != NULL) distroname = "artix"; + else if (strstr(buf, "CRUX") != NULL) distroname = "crux"; + else distroname = "linux"; + pclose(p); } #endif diff --git a/src/host.c b/src/host.c index 010af9e..03b2f2c 100644 --- a/src/host.c +++ b/src/host.c @@ -54,7 +54,7 @@ const char *run_host_command(const char *command) { out = nout; - memcpy(out + outsize, buf, len); + memccpy(out + outsize, buf, sizeof(buf), len); outsize += len; out[outsize] = '\0'; } diff --git a/src/logo/openbsd.h b/src/logo/openbsd.h index 8a58da2..bbd4df4 100644 --- a/src/logo/openbsd.h +++ b/src/logo/openbsd.h @@ -41,5 +41,7 @@ YELLOW " | " RESET "O O" YELLOW" | " RESET, YELLOW " |_ < ) 3 ) " RESET, YELLOW " / \\ / " RESET, YELLOW " /-_____-\\ " RESET, +YELLOW " " RESET, +YELLOW " " RESET, YELLOW " " RESET }; diff --git a/src/packages.c b/src/packages.c new file mode 100644 index 0000000..835aac4 --- /dev/null +++ b/src/packages.c @@ -0,0 +1,48 @@ +#include "packages.h" + +#include +#include +#include + +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() { +#if defined(__OpenBSD__) || defined(__NetBSD__) + printf("%s (pkg_info)", run_package_command("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\"")); +#endif +} diff --git a/src/packages.h b/src/packages.h new file mode 100644 index 0000000..0733f34 --- /dev/null +++ b/src/packages.h @@ -0,0 +1,6 @@ +#ifndef PACKAGES_H +#define PACKAGES_H + +void display_packages(); + +#endif -- cgit v1.2.3