From patchwork Sat Nov 28 00:00:02 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phil Sutter X-Patchwork-Id: 549604 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id C8F6B1402B8 for ; Sat, 28 Nov 2015 11:00:55 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751446AbbK1AAw (ORCPT ); Fri, 27 Nov 2015 19:00:52 -0500 Received: from orbit.nwl.cc ([176.31.251.142]:60093 "EHLO mail.nwl.cc" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751237AbbK1AAu (ORCPT ); Fri, 27 Nov 2015 19:00:50 -0500 Received: from mail.nwl.cc (orbit [127.0.0.1]) by mail.nwl.cc (Postfix) with ESMTP id 9E722214EB; Sat, 28 Nov 2015 01:00:49 +0100 (CET) Received: from xsao (orbit [IPv6:::1]) by mail.nwl.cc (Postfix) with ESMTP id 7AF7421383; Sat, 28 Nov 2015 01:00:49 +0100 (CET) From: Phil Sutter To: Stephen Hemminger Cc: netdev@vger.kernel.org Subject: [iproute PATCH 2/5] ss: reduce max indentation level in init_service_resolver() Date: Sat, 28 Nov 2015 01:00:02 +0100 Message-Id: <1448668805-28074-3-git-send-email-phil@nwl.cc> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1448668805-28074-1-git-send-email-phil@nwl.cc> References: <1448668805-28074-1-git-send-email-phil@nwl.cc> X-Virus-Scanned: ClamAV using ClamSMTP Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Exit early or continue on error instead of putting conditional into conditional to make reading the code a bit easier. Also, the call to memcpy() can be skipped by initialising prog with the desired prefix. Signed-off-by: Phil Sutter --- misc/ss.c | 53 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/misc/ss.c b/misc/ss.c index a9ae85ec564e9..4988d34e18c78 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -870,31 +870,38 @@ static void init_service_resolver(void) { char buf[128]; FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r"); - if (fp) { - fgets(buf, sizeof(buf), fp); - while (fgets(buf, sizeof(buf), fp) != NULL) { - unsigned int progn, port; - char proto[128], prog[128]; - if (sscanf(buf, "%u %*d %s %u %s", &progn, proto, - &port, prog+4) == 4) { - struct scache *c = malloc(sizeof(*c)); - if (c) { - c->port = port; - memcpy(prog, "rpc.", 4); - c->name = strdup(prog); - if (strcmp(proto, TCP_PROTO) == 0) - c->proto = TCP_PROTO; - else if (strcmp(proto, UDP_PROTO) == 0) - c->proto = UDP_PROTO; - else - c->proto = NULL; - c->next = rlist; - rlist = c; - } - } - } + + if (!fp) + return; + + if (!fgets(buf, sizeof(buf), fp)) { pclose(fp); + return; + } + while (fgets(buf, sizeof(buf), fp) != NULL) { + unsigned int progn, port; + char proto[128], prog[128] = "rpc."; + struct scache *c; + + if (sscanf(buf, "%u %*d %s %u %s", + &progn, proto, &port, prog+4) != 4) + continue; + + if (!(c = malloc(sizeof(*c)))) + continue; + + c->port = port; + c->name = strdup(prog); + if (strcmp(proto, TCP_PROTO) == 0) + c->proto = TCP_PROTO; + else if (strcmp(proto, UDP_PROTO) == 0) + c->proto = UDP_PROTO; + else + c->proto = NULL; + c->next = rlist; + rlist = c; } + pclose(fp); } static int ip_local_port_min, ip_local_port_max;