comparison src/online.c @ 33:e1004fcc93c9

flattened conditional nesting
author meillo@marmaro.de
date Thu, 06 May 2010 13:31:57 +0200
parents 7354c2e0eb31
children 8a92de5e8907
comparison
equal deleted inserted replaced
32:941f755e2965 33:e1004fcc93c9
42 gchar *name = NULL; 42 gchar *name = NULL;
43 43
44 old_signal = signal(SIGCHLD, SIG_DFL); 44 old_signal = signal(SIGCHLD, SIG_DFL);
45 45
46 in = peopen(pipe, "r", environ, &pid); 46 in = peopen(pipe, "r", environ, &pid);
47 if (in != NULL) { 47 if (in == NULL) {
48 gchar output[256]; 48 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
49 if (fgets(output, 255, in)) { 49 signal(SIGCHLD, old_signal);
50 g_strchomp(g_strchug(output)); 50 return NULL;
51 if (strlen(output) == 0) { 51 }
52 logwrite(LOG_ALERT, "only whitespace connection name\n"); 52
53 name = NULL; 53 gchar output[256];
54 } else { 54 if (fgets(output, 255, in)) {
55 name = g_strdup(output); 55 g_strchomp(g_strchug(output));
56 } 56 if (strlen(output) == 0) {
57 logwrite(LOG_ALERT, "only whitespace connection name\n");
58 name = NULL;
57 } else { 59 } else {
58 logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe); 60 name = g_strdup(output);
59 name = NULL;
60 } 61 }
61 fclose(in); 62 } else {
62 waitpid(pid, &status, 0); 63 logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe);
63 if (WEXITSTATUS(status) != EXIT_SUCCESS) { 64 name = NULL;
64 g_free(name); 65 }
65 name = NULL; 66 fclose(in);
66 } 67 waitpid(pid, &status, 0);
67 } else 68 if (WEXITSTATUS(status) != EXIT_SUCCESS) {
68 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno)); 69 g_free(name);
70 name = NULL;
71 }
69 72
70 signal(SIGCHLD, old_signal); 73 signal(SIGCHLD, old_signal);
71 74
72 return name; 75 return name;
73 } 76 }
74 77
75 gchar* 78 gchar*
76 detect_online() 79 detect_online()
77 { 80 {
78 if (conf.online_detect != NULL) { 81 if (conf.online_detect == NULL) {
79 if (strcmp(conf.online_detect, "file") == 0) { 82 return NULL;
80 DEBUG(3) debugf("online detection method 'file'\n"); 83 }
81 if (conf.online_file != NULL) { 84
82 struct stat st; 85 if (strcmp(conf.online_detect, "file") == 0) {
83 if (stat(conf.online_file, &st) == 0) { 86 DEBUG(3) debugf("online detection method 'file'\n");
84 FILE *fptr = fopen(conf.online_file, "r"); 87 if (conf.online_file != NULL) {
85 if (fptr) { 88 logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
86 char buf[256]; 89 return NULL;
87 if (fgets(buf, 256, fptr) == NULL) { 90 }
88 logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file); 91
89 fclose(fptr); 92 struct stat st;
90 return NULL; 93 if (stat(conf.online_file, &st) == 0) {
91 } 94 FILE *fptr = fopen(conf.online_file, "r");
92 g_strchomp(g_strchug(buf)); 95 if (!fptr) {
93 fclose(fptr); 96 logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
94 if (strlen(buf) == 0) {
95 logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file);
96 return NULL;
97 }
98 return g_strdup(buf);
99 } else {
100 logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
101 return NULL;
102 }
103 } else if (errno == ENOENT) {
104 logwrite(LOG_NOTICE, "not online.\n");
105 return NULL;
106 } else {
107 logwrite(LOG_ALERT, "stat of %s failed: %s", conf.online_file, strerror(errno));
108 return NULL;
109 }
110 } else
111 logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
112 #ifdef ENABLE_MSERVER
113 } else if (strcmp(conf.online_detect, "mserver") == 0) {
114 DEBUG(3) debugf("connection method 'mserver'\n");
115 return mserver_detect_online(conf.mserver_iface);
116 #endif
117 } else if (strcmp(conf.online_detect, "pipe") == 0) {
118 DEBUG(3) debugf("connection method 'pipe'\n");
119 if (conf.online_pipe)
120 return detect_online_pipe(conf.online_pipe);
121 else {
122 logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
123 return NULL; 97 return NULL;
124 } 98 }
125 } else if (strcmp(conf.online_detect, "argument") == 0) { 99 char buf[256];
126 return connection_name; 100 if (fgets(buf, 256, fptr) == NULL) {
101 logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file);
102 fclose(fptr);
103 return NULL;
104 }
105 g_strchomp(g_strchug(buf));
106 fclose(fptr);
107 if (strlen(buf) == 0) {
108 logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file);
109 return NULL;
110 }
111 return g_strdup(buf);
112 } else if (errno == ENOENT) {
113 logwrite(LOG_NOTICE, "not online.\n");
114 return NULL;
127 } else { 115 } else {
128 DEBUG(3) debugf("no connection method selected\n"); 116 logwrite(LOG_ALERT, "stat of %s failed: %s", conf.online_file, strerror(errno));
117 return NULL;
129 } 118 }
119
120 #ifdef ENABLE_MSERVER
121 } else if (strcmp(conf.online_detect, "mserver") == 0) {
122 DEBUG(3) debugf("connection method 'mserver'\n");
123 return mserver_detect_online(conf.mserver_iface);
124 #endif
125 } else if (strcmp(conf.online_detect, "pipe") == 0) {
126 DEBUG(3) debugf("connection method 'pipe'\n");
127 if (conf.online_pipe)
128 return detect_online_pipe(conf.online_pipe);
129 else {
130 logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
131 return NULL;
132 }
133 } else if (strcmp(conf.online_detect, "argument") == 0) {
134 return connection_name;
135 } else {
136 DEBUG(3) debugf("no connection method selected\n");
130 } 137 }
138
131 return NULL; 139 return NULL;
132 } 140 }