summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPatrick McDermott <patrick.mcdermott@libiquity.com>2023-05-17 04:32:54 (EDT)
committer Patrick McDermott <patrick.mcdermott@libiquity.com>2023-05-17 12:19:30 (EDT)
commit255d04438a6634605b73008351fe2bcf70995658 (patch)
tree6ff57af1489a6eaa20340c4f1c2a8fae8e5eef29 /src
parentbea607f4ed47f97751ac8f5fa4501b90c5a82e14 (diff)
specials: Read and search special files list
specials.txt is a temporary example file.
Diffstat (limited to 'src')
-rw-r--r--src/local.mk2
-rw-r--r--src/specials.c131
-rw-r--r--src/specials.h38
3 files changed, 171 insertions, 0 deletions
diff --git a/src/local.mk b/src/local.mk
index e6d77cc..5e8e4bf 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -8,6 +8,8 @@ opkg_opk_SOURCES += \
%reldir%/main.c \
%reldir%/opk.c \
%reldir%/opk.h \
+ %reldir%/specials.c \
+ %reldir%/specials.h \
%reldir%/ustar.c \
%reldir%/ustar.h
diff --git a/src/specials.c b/src/specials.c
new file mode 100644
index 0000000..8a9fac3
--- /dev/null
+++ b/src/specials.c
@@ -0,0 +1,131 @@
+/*
+ * Copyright (C) 2023 Patrick McDermott
+ *
+ * This file is part of opkg-opkg.
+ *
+ * opkg-opkg is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * opkg-opkg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with opkg-opkg. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <inttypes.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "defs.h"
+#include "specials.h"
+
+struct opkg_opk_specials {
+ char type;
+ uint32_t devmajor;
+ uint32_t devminor;
+ char *name;
+ struct opkg_opk_specials *next;
+};
+
+static int
+_opkg_opk_specials_read_v1(FILE *fp, struct opkg_opk_specials **specials)
+{
+ struct opkg_opk_specials *special;
+
+ *specials = NULL;
+ fscanf(fp, "%*[^\n]\n"); /* Header line */
+ while (feof(fp) == 0) {
+ special = malloc(sizeof(*special));
+ if (special == NULL) {
+ opkg_opk_specials_free(*specials);
+ return OPKG_OPK_ERROR;
+ }
+/* POSIX specifies the assignment-allocation character 'm', but ISO
+ * C doesn't, so GCC prints a warning. */
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wformat"
+ if (fscanf(fp, " %c %" SCNu32 " %" SCNu32 " %ms \n",
+ &special->type,
+ &special->devmajor, &special->devminor,
+ &special->name) != 4) {
+# pragma GCC diagnostic pop
+ opkg_opk_specials_free(*specials);
+ return OPKG_OPK_ERROR;
+ }
+ if (special->type != 'c' && special->type != 'b') {
+ opkg_opk_specials_free(*specials);
+ return OPKG_OPK_ERROR;
+ }
+ special->next = *specials;
+ *specials = special;
+ }
+
+ return OPKG_OPK_OK;
+}
+
+int
+opkg_opk_specials_read(const char *file_name,
+ struct opkg_opk_specials **specials)
+{
+ FILE *fp;
+ unsigned int version;
+
+ fp = fopen(file_name, "r");
+ if (fp == NULL) {
+ return OPKG_OPK_ERROR;
+ }
+ if (fscanf(fp, " version = %u \n", &version) != 1) {
+ fclose(fp);
+ return OPKG_OPK_ERROR;
+ }
+ switch (version) {
+ case 1U:
+ if (_opkg_opk_specials_read_v1(fp, specials) !=
+ OPKG_OPK_OK) {
+ fclose(fp);
+ return OPKG_OPK_ERROR;
+ }
+ break;
+ default:
+ fclose(fp);
+ return OPKG_OPK_ERROR;
+ }
+
+ fclose(fp); /* Just reading, so OK to ignore errors */
+ return OPKG_OPK_OK;
+}
+
+int
+opkg_opk_specials_find(struct opkg_opk_specials *specials, const char *find,
+ char *type, uint32_t *devmajor, uint32_t *devminor)
+{
+ for (; specials != NULL; specials = specials->next) {
+ if (strcmp(find, specials->name) == 0) {
+ *type = specials->type;
+ *devmajor = specials->devmajor;
+ *devminor = specials->devminor;
+ return OPKG_OPK_OK;
+ }
+ }
+
+ return OPKG_OPK_END;
+}
+
+void
+opkg_opk_specials_free(struct opkg_opk_specials *specials)
+{
+ struct opkg_opk_specials *special;
+
+ while (specials != NULL) {
+ special = specials;
+ specials = specials->next;
+ free(special->name);
+ free(special);
+ }
+}
diff --git a/src/specials.h b/src/specials.h
new file mode 100644
index 0000000..2662f27
--- /dev/null
+++ b/src/specials.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2023 Patrick McDermott
+ *
+ * This file is part of opkg-opkg.
+ *
+ * opkg-opkg is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * opkg-opkg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with opkg-opkg. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef OPKG_OPK_SPECIALS_H_
+#define OPKG_OPK_SPECIALS_H_
+
+#include <stdint.h>
+
+struct opkg_opk_specials;
+
+int
+opkg_opk_specials_read(const char *file_name,
+ struct opkg_opk_specials **specials);
+
+int
+opkg_opk_specials_find(struct opkg_opk_specials *specials, const char *find,
+ char *type, uint32_t *devmajor, uint32_t *devminor);
+
+void
+opkg_opk_specials_free(struct opkg_opk_specials *specials);
+
+#endif /* OPKG_OPK_SPECIALS_H_ */