]> granicus.if.org Git - strace/blob - ioctlsort.c
tests: fix format warnings on x32
[strace] / ioctlsort.c
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-2018 The strace developers.
5  * All rights reserved.
6  *
7  * SPDX-License-Identifier: LGPL-2.1-or-later
8  */
9
10 #ifdef HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "ioctl_iocdef.h"
19
20 struct ioctlent {
21         const char *info;
22         const char *name;
23         unsigned int dir;
24         unsigned int type_nr;
25         unsigned int size;
26 };
27
28 static int
29 is_prefix(const char *s1, const char *s2)
30 {
31         size_t len = strlen(s1);
32
33         if (len > strlen(s2))
34                 return 0;
35         return !memcmp(s1, s2, len);
36 }
37
38 static int
39 compare_name_info(const void *a, const void *b)
40 {
41         int rc;
42
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;
47
48         rc = strcmp(name1, name2);
49         if (rc)
50                 return rc;
51
52         /*
53          * exception from lexicographical order:
54          * "asm/" < "asm-generic/"
55          */
56         if (is_prefix("asm/", info1) &&
57             is_prefix("asm-generic/", info2))
58                 return -1;
59
60         if (is_prefix("asm/", info2) &&
61             is_prefix("asm-generic/", info1))
62                 return 1;
63
64         return strcmp(info1, info2);
65 }
66
67 static unsigned int
68 code(const struct ioctlent *e)
69 {
70         return e->type_nr |
71                 (e->size << _IOC_SIZESHIFT) |
72                 (e->dir << _IOC_DIRSHIFT);
73 }
74
75 static int
76 compare_code_name(const void *a, const void *b)
77 {
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);
84 }
85
86 static void
87 ioctlsort(struct ioctlent *ioctls, size_t nioctls)
88 {
89         size_t i;
90
91         qsort(ioctls, nioctls, sizeof(ioctls[0]), compare_name_info);
92
93         for (i = 1; i < nioctls; ++i)
94                 if (!strcmp(ioctls[i-1].name, ioctls[i].name)) {
95                         /*
96                          * If there are multiple definitions for the same
97                          * name, keep the first one and mark all the rest
98                          * for deletion.
99                          */
100                         ioctls[i].info = NULL;
101                 }
102
103         for (i = 1; i < nioctls; ++i)
104                 if (!ioctls[i].info) {
105                         /*
106                          * Change ioctl code of marked elements
107                          * to make them sorted to the end of array.
108                          */
109                         ioctls[i].dir =
110                         ioctls[i].type_nr =
111                         ioctls[i].size = 0xffffffffu;
112                 }
113
114         qsort(ioctls, nioctls, sizeof(ioctls[0]), compare_code_name);
115
116         puts("/* Generated by ioctlsort. */");
117         for (i = 0; i < nioctls; ++i) {
118                 if (!ioctls[i].info) {
119                         /*
120                          * We've reached the first element marked for deletion.
121                          */
122                         break;
123                 }
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));
128         }
129 }
130
131 static struct ioctlent ioctls[] = {
132 #ifdef IOCTLSORT_INC
133 # include IOCTLSORT_INC
134 #else
135 # include "ioctls_arch.h"
136 # include "ioctls_inc.h"
137 #endif
138 };
139
140 int
141 main(void)
142 {
143         ioctlsort(ioctls, sizeof(ioctls) / sizeof(ioctls[0]));
144         return 0;
145 }