vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Okay, here is a new patch that not depends on lreadline and that works fine. That is why no Makefile-change is needed in the new patch. I also removed the #ifdef stuff for buf[BUFSIZ]. the new read_line function reads a whole line in 1024-blocks. this patch also removes the unneded '?' case in the getopt loop. I checked the code with lint, pscan, flawfinder, splint and gcc -Wall (and the program itself by hand). --steffen Index: from.c ================================================== ================= RCS file: /cvs/src/usr.bin/from/from.c,v retrieving revision 1.12 diff -u -p -r1.12 from.c --- from.c 14 Mar 2006 19:39:49 -0000 1.12 +++ from.c 16 Aug 2006 14:32:52 -0000 @@ -54,6 +54,7 @@ static char rcsid[] = "$OpenBSD: from.c, #include <err.h> int match(char *, char *); +char * read_line(void); int main(int argc, char *argv[]) @@ -61,11 +62,8 @@ main(int argc, char *argv[]) struct passwd *pwd; int ch, newline; char *file, *sender, *p; -#if MAXPATHLEN > BUFSIZ - char buf[MAXPATHLEN]; -#else char buf[BUFSIZ]; -#endif + char *content; file = sender = NULL; while ((ch = getopt(argc, argv, "f:s:")) != -1) @@ -79,7 +77,6 @@ main(int argc, char *argv[]) if (isupper(*p)) *p = tolower(*p); break; - case '?': default: fprintf(stderr, "usage: from [-f file] [-s sender] [user]\n"); exit(1); @@ -115,14 +112,15 @@ main(int argc, char *argv[]) } if (!freopen(file, "r", stdin)) err(1, "%s", file); - for (newline = 1; fgets(buf, sizeof(buf), stdin) - if (*buf == '\n') { + + for (newline = 1; (content = read_line()) != NULL; free(content)) { + if (*content == '\n') { newline = 1; continue; } - if (newline && !strncmp(buf, "From ", 5) && - (!sender || match(buf + 5, sender))) - printf("%s", buf); + if (newline && !strncmp(content, "From ", 5) && + (!sender || match(content + 5, sender))) + printf("%s", content); newline = 0; } exit(0); @@ -152,3 +150,37 @@ match(char *line, char *sender) } /* NOTREACHED */ } + +/* read_line(): read a whole line and include the '\n'. */ + +char * +read_line(void) +{ + char *ptr, n; + size_t cursiz; + int curuse; + + cursiz = BUFSIZ; + + if (!(ptr = calloc(cursiz, sizeof(char)))) + err(1, "calloc"); + + for (n = '\0', curuse = 0; n != '\n'; ptr[curuse++] = n) { + if ((n = getc(stdin)) == EOF) { + if (curuse == 0) + return NULL; + else + return ptr; + } + + if (curuse >= (cursiz -1)) { + cursiz += BUFSIZ; + if (!(ptr = realloc(ptr, cursiz))) + err(1, "realloc"); + bzero(ptr+curuse+1, BUFSIZ - 1); + } + } + + return ptr; +} + |