2 * Copyright (c) 2001 Wichert Akkerman <wichert@cistron.nl>
3 * Copyright (c) 2004-2015 Dmitry V. Levin <ldv@altlinux.org>
4 * Copyright (c) 1999-2018 The strace developers.
7 * SPDX-License-Identifier: LGPL-2.1-or-later
18 #include "ioctl_iocdef.h"
29 is_prefix(const char *s1, const char *s2)
31 size_t len = strlen(s1);
35 return !memcmp(s1, s2, len);
39 compare_name_info(const void *a, const void *b)
43 const char *name1 = ((struct ioctlent *) a)->name;
44 const char *name2 = ((struct ioctlent *) b)->name;
45 const char *info1 = ((struct ioctlent *) a)->info;
46 const char *info2 = ((struct ioctlent *) b)->info;
48 rc = strcmp(name1, name2);
53 * exception from lexicographical order:
54 * "asm/" < "asm-generic/"
56 if (is_prefix("asm/", info1) &&
57 is_prefix("asm-generic/", info2))
60 if (is_prefix("asm/", info2) &&
61 is_prefix("asm-generic/", info1))
64 return strcmp(info1, info2);
68 code(const struct ioctlent *e)
71 (e->size << _IOC_SIZESHIFT) |
72 (e->dir << _IOC_DIRSHIFT);
76 compare_code_name(const void *a, const void *b)
78 unsigned int code1 = code((struct ioctlent *) a);
79 unsigned int code2 = code((struct ioctlent *) b);
80 const char *name1 = ((struct ioctlent *) a)->name;
81 const char *name2 = ((struct ioctlent *) b)->name;
82 return (code1 > code2) ?
83 1 : (code1 < code2) ? -1 : strcmp(name1, name2);
87 ioctlsort(struct ioctlent *ioctls, size_t nioctls)
91 qsort(ioctls, nioctls, sizeof(ioctls[0]), compare_name_info);
93 for (i = 1; i < nioctls; ++i)
94 if (!strcmp(ioctls[i-1].name, ioctls[i].name)) {
96 * If there are multiple definitions for the same
97 * name, keep the first one and mark all the rest
100 ioctls[i].info = NULL;
103 for (i = 1; i < nioctls; ++i)
104 if (!ioctls[i].info) {
106 * Change ioctl code of marked elements
107 * to make them sorted to the end of array.
111 ioctls[i].size = 0xffffffffu;
114 qsort(ioctls, nioctls, sizeof(ioctls[0]), compare_code_name);
116 puts("/* Generated by ioctlsort. */");
117 for (i = 0; i < nioctls; ++i) {
118 if (!ioctls[i].info) {
120 * We've reached the first element marked for deletion.
124 if (i == 0 || code(&ioctls[i-1]) != code(&ioctls[i]) ||
125 !is_prefix(ioctls[i-1].name, ioctls[i].name))
126 printf("{ \"%s\", %#010x },\n",
127 ioctls[i].name, code(ioctls+i));
131 static struct ioctlent ioctls[] = {
133 # include IOCTLSORT_INC
135 # include "ioctls_arch.h"
136 # include "ioctls_inc.h"
143 ioctlsort(ioctls, sizeof(ioctls) / sizeof(ioctls[0]));