- qsort(ioctlent, nioctlents, sizeof ioctlent[0], compare);
- for (i = 0; i < nioctlents; i++) {
- printf("{\"%s\", \"%s\", %#lx},\n",
- ioctlent[i].doth, ioctlent[i].symbol, ioctlent[i].code);
+static int
+compare_code_name(const void* a, const void* b)
+{
+ unsigned int code1 = code((struct ioctlent *) a);
+ unsigned int code2 = code((struct ioctlent *) b);
+ const char *name1 = ((struct ioctlent *) a)->name;
+ const char *name2 = ((struct ioctlent *) b)->name;
+ return (code1 > code2) ?
+ 1 : (code1 < code2) ? -1 : strcmp(name1, name2);
+}
+
+static void
+ioctlsort(struct ioctlent *ioctls, size_t nioctls)
+{
+ size_t i;
+
+ qsort(ioctls, nioctls, sizeof(ioctls[0]), compare_name_info);
+
+ for (i = 1; i < nioctls; ++i)
+ if (!strcmp(ioctls[i-1].name, ioctls[i].name)) {
+ /*
+ * If there are multiple definitions for the same
+ * name, keep the first one and mark all the rest
+ * for deletion.
+ */
+ ioctls[i].info = NULL;
+ }
+
+ for (i = 1; i < nioctls; ++i)
+ if (!ioctls[i].info) {
+ /*
+ * Change ioctl code of marked elements
+ * to make them sorted to the end of array.
+ */
+ ioctls[i].dir =
+ ioctls[i].type_nr =
+ ioctls[i].size = 0xffffffffu;
+ }
+
+ qsort(ioctls, nioctls, sizeof(ioctls[0]), compare_code_name);
+
+ puts("/* Generated by ioctlsort. */");
+ for (i = 0; i < nioctls; ++i) {
+ if (!ioctls[i].info) {
+ /*
+ * We've reached the first element marked for deletion.
+ */
+ break;
+ }
+ if (i == 0 || code(&ioctls[i-1]) != code(&ioctls[i]) ||
+ !is_prefix(ioctls[i-1].name, ioctls[i].name))
+ printf("{ \"%s\", %#010x },\n",
+ ioctls[i].name, code(ioctls+i));