From 562379b75157fd8908a86fb4620e5f906150691e Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 9 Aug 2015 15:49:36 +0300 Subject: [PATCH] Utilize early returns to reduce excess indentation --- main.c | 299 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 148 insertions(+), 151 deletions(-) diff --git a/main.c b/main.c index f8b4c2e..49239cf 100644 --- a/main.c +++ b/main.c @@ -66,6 +66,12 @@ Property *get_device_properties(char *node) { int pid; int pipe_fd[2]; + char *buf; + int bufsize; + int pos = 0; + int eof = 0; + Property *props = NULL; + int n_props = 0; pipe(pipe_fd); @@ -82,91 +88,82 @@ Property *get_device_properties(char *node) execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL); _exit(1); } - else if(pid>0) + else if(pid<0) { - /* Parent process */ - char *buf; - int bufsize; - int pos = 0; - int eof = 0; - Property *props = NULL; - int n_props = 0; - + close(pipe_fd[0]); close(pipe_fd[1]); - bufsize = 256; - buf = (char *)malloc(bufsize); - - while(1) - { - int newline; - int i; - Property prop; + return NULL; + } - if(!eof) - { - int len; - - len = read(pipe_fd[0], buf+pos, bufsize-pos); - if(len==0) - eof = 1; - else if(len==-1) - break; - pos += len; - } + /* Parent process */ + close(pipe_fd[1]); - newline = -1; - for(i=0; (newline<0 && i=1) { - /* Child process */ - if(verbosity>=1) - { - if(umount) - printf("Running pumount %s\n", device->node); - else - printf("Running pmount %s %s\n", device->node, device->label); - } - - close(pipe_fd[0]); - dup2(pipe_fd[1], 1); - dup2(pipe_fd[1], 2); - if(umount) - execl("/usr/bin/pumount", "pumount", device->node, NULL); + printf("Running pumount %s\n", device->node); else - execl("/usr/bin/pmount", "pmount", device->node, device->label, NULL); - _exit(1); + printf("Running pmount %s %s\n", device->node, device->label); } - else if(pid>0) - { - /* Parent process */ - char buf[1024]; - int pos = 0; - int status = 0; - fd_set fds; - struct timeval timeout; - - close(pipe_fd[1]); - FD_ZERO(&fds); - FD_SET(pipe_fd[0], &fds); - timeout.tv_sec = 0; - timeout.tv_usec = 200000; - - while(1) - { - /* The write fd for the pipe may be inherited by a fuse server - process and stay open indefinitely. */ - if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout)) - { - int len; - len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos-1); - if(len<=0) - break; - pos += len; - } - else if(waitpid(pid, &status, 0)) - { - pid = 0; - break; - } - } + close(pipe_fd[0]); + dup2(pipe_fd[1], 1); + dup2(pipe_fd[1], 2); - if(pid) - waitpid(pid, &status, 0); + if(umount) + execl("/usr/bin/pumount", "pumount", device->node, NULL); + else + execl("/usr/bin/pmount", "pmount", device->node, device->label, NULL); + _exit(1); + } + else if(pid<0) + return; - buf[pos] = 0; + /* Parent process */ - if(verbosity>=1) - { - if(WIFEXITED(status)) - { - if(WEXITSTATUS(status)) - printf("Command exited successfully\n"); - else - printf("Command exited with status %d\n", WEXITSTATUS(status)); - } - else if(WIFSIGNALED(status)) - printf("Command terminated with signal %d\n", WTERMSIG(status)); - else - printf("Command exited with unknown result %04X\n", status); - } + close(pipe_fd[1]); + FD_ZERO(&fds); + FD_SET(pipe_fd[0], &fds); + timeout.tv_sec = 0; + timeout.tv_usec = 200000; - if(!WIFEXITED(status) || WEXITSTATUS(status)) - { - GtkWidget *dialog; + while(1) + { + /* The write fd for the pipe may be inherited by a fuse server + process and stay open indefinitely. */ + if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout)) + { + int len; - /* Pmount terminated with nonzero status or a signal. Display an - error to the user. */ - dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf); - g_signal_connect(dialog, "response", >k_main_quit, NULL); - gtk_widget_show_all(dialog); - } + len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos-1); + if(len<=0) + break; + pos += len; + } + else if(waitpid(pid, &status, 0)) + { + pid = 0; + break; + } + } + + if(pid) + waitpid(pid, &status, 0); + + buf[pos] = 0; + + if(verbosity>=1) + { + if(WIFEXITED(status)) + { + if(WEXITSTATUS(status)) + printf("Command exited successfully\n"); else - gtk_main_quit(); + printf("Command exited with status %d\n", WEXITSTATUS(status)); } + else if(WIFSIGNALED(status)) + printf("Command terminated with signal %d\n", WTERMSIG(status)); + else + printf("Command exited with unknown result %04X\n", status); } + if(!WIFEXITED(status) || WEXITSTATUS(status)) + { + GtkWidget *dialog; + + /* Pmount terminated with nonzero status or a signal. Display an + error to the user. */ + dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf); + g_signal_connect(dialog, "response", >k_main_quit, NULL); + gtk_widget_show_all(dialog); + return; + } + + gtk_main_quit(); + (void)column; } -- 2.43.0