This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-tool.c
90 lines (70 loc) · 2.26 KB
/
setup-tool.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
#include <stdio.h>
#include <unistd.h>
#include <stddef.h>
#define PROGRAMS_PATH "/usr/bin/"
int checkProgramExists(const char* programName) {
char fullPath[100];
sprintf(fullPath, "%s%s", PROGRAMS_PATH, programName);
if (access(fullPath, F_OK) != -1) {
return 1; // Program exists
} else {
return 0; // Program does not exist
}
}
int main() {
const char* programs[] = {"setup-tool-cli", "setup-tool-tui", "setup-tool-gui"};
int numPrograms = sizeof(programs) / sizeof(programs[0]);
int installedPrograms[numPrograms];
int numInstalledPrograms = 0; // Track the number of installed programs
for (int i = 0; i < numPrograms; i++) {
installedPrograms[i] = checkProgramExists(programs[i]);
if (installedPrograms[i]) {
numInstalledPrograms++;
}
}
if (numInstalledPrograms == 0) {
printf("No installed programs found.\n");
return 0;
}
if (numInstalledPrograms == 1) {
int programIndex = 0;
while (!installedPrograms[programIndex]) {
programIndex++;
}
char fullPath[100];
sprintf(fullPath, "%s%s", PROGRAMS_PATH, programs[programIndex]);
execlp(fullPath, programs[programIndex], NULL);
perror("Failed to execute the program");
return 1;
}
printf("Installed Programs:\n");
int option = 1;
for (int i = 0; i < numPrograms; i++) {
if (installedPrograms[i]) {
printf("%d. %s\n", option, programs[i]);
option++;
}
}
int choice;
printf("Enter the number of the program to launch (or 0 to exit): ");
scanf("%d", &choice);
int selectedOption = 1;
int selectedProgramIndex = -1;
for (int i = 0; i < numPrograms; i++) {
if (installedPrograms[i]) {
if (selectedOption == choice) {
selectedProgramIndex = i;
break;
}
selectedOption++;
}
}
if (selectedProgramIndex != -1) {
char fullPath[100];
sprintf(fullPath, "%s%s", PROGRAMS_PATH, programs[selectedProgramIndex]);
execlp(fullPath, programs[selectedProgramIndex], NULL);
perror("Failed to execute the program");
return 1;
}
return 0;
}