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