#include #include struct /* struct to hold CGI name/value pairs */ { char name[128]; char val[128]; } elements[16]; struct phonebook_entry /* struct of each phone entry */ { char name[41]; /* HTML form variable size + 1 */ char phone[11]; /* HTML form variable size + 1 */ } entry; extern int errno; /***************************************************/ /* Function to add an entry to the phonebook */ /***************************************************/ void phonebook_add() { FILE *fp; if (strlen(elements[0].val) == 0 ) { printf("Name is required!\n"); exit(0); } if ((fp = fopen("C_phone.dat", "a"))==NULL) { printf("Unable to open C_phone.dat.\nContact Webmaster.\n"); exit(1); } strcpy(entry.name, elements[0].val); strcpy(entry.phone, elements[1].val); fwrite(&entry, sizeof(entry), 1, fp); fclose(fp); printf("Entry added successfully!\n"); } /***************************************************/ /* Function to delete an entry to the phonebook */ /***************************************************/ void phonebook_delete() { FILE *fp, *tempfp; int found = 0; if (rename("C_phone.dat", "C_phone.tmp") != 0 ) { printf("Unable to rename C_phone.dat to C_phone.tmp.\n"); printf("Contact Webmaster.\n"); exit(1); } if ((tempfp = fopen("C_phone.tmp", "r"))==NULL) { printf("Unable to open C_phone.tmp. Contact Webmaster.\n"); exit(1); } if ((fp = fopen("C_phone.dat", "w")) == NULL) { printf("Unable to open C_phone.dat. Contact Webmaster. \n"); fclose(tempfp); exit(1); } /* copy all entries from C_phone.tmp to C_phone.dat */ /* except the one to be deleted. */ while (!feof(tempfp)) { if (fread(&entry, sizeof(entry), 1, tempfp) ==1 ) { if (strcmp(entry.name, elements[0].val) != 0) fwrite(&entry, sizeof(entry), 1, fp); else found = 1; /* found name to be deleted */ } else break; /* no more records to read */ } if (found) printf("Entry successfully deleted!\n"); else printf("Entry not found!\n"); fclose(fp); fclose(tempfp); } /***************************************************/ /* Function to search for an entry in the phonebook*/ /***************************************************/ void phonebook_search() { FILE *fp; int found = 0; if ((fp=fopen("C_phone.dat", "r")) == NULL) { printf("Unable to open C_phone.dat. Contact Webmaster.\n"); exit(1); } while (!feof(fp)) { if (fread(&entry, sizeof(entry), 1, fp) == 1) { if (strcmp(entry.name, elements[0].val) == 0) { printf("Name\t= %s\nPhone\t= %s\n", entry.name, entry.phone); found = 1; break; } } else break; /* no more entries in phone book */ } fclose(fp); if(!found) printf("Entry not found in the phonebook.\n"); } main() { char * cgistr; char *content_len; int len, i, rc; /* send the MIME header first */ printf("Content-type: text/plain\n\n"); /* get the length of the cgi content */ content_len = getenv("CONTENT_LENGTH"); if (content_len==NULL) { printf("content-length is undefined!\n"); exit(1); } if ((len = atoi(content_len))==0) { printf("content-length is zero\n"); exit(1); } /* allocate memory for the cgi content */ if ((cgistr = malloc(len + 1))==NULL) { printf("cannot allocate memory, contact the webmaster\n"); exit(1); } /* read the cgi content from STDIN */ if ((rc = fread(cgistr, len, 1, stdin))!=1) { printf("cannot read the input stream (%d)! Contact the webmaster\n", rc); exit(1); } cgistr[len]='\0'; /* extract the contents of the form from cgistr */ for(i=0; cgistr[0] != '\0'; i++) { /* first split cgistr using the '&' as separator */ splitword(elements[i].val, cgistr, '&'); /*decode the string */ unescape_url(elements[i].val); /*now split the string into name and value using the '=' as separator */ splitword(elements[i].name, elements[i].val, '='); } /* call appropriate subrouting depending on the button,*/ /* the user pressed on the form. */ if (strcmp(elements[2].val, "ADD") == 0) phonebook_add(); else if (strcmp(elements[2].val, "DELETE") == 0) phonebook_delete(); else /* else it is SEARCH */ phonebook_search(); }