# HG changeset patch # User meillo@marmaro.de # Date 1273145517 -7200 # Node ID e1004fcc93c99b2818256eef742e1cc775b0b647 # Parent 941f755e2965f6c6f0fa3035c0ff1a4db62a9220 flattened conditional nesting diff -r 941f755e2965 -r e1004fcc93c9 src/online.c --- a/src/online.c Thu May 06 13:24:49 2010 +0200 +++ b/src/online.c Thu May 06 13:31:57 2010 +0200 @@ -44,28 +44,31 @@ old_signal = signal(SIGCHLD, SIG_DFL); in = peopen(pipe, "r", environ, &pid); - if (in != NULL) { - gchar output[256]; - if (fgets(output, 255, in)) { - g_strchomp(g_strchug(output)); - if (strlen(output) == 0) { - logwrite(LOG_ALERT, "only whitespace connection name\n"); - name = NULL; - } else { - name = g_strdup(output); - } + if (in == NULL) { + logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno)); + signal(SIGCHLD, old_signal); + return NULL; + } + + gchar output[256]; + if (fgets(output, 255, in)) { + g_strchomp(g_strchug(output)); + if (strlen(output) == 0) { + logwrite(LOG_ALERT, "only whitespace connection name\n"); + name = NULL; } else { - logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe); - name = NULL; + name = g_strdup(output); } - fclose(in); - waitpid(pid, &status, 0); - if (WEXITSTATUS(status) != EXIT_SUCCESS) { - g_free(name); - name = NULL; - } - } else - logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno)); + } else { + logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe); + name = NULL; + } + fclose(in); + waitpid(pid, &status, 0); + if (WEXITSTATUS(status) != EXIT_SUCCESS) { + g_free(name); + name = NULL; + } signal(SIGCHLD, old_signal); @@ -75,58 +78,63 @@ gchar* detect_online() { - if (conf.online_detect != NULL) { - if (strcmp(conf.online_detect, "file") == 0) { - DEBUG(3) debugf("online detection method 'file'\n"); - if (conf.online_file != NULL) { - struct stat st; - if (stat(conf.online_file, &st) == 0) { - FILE *fptr = fopen(conf.online_file, "r"); - if (fptr) { - char buf[256]; - if (fgets(buf, 256, fptr) == NULL) { - logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file); - fclose(fptr); - return NULL; - } - g_strchomp(g_strchug(buf)); - fclose(fptr); - if (strlen(buf) == 0) { - logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file); - return NULL; - } - return g_strdup(buf); - } else { - logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno)); - return NULL; - } - } else if (errno == ENOENT) { - logwrite(LOG_NOTICE, "not online.\n"); - return NULL; - } else { - logwrite(LOG_ALERT, "stat of %s failed: %s", conf.online_file, strerror(errno)); - return NULL; - } - } else - logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n"); -#ifdef ENABLE_MSERVER - } else if (strcmp(conf.online_detect, "mserver") == 0) { - DEBUG(3) debugf("connection method 'mserver'\n"); - return mserver_detect_online(conf.mserver_iface); -#endif - } else if (strcmp(conf.online_detect, "pipe") == 0) { - DEBUG(3) debugf("connection method 'pipe'\n"); - if (conf.online_pipe) - return detect_online_pipe(conf.online_pipe); - else { - logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n"); + if (conf.online_detect == NULL) { + return NULL; + } + + if (strcmp(conf.online_detect, "file") == 0) { + DEBUG(3) debugf("online detection method 'file'\n"); + if (conf.online_file != NULL) { + logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n"); + return NULL; + } + + struct stat st; + if (stat(conf.online_file, &st) == 0) { + FILE *fptr = fopen(conf.online_file, "r"); + if (!fptr) { + logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno)); return NULL; } - } else if (strcmp(conf.online_detect, "argument") == 0) { - return connection_name; + char buf[256]; + if (fgets(buf, 256, fptr) == NULL) { + logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file); + fclose(fptr); + return NULL; + } + g_strchomp(g_strchug(buf)); + fclose(fptr); + if (strlen(buf) == 0) { + logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file); + return NULL; + } + return g_strdup(buf); + } else if (errno == ENOENT) { + logwrite(LOG_NOTICE, "not online.\n"); + return NULL; } else { - DEBUG(3) debugf("no connection method selected\n"); + logwrite(LOG_ALERT, "stat of %s failed: %s", conf.online_file, strerror(errno)); + return NULL; } + +#ifdef ENABLE_MSERVER + } else if (strcmp(conf.online_detect, "mserver") == 0) { + DEBUG(3) debugf("connection method 'mserver'\n"); + return mserver_detect_online(conf.mserver_iface); +#endif + } else if (strcmp(conf.online_detect, "pipe") == 0) { + DEBUG(3) debugf("connection method 'pipe'\n"); + if (conf.online_pipe) + return detect_online_pipe(conf.online_pipe); + else { + logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n"); + return NULL; + } + } else if (strcmp(conf.online_detect, "argument") == 0) { + return connection_name; + } else { + DEBUG(3) debugf("no connection method selected\n"); } + return NULL; }