1/*-
2 * Copyright (c) 2005-2006,2016 John H. Baldwin <jhb@FreeBSD.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27#include <sys/ioccom.h>
28#include <ctype.h>
29#include <errno.h>
30#include <limits.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <sysdecode.h>
34
35static void
36usage(char **av)
37{
38	fprintf(stderr, "%s: <ioctl> [ ... ]\n", av[0]);
39	exit(1);
40}
41
42int
43main(int ac, char **av)
44{
45	unsigned long cmd;
46	const char *name;
47	char *cp;
48	int group, i;
49
50	if (ac < 2)
51		usage(av);
52	printf("  command :  dir  group num  len name\n");
53	for (i = 1; i < ac; i++) {
54		errno = 0;
55		cmd = strtoul(av[i], &cp, 0);
56		if (*cp != '\0' || errno != 0) {
57			fprintf(stderr, "Invalid integer: %s\n", av[i]);
58			usage(av);
59		}
60		printf("0x%08lx: ", cmd);
61		switch (cmd & IOC_DIRMASK) {
62		case IOC_VOID:
63			printf("VOID ");
64			break;
65		case IOC_OUT:
66			printf("OUT  ");
67			break;
68		case IOC_IN:
69			printf("IN   ");
70			break;
71		case IOC_INOUT:
72			printf("INOUT");
73			break;
74		default:
75			printf("%01lx ???", (cmd & IOC_DIRMASK) >> 29);
76			break;
77		}
78		printf(" ");
79		group = IOCGROUP(cmd);
80		if (isprint(group))
81			printf(" '%c' ", group);
82		else
83			printf(" 0x%02x", group);
84		printf(" %3lu %4lu", cmd & 0xff, IOCPARM_LEN(cmd));
85		name = sysdecode_ioctlname(cmd);
86		if (name != NULL)
87			printf(" %s", name);
88		printf("\n");
89	}
90	return (0);
91}
92