Changeset View
Changeset View
Standalone View
Standalone View
sys/boot/common/misc.c
Show First 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | |||||
/* | /* | ||||
* Read the specified part of a file to kernel space. Unlike regular | * Read the specified part of a file to kernel space. Unlike regular | ||||
* pread, the file pointer is advanced to the end of the read data, | * pread, the file pointer is advanced to the end of the read data, | ||||
* and it just returns 0 if successful. | * and it just returns 0 if successful. | ||||
*/ | */ | ||||
int | int | ||||
kern_pread(int fd, vm_offset_t dest, size_t len, off_t off) | kern_pread(int fd, vm_offset_t dest, size_t len, off_t off) | ||||
{ | { | ||||
ssize_t nread; | |||||
if (lseek(fd, off, SEEK_SET) == -1) { | if (lseek(fd, off, SEEK_SET) == -1) { | ||||
#ifdef DEBUG | #ifdef DEBUG | ||||
printf("\nlseek failed\n"); | printf("\nlseek failed\n"); | ||||
#endif | #endif | ||||
return (-1); | return (-1); | ||||
} | } | ||||
nread = archsw.arch_readin(fd, dest, len); | if ((size_t)archsw.arch_readin(fd, dest, len) != len) { | ||||
if (nread != len) { | |||||
#ifdef DEBUG | #ifdef DEBUG | ||||
printf("\nreadin failed\n"); | printf("\nreadin failed\n"); | ||||
#endif | #endif | ||||
return (-1); | return (-1); | ||||
} | } | ||||
return (0); | return (0); | ||||
} | } | ||||
/* | /* | ||||
* Read the specified part of a file to a malloced buffer. The file | * Read the specified part of a file to a malloced buffer. The file | ||||
* pointer is advanced to the end of the read data. | * pointer is advanced to the end of the read data. | ||||
*/ | */ | ||||
void * | void * | ||||
alloc_pread(int fd, off_t off, size_t len) | alloc_pread(int fd, off_t off, size_t len) | ||||
{ | { | ||||
void *buf; | void *buf; | ||||
ssize_t nread; | |||||
buf = malloc(len); | buf = malloc(len); | ||||
if (buf == NULL) { | if (buf == NULL) { | ||||
#ifdef DEBUG | #ifdef DEBUG | ||||
printf("\nmalloc(%d) failed\n", (int)len); | printf("\nmalloc(%d) failed\n", (int)len); | ||||
#endif | #endif | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
if (lseek(fd, off, SEEK_SET) == -1) { | if (lseek(fd, off, SEEK_SET) == -1) { | ||||
#ifdef DEBUG | #ifdef DEBUG | ||||
printf("\nlseek failed\n"); | printf("\nlseek failed\n"); | ||||
#endif | #endif | ||||
free(buf); | free(buf); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
nread = read(fd, buf, len); | if ((size_t)read(fd, buf, len) != len) { | ||||
if (nread != len) { | |||||
#ifdef DEBUG | #ifdef DEBUG | ||||
printf("\nread failed\n"); | printf("\nread failed\n"); | ||||
#endif | #endif | ||||
free(buf); | free(buf); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
return (buf); | return (buf); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 51 Lines • Show Last 20 Lines |