Skip to content

Commit

Permalink
utilfdt_read: pass back up the length of data read
Browse files Browse the repository at this point in the history
For a follow up commit, we want to be able to scan the buffer that was
returned to us.  In order to do that safely, we need to know how big
the buffer actually is, so create a new set of funcs to pass that back.

Acked-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
  • Loading branch information
vapier authored and Jon Loeliger committed Apr 22, 2013
1 parent 5543b88 commit a6d55e0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
19 changes: 16 additions & 3 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ char get_escape_char(const char *s, int *i)
return val;
}

int utilfdt_read_err(const char *filename, char **buffp)
int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len)
{
int fd = 0; /* assume stdin */
char *buf = NULL;
Expand Down Expand Up @@ -239,13 +239,20 @@ int utilfdt_read_err(const char *filename, char **buffp)
free(buf);
else
*buffp = buf;
*len = bufsize;
return ret;
}

char *utilfdt_read(const char *filename)
int utilfdt_read_err(const char *filename, char **buffp)
{
off_t len;
return utilfdt_read_err_len(filename, buffp, &len);
}

char *utilfdt_read_len(const char *filename, off_t *len)
{
char *buff;
int ret = utilfdt_read_err(filename, &buff);
int ret = utilfdt_read_err_len(filename, &buff, len);

if (ret) {
fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
Expand All @@ -256,6 +263,12 @@ char *utilfdt_read(const char *filename)
return buff;
}

char *utilfdt_read(const char *filename)
{
off_t len;
return utilfdt_read_len(filename, &len);
}

int utilfdt_write_err(const char *filename, const void *blob)
{
int fd = 1; /* assume stdout */
Expand Down
13 changes: 13 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ char get_escape_char(const char *s, int *i);
*/
char *utilfdt_read(const char *filename);

/**
* Like utilfdt_read(), but also passes back the size of the file read.
*
* @param len If non-NULL, the amount of data we managed to read
*/
char *utilfdt_read_len(const char *filename, off_t *len);

/**
* Read a device tree file into a buffer. Does not report errors, but only
* returns them. The value returned can be passed to strerror() to obtain
Expand All @@ -95,6 +102,12 @@ char *utilfdt_read(const char *filename);
*/
int utilfdt_read_err(const char *filename, char **buffp);

/**
* Like utilfdt_read_err(), but also passes back the size of the file read.
*
* @param len If non-NULL, the amount of data we managed to read
*/
int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len);

/**
* Write a device tree buffer to a file. This will report any errors on
Expand Down

0 comments on commit a6d55e0

Please sign in to comment.