public inbox for b.a.t.m.a.n@lists.open-mesh.org
 help / color / mirror / Atom feed
From: Sven Eckelmann <sven.eckelmann@openmesh.com>
To: b.a.t.m.a.n@lists.open-mesh.org
Cc: Sven Eckelmann <sven.eckelmann@openmesh.com>
Subject: [B.A.T.M.A.N.] [PATCH 10/10] batctl: Simplify concatenation of pathnames
Date: Thu, 23 Nov 2017 15:04:44 +0100	[thread overview]
Message-ID: <20171123140444.17119-11-sven.eckelmann@openmesh.com> (raw)
In-Reply-To: <20171123140444.17119-1-sven.eckelmann@openmesh.com>

The combination of strncpy and strncat is hard to read and it is rather
easy to introduce some kind of problems when using that. The usage of
snprintf simplifies it slightly.

Signed-off-by: Sven Eckelmann <sven.eckelmann@openmesh.com>
---
 bat-hosts.c | 4 +---
 functions.c | 8 ++------
 2 files changed, 3 insertions(+), 9 deletions(-)

diff --git a/bat-hosts.c b/bat-hosts.c
index b530243..54b0a18 100644
--- a/bat-hosts.c
+++ b/bat-hosts.c
@@ -194,9 +194,7 @@ void bat_hosts_init(int read_opt)
 			if (!homedir)
 				continue;
 
-			strncpy(confdir, homedir, CONF_DIR_LEN);
-			confdir[CONF_DIR_LEN - 1] = '\0';
-			strncat(confdir, &bat_hosts_path[i][1], CONF_DIR_LEN - strlen(confdir) - 1);
+			snprintf(confdir, CONF_DIR_LEN, "%s%s", homedir, &bat_hosts_path[i][1]);
 		} else {
 			strncpy(confdir, bat_hosts_path[i], CONF_DIR_LEN);
 			confdir[CONF_DIR_LEN - 1] = '\0';
diff --git a/functions.c b/functions.c
index 868e0ae..8bcf52d 100644
--- a/functions.c
+++ b/functions.c
@@ -208,9 +208,7 @@ int read_file(const char *dir, const char *fname, int read_opt,
 	if (read_opt & USE_BAT_HOSTS)
 		bat_hosts_init(read_opt);
 
-	strncpy(full_path, dir, sizeof(full_path));
-	full_path[sizeof(full_path) - 1] = '\0';
-	strncat(full_path, fname, sizeof(full_path) - strlen(full_path) - 1);
+	snprintf(full_path, sizeof(full_path), "%s%s", dir, fname);
 
 open:
 	line = 0;
@@ -349,9 +347,7 @@ int write_file(const char *dir, const char *fname, const char *arg1,
 	char full_path[500];
 	ssize_t write_len;
 
-	strncpy(full_path, dir, sizeof(full_path));
-	full_path[sizeof(full_path) - 1] = '\0';
-	strncat(full_path, fname, sizeof(full_path) - strlen(full_path) - 1);
+	snprintf(full_path, sizeof(full_path), "%s%s", dir, fname);
 
 	fd = open(full_path, O_WRONLY);
 
-- 
2.11.0


  parent reply	other threads:[~2017-11-23 14:04 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-23 14:04 [B.A.T.M.A.N.] [PATCH 00/10] batctl: Fixes and minor cleanup Sven Eckelmann
2017-11-23 14:04 ` [B.A.T.M.A.N.] [PATCH 01/10] batctl: Print dummy value when localtime failed Sven Eckelmann
2017-11-23 14:04 ` [B.A.T.M.A.N.] [PATCH 02/10] batctl: Handle failure during hash_iterator allocation Sven Eckelmann
2017-11-23 14:04 ` [B.A.T.M.A.N.] [PATCH 03/10] batctl: Handle allocation error for path_buff Sven Eckelmann
2017-11-23 14:04 ` [B.A.T.M.A.N.] [PATCH 04/10] batctl: Handle nlmsg_alloc errors Sven Eckelmann
2017-11-23 14:04 ` [B.A.T.M.A.N.] [PATCH 05/10] batctl: Handle nl_socket_alloc errors Sven Eckelmann
2017-11-23 14:04 ` [B.A.T.M.A.N.] [PATCH 06/10] batctl: Handle nl_cb_alloc errors Sven Eckelmann
2017-11-23 14:04 ` [B.A.T.M.A.N.] [PATCH 07/10] batctl: Free nl_sock on genl_ctrl_resolve error Sven Eckelmann
2017-11-23 14:04 ` [B.A.T.M.A.N.] [PATCH 08/10] batctl: Free nl_sock when if_nametoindex failed Sven Eckelmann
2017-11-23 14:04 ` [B.A.T.M.A.N.] [PATCH 09/10] batctl: tcpdump: Fix types for for TT v1 Sven Eckelmann
2017-11-23 14:04 ` Sven Eckelmann [this message]
2017-12-01 11:19 ` [B.A.T.M.A.N.] [PATCH 00/10] batctl: Fixes and minor cleanup Simon Wunderlich

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=20171123140444.17119-11-sven.eckelmann@openmesh.com \
    --to=sven.eckelmann@openmesh.com \
    --cc=b.a.t.m.a.n@lists.open-mesh.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