return 0;
}
+int os_filesize(int fd)
+{
+ off_t size;
+
+ size = os_lseek(fd, 0, OS_SEEK_END);
+ if (size < 0)
+ return -errno;
+ if (os_lseek(fd, 0, OS_SEEK_SET) < 0)
+ return -errno;
+
+ return size;
+}
+
int os_read_file(const char *fname, void **bufp, int *sizep)
{
off_t size;
printf("Cannot open file '%s'\n", fname);
goto err;
}
- size = os_lseek(fd, 0, OS_SEEK_END);
+ size = os_filesize(fd);
if (size < 0) {
- printf("Cannot seek to end of file '%s'\n", fname);
- goto err;
- }
- if (os_lseek(fd, 0, OS_SEEK_SET) < 0) {
- printf("Cannot seek to start of file '%s'\n", fname);
+ printf("Cannot get file size of '%s'\n", fname);
goto err;
}
+
*bufp = os_malloc(size);
if (!*bufp) {
printf("Not enough memory to read file '%s'\n", fname);
#define OS_SEEK_CUR 1
#define OS_SEEK_END 2
+/**
+ * os_filesize() - Calculate the size of a file
+ *
+ * @fd: File descriptor as returned by os_open()
+ * Return: file size or negative error code
+ */
+int os_filesize(int fd);
+
/**
* Access to the OS open() system call
*