xref: /plan9/sys/src/ape/lib/bsd/ioctl.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 /* posix */
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 
11 /* bsd extensions */
12 #include <sys/uio.h>
13 #include <sys/socket.h>
14 #include <sys/ioctl.h>
15 
16 int
ioctl(int fd,unsigned long request,void * arg)17 ioctl(int fd, unsigned long request, void* arg)
18 {
19 	struct stat d;
20 
21 	if(request == FIONREAD) {
22 		if(fstat(fd, &d) < 0) {
23 			errno = EBADF;
24 			return -1;
25 		}
26 		/* this works if the file is buffered somehow */
27 		*(long*)arg = d.st_size;
28 		return 0;
29 	} else {
30 		errno = EINVAL;
31 		return -1;
32 	}
33 }
34