public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Vadim Kochan <vadim4j@gmail.com>
To: netdev@vger.kernel.org
Cc: Vadim Kochan <vadim4j@gmail.com>
Subject: [PATCH iproute2 2/2] lib names: Add helper func for parse id and name from file
Date: Sat,  6 Dec 2014 04:05:12 +0200	[thread overview]
Message-ID: <1417831512-19452-3-git-send-email-vadim4j@gmail.com> (raw)
In-Reply-To: <1417831512-19452-1-git-send-email-vadim4j@gmail.com>

Signed-off-by: Vadim Kochan <vadim4j@gmail.com>
---
 lib/rt_names.c | 68 +++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 39 insertions(+), 29 deletions(-)

diff --git a/lib/rt_names.c b/lib/rt_names.c
index e6a1e01..2f14723 100644
--- a/lib/rt_names.c
+++ b/lib/rt_names.c
@@ -27,43 +27,62 @@
 #define CONFDIR "/etc/iproute2"
 #endif
 
+#define NAME_MAX_LEN 512
+
 struct rtnl_hash_entry {
 	struct rtnl_hash_entry *next;
 	const char *		name;
 	unsigned int		id;
 };
 
+static int fread_id_name(FILE *fp, int *id, char *namebuf)
+{
+	char buf[NAME_MAX_LEN];
+	while (fgets(buf, sizeof(buf), fp)) {
+		char *p = buf;
+
+		while (*p == ' ' || *p == '\t')
+			p++;
+
+		if (*p == '#' || *p == '\n' || *p == 0)
+			continue;
+
+		if (sscanf(p, "0x%x %s\n", id, namebuf) != 2 &&
+				sscanf(p, "0x%x %s #", id, namebuf) != 2 &&
+				sscanf(p, "%d %s\n", id, namebuf) != 2 &&
+				sscanf(p, "%d %s #", id, namebuf) != 2) {
+			strcpy(namebuf, p);
+			return -1;
+		}
+		return 1;
+	}
+	return 0;
+}
+
 static void
 rtnl_hash_initialize(const char *file, struct rtnl_hash_entry **hash, int size)
 {
 	struct rtnl_hash_entry *entry;
-	char buf[512];
 	FILE *fp;
+	int id;
+	char namebuf[NAME_MAX_LEN] = {0};
+	int ret;
 
 	fp = fopen(file, "r");
 	if (!fp)
 		return;
-	while (fgets(buf, sizeof(buf), fp)) {
-		char *p = buf;
-		int id;
-		char namebuf[512];
 
-		while (*p == ' ' || *p == '\t')
-			p++;
-		if (*p == '#' || *p == '\n' || *p == 0)
-			continue;
-		if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
-		    sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
-		    sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
-		    sscanf(p, "%d %s #", &id, namebuf) != 2) {
+	while ((ret = fread_id_name(fp, &id, &namebuf[0]))) {
+		if (ret == -1) {
 			fprintf(stderr, "Database %s is corrupted at %s\n",
-				file, p);
+					file, namebuf);
 			fclose(fp);
 			return;
 		}
 
 		if (id<0)
 			continue;
+
 		entry = malloc(sizeof(*entry));
 		entry->id   = id;
 		entry->name = strdup(namebuf);
@@ -75,31 +94,22 @@ rtnl_hash_initialize(const char *file, struct rtnl_hash_entry **hash, int size)
 
 static void rtnl_tab_initialize(const char *file, char **tab, int size)
 {
-	char buf[512];
 	FILE *fp;
+	int id;
+	char namebuf[NAME_MAX_LEN] = {0};
+	int ret;
 
 	fp = fopen(file, "r");
 	if (!fp)
 		return;
-	while (fgets(buf, sizeof(buf), fp)) {
-		char *p = buf;
-		int id;
-		char namebuf[512];
 
-		while (*p == ' ' || *p == '\t')
-			p++;
-		if (*p == '#' || *p == '\n' || *p == 0)
-			continue;
-		if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
-		    sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
-		    sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
-		    sscanf(p, "%d %s #", &id, namebuf) != 2) {
+	while ((ret = fread_id_name(fp, &id, &namebuf[0]))) {
+		if (ret == -1) {
 			fprintf(stderr, "Database %s is corrupted at %s\n",
-				file, p);
+					file, namebuf);
 			fclose(fp);
 			return;
 		}
-
 		if (id<0 || id>size)
 			continue;
 
-- 
2.1.3

  parent reply	other threads:[~2014-12-06  2:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-06  2:05 [PATCH iproute2 0/2] lib names: Refactoring and cleanups Vadim Kochan
2014-12-06  2:05 ` [PATCH iproute2 1/2] lib names: Use CONFDIR for specify 'group' file path Vadim Kochan
2014-12-06  2:05 ` Vadim Kochan [this message]
2014-12-10  4:38 ` [PATCH iproute2 0/2] lib names: Refactoring and cleanups Stephen Hemminger
2014-12-10 15:10   ` vadim4j
2014-12-10 23:46     ` Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1417831512-19452-3-git-send-email-vadim4j@gmail.com \
    --to=vadim4j@gmail.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox