All pastes #285767 Raw Edit

mmoreno80

public text v1 · immutable
#285767 ·published 2006-12-20 00:59 UTC
rendered paste body
#include <regex.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

int
main (int argc, char **argv)
{
  int i, j;
  int status;
  regex_t regex;
  regmatch_t *regmatch;
  status = regcomp (&regex, ".*((\\.[0-9])?)\\.tar\\.bz2", REG_EXTENDED);
  assert (status == EXIT_SUCCESS);
  regmatch = calloc (regex.re_nsub, sizeof (regmatch_t));
  for (j = 1; argv[j]; j++)
    if (regexec (&regex, argv[j], regex.re_nsub, regmatch, 0) == EXIT_SUCCESS)
      for (i = 0; i < regex.re_nsub; i++)
        {
          int start = regmatch[i].rm_so;
          int end = regmatch[i].rm_eo;
          if (start >= 0 && end >= 0)
            {
              char *str = argv[j];
              int size = end - start;
              char *cp = calloc (size + 1, sizeof (char));
              strncpy (cp, &str[start], size);
              cp[size] = 0;
              printf ("%i: %s (%i)\n", j, cp, size);
              free (cp);
            }
        }
  free (regmatch);
  regfree (&regex);
  return 0;
}