/* requires POSIX */

#include <stdio.h>
#include <regex.h>

main(argc, argv)
     int argc;
     char *argv[];
{
  regex_t regex1, regex2;
  regmatch_t unused_match;
  char errbuf[256];
  int errcode;

  if (argc != 5)
    {
      fprintf(stderr, "Usage: %s regexp1 string1 regexp2 string2\n",
	      argv[0]);
      exit(1);
    }

  /* EXAMPLE USAGE OF REGCOMP() */
  errcode=regcomp(&regex1, argv[1], REG_NOSUB|REG_NEWLINE);

  if (errcode)
    {
      /* EXAMPLE USAGE OF REGERROR() */
      regerror(errcode, &regex1, errbuf, 256);
      fprintf(stderr, "%s: regcomp(\"%s\") - %s\n",
	      argv[0], argv[1], errbuf);
    }

  if ((errcode=regcomp(&regex2, argv[3], REG_NOSUB|REG_NEWLINE)) != 0)
    {
      regerror(errcode, &regex2, errbuf, 256);
      fprintf(stderr, "%s: regcomp(\"%s\") - %s\n",
	      argv[0], argv[3], errbuf);
    }

  /* EXAMPLE USAGE OF REGEXEC() */
  errcode = regexec(&regex1, argv[2], 0, &unused_match, 0);

  if (!errcode) strcpy(errbuf, "does match");
  else regerror(errcode, &regex1, errbuf, 256);
  printf("%s: regexec(\"%s\", \"%s\") - %s\n",
	 argv[0], argv[1], argv[2], errbuf);

  errcode = regexec(&regex2, argv[4], 0, &unused_match, 0);

  if (!errcode) strcpy(errbuf, "does match");
  else regerror(errcode, &regex2, errbuf, 256);
  printf("%s: regexec(\"%s\", \"%s\") - %s\n",
	 argv[0], argv[3], argv[4], errbuf);

  exit(0);
}
