-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
334 lines (258 loc) · 8.29 KB
/
main.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#define VERSION "1.2.0"
#define UPDATE_CMD "curl -o- https://raw.githubusercontent.com/ArdaGnsrn/pvm-ubuntu/master/install.sh | bash"
#define UPDATE_CHECK_INTERVAL (6 * 60 * 60)
int is_php_package_available(char *version);
int install_apt_repository_if_not_exists();
void install_php_version(char *version);
void use_php_version(char *version);
void use_command(int argc, char *argv[]);
int is_php_version_installed(char *version);
void install_command(int argc, char *argv[]);
void list_command();
char *current_php_version();
void uninstall_command(int argc, char *argv[]);
void update_command();
void version_command();
int update_checker();
int main(int argc, char *argv[]) {
install_apt_repository_if_not_exists();
if (argc < 2) {
printf("\n");
printf("Running version %s.\n", VERSION);
printf("https://github.com/ArdaGnsrn/pvm-ubuntu/\n");
printf("\n");
printf("Usage:\n");
printf(" pvm install <version>\t\t:\tInstall a PHP version.\n");
printf(" pvm use <version>\t\t:\tUse a PHP version.\n");
printf(" pvm list\t\t\t:\tList installed PHP versions.\n");
printf(" pvm uninstall <version>\t:\tUninstall a PHP version.\n");
printf("\n");
printf(" pvm update\t\t\t:\tUpdate to the latest version.\n");
printf(" pvm version\t\t\t:\tShow the current version.\n");
printf("\n");
if (update_checker()) {
printf("\n");
printf("There is a new version available. You can update by running 'pvm update'.\n");
printf("\n");
}
return 1;
}
if (strcmp(argv[1], "install") == 0) {
install_command(argc, argv);
} else if (strcmp(argv[1], "uninstall") == 0) {
uninstall_command(argc, argv);
} else if (strcmp(argv[1], "use") == 0) {
use_command(argc, argv);
} else if (strcmp(argv[1], "list") == 0) {
list_command();
} else if (strcmp(argv[1], "update") == 0) {
update_command();
} else if (strcmp(argv[1], "version") == 0) {
version_command();
} else {
return main(1, argv);
}
return 0;
}
char *current_php_version() {
char command[1024];
char output[1024];
FILE *fp;
sprintf(command, "php -v | grep PHP | awk '{print $2}'");
fp = popen(command, "r");
if (fp == NULL) {
printf("There was an error while running the command.\n");
return "";
}
while (fgets(output, sizeof(output), fp) != NULL) {
char *version = strtok(output, "-");
pclose(fp);
return version;
}
pclose(fp);
return "";
}
void version_command() {
printf("Running version %s.", VERSION);
printf("\n");
}
void update_command() {
system(UPDATE_CMD);
printf("Updated to the latest version.\n");
}
int update_checker() {
time_t now;
time_t last_checked;
time(&now);
FILE *file;
file = fopen("/tmp/pvm-update-checker", "r");
if (file != NULL) {
char last_checked_str[20];
fgets(last_checked_str, 20, file);
last_checked = (time_t) strtol(last_checked_str, (char **) NULL, 10);
fclose(file);
} else {
last_checked = 0;
}
if (now - last_checked > UPDATE_CHECK_INTERVAL) {
system("curl --silent \"https://api.github.com/repos/ardagnsrn/pvm-ubuntu/releases/latest\" | grep '\"tag_name\":' | sed -E 's/.*\"([^\"]+)\".*/\\1/' > /tmp/pvm-latest-version");
}
file = fopen("/tmp/pvm-latest-version", "r");
if (file != NULL) {
char latest_version[20];
fgets(latest_version, 20, file);
fclose(file);
if (strstr(latest_version, VERSION) == NULL) {
return 1;
}
}
return 0;
}
void list_command() {
char command[1024];
char output[1024];
FILE *fp;
if (access("/usr/bin/php", F_OK) == -1) {
printf("No PHP versions found.\n");
return;
}
sprintf(command, "ls -d /usr/bin/php* | awk -F'/' '{print $4}'");
fp = popen(command, "r");
if (fp == NULL) {
printf("There was an error while running the command.\n");
return;
}
char *current_version = current_php_version();
// push array to store the versions
char versions[1024][1024];
while (fgets(output, sizeof(output), fp) != NULL) {
memmove(output, output + 3, strlen(output));
output[strcspn(output, "\n")] = 0;
if (strspn(output, "0123456789.") != strlen(output)) continue;
int i = 0;
while (strlen(versions[i]) > 0) i++;
strcpy(versions[i], output);
}
if (strlen(versions[0]) == 0) {
printf("No PHP versions found.\n");
return;
}
printf("\nAvailable PHP versions:\n");
for (int i = 0; i < 1024; i++) {
if (strlen(versions[i]) == 0) break;
char *version;
version = (char *) malloc(1024 * sizeof(char));
if (strstr(current_version, versions[i]) != NULL) sprintf(version, " * %s\n", versions[i]);
else sprintf(version, " %s\n", versions[i]);
printf("%s", version);
}
pclose(fp);
printf("\n");
}
void use_command(int argc, char *argv[]) {
if (argc < 3) {
printf("A version argument is required but missing.\n");
return;
}
use_php_version(argv[2]);
}
void install_command(int argc, char *argv[]) {
if (argc < 3) {
printf("A version argument is required but missing.\n");
return;
}
if (!is_php_package_available(argv[2])) {
printf("PHP version %s is not available.\n", argv[2]);
return;
}
install_php_version(argv[2]);
printf("\nPHP version %s is installed.\n", argv[2]);
printf("You can use it by running 'pvm use %s'.\n", argv[2]);
}
void uninstall_command(int argc, char *argv[]) {
if (argc < 3) {
printf("A version argument is required but missing.\n");
return;
}
if (!is_php_version_installed(argv[2])) {
printf("PHP version %s is not installed.\n", argv[2]);
return;
}
char command[1024];
sprintf(command, "sudo apt remove php%s -y", argv[2]);
system(command);
system("sudo apt autoremove -y");
printf("PHP version %s is uninstalled.\n", argv[2]);
}
int is_php_package_available(char *version) {
char command[1024];
char output[1024];
FILE *fp;
sprintf(command, "apt-cache show php%s | grep Version", version);
fp = popen(command, "r");
if (fp == NULL) {
printf("There was an error while running the command.\n");
return -1;
}
while (fgets(output, sizeof(output), fp) != NULL) {
if (strstr(output, "deb.sury.org") != NULL) {
pclose(fp);
return 1;
}
}
pclose(fp);
return 0;
}
void install_php_version(char *version) {
if (is_php_version_installed(version)) {
printf("Version %s is already installed.\n", version);
return;
}
char command[1024];
sprintf(command, "sudo apt install php%s -y", version);
system(command);
}
int is_php_version_installed(char *version) {
char path[1024];
sprintf(path, "/usr/bin/php%s", version);
if (access(path, F_OK) != -1) {
return 1;
}
return 0;
}
void use_php_version(char *version) {
if (!is_php_version_installed(version)) {
printf("PHP version %s is not installed.\n", version);
return;
}
char command[1024];
sprintf(command, "sudo update-alternatives --set php /usr/bin/php%s", version);
system(command);
printf("Now using PHP version %s.\n", version);
}
int install_apt_repository_if_not_exists() {
char output[1024];
FILE *fp;
fp = popen("grep -r --include '*.list' '^deb ' /etc/apt/sources.list /etc/apt/sources.list.d/", "r");
if (fp == NULL) {
printf("There was an error while running the command.\n");
return -1;
}
while (fgets(output, sizeof(output), fp) != NULL) {
if (strstr(output, "ondrej-ubuntu-php-jammy.list:deb") != NULL) {
pclose(fp);
return 1;
}
}
pclose(fp);
printf("\n\nInstalling PHP repository...\n\n");
system("sudo add-apt-repository ppa:ondrej/php -y");
system("sudo apt-get update -y");
printf("\n\nPHP repository installed.\n\n");
return 0;
}