getting started
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,2 @@
|
||||
# Build artifacts
|
||||
backup
|
||||
update
|
||||
|
83
update.c
Normal file
83
update.c
Normal file
@ -0,0 +1,83 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
int command_builder(char *command, char **args) {
|
||||
char *tmp_command = malloc(strlen(command));
|
||||
memcpy(tmp_command, command, strlen(command));
|
||||
char *tmp_token;
|
||||
size_t index = 0;
|
||||
|
||||
if (tmp_command == NULL) { return 1; }
|
||||
|
||||
tmp_token = strtok(tmp_command, " ");
|
||||
while(tmp_token != NULL) {
|
||||
args[index] = malloc(strlen(tmp_token));
|
||||
memcpy(args[index], tmp_token, strlen(tmp_token));
|
||||
tmp_token = strtok(NULL, " ");
|
||||
index++;
|
||||
}
|
||||
free(tmp_command);
|
||||
args[index++] = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int run_cmd_and_wait(char *command) {
|
||||
char **args = malloc(strlen(command));
|
||||
int ret = command_builder(command, args);
|
||||
if (ret != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
signed int pid = fork();
|
||||
int status;
|
||||
|
||||
if (pid == -1) {
|
||||
fprintf(stderr, "Couldn't fork. Error = %s\n", strerror(errno));
|
||||
return 1;
|
||||
} else if (pid > 0) {
|
||||
waitpid(pid, &status, 0);
|
||||
} else {
|
||||
execvp(args[0], args);
|
||||
}
|
||||
free(args);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
int main() {
|
||||
FILE *os_release = fopen("/etc/os-release", "r");
|
||||
if (!os_release) {
|
||||
fprintf(stderr, "Couldn't open os-release. Error = %s\n", strerror(errno));
|
||||
}
|
||||
char buffer[1024];
|
||||
while (fgets(buffer, sizeof buffer, os_release)) {
|
||||
if (strncmp(buffer, "NAME=", 5) == 0) {
|
||||
memmove(buffer, buffer + 6, (sizeof buffer) - 6);
|
||||
buffer[strcspn(buffer, "\"")] = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
int ret;
|
||||
char *args[100];
|
||||
if (strcmp(buffer, "EndeavourOS") == 0) {
|
||||
ret = run_cmd_and_wait("yay");
|
||||
} else if (strcmp(buffer, "Debian") == 0) {
|
||||
ret = run_cmd_and_wait("apt update");
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
ret = run_cmd_and_wait("apt upgrade");
|
||||
} else if (strcmp(buffer, "FreeBSD") == 0) {
|
||||
ret = run_cmd_and_wait("pkg update");
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
ret = run_cmd_and_wait("pkg upgrade");
|
||||
} else {
|
||||
fprintf(stderr, "OS %s not suported\n", buffer);
|
||||
return 1;
|
||||
}
|
||||
return ret;
|
||||
}
|
Reference in New Issue
Block a user