summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author諏訪子 <suwako@076.moe>2024-06-19 19:57:33 +0900
committer諏訪子 <suwako@076.moe>2024-06-19 19:57:33 +0900
commite8b7158d34bda940b31fca56c2c97b586d6ee937 (patch)
treeff03c51d5582d29b0d3020b12951eef2e599810b
parentb40fc45106967a7fd58581c6218f70b3b376712f (diff)
Linux: ディストロのロゴの表示
-rw-r--r--main.c6
-rw-r--r--src/distro.c34
-rw-r--r--src/distro.h3
-rw-r--r--src/logo/linux.c5
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 <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
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 <stdio.h>
#include <string.h>
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;
}