From 967ff83362de106cf68e4e3718c1ef5942691f61 Mon Sep 17 00:00:00 2001 From: rafa_99 Date: Wed, 30 Sep 2020 22:05:31 +0000 Subject: Removed Repeated Code --- flags.c | 104 ++++++++++++++-------------------------------------------------- utils.c | 9 ++++++ utils.h | 1 + 3 files changed, 32 insertions(+), 82 deletions(-) diff --git a/flags.c b/flags.c index bc0f22e..416898c 100644 --- a/flags.c +++ b/flags.c @@ -69,59 +69,39 @@ void cleanPackages(int os) void help() { printf("PKG Options:\n" - "c - removes orphans and cleans the package cache\n" - "h - display this message\n" - "i - installs selected packages\n" - "q - display installed packages\n" - "r - remove a selected package\n" - "s - look up for a package in the available repositories\n" - "u - updates repos and packages\n\n"); + "c - removes orphans and cleans the package cache\n" + "h - display this message\n" + "i - installs selected packages\n" + "q - display installed packages\n" + "r - remove a selected package\n" + "s - look up for a package in the available repositories\n" + "u - updates repos and packages\n\n"); } void installPackages(int os, char *packages) { if ( strlen(packages) > 0 ) { - char* command = (char *) calloc(strlen(packages) + 80, sizeof(char)); switch(os) { case apt: - strcpy(command, "apt install "); - strcat(command, packages); - system(command); - free(command); + commandProcessor(packages, "apt install "); break; case dnf: - strcpy(command, "dnf install "); - strcat(command, packages); - system(command); - free(command); + commandProcessor(packages, "dnf install "); break; case emerge: - strcpy(command, "emerge -aq "); - strcat(command, packages); - system(command); - free(command); + commandProcessor(packages, "emerge -aq "); break; case pacman: - strcpy(command, "pacman -S "); - strcat(command, packages); - system(command); - free(command); + commandProcessor(packages, "pacman -S "); break; case xbps: - strcpy(command, "xbps-install -S "); - strcat(command, packages); - system(command); - free(command); - break; - - default: - free(command); + commandProcessor(packages, "xbps-install -S "); } } } @@ -155,46 +135,26 @@ void removePackages(int os, char *packages) { if ( strlen(packages) > 0 ) { - char* command = (char *) calloc(strlen(packages) + 80, sizeof(char)); switch(os) { case apt: - strcpy(command, "apt autoremove --purge "); - strcat(command, packages); - system(command); - free(command); + commandProcessor(packages, "apt autoremove --purge "); break; case dnf: - strcpy(command, "dnf remove "); - strcat(command, packages); - system(command); - free(command); + commandProcessor(packages, "dnf remove "); break; case emerge: - strcpy(command, "emerge -caq "); - strcat(command, packages); - system(command); - free(command); + commandProcessor(packages, "emerge -caq "); break; case pacman: - strcpy(command, "pacman -Rnsc "); - strcat(command, packages); - system(command); - free(command); + commandProcessor(packages, "pacman -Rnsc "); break; case xbps: - strcpy(command, "xbps-remove -ROo "); - strcat(command, packages); - system(command); - free(command); - break; - - default: - free(command); + commandProcessor(packages, "xbps-remove -ROo "); } } } @@ -203,46 +163,26 @@ void searchPackages(int os, char *packages) { if ( strlen(packages) > 0 ) { - char* command = (char *) calloc(strlen(packages) + 80, sizeof(char)); switch(os) { case apt: - strcpy(command, "apt search "); - strcat(command, packages); - system(command); - free(command); + commandProcessor(packages, "apt search "); break; case dnf: - strcpy(command, "dnf search "); - strcat(command, packages); - system(command); - free(command); + commandProcessor(packages, "dnf search "); break; case emerge: - strcpy(command, "emerge -s "); - strcat(command, packages); - system(command); - free(command); + commandProcessor(packages, "emerge -s "); break; case pacman: - strcpy(command, "pacman -Ss "); - strcat(command, packages); - system(command); - free(command); + commandProcessor(packages, "pacman -Ss "); break; case xbps: - strcpy(command, "xbps-query -Rs "); - strcat(command, packages); - system(command); - free(command); - break; - - default: - free(command); + commandProcessor(packages, "xbps-query -Rs "); } } } diff --git a/utils.c b/utils.c index da61aed..5ca2174 100644 --- a/utils.c +++ b/utils.c @@ -65,3 +65,12 @@ char* stringedArgument(int argc, char** argv) } return arg; } + +void commandProcessor(char* packages, char* initCommand) +{ + char* command = (char *) calloc(strlen(packages) + 80, sizeof(char)); + strcpy(command, initCommand); + strcat(command, packages); + system(command); + free(command); +} diff --git a/utils.h b/utils.h index 2957918..f7cb640 100644 --- a/utils.h +++ b/utils.h @@ -15,3 +15,4 @@ enum os int checkIfFileExists(char *path); int detectOsType(); char* stringedArgument(int argc, char** argv); +void commandProcessor(char* packages, char* initCommand); -- cgit v1.2.3