#include #include int main(char **argv, int argc) { char buffer[1024]; char *ptr; char *see; char *startofemail; char *endofemail; char *startofcomment; char *endofcomment; int emaillen; int commentlen; while(fgets(buffer, 1024, stdin)) { ptr = buffer; if(!strncasecmp("From: ", ptr, 6)) { ptr+= 6; while(ptr && *ptr && isspace(*ptr)) ptr++; printf("** IN: %s", ptr); /* */ /* (comment) */ endofemail = NULL; startofemail=strchr(ptr, '<'); if(startofemail) { endofemail=strchr(++startofemail, '>'); emaillen = endofemail-startofemail; printf("FOUND <>\n"); } endofcomment = NULL; if(!endofemail) { startofcomment=strchr(ptr, '('); if(startofcomment) { endofcomment=strchr(++startofcomment, ')'); commentlen=endofcomment-startofcomment; printf("FOUND ()\n"); } } /******** We found neither an email field nor comment ******/ if(!endofemail && !endofcomment) { char at=0; /* We make an attempt to get an email address from the string! */ startofemail = ptr; see = ptr; while(*see) { if('@' == *see) { at = 1; } else if(isspace(*see)) { if(at) { break; } if('\n' == *see) break; at = 0; startofemail = see+1; } see++; } if(at) { endofemail = see; emaillen = endofemail-startofemail; printf("USE word with @!\n"); } else { /* No, then the whole line is the email address! */ endofemail = see; emaillen = endofemail-startofemail; printf("USE last word without space!\n"); } } /******** We got an email field but no comment ******/ if(!endofcomment && endofemail) { if(startofemail>ptr) { /* we set the text before the email address to be a comment */ startofcomment = ptr; endofcomment = startofemail; printf("USE comment BEFORE email\n"); } else { /* we set the text after the email address to be a comment */ startofcomment = endofemail+1; endofcomment = strchr(endofemail, '\n'); printf("USE comment AFTER email\n"); } commentlen=endofcomment-startofcomment-1; } /******** We got a comment field but no email ******/ if(endofcomment && !endofemail) { if(startofcomment>ptr) { /* we set the text before the comment to be the email */ startofemail = ptr; endofemail = startofcomment; printf("USE email BEFORE comment\n"); } else { /* we set the text after the comment to be the email */ startofemail = endofcomment+1; endofemail = strchr(endofcomment, '\n'); printf("USE email AFTER comment\n"); } emaillen=endofemail-startofemail-1; } if(endofemail) { /* this may be an email address */ printf("EMAIL: %.*s\n", emaillen, startofemail); } if(endofcomment) { /* this may be an email address */ printf("COMMENT: %.*s\n", commentlen, startofcomment); } if(!endofemail && !endofcomment) printf("-- FAILED: %s", ptr); } } return 0; }