- int fd, rc;
- int cmd = IOC_REQUEST_GETATTR;
-
- printf("ioctl type %d, nr %d size %d\n",
- _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
-
- fd = open("/dev/request", O_RDONLY);
- if (fd == -1) {
- printf("error opening /dev/request: %s\n", strerror(errno));
- return 1;
- }
-
- printf("getattr test... ");
- rc = ioctl(fd, IOC_REQUEST_GETATTR, NULL);
- printf("result: %d\n", rc);
-
- printf("readpage test... ");
- rc = ioctl(fd, IOC_REQUEST_READPAGE, NULL);
- printf("result: %d\n", rc);
-
- printf("setattr test... ");
- rc = ioctl(fd, IOC_REQUEST_SETATTR, NULL);
- printf("result: %d\n", rc);
- return 0;
+ int fd = 0;
+ int rc = 0;
+ int c = 0;
+ long cmd = 0;
+ unsigned long arg;
+ char *short_opts = "h", *name = argv[0];
+ static struct option long_opts[] = {
+#define OPT_GETATTR -2
+ {"getattr", no_argument, NULL, OPT_GETATTR},
+#define OPT_READPAGE -3
+ {"readpage", no_argument, NULL, OPT_READPAGE},
+#define OPT_SETATTR -4
+ {"setattr", no_argument, NULL, OPT_SETATTR},
+#define OPT_CREATE -5
+ {"create", no_argument, NULL, OPT_CREATE},
+#define OPT_OPEN -6
+ {"open", no_argument, NULL, OPT_OPEN},
+#define OPT_CLOSE -7
+ {"close", required_argument, NULL, OPT_CLOSE},
+#define OPT_HELP 'h'
+ {"help", no_argument, NULL, OPT_HELP},
+ {0}
+ };
+
+ do {
+ c = getopt_long(argc, argv, short_opts, long_opts, NULL);
+
+ switch (c) {
+ case OPT_HELP:
+ usage(argv[0], 0);
+ break;
+ case OPT_GETATTR:
+ cmd = IOC_REQUEST_GETATTR;
+ name = "getattr";
+ arg = 2;
+ break;
+ case OPT_SETATTR:
+ cmd = IOC_REQUEST_SETATTR;
+ name = "setattr";
+ arg = 2;
+ break;
+ case OPT_READPAGE:
+ cmd = IOC_REQUEST_READPAGE;
+ name = "readpage";
+ arg = 2;
+ break;
+ case OPT_CREATE:
+ cmd = IOC_REQUEST_CREATE;
+ name ="create";
+ arg = 2;
+ break;
+ case OPT_OPEN:
+ cmd = IOC_REQUEST_OPEN;
+ name = "open";
+ arg = 2;
+ break;
+ case OPT_CLOSE:
+ cmd = IOC_REQUEST_CLOSE;
+ name = "close";
+ arg = strtoul(optarg, NULL, 0);
+ break;
+ case '?':
+ usage(argv[0], 1);
+ }
+ } while (c != -1);
+
+ if (cmd == 0)
+ usage(argv[0], 1);
+
+ fd = open("/dev/request", O_RDONLY);
+ if (fd == -1) {
+ fprintf(stderr, "error opening /dev/request: %s\n",
+ strerror(errno));
+ exit(1);
+ }
+
+ fprintf(stderr, "Executing %s test (arg=%lu)...\n", name, arg);
+ if (cmd == IOC_REQUEST_OPEN) {
+ rc = ioctl(fd, cmd, &arg);
+ printf("%lu\n", arg);
+ } else
+ rc = ioctl(fd, cmd, arg);
+ fprintf(stderr, "result code: %d\n", rc);
+
+ return 0;