2 SPDX-License-Identifier: GPL-2.0-only
4 Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
16 void *zalloc(size_t size
)
18 return calloc(1, size
);
21 void __zfree(void **ptr
)
27 struct str_node
*str_node__new(const char *s
, bool dupstr
)
29 struct str_node
*snode
= malloc(sizeof(*snode
));
47 static void str_node__delete(struct str_node
*snode
, bool dupstr
)
57 int __strlist__add(struct strlist
*slist
, const char *new_entry
, void *priv
)
59 struct rb_node
**p
= &slist
->entries
.rb_node
;
60 struct rb_node
*parent
= NULL
;
67 sn
= rb_entry(parent
, struct str_node
, rb_node
);
68 rc
= strcmp(sn
->s
, new_entry
);
78 sn
= str_node__new(new_entry
, slist
->dupstr
);
82 rb_link_node(&sn
->rb_node
, parent
, p
);
83 rb_insert_color(&sn
->rb_node
, &slist
->entries
);
87 list_add_tail(&sn
->node
, &slist
->list_entries
);
92 int strlist__add(struct strlist
*slist
, const char *new_entry
)
94 return __strlist__add(slist
, new_entry
, NULL
);
97 int strlist__load(struct strlist
*slist
, const char *filename
)
101 FILE *fp
= fopen(filename
, "r");
106 while (fgets(entry
, sizeof(entry
), fp
) != NULL
) {
107 const size_t len
= strlen(entry
);
111 entry
[len
- 1] = '\0';
113 if (strlist__add(slist
, entry
) != 0)
123 struct strlist
*strlist__new(bool dupstr
)
125 struct strlist
*slist
= malloc(sizeof(*slist
));
128 slist
->entries
= RB_ROOT
;
129 INIT_LIST_HEAD(&slist
->list_entries
);
130 slist
->dupstr
= dupstr
;
136 void strlist__delete(struct strlist
*slist
)
139 struct str_node
*pos
;
140 struct rb_node
*next
= rb_first(&slist
->entries
);
143 pos
= rb_entry(next
, struct str_node
, rb_node
);
144 next
= rb_next(&pos
->rb_node
);
145 strlist__remove(slist
, pos
);
147 slist
->entries
= RB_ROOT
;
152 void strlist__remove(struct strlist
*slist
, struct str_node
*sn
)
154 rb_erase(&sn
->rb_node
, &slist
->entries
);
155 list_del_init(&sn
->node
);
156 str_node__delete(sn
, slist
->dupstr
);
159 bool strlist__has_entry(struct strlist
*slist
, const char *entry
)
161 struct rb_node
**p
= &slist
->entries
.rb_node
;
162 struct rb_node
*parent
= NULL
;
169 sn
= rb_entry(parent
, struct str_node
, rb_node
);
170 rc
= strcmp(sn
->s
, entry
);
183 Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Shdr
*shp
, const char *name
, size_t *index
)
189 if (elf_getshdrstrndx(elf
, &str_idx
))
192 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
195 gelf_getshdr(sec
, shp
);
196 str
= elf_strptr(elf
, str_idx
, shp
->sh_name
);
199 if (!strcmp(name
, str
)) {
210 Elf_Scn
*elf_section_by_idx(Elf
*elf
, GElf_Shdr
*shp
, int idx
, const char **name_out
)
215 sec
= elf_getscn(elf
, idx
);
218 if (!gelf_getshdr(sec
, shp
))
221 if (elf_getshdrstrndx(elf
, &str_idx
))
223 *name_out
= elf_strptr(elf
, str_idx
, shp
->sh_name
);
228 char *strlwr(char *s
)
230 int len
= strlen(s
), i
;
232 for (i
= 0; i
< len
; ++i
)
233 s
[i
] = tolower(s
[i
]);