lib names: Add helper func for parse id and name from file
Signed-off-by: Vadim Kochan <vadim4j@gmail.com>
This commit is contained in:
parent
4e5615b34c
commit
f00073e8b9
|
|
@ -27,43 +27,63 @@
|
||||||
#define CONFDIR "/etc/iproute2"
|
#define CONFDIR "/etc/iproute2"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define NAME_MAX_LEN 512
|
||||||
|
|
||||||
struct rtnl_hash_entry {
|
struct rtnl_hash_entry {
|
||||||
struct rtnl_hash_entry *next;
|
struct rtnl_hash_entry *next;
|
||||||
const char * name;
|
const char * name;
|
||||||
unsigned int id;
|
unsigned int id;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static int fread_id_name(FILE *fp, int *id, char *namebuf)
|
||||||
|
{
|
||||||
|
char buf[NAME_MAX_LEN];
|
||||||
|
|
||||||
|
while (fgets(buf, sizeof(buf), fp)) {
|
||||||
|
char *p = buf;
|
||||||
|
|
||||||
|
while (*p == ' ' || *p == '\t')
|
||||||
|
p++;
|
||||||
|
|
||||||
|
if (*p == '#' || *p == '\n' || *p == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (sscanf(p, "0x%x %s\n", id, namebuf) != 2 &&
|
||||||
|
sscanf(p, "0x%x %s #", id, namebuf) != 2 &&
|
||||||
|
sscanf(p, "%d %s\n", id, namebuf) != 2 &&
|
||||||
|
sscanf(p, "%d %s #", id, namebuf) != 2) {
|
||||||
|
strcpy(namebuf, p);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
rtnl_hash_initialize(const char *file, struct rtnl_hash_entry **hash, int size)
|
rtnl_hash_initialize(const char *file, struct rtnl_hash_entry **hash, int size)
|
||||||
{
|
{
|
||||||
struct rtnl_hash_entry *entry;
|
struct rtnl_hash_entry *entry;
|
||||||
char buf[512];
|
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
int id;
|
||||||
|
char namebuf[NAME_MAX_LEN] = {0};
|
||||||
|
int ret;
|
||||||
|
|
||||||
fp = fopen(file, "r");
|
fp = fopen(file, "r");
|
||||||
if (!fp)
|
if (!fp)
|
||||||
return;
|
return;
|
||||||
while (fgets(buf, sizeof(buf), fp)) {
|
|
||||||
char *p = buf;
|
|
||||||
int id;
|
|
||||||
char namebuf[512];
|
|
||||||
|
|
||||||
while (*p == ' ' || *p == '\t')
|
while ((ret = fread_id_name(fp, &id, &namebuf[0]))) {
|
||||||
p++;
|
if (ret == -1) {
|
||||||
if (*p == '#' || *p == '\n' || *p == 0)
|
|
||||||
continue;
|
|
||||||
if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
|
|
||||||
sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
|
|
||||||
sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
|
|
||||||
sscanf(p, "%d %s #", &id, namebuf) != 2) {
|
|
||||||
fprintf(stderr, "Database %s is corrupted at %s\n",
|
fprintf(stderr, "Database %s is corrupted at %s\n",
|
||||||
file, p);
|
file, namebuf);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id<0)
|
if (id<0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
entry = malloc(sizeof(*entry));
|
entry = malloc(sizeof(*entry));
|
||||||
entry->id = id;
|
entry->id = id;
|
||||||
entry->name = strdup(namebuf);
|
entry->name = strdup(namebuf);
|
||||||
|
|
@ -75,31 +95,22 @@ rtnl_hash_initialize(const char *file, struct rtnl_hash_entry **hash, int size)
|
||||||
|
|
||||||
static void rtnl_tab_initialize(const char *file, char **tab, int size)
|
static void rtnl_tab_initialize(const char *file, char **tab, int size)
|
||||||
{
|
{
|
||||||
char buf[512];
|
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
int id;
|
||||||
|
char namebuf[NAME_MAX_LEN] = {0};
|
||||||
|
int ret;
|
||||||
|
|
||||||
fp = fopen(file, "r");
|
fp = fopen(file, "r");
|
||||||
if (!fp)
|
if (!fp)
|
||||||
return;
|
return;
|
||||||
while (fgets(buf, sizeof(buf), fp)) {
|
|
||||||
char *p = buf;
|
|
||||||
int id;
|
|
||||||
char namebuf[512];
|
|
||||||
|
|
||||||
while (*p == ' ' || *p == '\t')
|
while ((ret = fread_id_name(fp, &id, &namebuf[0]))) {
|
||||||
p++;
|
if (ret == -1) {
|
||||||
if (*p == '#' || *p == '\n' || *p == 0)
|
|
||||||
continue;
|
|
||||||
if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
|
|
||||||
sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
|
|
||||||
sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
|
|
||||||
sscanf(p, "%d %s #", &id, namebuf) != 2) {
|
|
||||||
fprintf(stderr, "Database %s is corrupted at %s\n",
|
fprintf(stderr, "Database %s is corrupted at %s\n",
|
||||||
file, p);
|
file, namebuf);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id<0 || id>size)
|
if (id<0 || id>size)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
@ -185,8 +196,7 @@ int rtnl_rtprot_a2n(__u32 *id, const char *arg)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char * rtnl_rtscope_tab[256] = {
|
||||||
static char * rtnl_rtscope_tab[256] = {
|
|
||||||
"global",
|
"global",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue