9
0
Fork 0

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>
This commit is contained in:
Sascha Hauer 2016-04-11 11:06:07 +02:00
parent db33f32842
commit 4f17444ecc
10 changed files with 40 additions and 24 deletions

View File

@ -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>

View File

@ -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.

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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[];

View File

@ -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 */

View File

@ -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;
}