mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 05/14] libfile: move open_and_lseek() to libfile
Date: Tue, 19 Apr 2016 09:36:43 +0200	[thread overview]
Message-ID: <1461051412-25711-6-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1461051412-25711-1-git-send-email-s.hauer@pengutronix.de>

libfile is a collection of helpers for handling files. open_and_lseek()
is a perfect match for this, so move it there.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/md.c     |  1 +
 commands/mem.c    | 23 -----------------------
 commands/memcmp.c |  1 +
 commands/memcpy.c |  1 +
 commands/memset.c |  1 +
 commands/mm.c     |  1 +
 commands/mw.c     |  1 +
 include/common.h  |  1 -
 include/libfile.h |  2 ++
 lib/libfile.c     | 32 ++++++++++++++++++++++++++++++++
 10 files changed, 40 insertions(+), 24 deletions(-)

diff --git a/commands/md.c b/commands/md.c
index c88259a..3e83c72 100644
--- a/commands/md.c
+++ b/commands/md.c
@@ -28,6 +28,7 @@
 #include <malloc.h>
 #include <errno.h>
 #include <fs.h>
+#include <libfile.h>
 #include <fcntl.h>
 #include <getopt.h>
 #include <linux/stat.h>
diff --git a/commands/mem.c b/commands/mem.c
index 23c703f..907f1f7 100644
--- a/commands/mem.c
+++ b/commands/mem.c
@@ -41,29 +41,6 @@
 
 char *mem_rw_buf;
 
-int open_and_lseek(const char *filename, int mode, loff_t pos)
-{
-	int fd, ret;
-
-	fd = open(filename, mode | O_RDONLY);
-	if (fd < 0) {
-		perror("open");
-		return fd;
-	}
-
-	if (!pos)
-		return fd;
-
-	ret = lseek(fd, pos, SEEK_SET);
-	if (ret == -1) {
-		perror("lseek");
-		close(fd);
-		return -errno;
-	}
-
-	return fd;
-}
-
 /*
  * Common function for parsing options for the 'md', 'mw', 'memcpy', 'memcmp'
  * commands.
diff --git a/commands/memcmp.c b/commands/memcmp.c
index e079d5f..ce044df 100644
--- a/commands/memcmp.c
+++ b/commands/memcmp.c
@@ -28,6 +28,7 @@
 #include <malloc.h>
 #include <errno.h>
 #include <fs.h>
+#include <libfile.h>
 #include <fcntl.h>
 #include <getopt.h>
 #include <linux/stat.h>
diff --git a/commands/memcpy.c b/commands/memcpy.c
index 9c8b645..168ef3b 100644
--- a/commands/memcpy.c
+++ b/commands/memcpy.c
@@ -28,6 +28,7 @@
 #include <malloc.h>
 #include <errno.h>
 #include <fs.h>
+#include <libfile.h>
 #include <fcntl.h>
 #include <getopt.h>
 #include <linux/stat.h>
diff --git a/commands/memset.c b/commands/memset.c
index fc5b659..f871e07 100644
--- a/commands/memset.c
+++ b/commands/memset.c
@@ -28,6 +28,7 @@
 #include <malloc.h>
 #include <errno.h>
 #include <fs.h>
+#include <libfile.h>
 #include <fcntl.h>
 #include <getopt.h>
 #include <linux/stat.h>
diff --git a/commands/mm.c b/commands/mm.c
index 7c890a6..6d2a887 100644
--- a/commands/mm.c
+++ b/commands/mm.c
@@ -22,6 +22,7 @@
 #include <malloc.h>
 #include <errno.h>
 #include <fs.h>
+#include <libfile.h>
 #include <fcntl.h>
 #include <getopt.h>
 #include <linux/stat.h>
diff --git a/commands/mw.c b/commands/mw.c
index 8ca3c61..bb6a16e 100644
--- a/commands/mw.c
+++ b/commands/mw.c
@@ -28,6 +28,7 @@
 #include <malloc.h>
 #include <errno.h>
 #include <fs.h>
+#include <libfile.h>
 #include <fcntl.h>
 #include <getopt.h>
 #include <linux/stat.h>
diff --git a/include/common.h b/include/common.h
index 54120c4..680a0af 100644
--- a/include/common.h
+++ b/include/common.h
@@ -128,7 +128,6 @@ static inline void print_hex_dump(const char *level, const char *prefix_str,
 
 int mem_parse_options(int argc, char *argv[], char *optstr, int *mode,
 		char **sourcefile, char **destfile, int *swab);
-int open_and_lseek(const char *filename, int mode, loff_t pos);
 #define RW_BUF_SIZE	(unsigned)4096
 
 extern const char version_string[];
diff --git a/include/libfile.h b/include/libfile.h
index d5b914a..51fa060 100644
--- a/include/libfile.h
+++ b/include/libfile.h
@@ -19,4 +19,6 @@ int copy_recursive(const char *src, const char *dst);
 
 int compare_file(const char *f1, const char *f2);
 
+int open_and_lseek(const char *filename, int mode, loff_t pos);
+
 #endif /* __LIBFILE_H */
