suwako / farfetch アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

master
ブランチ master
farfetch / src / distro.c
7.1 KB、180行 生 Blame 履歴
1#if defined(__linux__) || defined(__sun)
2#include "distro.h"
3#include "common.h"
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9
10const char *distroname;
11
12bool is_distro(const char *buf) {
13#if defined(__sunos)
14 if (strncmp(buf, "openindiana") == 0) return 1;
15 else if (strncmp(buf, "omnios") == 0) return 1;
16 else if (strncmp(buf, "solaris") == 0) return 1;
17#else
18 if (strncmp(buf, "alpine", sizeof("alpine")) == 0) return 1;
19 else if (strncmp(buf, "arch", sizeof("arch")) == 0) return 1;
20 else if (strncmp(buf, "arco", sizeof("arco")) == 0) return 1;
21 else if (strncmp(buf, "artix", sizeof("artix")) == 0) return 1;
22 else if (strncmp(buf, "centos", sizeof("centos")) == 0) return 1;
23 else if (strncmp(buf, "crux", sizeof("crux")) == 0) return 1;
24 else if (strncmp(buf, "debian", sizeof("debian")) == 0) return 1;
25 else if (strncmp(buf, "devuan", sizeof("devuan")) == 0) return 1;
26 else if (strncmp(buf, "fedora", sizeof("fedora")) == 0) return 1;
27 else if (strncmp(buf, "gentoo", sizeof("gentoo")) == 0) return 1;
28 else if (strncmp(buf, "hyperbola", sizeof("hyperbola")) == 0) return 1;
29 else if (strncmp(buf, "linuxmint", sizeof("linuxmint")) == 0) return 1;
30 else if (strncmp(buf, "manjaro", sizeof("manjaro")) == 0) return 1;
31 else if (strncmp(buf, "opensuse", sizeof("opensuse")) == 0) return 1;
32 else if (strncmp(buf, "parabola", sizeof("parabola")) == 0) return 1;
33 else if (strncmp(buf, "popos", sizeof("popos")) == 0) return 1;
34 else if (strncmp(buf, "postmarketos", sizeof("postmarketos")) == 0) return 1;
35 else if (strncmp(buf, "redhat", sizeof("redhat")) == 0) return 1;
36 else if (strncmp(buf, "rocky", sizeof("rocky")) == 0) return 1;
37 else if (strncmp(buf, "ubuntu", sizeof("ubuntu")) == 0) return 1;
38 else if (strncmp(buf, "void", sizeof("void")) == 0) return 1;
39 else if (strncmp(buf, "linux", sizeof("linux"))) return 1;
40#endif
41
42 return 0;
43}
44
45const char *display_distro() {
46 char buf[1288];
47 char *out = NULL;
48 size_t outsize = 0;
49
50 const char *cmd = from_cache("/tmp/farfetch/distro");
51
52 if (cmd) {
53 return cmd;
54 }
55
56 if (access("/bedrock/etc/bedrock-release", F_OK) != -1) {
57 cmd = "cat /bedrock/etc/bedrock-release | grep '^PRETTY_NAME' | "
58 "cut -d '=' -f2 | tr -d '\"'";
59 } else if (access("/etc/redstar-release", F_OK) != -1) {
60 cmd = "echo \"Red Star OS\" && cat /etc/redstar-release | "
61 "awk -F'[^0-9]' '$0=$2'";
62 } else if (access("/etc/siduction-version", F_OK) != -1) {
63 cmd = "echo \"Siduction\" && lsb_release -sic | "
64 "grep '^PRETTY_NAME' | cut -d '=' -f2 | tr -d '\"'";
65 } else if (access("/etc/mcst_version", F_OK) != -1) {
66 cmd = "echo \"OS Elbrus\" && cat /etc/mcst_version | "
67 "grep '^PRETTY_NAME' | cut -d '=' -f2 | tr -d '\"'";
68 } else if (access("/etc/GoboLinuxVersion", F_OK) != -1) {
69 cmd = "echo \"GoboLinux\" && cat /etc/GoboLinuxVersion | "
70 "grep '^PRETTY_NAME' | cut -d '=' -f2 | tr -d '\"'";
71 } else if (access("/etc/os-release", F_OK) != -1) {
72 cmd = "if grep -q \"^PRETTY_NAME=\" /etc/os-release; then "
73 "grep '^PRETTY_NAME=' /etc/os-release | cut -d \"=\" -f2 | tr -d '\"'; "
74 "else "
75 "grep '^NAME=' /etc/os-release | cut -d \"=\" -f2 | tr -d '\"'; "
76 "fi";
77 } else if (access("/usr/lib/os-release", F_OK) != -1) {
78 cmd = "cat /usr/lib/os-release | "
79 "grep '^PRETTY_NAME' | cut -d '=' -f2 | tr -d '\"'";
80 } else if (access("/etc/openwrt_release", F_OK) != -1) {
81 cmd = "cat /etc/openwrt_release | "
82 "grep '^PRETTY_NAME' | cut -d '=' -f2 | tr -d '\"'";
83 } else if (access("/etc/lsb-release", F_OK) != -1) {
84 cmd = "cat /etc/lsb-release | "
85 "grep '^DISTRIB_DESCRIPTION' | cut -d '=' -f2 | tr -d '\"'";
86 } else {
87 perror("不明なディストリビューション。");
88 }
89
90 FILE *p = popen(cmd, "r");
91 if (!p) {
92 fprintf(stderr, "ディストロを見つけられるコマンドを実効に失敗: %s", cmd);
93 return NULL;
94 }
95
96 while (fgets(buf, sizeof(buf), p) != NULL) {
97 buf[strcspn(buf, "\n")] = '\0';
98
99 size_t len = strlen(buf);
100 char *nout = realloc(out, outsize + len + 1);
101 if (nout == NULL) {
102 perror("メモリの役割に失敗");
103 free(out);
104 pclose(p);
105 return NULL;
106 }
107
108 out = nout;
109 memccpy(out + outsize, buf, sizeof(buf), len);
110 outsize += len;
111 out[outsize] = '\0';
112 }
113
114 to_cache("/tmp/farfetch/distro", out);
115 pclose(p);
116
117 return out;
118}
119
120void get_distro() {
121 const char *buf = display_distro();
122 if (!buf) {
123#if defined(__sunos)
124 distroname = "solaris";
125#else
126 distroname = "linux";
127#endif
128 return;
129 }
130
131 if (strstr(buf, "Alpine") != NULL) distroname = "alpine";
132 else if (strstr(buf, "Arch Linux") != NULL) distroname = "arch";
133 else if (strstr(buf, "ArcoLinux") != NULL) distroname = "arco";
134 else if (strstr(buf, "Artix Linux") != NULL) distroname = "artix";
135 else if (strstr(buf, "CentOS") != NULL) distroname = "centos";
136 else if (strstr(buf, "CRUX") != NULL) distroname = "crux";
137 else if (strstr(buf, "Debian") != NULL) distroname = "debian";
138 else if (strstr(buf, "Devuan") != NULL) distroname = "devuan";
139 else if (strstr(buf, "Fedora") != NULL) distroname = "fedora";
140 else if (strstr(buf, "Gentoo") != NULL) distroname = "gentoo";
141 else if (strstr(buf, "Hyperbola") != NULL) distroname = "hyperbola";
142 else if (strstr(buf, "Linux Mint") != NULL) distroname = "linuxmint";
143 else if (strstr(buf, "Manjaro") != NULL) distroname = "manjaro";
144 else if (strstr(buf, "OpenIndiana") != NULL) distroname = "openindiana";
145 else if (strstr(buf, "openSUSE") != NULL) distroname = "opensuse";
146 else if (strstr(buf, "OmniOS") != NULL) distroname = "omnios";
147 else if (strstr(buf, "Parabola") != NULL) distroname = "parabola";
148 else if (strstr(buf, "Pop!_OS") != NULL) distroname = "popos";
149 else if (strstr(buf, "postmarketOS") != NULL) distroname = "postmarketos";
150 else if (strstr(buf, "Red Hat") != NULL) distroname = "redhat";
151 else if (strstr(buf, "Rocky") != NULL) distroname = "rocky";
152 else if (strstr(buf, "Ubuntu") != NULL) distroname = "ubuntu";
153 else if (strstr(buf, "Void Linux") != NULL) distroname = "void";
154#if defined(__sunos)
155 else distroname = "solaris";
156#else
157 else distroname = "linux";
158#endif
159
160 if (strncmp(distroname, "ubuntu", strlen("ubuntu")) == 0) {
161 const char *desktop = run_command_s("echo $XDG_CURRENT_DESKTOP");
162 if (strncmp(desktop, "KDE", strlen("KDE")) == 0) distroname = "kubuntu";
163 else if (strncmp(desktop, "XFCE", strlen("XFCE")) == 0) distroname = "xubuntu";
164 else if (strncmp(desktop, "LXQt", strlen("LXQt")) == 0) distroname = "lubuntu";
165 else if (strncmp(desktop, "MATE", strlen("MATE")) == 0) distroname = "ubuntumate";
166 else if (strncmp(desktop, "X-Cinnamon", strlen("X-Cinnamon")) == 0)
167 distroname = "ubuntucinnamon";
168 else if (strncmp(desktop, "Budgie:GNOME", strlen("Budgie:GNOME")) == 0)
169 distroname = "ubuntubudgie";
170 else if (strncmp(desktop, "Lomiri", strlen("Lomiri")) == 0)
171 distroname = "ubuntulomiri";
172 else if (strncmp(
173 desktop,
174 "Unity:Unity7:ubuntu",
175 strlen("Unity:Unity7:ubuntu")
176 ) == 0)
177 distroname = "ubuntuunity";
178 }
179}
180#endif