tailgrep / tg.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pcre.h>

//BEGIN CPP

#define CHUNK 1024

//END CPP

// BEGIN Function prototypes

int regexMatch (char *line, const char *regexPattern);

// END Function prototypes

// BEGIN Functions

void
readPrint (FILE *inputFile, char *readBuffer)
{
  if (inputFile)
  {
    unsigned long curOffset = ftell(inputFile);
    size_t nread;

    while (1)
    {
      while ((nread = fread(readBuffer, 1, CHUNK, inputFile)) > 0)
      {
        fwrite(readBuffer, 1, nread, stdout);
        curOffset = ftell(inputFile);
        int regexMatches = regexMatch(readBuffer,"*");
        printf("Matches = %d", regexMatches);
      }
      sleep(1);
    }
    fclose(inputFile);
  }
}

int
regexMatch (char *line, const char *regexPattern)
{
  const char *error;
  int errorOffset;
  pcre *reCompiled;

  reCompiled = pcre_compile(
          regexPattern,
          0,
          &error,
          &errorOffset,
          NULL);

  if (reCompiled == NULL)
  {
    printf("pcre_compile failed (offset: %d), %s\n", errorOffset, error);
    return -1;
  }

  unsigned int lineLength = (int)strlen(line);
  int ovector[1];
  int regexMatchResult = pcre_exec(
              reCompiled,
              NULL,
              line,
              lineLength,
              0,
              0,
              ovector,
              1);
  
  if (regexMatchResult == NULL)
  {
    printf("pcre_exec failed");
    return -1;
  }
  else
  {
    return regexMatchResult;
  }

}

// END functions

// BEGIN main
int
main (int argc, char *argv[])
{   

  char *filename = NULL;
  char *pattern = NULL;
  char *readBuffer = malloc(CHUNK);
  FILE *ifp;

  if (argc - 1 < 1) 
  {
    fprintf(stderr, "Usage: %s <filename> <pattern>\n", argv[0]);
    return 1;
  } 


  if (argc - 1 >= 1) 
  {
    while (--argc > 0)
    {
      filename = argv[1];
      pattern = argv[2];
    }
    ifp = fopen(filename, "r");
  }
  else
  {
    filename = "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, readBuffer);

  free (readBuffer);
  
  return 0;
}
// END main
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.