changeset 27:91830455b49c

mygetline is turned into myfgetline since it works on FILE *'s and mygetline(int fd) was added.
author mmj
date Fri, 23 Apr 2004 07:14:22 +1000
parents 035484b0da60
children b7b8efd520ec
files src/do_all_the_voodo_here.c src/gethdrline.c src/mlmmj-recieve.c src/mlmmj-unsubscribe.c src/mygetline.c src/subscriberfuncs.c
diffstat 6 files changed, 38 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/do_all_the_voodo_here.c	Fri Apr 23 03:41:20 2004 +1000
+++ b/src/do_all_the_voodo_here.c	Fri Apr 23 07:14:22 2004 +1000
@@ -63,7 +63,7 @@
 		if((strlen(hdrline) == 1) && (hdrline[0] == '\n')){
 			if(hdradd) {
 				fflush(out);
-				while((line = mygetline(hdradd))) {
+				while((line = myfgetline(hdradd))) {
 					fputs(line, out);
 					free(line);
 				}
@@ -87,7 +87,7 @@
 	}
 
 	/* Just print the rest of the mail */
-	while((line = mygetline(in))) {
+	while((line = myfgetline(in))) {
 		fputs(line, out);
 		free(line);
 	}
@@ -96,7 +96,7 @@
 
 	/* No more, lets add the footer if one*/
 	if(footers) {
-		while((line = mygetline(footers))) {
+		while((line = myfgetline(footers))) {
 			fputs(line, out);
 			free(line);
 		}
--- a/src/gethdrline.c	Fri Apr 23 03:41:20 2004 +1000
+++ b/src/gethdrline.c	Fri Apr 23 07:14:22 2004 +1000
@@ -19,12 +19,12 @@
 	int ch;
 	
 	for(;;) {
-		line = mygetline(infile);
+		line = myfgetline(infile);
 		if(line == NULL)
 			return NULL;
 		ch = getc(infile); ungetc(ch, infile);
 		if(ch == '\t' || ch == ' ') {
-			nextline = mygetline(infile);
+			nextline = myfgetline(infile);
 			tmp = retstr;
 			retstr = concatstr(3, retstr, line, nextline);
 			free(tmp); free(line); free(nextline);
--- a/src/mlmmj-recieve.c	Fri Apr 23 03:41:20 2004 +1000
+++ b/src/mlmmj-recieve.c	Fri Apr 23 07:14:22 2004 +1000
@@ -74,7 +74,7 @@
 		exit(EXIT_FAILURE);
 	}
 	
-	while((line = mygetline(stdin))) {
+	while((line = myfgetline(stdin))) {
 		writen(fd, line, strlen(line));
 		fsync(fd);
 		free(line);
--- a/src/mlmmj-unsubscribe.c	Fri Apr 23 03:41:20 2004 +1000
+++ b/src/mlmmj-unsubscribe.c	Fri Apr 23 07:14:22 2004 +1000
@@ -244,7 +244,7 @@
 		exit(EXIT_FAILURE);
 	}
 
-	while((buf = mygetline(subfile))) {
+	while((buf = myfgetline(subfile))) {
 		if(strncasecmp(buf, address, strlen(address)) != 0)
 			writen(subwritefd, buf, strlen(buf));
 		free(buf);
--- a/src/mygetline.c	Fri Apr 23 03:41:20 2004 +1000
+++ b/src/mygetline.c	Fri Apr 23 07:14:22 2004 +1000
@@ -1,4 +1,5 @@
 /* Copyright (C) 2004 Morten K. Poulsen <morten at afdelingp.dk>
+ * Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj.dk>
  *
  * $Id$
  *
@@ -9,9 +10,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include "mygetline.h"
 
-char *mygetline(FILE *infile)
+char *myfgetline(FILE *infile)
 {
 	size_t buf_size = BUFSIZE;  /* initial buffer size */
 	size_t buf_used;
@@ -39,12 +41,38 @@
 
 	}
 }
+char *mygetline(int fd)
+{
+	size_t i = 0, buf_size = BUFSIZE;  /* initial buffer size */
+	char *buf = malloc(buf_size);
+	int ch;
+
+	buf[0] = '\0';
+	while(read(fd, &ch, 1) > 0) {	
+		if(i == buf_size - 1) {
+			buf_size *= 2;
+			buf = realloc(buf, buf_size);
+		}
+		buf[i++] = ch;
+		if(ch == '\n') {
+			buf[i] = '\0';
+			return buf;
+		}
+	}
+
+	if(buf[0]) {
+		buf[i] = '\0';
+		return buf;
+	}
+
+	return NULL;
+}
 #if 0
 int main(int argc, char **argv)
 {
 	char *str;
 	
-	while((str = mygetline(stdin))) {
+	while((str = mygetline(fileno(stdin)))) {
 		printf("%s", str);
 		free(str);
 	}
--- a/src/subscriberfuncs.c	Fri Apr 23 03:41:20 2004 +1000
+++ b/src/subscriberfuncs.c	Fri Apr 23 07:14:22 2004 +1000
@@ -26,7 +26,7 @@
 		exit(EXIT_FAILURE);
 	}
 	
-	while ((buf = mygetline(subfile))) {
+	while ((buf = myfgetline(subfile))) {
 		while (buf[0] && isspace(buf[strlen(buf)-1]))
 			buf[strlen(buf)-1] = '\0';
 		if (strcasecmp(buf, address) == 0) {