/*************************************************************************** list.c ------------------- begin : Fri Mar 21 01:05:09 CST 2003 copyright : (C) 2003 by Andy Ruder email : aeruder@yahoo.com ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include #include #include struct bounty { char name[100]; int value; char reason[5000]; struct bounty * next; }; struct bounty * bounty_list = NULL; int add_bounty(const char * name, int value, const char * reason) { struct bounty * new_bounty = NULL; new_bounty = malloc(sizeof(struct bounty)); if (new_bounty == NULL) /* If malloc returns NULL, that means */ return 0; /* there is not enough memory available. */ strncpy(new_bounty->name, name, 99)[99] = 0; strncpy(new_bounty->reason, reason, 4999)[4999] = 0; new_bounty->value = value; new_bounty->next = bounty_list; /* Two most confusing lines in here. */ bounty_list = new_bounty; return 1; } void clobber_returns(char *x) { int y; y = strlen(x) - 1; while (y > 0) { if ((x[y] != '\n') && (x[y] != '\r')) break; y--; } x[y+1] = 0; } int get_values(void) { char name[100]; char reason[5000]; char stupid[100]; int value; printf("Just press enter with no input when you are done.\n"); while (1) { printf ("\nBounty's Name: "); scanf("%99[^\n]%*c", name); name[99] = 0; // clobber_returns(name); if (strlen(name) == 0) break; printf("Reason for Bounty: "); scanf("%4999[^\n]%*c", reason); reason[4999] = 0; //clobber_returns(reason); if (strlen(reason) == 0) break; printf("Amount on head : $"); fgets(stupid, 99, stdin); stupid[99] = 0; value = atoi(stupid); if (value == 0) break; add_bounty(name, value, reason); } return 1; } void delete_list(void) { struct bounty * temp; struct bounty * temp2; for (temp = bounty_list; temp != NULL; temp = bounty_list) { temp2 = temp->next; free(temp); bounty_list = temp2; } } int main(void) { struct bounty * temp; get_values(); printf("\n\n\nList printout.\n\n\n"); for (temp = bounty_list; temp != NULL; temp = temp->next) { printf("\n%s : ", temp->name); printf(" Reason - %s \n", temp->reason); printf(" Value - $%d \n", temp->value); } delete_list(); return 1; }