fordon_greeman / pkger

Branch: f8394e1
Switch branches/tags
main
theidioticdev init d53ca2b 29d ago
99 lines · 1.9 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 "slpkger"
9 #define DEST "/home/fordon/local/bin"
10
11 /* helper fn for running commands via execvp */
12 int
13 runCmd (char *const argv[])
14 {
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;
25 }
26 return 0;
27 }
28
29 /* build fn */
30 int
31 build (void)
32 {
33 char *const buildCmd[] = {
34 CC, "-Wall", "-Wextra", "slpkger.c", "-o", PROG_NAME, NULL
35 };
36
37 printf ("[BUILD]: " CC " -Wall -Wextra slpkger.c -o %s\n", PROG_NAME);
38 if (!runCmd (buildCmd))
39 {
40 fprintf (stderr, "[ERROR]: compilation failed\n");
41 return 0;
42 }
43 printf ("[SUCCESS]: compiled successfully\n");
44 return 1;
45 }
46 /* install fn */
47 int
48 install (void)
49 {
50 if (!build ())
51 return 0;
52
53 char *const installCmd[] = {
54 "cp", PROG_NAME, DEST "/" PROG_NAME, NULL
55 };
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 */
68 int
69 uninstall (void)
70 {
71 if (remove(DEST "/" PROG_NAME) != 0)
72 {
73 fprintf(stderr, "[ERROR]: uninstall failed.\n");
74 return 0;
75 }
76 printf("[SUCCESS]: uninstalled successfully\n");
77 return 1;
78 }
79
80 int
81 main (int argc, char **argv)
82 {
83 if (argc < 2 || strcmp(argv[1], "--build-only") == 0)
84 {
85 return build() ? 0 : 1;
86 }
87
88 if (strcmp(argv[1], "--install") == 0)
89 {
90 return install() ? 0 : 1;
91 }
92
93 if (strcmp(argv[1], "--uninstall") == 0)
94 {
95 return uninstall() ? 0 : 1;
96 }
97 fprintf(stderr, "Unknown argument: %s\n", argv[1]);
98 return 1;
99 }