Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Unix Operating Systems > OpenBSD > lucky.openbsd.tech

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-22-2008, 12:39 PM
Steffen Wendzel
 
Posts: n/a
Default this time: a working patch

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;
+}
+

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 04:44 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145