-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_info.c
199 lines (176 loc) · 5.65 KB
/
system_info.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <pwd.h>
#include <string.h>
#include <limits.h>
#include <fcntl.h>
#include "system_info.h"
#include "config.h"
void get_username(char *username, size_t size) {
char *env_user = getenv("USER");
struct passwd *pwd = getpwuid(getuid());
strncpy(username, env_user ? env_user : (pwd && pwd->pw_name ? pwd->pw_name : "Unknown"), size);
}
void get_hostname(char *hostname, size_t size) {
if (gethostname(hostname, size) != 0) {
strncpy(hostname, "Unknown", size);
}
}
void get_shell(char *shell, size_t size, const char *distro) {
if (strcmp(distro, "FreeBSD") == 0) {
struct passwd *pwd = getpwuid(getuid());
strncpy(shell, pwd && pwd->pw_shell ? strrchr(pwd->pw_shell, '/') + 1 : "Unknown", size);
} else {
char path[PATH_MAX];
snprintf(path, sizeof(path), PROC_PATH_FORMAT, getppid());
ssize_t len = readlink(path, shell, size - 1);
if (len != -1) {
shell[len] = '\0';
strncpy(shell, strrchr(shell, '/') + 1, size);
} else {
strncpy(shell, "Unknown", size);
}
}
}
void get_wm(char *wm, size_t size) {
FILE *proc = popen(
"ps -e | grep -E 'dwm|i3|xfwm4|openbox|kwin|sway|gnome-shell|mate-session|xfce4-session|lxqt|cinnamon|fluxbox|herbstluftwm|bspwm|awesome|spectrwm|wmii|xmonad|icewm|jwm' | awk '{print $4}'",
"r"
);
if (proc && fgets(wm, size, proc)) {
wm[strcspn(wm, "\n")] = '\0';
} else {
strncpy(wm, "Unknown", size);
}
if (proc) pclose(proc);
}
pid_t get_ppid(pid_t pid) {
char path[BUFFER_SIZE];
snprintf(path, sizeof(path), "/proc/%d/stat", pid);
FILE *stat_file = fopen(path, "r");
if (!stat_file) {
return -1;
}
pid_t ppid;
fscanf(stat_file, "%*d %*s %*c %d", &ppid);
fclose(stat_file);
return ppid;
}
void get_process_name(pid_t pid, char *name, size_t size) {
char path[BUFFER_SIZE];
snprintf(path, sizeof(path), "/proc/%d/exe", pid);
ssize_t len = readlink(path, name, size - 1);
if (len != -1) {
name[len] = '\0';
} else {
strncpy(name, "Unknown", size);
}
}
void get_term(char *term, size_t size) {
char *term_program = getenv("TERM_PROGRAM");
char *ssh_connection = getenv("SSH_CONNECTION");
char *wt_session = getenv("WT_SESSION");
char *term_env = getenv("TERM");
if (term_program) {
if (strcmp(term_program, "iTerm.app") == 0) {
strncpy(term, "iTerm2", size);
return;
} else if (strcmp(term_program, "Terminal.app") == 0) {
strncpy(term, "Apple Terminal", size);
return;
} else if (strcmp(term_program, "Hyper") == 0) {
strncpy(term, "HyperTerm", size);
return;
} else {
strncpy(term, term_program, size);
return;
}
}
if (term_env) {
if (strcmp(term_env, "tw52") == 0 || strcmp(term_env, "tw100") == 0) {
strncpy(term, "TosWin2", size);
return;
}
}
if (ssh_connection) {
char *ssh_tty = getenv("SSH_TTY");
if (ssh_tty) {
strncpy(term, ssh_tty, size);
return;
}
}
if (wt_session) {
strncpy(term, "Windows Terminal", size);
return;
}
pid_t parent = getppid();
while (parent > 0) {
char name[BUFFER_SIZE] = {0};
get_process_name(parent, name, sizeof(name));
char *base_name = strrchr(name, '/');
if (base_name) {
base_name++;
} else {
base_name = name;
}
if (strstr(base_name, "gnome-terminal")) {
strncpy(term, "gnome-terminal", size);
return;
} else if (strstr(base_name, "urxvtd")) {
strncpy(term, "urxvt", size);
return;
} else if (strstr(base_name, "konsole")) {
strncpy(term, "Konsole", size);
return;
} else if (strstr(base_name, "alacritty")) {
strncpy(term, "Alacritty", size);
return;
} else if (strstr(base_name, "xterm")) {
strncpy(term, "xterm", size);
return;
} else if (strstr(base_name, "kitty")) {
strncpy(term, "kitty", size);
return;
} else if (strstr(base_name, "wayland-terminal")) {
strncpy(term, "Wayland Terminal", size);
return;
} else if (strstr(base_name, "ptyxis")) {
strncpy(term, "ptyxis-agent", size);
return;
}
if (strstr(base_name, "bash") || strstr(base_name, "zsh") || strstr(base_name, "sh")) {
parent = get_ppid(parent);
continue;
}
if (strlen(base_name) > 0) {
strncpy(term, base_name, size);
return;
}
parent = get_ppid(parent);
}
strncpy(term, "Unknown", size);
}
void get_distro(char *distro, size_t size) {
FILE *os_release = fopen("/etc/os-release", "r");
if (os_release) {
char line[BUFFER_SIZE];
while (fgets(line, sizeof(line), os_release)) {
if (strncmp(line, "PRETTY_NAME=", 12) == 0) {
char *start = strchr(line, '"');
char *end = strrchr(line, '"');
if (start && end && start != end) {
*end = '\0';
strncpy(distro, start + 1, size);
} else {
strncpy(distro, "Unknown", size);
}
break;
}
}
fclose(os_release);
} else {
strncpy(distro, "Unknown", size);
}
}