Skip to content

Commit

Permalink
examples/lte_azureiot: Use stat() instead of fseek() to get filelength
Browse files Browse the repository at this point in the history
Sometimes filelength() returns invalid length with fseek() implementations.
It can be avoided with stat() one.

Signed-off-by: Takumi Ando <[email protected]>
  • Loading branch information
takumiando committed Feb 2, 2022
1 parent e26b7d6 commit f6aa44c
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions examples/lte_azureiot/lte_azureiot_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/stat.h>
#include "azureiot_if.h"
#include "lte_connection.h"
#include "mbedtls_if.h"
Expand Down Expand Up @@ -495,25 +496,19 @@ static int recv_message(struct azureiot_info *info,
/* ------------------------------------------------------------------------ */
static int filelength(const char *file_name)
{
int size = ERROR;
FILE *fp = fopen(file_name, "rb");
struct stat st;

if (fp)
if (stat(file_name, &st))
{
if (fseek(fp, 0L, SEEK_END) == 0)
{
fpos_t pos;

if (fgetpos(fp, &pos) == 0)
{
size = (int)pos;
}
}
return ERROR;
}

fclose(fp);
if ((st.st_mode & S_IFMT) != S_IFREG)
{
return ERROR;
}

return (int)size;
return st.st_size;
}

/* ------------------------------------------------------------------------ */
Expand Down

0 comments on commit f6aa44c

Please sign in to comment.