mydev_test.c (1132B)
1 #include <err.h> 2 #include <fcntl.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <unistd.h> 7 8 #include "mydev.h" 9 10 int 11 main(int argc, char *argv[]) 12 { 13 bar_t bar; 14 char buf[BUFSIZ]; 15 int fd; 16 17 if ((fd = open("/dev/mydev", O_RDWR)) < 0) 18 err(1, "open(/dev/mydev)"); 19 20 if (ioctl(fd, MYDEVIOC_READ, &bar) != 0) 21 err(1, "ioctl(MYDEVIOC_READ)"); 22 printf("%s: ioctl(MYDEVIOC_READ)\t-> x=%d, y=%d\n", 23 getprogname(), bar.x, bar.y); 24 25 printf("%s: ioctl(MYDEVIOC_WRITE)\t-> ", getprogname()); 26 fflush(stdout); 27 28 bar.x = 10; 29 bar.y = 20; 30 if (ioctl(fd, MYDEVIOC_WRITE, &bar) != 0) 31 err(1, "ioctl(MYDEVIOC_WRITE)"); 32 33 if (ioctl(fd, MYDEVIOC_RDWR, &bar) != 0) 34 err(1, "ioctl(MYDEVIOC_RDWR)"); 35 printf("%s: ioctl(MYDEVIOC_RDWR)\t-> x=%d, y=%d\n", 36 getprogname(), bar.x, bar.y); 37 38 (void)strlcpy(buf, "hello from test program", sizeof(buf)); 39 40 printf("%s: write()\t\t\t-> %s\n", getprogname(), buf); 41 fflush(stdout); 42 43 if (write(fd, buf, sizeof(buf)) < 0) 44 err(1, "write"); 45 if (read(fd, buf, sizeof(buf)) < 0) 46 err(1, "read"); 47 printf("%s: read()\t\t\t-> %s\n", getprogname(), buf); 48 49 close(fd); 50 51 return (0); 52 }