-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathomnipkg.c
More file actions
175 lines (158 loc) · 5.64 KB
/
Copy pathomnipkg.c
File metadata and controls
175 lines (158 loc) · 5.64 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/stat.h> // For stat() in detect_distro_base
// Forward declarations
void run_put(int argc, char *argv[]); // Actual definition is expected in omniput.c
void detect_distro_base();
// Retained from original, as it's a general utility
int execute_command(const char *command) {
printf("Executing: %s\n", command);
int status = system(command);
if (status == -1) {
perror("Failed to execute command (system call error)");
return EXIT_FAILURE;
} else if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
fprintf(stderr, "Command failed with exit status %d.\n", WEXITSTATUS(status));
return EXIT_FAILURE;
} else if (WIFSIGNALED(status)) {
fprintf(stderr, "Command terminated by signal %d.\n", WTERMSIG(status));
return EXIT_FAILURE;
}
printf("Command executed successfully.\n");
return EXIT_SUCCESS;
}
void print_distro_token(const char* token_str) {
if (token_str && *token_str) {
char buffer[256];
strncpy(buffer, token_str, sizeof(buffer) - 1);
buffer[sizeof(buffer) - 1] = '\0';
char* start = buffer;
char* end = buffer + strlen(buffer) - 1;
if (*start == '"' && end > start) {
start++;
if (*end == '"') {
*end = '\0';
}
}
char *token = strtok(start, " ");
while (token != NULL) {
if (strlen(token) > 0) {
printf("\"%s\" ", token);
}
token = strtok(NULL, " ");
}
}
}
void detect_distro_base() {
FILE *fp;
char line[256];
char id[256] = "";
char id_like[256] = "";
int found_id_like = 0;
int found_id = 0;
int printed_something = 0;
fp = fopen("/etc/os-release", "r");
if (fp != NULL) {
while (fgets(line, sizeof(line), fp)) {
if (strncmp(line, "ID_LIKE=", 8) == 0) {
strncpy(id_like, line + 8, sizeof(id_like) - 1);
id_like[sizeof(id_like) - 1] = '\0';
id_like[strcspn(id_like, "\n")] = 0;
found_id_like = 1;
} else if (strncmp(line, "ID=", 3) == 0) {
strncpy(id, line + 3, sizeof(id) - 1);
id[sizeof(id) - 1] = '\0';
id[strcspn(id, "\n")] = 0;
found_id = 1;
}
}
fclose(fp);
if (found_id_like && strlen(id_like) > 0) {
print_distro_token(id_like);
printed_something = 1;
}
if (!printed_something && found_id && strlen(id) > 0) {
print_distro_token(id);
printed_something = 1;
}
}
struct stat st;
if (stat("/etc/debian_version", &st) == 0) {
if (!strstr(id_like, "debian") && !strstr(id, "debian")) {
printf("\"debian\" ");
printed_something = 1;
}
}
if (stat("/etc/arch-release", &st) == 0) {
if (!strstr(id_like, "arch") && !strstr(id, "arch")) {
printf("\"arch\" ");
printed_something = 1;
}
}
if (stat("/etc/fedora-release", &st) == 0) {
if (!strstr(id_like, "fedora") && !strstr(id, "fedora")) {
printf("\"fedora\" ");
printed_something = 1;
}
}
if (stat("/etc/redhat-release", &st) == 0 || stat("/etc/centos-release", &st) == 0) {
if (!strstr(id_like, "rhel") && !strstr(id, "rhel") &&
!strstr(id_like, "centos") && !strstr(id, "centos")) {
printf("\"rhel\" ");
printed_something = 1;
}
}
if (stat("/etc/SuSE-release", &st) == 0 || stat("/etc/suse-release", &st) == 0) {
if (!strstr(id_like, "suse") && !strstr(id, "suse") &&
!strstr(id_like, "opensuse") && !strstr(id, "opensuse")) {
printf("\"suse\" ");
printed_something = 1;
}
}
if (stat("/etc/gentoo-release", &st) == 0) {
if (!strstr(id_like, "gentoo") && !strstr(id, "gentoo")) {
printf("\"gentoo\" ");
printed_something = 1;
}
}
if (printed_something) {
printf("\n");
} else {
fprintf(stderr, "Could not determine distro base.\n");
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <command> [args...]\n", argv[0]);
fprintf(stderr, "Available commands:\n");
fprintf(stderr, " put <action> [args...]\n");
fprintf(stderr, " Primary command for package operations (e.g., install, remove).\n");
fprintf(stderr, " Supported actions are determined by omniput.c.\n");
fprintf(stderr, " Example: %s put install <package1> [package2...]\n", argv[0]);
fprintf(stderr, " defdis\n");
fprintf(stderr, " Detects and prints the base distribution(s) (e.g., \"debian\" \"arch\").\n");
return EXIT_FAILURE;
}
if (strcmp(argv[1], "put") == 0) {
if (argc < 3) {
fprintf(stderr, "Usage: %s put <action> [args...]\n", argv[0]);
fprintf(stderr, "Supported actions (e.g., install, remove) are determined by omniput.c.\n");
return EXIT_FAILURE;
}
run_put(argc - 2, &argv[2]);
} else if (strcmp(argv[1], "defdis") == 0) {
if (argc != 2) {
fprintf(stderr, "Usage: %s defdis\n", argv[0]);
return EXIT_FAILURE;
}
detect_distro_base();
} else {
fprintf(stderr, "Unknown command: %s\n", argv[1]);
fprintf(stderr, "Run '%s' without arguments to see usage.\n", argv[0]);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}