Skip to content

Commit a6fb2aa

Browse files
committed
gridinit_cmd: Minor simplification
1 parent 9749f27 commit a6fb2aa

File tree

1 file changed

+22
-56
lines changed

1 file changed

+22
-56
lines changed

main/gridinit_cmd.c

Lines changed: 22 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -152,37 +152,28 @@ static const gchar description[] =
152152
static int
153153
__open_unix_client(const char *path)
154154
{
155-
int sock;
156-
struct sockaddr_un local = {};
155+
struct sockaddr_un local = {0};
157156

158157
if (!path || strlen(path) >= sizeof(local.sun_path)) {
159158
errno = EINVAL;
160159
return -1;
161160
}
161+
local.sun_family = AF_UNIX;
162+
g_strlcpy(local.sun_path, path, sizeof(local.sun_path));
162163

163-
/* Create ressources to monitor */
164-
sock = socket(PF_UNIX, SOCK_STREAM, 0);
164+
int sock = socket(PF_UNIX, SOCK_STREAM, 0);
165165
if (sock < 0)
166166
return -1;
167167

168-
/* Bind to file */
169-
local.sun_family = AF_UNIX;
170-
g_strlcpy(local.sun_path, path, sizeof(local.sun_path)-1);
171-
172-
if (-1 == connect(sock, (struct sockaddr *)&local, sizeof(local)))
173-
goto label_error;
174-
175-
errno = 0;
176-
return sock;
177-
178-
label_error:
179-
if (sock >= 0) {
180-
typeof(errno) errsav;
181-
errsav = errno;
168+
if (-1 == connect(sock, (struct sockaddr *)&local, sizeof(local))) {
169+
int errsav = errno;
182170
close(sock);
183171
errno = errsav;
172+
return -1;
184173
}
185-
return -1;
174+
175+
errno = 0;
176+
return sock;
186177
}
187178

188179
static gint
@@ -223,8 +214,7 @@ static size_t
223214
get_longest_key(GList *all_jobs)
224215
{
225216
size_t maxlen = 4;
226-
GList *l;
227-
for (l=all_jobs; l ;l=l->next) {
217+
for (GList *l=all_jobs; l ;l=l->next) {
228218
struct child_info_s *ci = l->data;
229219
size_t len = strlen(ci->key);
230220
if (len > maxlen)
@@ -233,36 +223,18 @@ get_longest_key(GList *all_jobs)
233223
return maxlen;
234224
}
235225

236-
static size_t
237-
my_chomp(gchar *str)
238-
{
239-
gchar c;
240-
size_t len;
241-
242-
len = strlen(str);
243-
while (len && (c=str[len-1]) && g_ascii_isspace(c))
244-
str[--len] = '\0';
245-
return len;
246-
}
247-
248226
static void
249227
unpack_line(gchar *str, gchar **start, int *code)
250228
{
251-
gchar c, *p = NULL;
252-
253229
*start = str;
254230
*code = EINVAL;
255-
if (!str || !*str)
256-
return ;
257-
if (!my_chomp(str))
231+
if (!str)
258232
return ;
233+
str = g_strstrip(str);
234+
gchar *p = NULL;
259235
*code = g_ascii_strtoll(str, &p, 10);
260-
261-
if (p) {
262-
while ((c = *p) && g_ascii_isspace(c))
263-
p++;
264-
*start = p;
265-
}
236+
if (p)
237+
*start = g_strchug(p);
266238
}
267239

268240
static GList*
@@ -272,10 +244,8 @@ read_services_list(FILE *in_stream)
272244

273245
while (!feof(in_stream) && !ferror(in_stream)) {
274246
if (NULL != fgets(line, sizeof(line), in_stream)) {
275-
276-
(void) my_chomp(line);
277-
278-
gchar **tokens = g_strsplit_set(line, " \t\r\n", 15);
247+
gchar *l = g_strstrip(line);
248+
gchar **tokens = g_strsplit_set(l, " \t\r\n", 15);
279249
if (tokens) {
280250
if (g_strv_length(tokens) == 15) {
281251
struct child_info_s ci;
@@ -309,10 +279,8 @@ read_services_list(FILE *in_stream)
309279
static void
310280
dump_as_is(FILE *in_stream, void *udata)
311281
{
312-
int code;
313-
gchar *start;
314282
gboolean first = TRUE;
315-
struct dump_as_is_arg_s *dump_args;
283+
struct dump_as_is_arg_s *dump_args = udata;
316284

317285
FORMAT format_t = parse_format(format);
318286

@@ -324,16 +292,14 @@ dump_as_is(FILE *in_stream, void *udata)
324292
else
325293
kw = &KEYWORDS_NORMAL;
326294

327-
328-
dump_args = udata;
329-
330295
print_header(format_t);
331296

332297
while (!feof(in_stream) && !ferror(in_stream)) {
333298
bzero(line, sizeof(line));
334299
if (NULL != fgets(line, sizeof(line), in_stream)) {
335-
start = NULL;
336-
(void)unpack_line(line, &start, &code);
300+
int code = 0;
301+
gchar *start = NULL;
302+
unpack_line(line, &start, &code);
337303

338304
if (dump_args) {
339305
if (code==0 || code==EALREADY)

0 commit comments

Comments
 (0)