1 /*
2 * Copyright (c) 2001 Wichert Akkerman <wichert@cistron.nl>
3 * Copyright (c) 2004-2015 Dmitry V. Levin <ldv@altlinux.org>
4 * Copyright (c) 1999-2017 The strace developers.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "ioctl_iocdef.h"
39
40 struct ioctlent {
41 const char *info;
42 const char *name;
43 unsigned int dir;
44 unsigned int type_nr;
45 unsigned int size;
46 };
47
48 static int
is_prefix(const char * s1,const char * s2)49 is_prefix(const char *s1, const char *s2)
50 {
51 size_t len = strlen(s1);
52
53 if (len > strlen(s2))
54 return 0;
55 return !memcmp(s1, s2, len);
56 }
57
58 static int
compare_name_info(const void * a,const void * b)59 compare_name_info(const void *a, const void *b)
60 {
61 int rc;
62
63 const char *name1 = ((struct ioctlent *) a)->name;
64 const char *name2 = ((struct ioctlent *) b)->name;
65 const char *info1 = ((struct ioctlent *) a)->info;
66 const char *info2 = ((struct ioctlent *) b)->info;
67
68 rc = strcmp(name1, name2);
69 if (rc)
70 return rc;
71
72 /*
73 * exception from lexicographical order:
74 * "asm/" < "asm-generic/"
75 */
76 if (is_prefix("asm/", info1) &&
77 is_prefix("asm-generic/", info2))
78 return -1;
79
80 if (is_prefix("asm/", info2) &&
81 is_prefix("asm-generic/", info1))
82 return 1;
83
84 return strcmp(info1, info2);
85 }
86
87 static unsigned int
code(const struct ioctlent * e)88 code(const struct ioctlent *e)
89 {
90 return e->type_nr |
91 (e->size << _IOC_SIZESHIFT) |
92 (e->dir << _IOC_DIRSHIFT);
93 }
94
95 static int
compare_code_name(const void * a,const void * b)96 compare_code_name(const void *a, const void *b)
97 {
98 unsigned int code1 = code((struct ioctlent *) a);
99 unsigned int code2 = code((struct ioctlent *) b);
100 const char *name1 = ((struct ioctlent *) a)->name;
101 const char *name2 = ((struct ioctlent *) b)->name;
102 return (code1 > code2) ?
103 1 : (code1 < code2) ? -1 : strcmp(name1, name2);
104 }
105
106 static void
ioctlsort(struct ioctlent * ioctls,size_t nioctls)107 ioctlsort(struct ioctlent *ioctls, size_t nioctls)
108 {
109 size_t i;
110
111 qsort(ioctls, nioctls, sizeof(ioctls[0]), compare_name_info);
112
113 for (i = 1; i < nioctls; ++i)
114 if (!strcmp(ioctls[i-1].name, ioctls[i].name)) {
115 /*
116 * If there are multiple definitions for the same
117 * name, keep the first one and mark all the rest
118 * for deletion.
119 */
120 ioctls[i].info = NULL;
121 }
122
123 for (i = 1; i < nioctls; ++i)
124 if (!ioctls[i].info) {
125 /*
126 * Change ioctl code of marked elements
127 * to make them sorted to the end of array.
128 */
129 ioctls[i].dir =
130 ioctls[i].type_nr =
131 ioctls[i].size = 0xffffffffu;
132 }
133
134 qsort(ioctls, nioctls, sizeof(ioctls[0]), compare_code_name);
135
136 puts("/* Generated by ioctlsort. */");
137 for (i = 0; i < nioctls; ++i) {
138 if (!ioctls[i].info) {
139 /*
140 * We've reached the first element marked for deletion.
141 */
142 break;
143 }
144 if (i == 0 || code(&ioctls[i-1]) != code(&ioctls[i]) ||
145 !is_prefix(ioctls[i-1].name, ioctls[i].name))
146 printf("{ \"%s\", %#010x },\n",
147 ioctls[i].name, code(ioctls+i));
148 }
149 }
150
151 static struct ioctlent ioctls[] = {
152 #ifdef IOCTLSORT_INC
153 # include IOCTLSORT_INC
154 #else
155 # include "ioctls_arch.h"
156 # include "ioctls_inc.h"
157 #endif
158 };
159
160 int
main(void)161 main(void)
162 {
163 ioctlsort(ioctls, sizeof(ioctls) / sizeof(ioctls[0]));
164 return 0;
165 }
166