OpenBSD対応

このコミットが含まれているのは:
守矢諏訪子 2023-08-27 17:27:36 +09:00
コミット d74c8024d1
3個のファイルの変更180行の追加48行の削除

ファイルの表示

@ -1,19 +1,19 @@
UNAME_S := $(shell uname -s)
CFLAGS=-O2 -Wall -Wextra -lX11 -lpci
CFLAGS=-O2 -Wall -Wextra
PREFIX=/usr
CACHE=$(shell if [ "$$XDG_CACHE_HOME" ]; then echo "$$XDG_CACHE_HOME"; else echo "$$HOME"/.cache; fi)
ifeq ($(UNAME_S),Linux)
CFLAGS += -lwayland-client -D LINUX
CFLAGS += -lX11 -lwayland-client -lcpi -D LINUX
endif
ifeq ($(UNAME_S),FreeBSD)
PREFIX=/usr/local
CFLAGS += -D FREEBSD
CFLAGS += -lX11 -D FREEBSD
endif
ifeq ($(UNAME_S),OpenBSD)
PREFIX=/usr/local
CFLAGS += -D OPENBSD
CFLAGS += -L/usr/X11R6/lib -lX11 -I/usr/X11R6/include -D OPENBSD
endif
all: paleofetch

ファイルの表示

