diff options
| author | Marcel Holtmann <marcel@holtmann.org> | 2005-04-18 12:19:26 +0000 | 
|---|---|---|
| committer | Marcel Holtmann <marcel@holtmann.org> | 2005-04-18 12:19:26 +0000 | 
| commit | 089583df538a11302e504659e7652389fdfa9aeb (patch) | |
| tree | 8c10cbab0c2995ed28ce123c3886b3555e96ed00 | |
| parent | c3881e06e2262909d8e1c190622002c5e02d724f (diff) | |
Use mmap() for reading the OUI company name
| -rw-r--r-- | tools/oui.c | 49 | 
1 files 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 <stdio.h>  #include <errno.h> +#include <fcntl.h> +#include <unistd.h>  #include <malloc.h>  #include <string.h>  #include <sys/stat.h> +#include <sys/mman.h>  #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;  }  | 
