diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2016-04-11 11:06:07 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2016-04-15 12:21:45 +0200 |
commit | 4f17444eccebeeadeaed212568a202ef97f4e71f (patch) | |
tree | b006ce0f54caf425510289408ed771c1276ac42f /lib | |
parent | db33f32842973245f0adcae2fb7fd23ec4325630 (diff) | |
download | barebox-4f17444eccebeeadeaed212568a202ef97f4e71f.tar.gz barebox-4f17444eccebeeadeaed212568a202ef97f4e71f.tar.xz |
libfile: move open_and_lseek() to libfile
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>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libfile.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/libfile.c b/lib/libfile.c index a27460c10d..2c72ffe066 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; +} |