@ -8,17 +8,17 @@
#include <sys/utsname.h>
#ifdef __linux__
#include <pci/pci.h>
#include <sys/sysinfo.h>
#elif __FreeBSD__
#elif __OpenBSD__
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/types.h>
#endif
#include <sys/statvfs.h>
#ifdef __linux__
#include <pci/pci.h>
#elif __FreeBSD__
#elif __OpenBSD__
#endif
#include <X11/Xlib.h>
#include <X11/Xatom.h>
@ -55,7 +55,9 @@ struct {
struct statvfs file_stats;
struct utsname uname_info;
struct sysinfo my_sysinfo;
#ifdef __linux__
struct sysinfo my_sysinfo;
#endif
int title_length, status;
int remove_newline(char *s) {
@ -91,9 +93,9 @@ void replace_substring(char *str, const char *sub_str, const char *repl_str, siz
if (start) {
size_t tail_len = strlen(start + sub_len);
if (tail_len + repl_len < BUF_SIZE / 2) {
strcpy(buffer, start + sub_len);
strlcpy(buffer, start + sub_len, sizeof(buffer));
strncpy(start, repl_str, repl_len);
strcpy(start + repl_len, buffer);
strlcpy(start + repl_len, buffer, sizeof(start + repl_len));
} else {
status = -1;
halt_and_catch_fire("new substring too long to replace");
@ -130,11 +132,13 @@ static char *get_bar() {
}
static char *get_os() {
char *os = malloc(BUF_SIZE), *name = malloc(BUF_SIZE), *line = NULL;
size_t len;
char *version = malloc(BUF_SIZE);
int getname = 0, getver = 0;
char *os = malloc(BUF_SIZE);
#ifdef __linux__
char name[BUF_SIZE], version[BUF_SIZE];
char *line = NULL;
size_t len;
int getname = 0, getver = 0;
FILE *os_release = fopen("/etc/os-release", "r");
if (os_release == NULL) {
status = -1;
@ -171,10 +175,16 @@ static char *get_os() {
snprintf(os, BUF_SIZE, "%s %s", name, uname_info.machine);
}
free(line);
fclose(os_release);
free(name);
free(version);
if (line) free(line);
if (os_release) fclose(os_release);
#elif __OpenBSD__
struct utsname uname_info;
if (uname(&uname_info) != 0) {
snprintf(os, BUF_SIZE, "不明");
} else {
snprintf(os, BUF_SIZE, "%s %s %s", uname_info.sysname, uname_info.release, uname_info.machine);
}
#endif
return os;
}
@ -198,6 +208,7 @@ static char *get_kernel() {
}
static char *get_host() {
#ifdef __linux__
FILE *product_name, *product_version, *model;
char buffer[BUF_SIZE / 2];
char *host = NULL;
@ -244,29 +255,66 @@ static char *get_host() {
status = -1;
halt_and_catch_fire("unable to get host");
return NULL;
#elif __OpenBSD__
char *buffer;
size_t buffer_len;
int mib[] = { CTL_HW, HW_MACHINE };
if (sysctl(mib, 2, NULL, &buffer_len, NULL, 0) == -1) {
return NULL;
}
buffer = malloc(buffer_len);
if (sysctl(mib, 2, buffer, &buffer_len, NULL, 0) == -1) {
free(buffer);
return NULL;
}
return buffer;
#endif
}
static char *get_uptime() {
long seconds = my_sysinfo.uptime;
struct { char *name; int secs; } units[] = {
{ "day", 60 * 60 * 24 },
{ "hour", 60 * 60 },
{ "min", 60 },
};
char *uptime = malloc(BUF_SIZE);
int len = 0;
int n, len = 0;
char *uptime = malloc(BUF_SIZE);
for (int i = 0; i < 3; ++i ) {
if ((n = seconds / units[i].secs) || i == 2) {
len += snprintf(uptime + len, BUF_SIZE - len, "%d %s%s, ", n, units[i].name, n != 1 ? "s": "");
#if defined(LINUX)
long seconds = my_sysinfo.uptime;
#elif defined(OPENBSD)
int mib[2];
struct timeval boottime;
size_t size = sizeof(boottime);
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
if (sysctl(mib, 2, &boottime, &size, NULL, 0) == -1) {
// handle error
return NULL;
}
seconds %= units[i].secs;
}
uptime[len - 2] = '\0';
return uptime;
time_t now = time(NULL);
long seconds = now - boottime.tv_sec;
#endif
struct { char *name; int secs; } units[] = {
{ "day", 60 * 60 * 24 },
{ "hour", 60 * 60 },
{ "min", 60 },
};
int n;
for (int i = 0; i < 3; ++i) {
if ((n = seconds / units[i].secs) || i == 2) {
len += snprintf(uptime + len, BUF_SIZE - len, "%d %s%s, ", n, units[i].name, n != 1 ? "s" : "");
}
seconds %= units[i].secs;
}
uptime[len - 2] = '\0';
return uptime;
}
static char *get_packages() {
char *packages = malloc(BUF_SIZE);
snprintf(packages, BUF_SIZE, "%s", PACKAGES);
@ -290,6 +338,7 @@ static char *get_shell() {
static char *get_resolution() {
int screen, width, height;
char *resolution = malloc(BUF_SIZE);
#ifdef __linux__
char *wayland_display = getenv("WAYLAND_DISPLAY");
if (wayland_display != NULL) { // TODO
@ -306,6 +355,7 @@ static char *get_resolution() {
wl_display_disconnect(dp);
} else {
#endif
Display *display = XOpenDisplay(NULL);
if (display != NULL) {
screen = DefaultScreen(display);
@ -354,12 +404,15 @@ static char *get_resolution() {
closedir(dir);
}
#ifdef __linux__
}
#endif
return resolution;
}
static char *get_wm() {
#ifdef __linux__
char *wayland_display = getenv("WAYLAND_DISPLAY");
if (wayland_display != NULL) {
char *wayland_wms[] = {
@ -374,11 +427,11 @@ static char *get_wm() {
char command[1024] = "ps aux | grep -m 1 -E -o '";
size_t num_wms = sizeof(wayland_wms) / sizeof(wayland_wms[0]);
for (size_t i = 0; i < num_wms - 1; i++) {
strcat(command, wayland_wms[i]);
strcat(command, "|");
strncat(command, wayland_wms[i], sizeof(command));
strncat(command, "|", sizeof(command));
}
strcat(command, wayland_wms[sizeof(wayland_wms) / sizeof(wayland_wms[0]) - 1]);
strcat(command, "'");
strncat(command, wayland_wms[sizeof(wayland_wms) / sizeof(wayland_wms[0]) - 1], sizeof(command));
strncat(command, "'", sizeof(command));
FILE *fp = popen(command, "r");
if (fp == NULL) {
@ -393,12 +446,13 @@ static char *get_wm() {
*newline = '\0';
}
} else {
strcpy(wm_name, "不明");
strlcpy(wm_name, "不明", sizeof(wm_name));
}
pclose(fp);
return strdup(wm_name);
}
#endif
Display *dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
@ -438,6 +492,7 @@ static char *get_wm() {
return wm_name;
}
#ifdef __linux__
static void get_terminal_wayland(char *terminal) {
struct wl_display *display = wl_display_connect(NULL);
if (!display) {
@ -448,6 +503,7 @@ static void get_terminal_wayland(char *terminal) {
wl_display_disconnect(display);
}
#endif
static void get_terminal_x11(char *terminal) {
Display *display = XOpenDisplay(NULL);
@ -482,11 +538,13 @@ static char *get_terminal() {
if (!terminal) {
return "不明";
}
#ifdef __linux__
char *wayland_display = getenv("WAYLAND_DISPLAY");
if (wayland_display != NULL) { // TODO
get_terminal_wayland(terminal);
} else {
#endif
get_terminal_x11(terminal);
if (!(*terminal)) {
@ -495,7 +553,9 @@ static char *get_terminal() {
strncpy(terminal, ttyname(STDIN_FILENO), BUF_SIZE -1);
}
}
#ifdef __linux__
}
#endif
return terminal;
}
@ -570,13 +630,19 @@ static char *get_cpu() {
return cpu;
}
#ifdef __linux__
static char *find_gpu(int index) {
#elif __OpenBSD__
static char *find_gpu() {
#endif
char *gpu = malloc(BUF_SIZE);
bool found = false;
#ifdef __linux__
char buffer[BUF_SIZE], *device_class;
struct pci_access *pacc;
struct pci_dev *dev;
int gpu_index = 0;
bool found = false;
char *gpu = malloc(BUF_SIZE);
pacc = pci_alloc();
pci_init(pacc);
@ -598,6 +664,26 @@ static char *find_gpu(int index) {
}
pci_cleanup(pacc);
#elif __OpenBSD__
FILE *fp;
char buf[128];
fp = popen("pcictl /dev/pci0 list", "r");
if (fp == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
// For demonstration, let's say we're looking for lines containing "VGA"
if (strstr(buf, "VGA") != NULL) {
snprintf(gpu, BUF_SIZE, "%s", buf);
found = true;
break;
}
}
pclose(fp);
#endif
if (!found) {
*gpu = '\0';
@ -616,7 +702,11 @@ static char *find_gpu(int index) {
}
static char *get_gpu1() {
#ifdef __linux__
return find_gpu(0);
#elif __OpenBSD__
return find_gpu();
#endif
}
static char *get_memory() {
@ -659,7 +749,7 @@ static char *get_colors1() {
char *s = colors1;
for (int i = 0; i < 8; i++) {
sprintf(s, "\e[4%dm ", i);
snprintf(s, sizeof(s), "\e[4%dm ", i);
s += 8;
}
snprintf(s, 5, "\e[0m");
@ -672,7 +762,7 @@ static char *get_colors2() {
char *s = colors2;
for (int i = 8; i < 16; i++) {
sprintf(s, "\e[48;5;%dm ", i);
snprintf(s, sizeof(s), "\e[48;5;%dm ", i);
s += 12 + (i >= 10 ? 1 : 0);
}
snprintf(s, 5, "\e[0m");
@ -719,8 +809,8 @@ char *get_value(struct conf c, int read_cache, char *cache_data) {
value = c.function();
if (c.cached) {
char *buf = malloc(BUF_SIZE);
sprintf(buf, "%s=%s;", c.label, value);
strcat(cache_data, buf);
snprintf(buf, sizeof(buf), "%s=%s;", c.label, value);
strncat(cache_data, buf, BUF_SIZE - 1);
free(buf);
}
}
@ -732,14 +822,27 @@ int main(int argc, char *argv[]) {
char *cache, *cache_data = NULL;
FILE *cache_file;
int read_cache;
#ifdef __linux__
char *wayland_display = getenv("WAYLAND_DISPLAY");
if (wayland_display != NULL) { // TODO
} else {
#endif
Display *display;
status = uname(&uname_info);
halt_and_catch_fire("uname failed");
#ifdef __linux__
status = sysinfo(&my_sysinfo);
#elif __OpenBSD__
int mib[2];
struct timeval boottime;
size_t size = sizeof(boottime);
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
status = sysctl(mib, 2, &boottime, &size, NULL, 0);
#endif
halt_and_catch_fire("sysinfo failed");
display = XOpenDisplay(NULL);
@ -794,7 +897,9 @@ int main(int argc, char *argv[]) {
if (display != NULL) {
XCloseDisplay(display);
}
#ifdef __linux__
}
#endif
return 0;
}

ファイルの表示

@ -1,5 +1,32 @@
/* Forward-declare our functions so users can mention them in their
* configs at the top of the file rather than near the bottom. */
#ifdef __OpenBSD__
# ifndef ENODATA
# define ENODATA 9910
# endif
#endif
#ifdef __FreeBSD__
# ifndef ENODATA
# define ENODATA 9911
# endif
#endif
#ifdef PLAN9
# ifndef ENODATA
# define ENODATA 9912
# endif
#endif
#ifdef __sun
# ifndef ENODATA
# define ENODATA 9913
# endif
#endif
#ifdef __linux__
# ifndef ENODATA
# define ENODATA 61
# endif
#endif
static char *get_title(),
*get_bar(),