Still sad
[eleutheria.git] / netbsd / cdev / testdev.c
blobc8f8cb3453e6cd064be9c9f78a8f014d6ac0d967
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/ioctl.h>
6 #include <sys/mydev.h>
7 #include <prop/proplib.h>
9 int main(void)
11 struct mydev_params params;
12 prop_dictionary_t pd;
13 prop_string_t ps;
14 int devfd;
16 /* Open device */
17 if ((devfd = open("/dev/mydev", O_RDONLY, 0)) < 0) {
18 fprintf(stderr, "Failed to open /dev/mydev\n");
19 exit(EXIT_FAILURE);
22 /* Send ioctl request in the traditional way */
23 params.number = 42;
24 strncpy(params.string, "Hello World", MAX_STR);
26 if (ioctl(devfd, MYDEVOLDIOCTL, &params) == -1) {
27 close(devfd);
28 err(EXIT_FAILURE, "ioctl()");
31 /* Create dictionary and add a <key, value> pair in it */
32 pd = prop_dictionary_create();
33 if (pd == NULL) {
34 close(devfd);
35 err(EXIT_FAILURE, "prop_dictionary_create()");
38 ps = prop_string_create_cstring("value");
39 if (ps == NULL) {
40 close(devfd);
41 prop_object_release(pd);
42 err(EXIT_FAILURE, "prop_string_create_cstring()");
45 if (prop_dictionary_set(pd, "key", ps) == false) {
46 close(devfd);
47 prop_object_release(ps);
48 prop_object_release(pd);
49 err(EXIT_FAILURE, "prop_dictionary_set()");
52 prop_object_release(ps);
54 /* Send dictionary to kernel space */
55 prop_dictionary_send_ioctl(pd, devfd, MYDEVSETPROPS);
57 prop_object_release(pd);
59 /* Close device */
60 if (close(devfd) == -1)
61 err(EXIT_FAILURE, "close()");
63 return EXIT_SUCCESS;