file_utils: Add a new get_file_size function

The get_file_size function allows to conveniently get the size of the file.

Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
This commit is contained in:
Adrian Warecki 2023-02-22 18:17:45 +01:00 committed by pjdobrowolski
parent 055ea7eca8
commit 6a64cb9c1b
2 changed files with 44 additions and 0 deletions

View File

@ -36,3 +36,38 @@ int create_file_name(char *new_name, const size_t name_size, const char *templat
return 0;
}
/**
* Get file size
* @param [in] f file handle
* @param [in] filename File name used to display the error message
* @param [out] size output for file size
* @param error code, 0 when success
*/
int get_file_size(FILE *f, const char* filename, size_t *size)
{
int ret;
assert(size);
/* get file size */
ret = fseek(f, 0, SEEK_END);
if (ret < 0) {
fprintf(stderr, "error: unable to seek eof %s %d\n", filename, errno);
return -errno;
}
*size = ftell(f);
if (*size < 0) {
fprintf(stderr, "error: unable to get file size for %s %d\n", filename, errno);
return -errno;
}
ret = fseek(f, 0, SEEK_SET);
if (ret < 0) {
fprintf(stderr, "error: unable to seek set %s %d\n", filename, errno);
return -errno;
}
return 0;
}

View File

@ -19,4 +19,13 @@
int create_file_name(char *new_name, const size_t name_size, const char *template_name,
const char *new_ext);
/**
* Get file size
* @param [in] f file handle
* @param [in] filename File name used to display the error message
* @param [out] size output for file size
* @param error code, 0 when success
*/
int get_file_size(FILE *f, const char *filename, size_t *size);
#endif /* __FILE_UTILS_H__ */