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 --- src/distro.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'src/distro.c') 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 -- cgit v1.2.3