/* SPDX-License-Identifier: LGPL-2.1+ */
#include <ctype.h>
+#include <stdio.h>
#include "alloc-util.h"
#include "conf-files.h"
#include "strbuf.h"
#include "string-util.h"
#include "strv.h"
+#include "tmpfile-util.h"
static const char *default_hwdb_bin_dir = "/etc/udev";
static const char * const conf_file_dirs[] = {
static int trie_node_add_value(struct trie *trie, struct trie_node *node,
const char *key, const char *value,
const char *filename, uint16_t file_priority, uint32_t line_number, bool compat) {
- ssize_t k, v, fn;
+ ssize_t k, v, fn = 0;
struct trie_value_entry *val;
k = strbuf_add_string(trie->strings, key, strlen(key));
return -ENOMEM;
trie->values_count++;
node->values = val;
- node->values[node->values_count].key_off = k;
- node->values[node->values_count].value_off = v;
- node->values[node->values_count].filename_off = fn;
- node->values[node->values_count].file_priority = file_priority;
- node->values[node->values_count].line_number = line_number;
+ node->values[node->values_count] = (struct trie_value_entry) {
+ .key_off = k,
+ .value_off = v,
+ .filename_off = fn,
+ .file_priority = file_priority,
+ .line_number = line_number,
+ };
node->values_count++;
typesafe_qsort_r(node->values, node->values_count, trie_values_cmp, trie);
return 0;
continue;
/* split node */
- new_child = new0(struct trie_node, 1);
+ new_child = new(struct trie_node, 1);
if (!new_child)
return -ENOMEM;
/* move values from parent to child */
- new_child->prefix_off = node->prefix_off + p+1;
- new_child->children = node->children;
- new_child->children_count = node->children_count;
- new_child->values = node->values;
- new_child->values_count = node->values_count;
+ *new_child = (struct trie_node) {
+ .prefix_off = node->prefix_off + p+1,
+ .children = node->children,
+ .children_count = node->children_count,
+ .values = node->values,
+ .values_count = node->values_count,
+ };
/* update parent; use strdup() because the source gets realloc()d */
s = strndup(trie->strings->buf + node->prefix_off, p);
if (off < 0)
return off;
- node->prefix_off = off;
- node->children = NULL;
- node->children_count = 0;
- node->values = NULL;
- node->values_count = 0;
+ *node = (struct trie_node) {
+ .prefix_off = off,
+ };
r = node_add_child(trie, node, new_child, c);
if (r < 0)
return r;
ssize_t off;
/* new child */
- new_child = new0(struct trie_node, 1);
+ new_child = new(struct trie_node, 1);
if (!new_child)
return -ENOMEM;
if (off < 0)
return off;
- new_child->prefix_off = off;
+ *new_child = (struct trie_node) {
+ .prefix_off = off,
+ };
+
r = node_add_child(trie, node, new_child, c);
if (r < 0)
return r;
HW_DATA,
} state = HW_NONE;
_cleanup_fclose_ FILE *f = NULL;
- char line[LINE_MAX];
_cleanup_strv_free_ char **match_list = NULL;
uint32_t line_number = 0;
char *match = NULL;
if (!f)
return -errno;
- while (fgets(line, sizeof(line), f)) {
+ for (;;) {
+ _cleanup_free_ char *line = NULL;
size_t len;
char *pos;
+ r = read_line(f, LONG_LINE_MAX, &line);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ break;
+
++line_number;
/* comment line */
return r;
}
+
+int hwdb_query(const char *modalias) {
+ _cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL;
+ const char *key, *value;
+ int r;
+
+ assert(modalias);
+
+ r = sd_hwdb_new(&hwdb);
+ if (r < 0)
+ return r;
+
+ SD_HWDB_FOREACH_PROPERTY(hwdb, modalias, key, value)
+ printf("%s=%s\n", key, value);
+
+ return 0;
+}