fordon_greeman / sfetch

fix wm detection Browse files
theidioticdev committed 27d ago parent 096ab11 commit 8ac9cc1b65310acd8f83c22a6cfa1efe918f9624
Showing 4 changed files with 80 additions and 43 deletions. UnifiedSplit
+60 −31 bs.c
@@ -1,70 +1,99 @@
1 #include <stdio.h> 1 #include <stdio.h>
2 +#include <unistd.h>
3 +#include <sys/wait.h>
2 #include <stdlib.h> 4 #include <stdlib.h>
3 #include <string.h> 5 #include <string.h>
4 6
7 +#define CC "cc"
5 #define PROG_NAME "sfetch" 8 #define PROG_NAME "sfetch"
6 -#define COMPILE_CMD "gcc -Wall -Wextra -O3 src/main.c -o " PROG_NAME
7 #define DEST "/usr/local/bin" 9 #define DEST "/usr/local/bin"
8 -#define INSTALL_CMD "cp " PROG_NAME " " DEST "/" PROG_NAME
9 -#define UNINSTALL_CMD "rm " DEST "/" PROG_NAME
10 10
11 +/* helper fn for running commands via execvp */
11 int 12 int
12 -build(void) 13 +runCmd (char *const argv[])
13 { 14 {
14 - printf("[BUILD]: %s\n", COMPILE_CMD); 15 + pid_t pid = fork();
15 - if (system(COMPILE_CMD) != 0) 16 + if (pid == 0)
16 - { 17 + {
17 - fprintf(stderr, "[ERROR]: compilation failed\n"); 18 + execvp(argv[0], argv);
18 - return 0; 19 + perror("execvp failed");
20 + _exit(1);
21 + } else if (pid > 0) {
22 + int status;
23 + waitpid(pid, &status, 0);
24 + return WIFEXITED(status) && WEXITSTATUS(status) == 0;
19 } 25 }
20 - printf("[SUCCESS]: compiled successfully\n"); 26 + return 0;
21 - return 1;
22 } 27 }
23 28
24 -int 29 +/* build fn */
25 -install(void) 30 +int
26 -{ 31 +build (void)
27 - if (!build()) return 0;
28 - printf("[INSTALL]: %s\n", INSTALL_CMD);
29 - if (system(INSTALL_CMD) != 0)
30 { 32 {
31 - fprintf(stderr, "[ERROR]: install failed\n"); 33 + char *const buildCmd[] = {
34 + CC, "-Wall", "-Wextra", "-O3", "src/main.c", "-o", PROG_NAME, NULL
35 + };
36 +
37 + printf ("[BUILD]: " CC " -Wall -Wextra -O3 src/main.c -o %s\n", PROG_NAME);
38 + if (!runCmd (buildCmd))
39 + {
40 + fprintf (stderr, "[ERROR]: compilation failed\n");
32 return 0; 41 return 0;
33 - } 42 + }
34 - printf("[SUCCESS]: installed successfully\n"); 43 + printf ("[SUCCESS]: compiled successfully\n");
35 return 1; 44 return 1;
36 } 45 }
46 +/* install fn */
47 +int
48 +install (void)
49 +{
50 + if (!build ())
51 + return 0;
37 52
53 + char *const installCmd[] = {
54 + "cp", PROG_NAME, DEST "/" PROG_NAME, NULL
55 + };
38 56
57 + printf ("[INSTALL]: cp %s %s/%s\n", PROG_NAME, DEST, PROG_NAME);
58 + if (!runCmd (installCmd))
59 + {
60 + fprintf (stderr, "[ERROR]: install failed\n");
61 + return 0;
62 + }
63 + printf ("[SUCCESS]: installed successfully\n");
64 + return 1;
65 +}
66 +
67 +/* uninstall fn */
39 int 68 int
40 -uninstall(void) 69 +uninstall (void)
41 -{
42 - if (system(UNINSTALL_CMD) != 0)
43 { 70 {
71 + if (remove(DEST "/" PROG_NAME) != 0)
72 + {
44 fprintf(stderr, "[ERROR]: uninstall failed.\n"); 73 fprintf(stderr, "[ERROR]: uninstall failed.\n");
45 return 0; 74 return 0;
46 - } 75 + }
47 printf("[SUCCESS]: uninstalled successfully\n"); 76 printf("[SUCCESS]: uninstalled successfully\n");
48 return 1; 77 return 1;
49 } 78 }
50 79
51 int 80 int
52 -main(int argc, char **argv) 81 +main (int argc, char **argv)
53 { 82 {
54 if (argc < 2 || strcmp(argv[1], "--build-only") == 0) 83 if (argc < 2 || strcmp(argv[1], "--build-only") == 0)
55 - { 84 + {
56 return build() ? 0 : 1; 85 return build() ? 0 : 1;
57 - } 86 + }
58 87
59 if (strcmp(argv[1], "--install") == 0) 88 if (strcmp(argv[1], "--install") == 0)
60 - { 89 + {
61 return install() ? 0 : 1; 90 return install() ? 0 : 1;
62 - } 91 + }
63 92
64 if (strcmp(argv[1], "--uninstall") == 0) 93 if (strcmp(argv[1], "--uninstall") == 0)
65 - { 94 + {
66 return uninstall() ? 0 : 1; 95 return uninstall() ? 0 : 1;
67 - } 96 + }
68 fprintf(stderr, "Unknown argument: %s\n", argv[1]); 97 fprintf(stderr, "Unknown argument: %s\n", argv[1]);
69 return 1; 98 return 1;
70 } 99 }
+0 −0 build Binary files a/build and b/build differ
+8 −0 readme.org
@@ -21,3 +21,11 @@ sudo/doas ./build --install # to install system-wide, tweak bs.c and recompile i
21 ./build # to build only 21 ./build # to build only
22 sudo/doas ./build --uninstall # uninstall it 22 sudo/doas ./build --uninstall # uninstall it
23 #+END_SRC 23 #+END_SRC
24 +
25 +note: if you use gentoo, you can do this instead:
26 +#+BEGIN_SRC sh :eval no
27 + # you must have eselect-repository package
28 + eselect repository add acooliodude-overlay git https://codeberg.org/ACoolioDude/acooliodude-overlay
29 + emerge --sync acooliodude-overlay | emiant sync -r acooliodude-overlay
30 + emerge --ask sfetch
31 +#+END_SRC
+12 −12 src/main.c
@@ -47,15 +47,15 @@ printOS(void)
47 { 47 {
48 if(strncmp(line, "PRETTY_NAME=", 12) == 0) 48 if(strncmp(line, "PRETTY_NAME=", 12) == 0)
49 { 49 {
50 - char *name = line + 12; 50 + char *name = line + 12;
51 - if (*name == '"') name++; 51 + if (*name == '"') name++;
52 - size_t len = strlen(name); 52 + size_t len = strlen(name);
53 - if (len > 0 && name[len - 1] == '\n') name[--len] = '\0'; 53 + if (len > 0 && name[len - 1] == '\n') name[--len] = '\0';
54 - if (len > 0 && name[len - 1] == '"') name[--len] = '\0'; 54 + if (len > 0 && name[len - 1] == '"') name[--len] = '\0';
55 - printf(C_CYAN "OS: " C_RESET "%s\n", name); 55 + printf(C_CYAN "OS: " C_RESET "%s\n", name);
56 - fclose(fp); 56 + fclose(fp);
57 - return; 57 + return;
58 - } 58 + }
59 } 59 }
60 fclose(fp); 60 fclose(fp);
61 printf(C_CYAN "OS: " C_RESET "Generic Linux\n"); 61 printf(C_CYAN "OS: " C_RESET "Generic Linux\n");
@@ -63,8 +63,8 @@ printOS(void)
63 struct utsname otherOS; 63 struct utsname otherOS;
64 if (uname(&otherOS) != 0) 64 if (uname(&otherOS) != 0)
65 { 65 {
66 - perror("uname"); 66 + perror("uname");
67 - return; 67 + return;
68 } 68 }
69 printf(C_CYAN "OS: " C_RESET "%s\n", otherOS.sysname); 69 printf(C_CYAN "OS: " C_RESET "%s\n", otherOS.sysname);
70 #endif 70 #endif
@@ -73,7 +73,7 @@ printOS(void)
73 int 73 int
74 printStats(void) 74 printStats(void)
75 { 75 {
76 - char *wm = getenv("XDG_SESSION_DESKTOP"); 76 + char *wm = getenv("XDG_CURRENT_DESKTOP");
77 if (!wm) 77 if (!wm)
78 { 78 {
79 wm = getenv("DESKTOP_SESSION"); 79 wm = getenv("DESKTOP_SESSION");