From e8b7158d34bda940b31fca56c2c97b586d6ee937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Wed, 19 Jun 2024 19:57:33 +0900 Subject: =?UTF-8?q?Linux:=20=E3=83=87=E3=82=A3=E3=82=B9=E3=83=88=E3=83=AD?= =?UTF-8?q?=E3=81=AE=E3=83=AD=E3=82=B4=E3=81=AE=E8=A1=A8=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 6 +++--- src/distro.c | 34 +++++++++++++++++++++++++++++----- src/distro.h | 3 ++- src/logo/linux.c | 5 +++-- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/main.c b/main.c index 11d5f9f..b6e7531 100644 --- a/main.c +++ b/main.c @@ -35,6 +35,7 @@ int main(int argc, char *argv[]) { #include "src/logo/freebsd.h" getOS(); #elif defined(__linux__) + get_distro(); #include "src/logo/linux.h" getDistro(distroname); #else @@ -102,9 +103,8 @@ int main(int argc, char *argv[]) { #if defined(__linux__) printf("%s ", LOGO[lc]); - printf(COLOR"%s%s"RESET, "Distro", ": "); - display_distro(); - printf("\n"); + printf("%s%s%s"RESET, color, "Distro", ": "); + printf("%s\n", display_distro()); lc++; #endif diff --git a/src/distro.c b/src/distro.c index f39d1ea..784b76e 100644 --- a/src/distro.c +++ b/src/distro.c @@ -2,13 +2,17 @@ #include "distro.h" #include +#include #include #include const char *distroname; -void display_distro() { +const char *display_distro() { char buf[1288]; + char *out = NULL; + size_t outsize = 0; + const char *cmd = NULL; if (access("/bedrock/etc/bedrock-release", F_OK) != -1) { @@ -45,14 +49,36 @@ void display_distro() { FILE *p = popen(cmd, "r"); if (!p) { fprintf(stderr, "ディストロを見つけられるコマンドを実効に失敗: %s", cmd); - return; + return NULL; } while (fgets(buf, sizeof(buf), p) != NULL) { buf[strcspn(buf, "\n")] = '\0'; - printf("%s", buf); + + 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 get_distro() { + const char *buf = display_distro(); + if (strstr(buf, "Devuan") != NULL) distroname = "devuan"; else if (strstr(buf, "Void Linux") != NULL) distroname = "void"; else if (strstr(buf, "Debian") != NULL) distroname = "debian"; @@ -60,7 +86,5 @@ void display_distro() { 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/distro.h b/src/distro.h index 3897338..3dc6eb3 100644 --- a/src/distro.h +++ b/src/distro.h @@ -2,7 +2,8 @@ #ifndef DISTRO_H #define DISTRO_H -void display_distro(); +const char *display_distro(); +void get_distro(); extern const char *distroname; diff --git a/src/logo/linux.c b/src/logo/linux.c index 1f16959..8d13740 100644 --- a/src/logo/linux.c +++ b/src/logo/linux.c @@ -1,6 +1,7 @@ #if defined(__linux__) #include "linux.h" +#include #include char *LOGO[23]; @@ -213,8 +214,8 @@ void getDistro(const char *distroname) { LOGO[14] = MAGENTA " `'''` " RESET; LOGO[15] = MAGENTA " " RESET; LOGO[16] = MAGENTA " " RESET; - LOGO[17] = MA8ENTA " " RESET; - for (int i = 19; i < 23; i++) { + LOGO[17] = MAGENTA " " RESET; + for (int i = 18; i < 23; i++) { LOGO[i] = MAGENTA " " RESET; } -- cgit v1.2.3