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:
parent
db33f32842
commit
4f17444ecc
|
@ -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>
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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[];
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue