changeset 160:4118886d83ef

FILE* is now fd
author mmj
date Thu, 03 Jun 2004 20:45:33 +1000
parents 8a8bb6d5567b
children 4b17bdd59e54
files src/gethdrline.c
diffstat 1 files changed, 17 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/gethdrline.c	Thu Jun 03 16:36:20 2004 +1000
+++ b/src/gethdrline.c	Thu Jun 03 20:45:33 2004 +1000
@@ -6,31 +6,35 @@
  * Public License as described at http://www.gnu.org/licenses/gpl.txt
  */
 
-#include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include "mygetline.h"
 #include "gethdrline.h"
 #include "strgen.h"
 
-char *gethdrline(FILE *infile)
+char *gethdrline(int fd)
 {
 	char *line = NULL, *retstr = NULL, *nextline = NULL, *tmp = NULL;
-	int ch;
+	char ch;
 	
 	for(;;) {
-		line = myfgetline(infile);
+		line = mygetline(fd);
 		if(line == NULL)
 			return NULL;
-		ch = getc(infile); ungetc(ch, infile);
+		if(read(fd, &ch, 1) == (size_t)1)
+			lseek(fd, -1, SEEK_CUR);
 		if(ch == '\t' || ch == ' ') {
-			nextline = myfgetline(infile);
+			nextline = mygetline(fd);
 			tmp = retstr;
 			retstr = concatstr(3, retstr, line, nextline);
-			free(tmp); free(line); free(nextline);
+			free(tmp);
+			free(line);
+			free(nextline);
 			tmp = line = nextline = NULL;
-			ch = getc(infile); ungetc(ch, infile);
-			if(!(ch == '\t' || ch == ' '))
+			if(read(fd, &ch, 1) == (size_t)1)
+				lseek(fd, -1, SEEK_CUR);
+			if(ch != '\t' && ch != ' ')
 				return retstr;
 		} else {
 			tmp = retstr;
@@ -42,12 +46,14 @@
 	}
 }
 #if 0
+#include <stdio.h>
+
 int main(int argc, char **argv)
 {
 	char *str;
 
-	while((str = gethdrline(stdin))) {
-		printf("%s", str);
+	while((str = gethdrline(fileno(stdin)))) {
+		printf("[%s]", str);
 		free(str);
 	}