Commits

Tony Cesaro committed 0f7ceb2

Moved concatenate portion into a function, working on framework for exclude/include functionality

Comments (0)

Files changed (2)

+CFLAGS=-Wall -g
+
+all: tg
+
+clean:
+	rm -f tg
 
 #define CHUNK 1024
 
-int main (int argc, char *argv[])
+void
+readPrint (FILE *ifp, size_t nread, char *readbuffer)
+{
+	if (ifp)
+	{
+		while ((nread = fread(readbuffer, 1, CHUNK, ifp)) > 0)
+		{
+			fwrite(readbuffer, 1, nread, stdout);
+		}
+		
+		fclose(ifp);
+	}
+}
+
+void
+grepInclude ()
+{
+
+}
+
+void
+grepExclude ()
+{
+
+}
+
+int
+main (int argc, char *argv[])
 {	
-	if (argc < 2 || argc > 3)
+
+	char *filename = NULL;
+	char *pattern = NULL;
+	char *readbuffer = malloc(CHUNK);
+	size_t nread;
+    FILE *ifp;
+
+	if (argc < 1 && stdin == NULL) 
 	{
 		fprintf(stderr, "Usage: %s <filename> <pattern>\n", argv[0]);
 		return 1;
 	} 
 
-	char* filename = NULL;
-	char* pattern = NULL;
 
-	while (--argc > 0)
-	{
-		filename = argv[1];
-		pattern = argv[2];
-	}
-
-	printf ("File being concatenated: %s\n", filename);
-	printf ("Pattern used for filter: %s\n\n", pattern);
-
-	FILE *ifp;
-	size_t nread;
-	
-	ifp = fopen(filename, "r");
+	if (argc > 1) 
+    {
+	    while (--argc > 0)
+	    {
+	    	filename = argv[1];
+	    	pattern = argv[2];
+	    }
+	    ifp = fopen(filename, "r");
+    }
+    else
+    {
+        filename = "stdin";
+	    ifp = stdin;
+    }
 
 	if (ifp == NULL)
 	{
 		exit(1);
 	}
 
-	char *readbuffer = malloc(CHUNK);
-
 	if (readbuffer == NULL)
 	{
 		fprintf(stderr, "Could not allocate read buffer.\n");
+		exit(1);
 	}
 
-	if (ifp)
-	{
-		while ((nread = fread(readbuffer, 1, CHUNK, ifp)) > 0)
-		{
-			fwrite(readbuffer, 1, nread, stdout);
-		}
-		
-		fclose(ifp);
-	}
+	printf ("File being concatenated: %s\n", filename);
+	printf ("Pattern used for filter: %s\n\n", pattern);
+    readPrint (ifp, nread, readbuffer);
 
-	free(readbuffer);
+	free (readbuffer);
 	
 	return 0;
 }