#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <regex.h>

char pattern[100];
char string[100];
int match (char *reg_exp, char *str);

main () {
  int  status;
  char *p;
  regex_t   re;



  strcpy(string,"@909.merit.ed-u");
  strcpy(string,"	gerald.winters@merit.edu");

  printf("string-<%s>\n", string);
  if ((p = strchr (string, '@')) == NULL) {
    printf ("no '@' in email...\n");
    exit (0);
  }

  if (strchr (p + 1, '@') != NULL) {
    printf ("too many '@' in email...\n");
    exit (0);
  }

  strcpy(pattern,"^[ ]*[^]()<>,;:\\\". ]+(.[^]()<>,;:\\\". ]+)*@");
  if (match (pattern, string) != 0) {
    printf("something wrong with part before domain...\n");
    exit (0);
  }

  strcpy(pattern,"^@[[:alnum:]]+([.-][[:alnum:]]+)*[ ]*$");
  if (match (pattern, p) != 0) {
    printf("something wrong with domain part...\n");
    exit (0);
  }

  printf("match...\n");

}

/*
 * return 0 for match
 * otherwise return non-zero
 */
int match (char *reg_exp, char *str) {
  int  status;
  regex_t   re;

  if (regcomp(&re, reg_exp, REG_EXTENDED|REG_NOSUB) != 0) {
    printf("couldn't compile...\n");
    return (-1);      /* report error */
  }

  status = regexec(&re, str, (size_t) 0, NULL, 0);
  regfree(&re);

  return (status);
}
