add rk_undumpdata
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@23391 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
@@ -55,3 +55,44 @@ rk_dumpdata (const char *filename, const void *buf, size_t size)
|
||||
net_write(fd, buf, size);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
/*
|
||||
* Read all data from a filename, care about errors.
|
||||
*/
|
||||
|
||||
int ROKEN_LIB_FUNCTION
|
||||
rk_undumpdata(const char *filename, void **buf, size_t *size)
|
||||
{
|
||||
struct stat sb;
|
||||
int fd, ret;
|
||||
ssize_t sret;
|
||||
|
||||
*buf = NULL;
|
||||
|
||||
fd = open(filename, O_RDONLY, 0);
|
||||
if (fd < 0)
|
||||
return errno;
|
||||
if (fstat(fd, &sb) != 0){
|
||||
ret = errno;
|
||||
goto out;
|
||||
}
|
||||
*buf = malloc(sb.st_size);
|
||||
if (*buf == NULL) {
|
||||
ret = ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
*size = sb.st_size;
|
||||
|
||||
sret = net_read(fd, *buf, *size);
|
||||
if (sret < 0)
|
||||
ret = errno;
|
||||
else if (sret != *size) {
|
||||
ret = EINVAL;
|
||||
free(*buf);
|
||||
*buf = NULL;
|
||||
}
|
||||
|
||||
out:
|
||||
close(fd);
|
||||
return ret;
|
||||
}
|
||||
|
Reference in New Issue
Block a user