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 1 #include <stdio.h>
2 +#include <unistd.h>
3 +#include <sys/wait.h>
2 4 #include <stdlib.h>
3 5 #include <string.h>
4 6
7 +#define CC "cc"
5 8 #define PROG_NAME "sfetch"
6 -#define COMPILE_CMD "gcc -Wall -Wextra -O3 src/main.c -o " PROG_NAME
7 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 12 int
12 -build(void)
13 +runCmd (char *const argv[])
13 14 {
14 - printf("[BUILD]: %s\n", COMPILE_CMD);
15 - if (system(COMPILE_CMD) != 0)
16 - {
17 - fprintf(stderr, "[ERROR]: compilation failed\n");
18 - return 0;
15 + pid_t pid = fork();
16 + if (pid == 0)
17 + {
18 + execvp(argv[0], argv);
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");
21 - return 1;
26 + return 0;
22 27 }
23 28
24 -int
25 -install(void)
26 -{
27 - if (!build()) return 0;
28 - printf("[INSTALL]: %s\n", INSTALL_CMD);
29 - if (system(INSTALL_CMD) != 0)
29 +/* build fn */
30 +int
31 +build (void)
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 41 return 0;
33 - }
34 - printf("[SUCCESS]: installed successfully\n");
42 + }
43 + printf ("[SUCCESS]: compiled successfully\n");
35 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 68 int
40 -uninstall(void)
41 -{
42 - if (system(UNINSTALL_CMD) != 0)
69 +uninstall (void)
43 70 {
71 + if (remove(DEST "/" PROG_NAME) != 0)
72 + {
44 73 fprintf(stderr, "[ERROR]: uninstall failed.\n");
45 74 return 0;
46 - }
75 + }
47 76 printf("[SUCCESS]: uninstalled successfully\n");
48 77 return 1;
49 78 }
50 79
51 80 int
52 -main(int argc, char **argv)
81 +main (int argc, char **argv)
53 82 {
54 83 if (argc < 2 || strcmp(argv[1], "--build-only") == 0)
55 - {
84 + {
56 85 return build() ? 0 : 1;
57 - }
86 + }
58 87
59 88 if (strcmp(argv[1], "--install") == 0)
60 - {
89 + {
61 90 return install() ? 0 : 1;
62 - }
91 + }
63 92
64 93 if (strcmp(argv[1], "--uninstall") == 0)
65 - {
94 + {
66 95 return uninstall() ? 0 : 1;
67 - }
96 + }
68 97 fprintf(stderr, "Unknown argument: %s\n", argv[1]);
69 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 21 ./build # to build only
22 22 sudo/doas ./build --uninstall # uninstall it
23 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 48 if(strncmp(line, "PRETTY_NAME=", 12) == 0)
49 49 {
50 - char *name = line + 12;
51 - if (*name == '"') name++;
52 - size_t len = strlen(name);
53 - if (len > 0 && name[len - 1] == '\n') name[--len] = '\0';
54 - if (len > 0 && name[len - 1] == '"') name[--len] = '\0';
55 - printf(C_CYAN "OS: " C_RESET "%s\n", name);
56 - fclose(fp);
57 - return;
58 - }
50 + char *name = line + 12;
51 + if (*name == '"') name++;
52 + size_t len = strlen(name);
53 + if (len > 0 && name[len - 1] == '\n') name[--len] = '\0';
54 + if (len > 0 && name[len - 1] == '"') name[--len] = '\0';
55 + printf(C_CYAN "OS: " C_RESET "%s\n", name);
56 + fclose(fp);
57 + return;
58 + }
59 59 }
60 60 fclose(fp);
61 61 printf(C_CYAN "OS: " C_RESET "Generic Linux\n");
@@ -63,8 +63,8 @@ printOS(void)
63 63 struct utsname otherOS;
64 64 if (uname(&otherOS) != 0)
65 65 {
66 - perror("uname");
67 - return;
66 + perror("uname");
67 + return;
68 68 }
69 69 printf(C_CYAN "OS: " C_RESET "%s\n", otherOS.sysname);
70 70 #endif
@@ -73,7 +73,7 @@ printOS(void)
73 73 int
74 74 printStats(void)
75 75 {
76 - char *wm = getenv("XDG_SESSION_DESKTOP");
76 + char *wm = getenv("XDG_CURRENT_DESKTOP");
77 77 if (!wm)
78 78 {
79 79 wm = getenv("DESKTOP_SESSION");