From 55fb5c54c6151fd8120cc44e5e9c302e08972e4d Mon Sep 17 00:00:00 2001 From: BritishTeapot Date: Mon, 14 Apr 2025 16:46:06 +0200 Subject: [PATCH] Improved code readability --- src/ui-socket.c | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/ui-socket.c b/src/ui-socket.c index afbbc27..8731697 100644 --- a/src/ui-socket.c +++ b/src/ui-socket.c @@ -13,7 +13,6 @@ #include "perm_permissions_table.h" #include "temp_permissions_table.h" #include "ui-socket.h" -#include #include #include #include @@ -22,9 +21,10 @@ #include #include +#define ZENITY_TEMP_ALLOW_MESSAGE "Allow this time\n" + int init_ui_socket(const char *perm_permissions_db_filename) { - char line[256]; - FILE *fp; + FILE *fp = NULL; if (init_temp_permissions_table()) { fprintf(stderr, "Could not initialize temporary permissions table.\n"); @@ -43,13 +43,11 @@ int init_ui_socket(const char *perm_permissions_db_filename) { return 1; } - while (fgets(line, sizeof(line), fp)) - printf("%s", line); pclose(fp); return 0; } -void destroy_ui_socket() { +void destroy_ui_socket(void) { destroy_temp_permissions_table(); destroy_perm_permissions_table(); } @@ -63,16 +61,16 @@ void destroy_ui_socket() { * @return: 0 if access is denied, 1 if access is allowed, 2 if access is * allowed for the runtime of the process */ -int ask_access(const char *filename, struct process_info pi) { - FILE *fp; +int ask_access(const char *filename, struct process_info proc_info) { + FILE *fp = NULL; size_t command_len = - 139 + sizeof(pid_t) * 8 + strlen(pi.name) + strlen(filename); + 139 + sizeof(pid_t) * 8 + strlen(proc_info.name) + strlen(filename); char *command = (char *)malloc(command_len); snprintf(command, command_len, "zenity --question --extra-button \"Allow this time\" --title " "\"Allow Access?\" --text \"Allow process " "%s with PID %d to access %s\"", - pi.name, pi.PID, filename); + proc_info.name, proc_info.PID, filename); // Zenity Question Message Popup fp = popen(command, "r"); @@ -86,10 +84,10 @@ int ask_access(const char *filename, struct process_info pi) { // if the user clicks the "Allow this time" button, `zenity` will only // write it to `stdout`, but the exit code will still be `1`. So, we need // to manually check the output. - char buffer[1024]; + char buffer[sizeof(ZENITY_TEMP_ALLOW_MESSAGE) + 1]; while (fgets(buffer, sizeof(buffer), fp)) { printf("%s", buffer); - if (strcmp(buffer, "Allow this time\n") == 0) { + if (strcmp(buffer, ZENITY_TEMP_ALLOW_MESSAGE) == 0) { pclose(fp); return 2; } @@ -116,9 +114,11 @@ int ask_access(const char *filename, struct process_info pi) { * @param opts: options (GRANT_TEMP, GRANT_PERM) * @return: 0 if access is denied, 1 if access is allowed */ -int interactive_access(const char *filename, struct process_info pi, int opts) { +int interactive_access(const char *filename, struct process_info proc_info, + int opts) { - if (check_temp_access(filename, pi) || check_perm_access(filename, pi)) { + if (check_temp_access(filename, proc_info) || + check_perm_access(filename, proc_info)) { // access was already granted before return 1; } @@ -127,22 +127,24 @@ int interactive_access(const char *filename, struct process_info pi, int opts) { // permissions are granted if (opts & GRANT_PERM) { - give_perm_access(filename, pi); + give_perm_access(filename, proc_info); return 1; } if (opts & GRANT_TEMP) { - give_temp_access(filename, pi); + give_temp_access(filename, proc_info); return 1; } - int user_response = ask_access(filename, pi); + int user_response = ask_access(filename, proc_info); if (user_response == 1) { // user said "yes" - give_perm_access(filename, pi); + give_perm_access(filename, proc_info); return 1; - } else if (user_response == 2) { + } + + if (user_response == 2) { // user said "yes, but only this time" - give_temp_access(filename, pi); + give_temp_access(filename, proc_info); return 1; }