From cf13f2d8c1622a46f6499dcb762ea2b9370d99fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Mon, 17 Jun 2024 22:41:46 +0900 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E5=88=9D=E3=82=B3=E3=83=9F=E3=83=83?= =?UTF-8?q?=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ CHANGELOG.md | 2 ++ LICENSE.txt | 14 +++++++++ Makefile | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 28 ++++++++++++++++++ farfetch.conf | 3 ++ main.c | 53 +++++++++++++++++++++++++++++++++ src/distro.c | 48 ++++++++++++++++++++++++++++++ src/distro.h | 8 +++++ src/host.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ src/host.h | 6 ++++ src/os.c | 52 +++++++++++++++++++++++++++++++++ src/os.h | 8 +++++ src/user.c | 52 +++++++++++++++++++++++++++++++++ src/user.h | 7 +++++ 15 files changed, 441 insertions(+) create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE.txt create mode 100644 Makefile create mode 100644 README.md create mode 100644 farfetch.conf create mode 100644 main.c create mode 100644 src/distro.c create mode 100644 src/distro.h create mode 100644 src/host.c create mode 100644 src/host.h create mode 100644 src/os.c create mode 100644 src/os.h create mode 100644 src/user.c create mode 100644 src/user.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..41213c9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +farfetch +*.core diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..d339f8a --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,2 @@ +# 0.0.1 +* 開始 diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..c4d9131 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,14 @@ +Copyright © 2004-2011 by Internet Systems Consortium, Inc. ("ISC") +Copyright © 2018-2024 by 076.moe + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD +TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, +DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS +ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..97bcbc2 --- /dev/null +++ b/Makefile @@ -0,0 +1,81 @@ +UNAME_S != uname -s +UNAME_M != uname -m + +NAME != cat main.c | grep "const char \*sofname" | awk '{print $$5}' | \ + sed "s/\"//g" | sed "s/;//" +VERSION != cat main.c | grep "const char \*version" | awk '{print $$5}' | \ + sed "s/\"//g" | sed "s/;//" +PREFIX = /usr/local +.if ${UNAME_S} == "Linux" +PREFIX = /usr +.endif + +MANPREFIX = ${PREFIX}/share/man + +.if ${UNAME_S} == "OpenBSD" +MANPREFIX = ${PREFIX}/man +.endif + +CC = cc +FILES = main.c src/*.c +CFLAGS = -Wall -Wextra -O3 -I${PREFIX}/include -L${PREFIX}/lib +.if ${UNAME_S} == "NetBSD" +CFLAGS += -I/usr/pkg/include -L/usr/pkg/lib -I/usr/include -L/usr/lib +.endif +LDFLAGS = + +all: + ${CC} ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS} + strip ${NAME} + +clean: + rm -f ${NAME} + +dist: clean + mkdir -p dist + mkdir -p ${NAME}-${VERSION} + cp -R LICENSE.txt Makefile README.md CHANGELOG.md \ + ${NAME}-completion.zsh ${NAME}.1 main.c src ${NAME}-${VERSION} + tar zcfv dist/${NAME}-${VERSION}.tar.gz ${NAME}-${VERSION} + rm -rf ${NAME}-${VERSION} + +depend: + ${DEPS} + +release-openbsd: + mkdir -p release + ${CC} ${CFLAGS} -o release/${NAME}-${VERSION}-openbsd-${UNAME_M} ${FILES} \ + -static ${LDFLAGS} -lc + strip release/${NAME}-${VERSION}-openbsd-${UNAME_M} + +release-freebsd: + mkdir -p release + ${CC} ${CFLAGS} -o release/${NAME}-${VERSION}-freebsd-${UNAME_M} ${FILES} \ + -static ${LDFLAGS} -lc + strip release/${NAME}-${VERSION}-freebsd-${UNAME_M} + +release-netbsd: + mkdir -p release + export LD_LIBRARY_PATH=/usr/pkg/lib:/usr/local/lib:$LD_LIBRARY_PATH + ${CC} ${CFLAGS} -o release/${NAME}-${VERSION}-netbsd-${UNAME_M} ${FILES} \ + -static ${LDFLAGS} -lc + strip release/${NAME}-${VERSION}-netbsd-${UNAME_M} + +release-linux: + mkdir -p release + ${CC} ${CFLAGS} -o release/${NAME}-${VERSION}-linux-${UNAME_M} ${FILES} \ + -static ${LDFLAGS} -lc + strip release/${NAME}-${VERSION}-linux-${UNAME_M} + +install: all + mkdir -p ${DESTDIR}${PREFIX}/bin + cp -f ${NAME} ${DESTDIR}${PREFIX}/bin + chmod 755 ${DESTDIR}${PREFIX}/bin/${NAME} + mkdir -p ${DESTDIR}${MANPREFIX}/man1 + sed "s/VERSION/${VERSION}/g" < ${NAME}.1 > ${DESTDIR}${MANPREFIX}/man1/${NAME}.1 + chmod 644 ${DESTDIR}${MANPREFIX}/man1/${NAME}.1 + +uninstall: + rm -f ${DESTDIR}${PREFIX}/bin/${NAME} + +.PHONY: all clean dist install uninstall diff --git a/README.md b/README.md new file mode 100644 index 0000000..109dadc --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# farfetch + +## インストールする方法 | Installation +### CRUX +```sh +doas prt-get bmake +bmake +doas bmake install +``` + +### Artix +```sh +doas pacman -S base-devel bmake +bmake +doas bmake install +``` + +### OpenBSD +```sh +make +doas make install +``` + +### FreeBSD +```sh +make +doas make install +``` diff --git a/farfetch.conf b/farfetch.conf new file mode 100644 index 0000000..e05a807 --- /dev/null +++ b/farfetch.conf @@ -0,0 +1,3 @@ +logo "default" +show cpu +hide gpu diff --git a/main.c b/main.c new file mode 100644 index 0000000..5280995 --- /dev/null +++ b/main.c @@ -0,0 +1,53 @@ +#include +#include +#include + +#include "src/user.h" +#include "src/os.h" +#include "src/host.h" +#if defined(__linux__) +#include "src/distro.h" +#endif + +const char *sofname = "farfetch"; +const char *version = "0.0.1"; + +int main() { + display_user_name(); + printf("@"); + display_user_host(); + puts("------------------"); + + printf("OS: "); + display_os_name(); + printf(" "); + display_os_vers(); + printf(" "); + display_os_arch(); + printf("\n"); + + printf("Host: "); + display_host_model(); + printf("\n"); + +#if defined(__linux__) + printf("Distro: "); + display_distro(); + printf("\n"); +#endif + + // TODO: + // * ロゴ + // * カーネル(LinuxとIllumosのみ) + // * 起動時間 + // * パッケージ + // * libc + // * シェル + // * 解像度 + // * 端末 + // * CPU + // * GPU + // * メモリー + // * ストレージ + return 0; +} diff --git a/src/distro.c b/src/distro.c new file mode 100644 index 0000000..e94922b --- /dev/null +++ b/src/distro.c @@ -0,0 +1,48 @@ +#if defined(__linux__) +#include "distro.h" + +#include +#include + +#include + +void display_distro() { + char buf[64]; + const char *cmd; + + if (access("/bedrock/etc/bedrock-release", F_OK) != -1) { + cmd = "cat /bedrock/etc/bedrock-release"; + } else if (access("/etc/redstar-release", F_OK) != -1) { + cmd = "echo \"Red Star OS\" && cat /etc/redstar-release | awk -F'[^0-9]' '$0=$2'"; + } else if (access("/etc/siduction-version", F_OK) != -1) { + cmd = "echo \"Siduction\" && lsb_release -sic"; + } else if (access("/etc/mcst_version", F_OK) != -1) { + cmd = "echo \"OS Elbrus\" && cat /etc/mcst_version"; + } else if (access("/etc/GoboLinuxVersion", F_OK) != -1) { + cmd = "echo \"GoboLinux\" && cat /etc/GoboLinuxVersion"; + } else if (access("/etc/os-release", F_OK) != -1) { + cmd = "cat /etc/os-release"; + } else if (access("/usr/lib/os-release", F_OK) != -1) { + cmd = "cat /usr/lib/os-release"; + } else if (access("/etc/openwrt_release", F_OK) != -1) { + cmd = "cat /etc/openwrt_release"; + } else if (access("/etc/lsb-release", F_OK) != -1) { + cmd = "cat /etc/lsb-release"; + } else { + perror("不明なディストリビューション。"); + } + + FILE *p = popen(cmd, "r"); + if (!p) { + fprintf(stderr, "ディストロを見つけられるコマンドを実効に失敗: %s", cmd); + return; + } + + while (fgets(buf, sizeof(buf), p) != NULL) { + buf[strcspn(buf, "\n")] = '\0'; + printf("%s", buf); + } + + pclose(p); +} +#endif diff --git a/src/distro.h b/src/distro.h new file mode 100644 index 0000000..c4ecfd6 --- /dev/null +++ b/src/distro.h @@ -0,0 +1,8 @@ +#if defined(__linux__) +#ifndef DISTRO_H +#define DISTRO_H + +void display_distro(); + +#endif +#endif diff --git a/src/host.c b/src/host.c new file mode 100644 index 0000000..2cc02ff --- /dev/null +++ b/src/host.c @@ -0,0 +1,77 @@ +#include "host.h" + +#include +#include + +#if defined(__linux__) +#include +#elif defined(__APPLE__) +#include +#endif + +void run_command(const char *command) { + char buf[64]; + + FILE *p = popen(command, "r"); + if (!p) { + fprintf(stderr, "ホストコマンドを実効に失敗: %s", command); + return; + } + + while (fgets(buf, sizeof(buf), p) != NULL) { + buf[strcspn(buf, "\n")] = '\0'; + printf("%s", buf); + } + + pclose(p); +} + +void display_host_model() { +#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__minix) + run_command("sysctl -n hw.vendor hw.product"); +#elif defined(__sun) + run_command("prtconf -b | awk -F':' '/banner-name/ {printf $2}'"); +#elif defined(__linux__) + const char *cmd1, *cmd2; + if (access("/system/app/", F_OK) != -1) { + cmd1 = "getprop ro.product.brand"; + cmd2 = "getprop ro.product.model"; + } else if ( + access("/sys/devices/virtual/dmi/id/product_name", F_OK) != -1 || + access("/sys/devices/virtual/dmi/id/product_version", F_OK) != 1 + ) { + cmd1 = "cat /sys/devices/virtual/dmi/id/product_name"; + cmd2 = "cat /sys/devices/virtual/dmi/id/product_version"; + } else if (access("/sys/firmware/base/model", F_OK) != -1) { + cmd1 = "cat /sys/firmware/devicetree/base/model"; + } else if (access("/tmp/sysinfo/model", F_OK) != 1) { + cmd1 = "cat /tmp/sysinfo/model"; + } + + if (!cmd1) { + printf("Unknown"); + } else { + run_command(cmd1); + } + + if (cmd2) { + printf(" "); + run_command(cmd2); + } +#elif defined(__APPLE__) + run_command("sysctl -n hw.model"); + + FILE *p = popen("kextstat | grep -F -e \"FakeSMC\" -e \"VirtualSMC\"", "r"); + if (!p) { + fprintf(stderr, "ホストコマンドを実効に失敗"); + return; + } + + while (fgets(buf, sizeof(buf), p) != NULL) { + buf[strcspn(buf, "\n")] = '\0'; + printf("%s", buf); + } + + pclose(p); +#endif +} diff --git a/src/host.h b/src/host.h new file mode 100644 index 0000000..77b8ba5 --- /dev/null +++ b/src/host.h @@ -0,0 +1,6 @@ +#ifndef HOST_H +#define HOST_H + +void display_host_model(); + +#endif diff --git a/src/os.c b/src/os.c new file mode 100644 index 0000000..4662acd --- /dev/null +++ b/src/os.c @@ -0,0 +1,52 @@ +#include "os.h" + +#include +#include + +void display_os_name() { + char buf[64]; + FILE *p = popen("uname -s", "r"); + if (!p) { + fprintf(stderr, "「uname」コマンドを実効に失敗"); + return; + } + + while (fgets(buf, sizeof(buf), p) != NULL) { + buf[strcspn(buf, "\n")] = '\0'; + printf("%s", buf); + } + + pclose(p); +} + +void display_os_vers() { + char buf[16]; + FILE *p = popen("uname -r", "r"); + if (!p) { + fprintf(stderr, "「uname」コマンドを実効に失敗"); + return; + } + + while (fgets(buf, sizeof(buf), p) != NULL) { + buf[strcspn(buf, "\n")] = '\0'; + printf("%s", buf); + } + + pclose(p); +} + +void display_os_arch() { + char buf[16]; + FILE *p = popen("uname -m", "r"); + if (!p) { + fprintf(stderr, "「uname」コマンドを実効に失敗"); + return; + } + + while (fgets(buf, sizeof(buf), p) != NULL) { + buf[strcspn(buf, "\n")] = '\0'; + printf("%s", buf); + } + + pclose(p); +} diff --git a/src/os.h b/src/os.h new file mode 100644 index 0000000..690ed47 --- /dev/null +++ b/src/os.h @@ -0,0 +1,8 @@ +#ifndef OS_H +#define OS_H + +void display_os_name(); +void display_os_vers(); +void display_os_arch(); + +#endif diff --git a/src/user.c b/src/user.c new file mode 100644 index 0000000..27a6b79 --- /dev/null +++ b/src/user.c @@ -0,0 +1,52 @@ +#include "user.h" + +#include +#include + +void display_user_name() { + char buf[128]; + FILE *p = popen("whoami", "r"); + if (!p) { + fprintf(stderr, "「whoami」コマンドを実効に失敗"); + return; + } + + while (fgets(buf, sizeof(buf), p) != NULL) { + buf[strcspn(buf, "\n")] = '\0'; + printf("%s", buf); + } + + pclose(p); +} + +void display_user_host() { + char buf[64]; + const char *filename; +#ifdef __OpenBSD__ + filename = "/etc/myname"; +#else + filename = "/etc/hostname"; +#endif + FILE *f = fopen(filename, "r"); + if (!f) { + snprintf(buf, sizeof(buf), "「%s」を見つけられません。", filename); + perror(buf); + return; + } + + while (fgets(buf, sizeof(buf), f) != NULL) { + printf("%s", buf); + } + + /* char hostname[128]; */ + /* int cnt = 0; */ + /* for (int i = 0; i < 128; i++) { */ + /* unsigned char key; */ + /* fread(&key, sizeof(key), 1, f); */ + /* hostname[i] = key; */ + /* cnt += 1; */ + /* } */ + + /* hostname[cnt] = '\0'; */ + fclose(f); +} diff --git a/src/user.h b/src/user.h new file mode 100644 index 0000000..fc63853 --- /dev/null +++ b/src/user.h @@ -0,0 +1,7 @@ +#ifndef USER_H +#define USER_H + +void display_user_name(); +void display_user_host(); + +#endif