fordon_greeman / sfetch

Branch: ba3340e
Switch branches/tags
main
theidioticdev fix build system 2e3254a 27d ago
96 lines · 1.7 KB Raw Blame History
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <sys/wait.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #define CC "cc"
8 #define PROG_NAME "sfetch"
9 #define DEST "/usr/local/bin"
10
11 int
12 runCmd (char *const argv[])
13 {
14 pid_t pid = fork();
15 if (pid == 0)
16 {
17 execvp(argv[0], argv);
18 perror("execvp failed");
19 _exit(1);
20 } else if (pid > 0) {
21 int status;
22 waitpid(pid, &status, 0);
23 return WIFEXITED(status) && WEXITSTATUS(status) == 0;
24 }
25 return 0;
26 }
27
28 int
29 build (void)
30 {
31 char *const buildCmd[] = {
32 CC, "-O3", "sfetch.c", "-o", PROG_NAME, NULL
33 };
34
35 printf ("[BUILD]: " CC " -O3 sfetch.c -o %s\n", PROG_NAME);
36 if (!runCmd (buildCmd))
37 {
38 fprintf (stderr, "[ERROR]: compilation failed\n");
39 return 0;
40 }
41 printf ("[SUCCESS]: compiled successfully\n");
42 return 1;
43 }
44
45 int
46 install (void)
47 {
48 if (!build ())
49 return 0;
50
51 char *const installCmd[] = {
52 "cp", PROG_NAME, DEST "/" PROG_NAME, NULL
53 };
54
55 printf ("[INSTALL]: cp %s %s/%s\n", PROG_NAME, DEST, PROG_NAME);
56 if (!runCmd (installCmd))
57 {
58 fprintf (stderr, "[ERROR]: install failed\n");
59 return 0;
60 }
61 printf ("[SUCCESS]: installed successfully\n");
62 return 1;
63 }
64
65 int
66 uninstall (void)
67 {
68 if (remove(DEST "/" PROG_NAME) != 0)
69 {
70 fprintf(stderr, "[ERROR]: uninstall failed.\n");
71 return 0;
72 }
73 printf("[SUCCESS]: uninstalled successfully\n");
74 return 1;
75 }
76
77 int
78 main (int argc, char **argv)
79 {
80 if (argc < 2 || strcmp(argv[1], "--build-only") == 0)
81 {
82 return build() ? 0 : 1;
83 }
84
85 if (strcmp(argv[1], "--install") == 0)
86 {
87 return install() ? 0 : 1;
88 }
89
90 if (strcmp(argv[1], "--uninstall") == 0)
91 {
92 return uninstall() ? 0 : 1;
93 }
94 fprintf(stderr, "Unknown argument: %s\n", argv[1]);
95 return 1;
96 }