From 089583df538a11302e504659e7652389fdfa9aeb Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Mon, 18 Apr 2005 12:19:26 +0000 Subject: Use mmap() for reading the OUI company name --- tools/oui.c | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/tools/oui.c b/tools/oui.c index 7dff358b..985e6e20 100644 --- a/tools/oui.c +++ b/tools/oui.c @@ -32,9 +32,12 @@ #include #include +#include +#include #include #include #include +#include #include "oui.h" @@ -42,44 +45,50 @@ #define OUIFILE "/usr/share/misc/oui.txt" -#define AWKCMD "/usr/bin/awk" -#define TRCMD "/usr/bin/tr" - char *ouitocomp(const char *oui) { struct stat st; - FILE *input; - char cmd[512]; - char *str; - size_t len; + char *str, *map, *off, *end; + int fd; - if (stat(OUIFILE, &st) < 0) - return NULL; - if (stat(AWKCMD, &st) < 0) + fd = open(OUIFILE, O_RDONLY); + if (fd < 0) return NULL; - if (stat(TRCMD, &st) < 0) + if (fstat(fd, &st) < 0) { + close(fd); return NULL; + } str = malloc(128); - if (!str) + if (!str) { + close(fd); return NULL; + } memset(str, 0, 128); - snprintf(cmd, sizeof(cmd) - 1, "%s -F'\\t' '/^" - "%s.*\\(hex\\).*/{ print $3 }' %s" - " | %s -d '\\n\\r'", AWKCMD, oui, OUIFILE, TRCMD); - - input = popen(cmd, "r"); - if (!input) { + map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); + if (map == MAP_FAILED) { free(str); + close(fd); return NULL; } - len = fread(str, 127, 1, input); - pclose(input); + off = strstr(map, oui); + if (off) { + off += 18; + end = strpbrk(off, "\r\n"); + strncpy(str, off, end - off); + } else { + free(str); + str = NULL; + } + + munmap(map, st.st_size); + + close(fd); return str; } -- cgit