diff --git a/lib/libfile.c b/lib/libfile.c
index a27460c..2c72ffe 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -442,3 +442,35 @@ err_out1:
 	close(fd1);
 	return ret;
 }
+
+/**
+ * open_and_lseek - open file and lseek to position
+ * @filename:	The file to open
+ * @mode:	The file open mode
+ * @pos:	The position to lseek to
+ *
+ * Return: If successful this function returns a positive filedescriptor
+ *         number, otherwise a negative error code is returned
+ */
+int open_and_lseek(const char *filename, int mode, loff_t pos)
+{
+	int fd, ret;
+
+	fd = open(filename, mode | O_RDONLY);
+	if (fd < 0) {
+		perror("open");
+		return fd;
+	}
+
+	if (!pos)
+		return fd;
+
+	ret = lseek(fd, pos, SEEK_SET);
+	if (ret == -1) {
+		perror("lseek");
+		close(fd);
+		return -errno;
+	}
+
+	return fd;
+}
-- 
2.7.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2016-04-19  7:37 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-19  7:36 include/prototype cleanup Sascha Hauer
2016-04-19  7:36 ` [PATCH 01/14] include: move run_command prototype to command.h Sascha Hauer
2016-04-19  7:36 ` [PATCH 02/14] include/common.h: remove unused struct memarea_info Sascha Hauer
2016-04-19  7:36 ` [PATCH 03/14] include: move shell prototypes to shell.h Sascha Hauer
2016-04-19  7:36 ` [PATCH 04/14] include: move crc specific stuff to crc.h Sascha Hauer
2016-04-19  7:36 ` Sascha Hauer [this message]
2016-04-19  7:36 ` [PATCH 06/14] show_progress: print spaces with %*s Sascha Hauer
2016-04-19  7:36 ` [PATCH 07/14] string: Fix (v)asprintf prototypes Sascha Hauer
2016-04-19  7:36 ` [PATCH 08/14] move make_directory declaration to libfile.h Sascha Hauer
2016-04-19  7:36 ` [PATCH 09/14] move unlink_recursive " Sascha Hauer
2016-04-19  7:36 ` [PATCH 10/14] fs: move libc function prototypes to their correct locations Sascha Hauer
2016-04-19  7:36 ` [PATCH 11/14] stdio: rename getc to getchar Sascha Hauer
2016-04-19  7:36 ` [PATCH 12/14] stdio: replace fprintf(stderr,...) with eprintf Sascha Hauer
2016-04-19  7:36 ` [PATCH 13/14] stdio: Replace FILE functions with filedescriptor functions Sascha Hauer
2016-04-19  7:36 ` [PATCH 14/14] stdio: Whitespace cleanup Sascha Hauer

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=1461051412-25711-6-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.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