xref: /minix3/minix/lib/libc/sys/pread.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc #include <sys/cdefs.h>
2*433d6423SLionel Sambuc #include "namespace.h"
3*433d6423SLionel Sambuc 
4*433d6423SLionel Sambuc #include <errno.h>
5*433d6423SLionel Sambuc #include <unistd.h>
6*433d6423SLionel Sambuc 
7*433d6423SLionel Sambuc #ifdef __weak_alias
__weak_alias(pread,_pread)8*433d6423SLionel Sambuc __weak_alias(pread, _pread)
9*433d6423SLionel Sambuc #endif
10*433d6423SLionel Sambuc 
11*433d6423SLionel Sambuc ssize_t pread(int fd, void *buffer, size_t nbytes, off_t where)
12*433d6423SLionel Sambuc {
13*433d6423SLionel Sambuc 	off_t here;
14*433d6423SLionel Sambuc 	ssize_t r;
15*433d6423SLionel Sambuc 
16*433d6423SLionel Sambuc 	if((here = lseek(fd, 0, SEEK_CUR)) < 0)
17*433d6423SLionel Sambuc 		return -1;
18*433d6423SLionel Sambuc 
19*433d6423SLionel Sambuc 	if(lseek(fd, where, SEEK_SET) < 0)
20*433d6423SLionel Sambuc 		return -1;
21*433d6423SLionel Sambuc 
22*433d6423SLionel Sambuc 	if((r=read(fd, buffer, nbytes)) < 0) {
23*433d6423SLionel Sambuc 		int e = errno;
24*433d6423SLionel Sambuc 		lseek(fd, here, SEEK_SET);
25*433d6423SLionel Sambuc 		errno = e;
26*433d6423SLionel Sambuc 		return -1;
27*433d6423SLionel Sambuc 	}
28*433d6423SLionel Sambuc 
29*433d6423SLionel Sambuc 	if(lseek(fd, here, SEEK_SET) < 0)
30*433d6423SLionel Sambuc 		return -1;
31*433d6423SLionel Sambuc 
32*433d6423SLionel Sambuc 	return r;
33*433d6423SLionel Sambuc }
34*433d6423SLionel Sambuc 
35