Commits

Tony Cesaro committed 3fe6376

Added some currently broken code, trying to emulate tail -f behavior...

  • Participants
  • Parent commits 0f7ceb2

Comments (0)

Files changed (1)

 #define CHUNK 1024
 
 void
-readPrint (FILE *ifp, size_t nread, char *readbuffer)
+readPrint (FILE *inputFile, size_t nread, char *readBuffer)
 {
-	if (ifp)
-	{
-		while ((nread = fread(readbuffer, 1, CHUNK, ifp)) > 0)
-		{
-			fwrite(readbuffer, 1, nread, stdout);
-		}
-		
-		fclose(ifp);
-	}
+    if (inputFile)
+    {
+        int curOffset = ftell(inputFile);
+
+        while (1) {
+            int fSeekResult = fseek(inputFile, curOffset, 0);
+
+            if (fSeekResult == NULL)
+            {
+                fprintf(stderr, "File seek error.\n");
+                exit(1);
+            }
+
+            while ((nread = fread(readBuffer, 1, CHUNK, inputFile)) > 0)
+            {
+                fwrite(readBuffer, 1, nread, stdout);
+                curOffset = ftell(inputFile);
+                printf("%d\n", curOffset);
+            }
+        }
+        fclose(inputFile);
+    }
 }
 
 void
 
 int
 main (int argc, char *argv[])
-{	
+{   
 
-	char *filename = NULL;
-	char *pattern = NULL;
-	char *readbuffer = malloc(CHUNK);
-	size_t nread;
+    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;
-	} 
+    if (argc - 1 < 1) 
+    {
+        fprintf(stderr, "Usage: %s <filename> <pattern>\n", argv[0]);
+        return 1;
+    } 
 
 
-	if (argc > 1) 
+    if (argc - 1 >= 1) 
     {
-	    while (--argc > 0)
-	    {
-	    	filename = argv[1];
-	    	pattern = argv[2];
-	    }
-	    ifp = fopen(filename, "r");
+        while (--argc > 0)
+        {
+            filename = argv[1];
+            pattern = argv[2];
+        }
+        ifp = fopen(filename, "r");
     }
     else
     {
         filename = "stdin";
-	    ifp = stdin;
+        ifp = stdin;
     }
 
-	if (ifp == NULL)
-	{
-		fprintf(stderr, "File open error.\n");
-		exit(1);
-	}
-
-	if (readbuffer == NULL)
-	{
-		fprintf(stderr, "Could not allocate read buffer.\n");
-		exit(1);
-	}
-
-	printf ("File being concatenated: %s\n", filename);
-	printf ("Pattern used for filter: %s\n\n", pattern);
-    readPrint (ifp, nread, readbuffer);
-
-	free (readbuffer);
-	
-	return 0;
+    if (ifp == NULL)
+    {
+        fprintf(stderr, "File open error.\n");
+        exit(1);
+    }
+
+    if (readBuffer == NULL)
+    {
+        fprintf(stderr, "Could not allocate read buffer.\n");
+        exit(1);
+    }
+
+    printf ("File being concatenated: %s\n", filename);
+    printf ("Pattern used for filter: %s\n\n", pattern);
+    readPrint (ifp, nread, readBuffer);
+
+    free (readBuffer);
+    
+    return 0;
 }