Mercurial > hg > mlmmj
changeset 41:57cc975a4721
mmap the subscriber file
author | mmj |
---|---|
date | Sat, 24 Apr 2004 20:27:16 +1000 |
parents | 6708b06cf9c9 |
children | d318f8048925 |
files | src/subscriberfuncs.c |
diffstat | 1 files changed, 37 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/src/subscriberfuncs.c Sat Apr 24 02:52:57 2004 +1000 +++ b/src/subscriberfuncs.c Sat Apr 24 20:27:16 2004 +1000 @@ -10,28 +10,54 @@ #include <string.h> #include <ctype.h> #include <stdlib.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <unistd.h> #include "mlmmj.h" #include "subscriberfuncs.h" #include "mygetline.h" #include "log_error.h" -int find_subscriber(int subfilefd, const char *address) +int find_subscriber(int fd, const char *address) { - char *buf; + char *start, *cur, *next; + struct stat st; + size_t len; - while ((buf = mygetline(subfilefd))) { - while (buf[0] && isspace(buf[strlen(buf)-1])) - buf[strlen(buf)-1] = '\0'; - if (strcasecmp(buf, address) == 0) { - free(buf); - return 0; - } - free(buf); + if(fstat(fd, &st) < 0) { + log_error(LOG_ARGS, "Could not stat fd"); + return 1; } - free(buf); - + if((start = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == + (void *)-1) { + log_error(LOG_ARGS, "Could not mmap fd"); return 1; } + for(next = cur = start; next < start + st.st_size; next++) { + if(*next == '\n') { + len = next - cur; + if((strlen(address) == len) && + (strncasecmp(address, cur, len) == 0)) { + munmap(start, st.st_size); + return 0; + } + cur = next + 1; + } + } + + if(next > cur) { + len = next - cur; + if((strlen(address) == len) && + (strncasecmp(address, cur, len) == 0)) { + munmap(start, st.st_size); + return 0; + } + } + + munmap(start, st.st_size); + return 1; +}