summaryrefslogtreecommitdiffstats
path: root/src/specials.c
blob: 8a9fac3d4c5ddafbed7107120aa6fa8c1be4fc87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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);
	}
}