void close_connect(t_user **usr) { t_user *tmp; tmp = NULL; close((*usr)->fd); sort_user(usr, &tmp); }
/* Add the transaction represented by user_name and amount to the appropriate * transaction list, and update the balances of the corresponding user and group. * Note that updating a user's balance might require the user to be moved to a * different position in the list to keep the list in sorted order. Returns 0 on * success, and -1 if the specified user does not exist. */ int add_xct(Group *group, const char *user_name, double amount) { /* Allocate space for the new xct that is to be added to the list of * xcts. */ Xct *new_xct = (Xct*) malloc (sizeof(Xct)); /* Checks if malloc failed, if malloc failed, a error message is printed to * standard output and exit code of 256. */ if (new_xct == NULL) { print_exit("ERROR: Malloc failed", 256); /* If malloc didn't fail, the user name and the amount is assigned to the new * xct and the next pointer is set to NULL temporarily. */ } else { int size_to_malloc = (strlen(user_name)+1)*sizeof(char); new_xct->name = (char*) malloc (size_to_malloc); /* Checks if the current malloc failed, if it failed a message is printed * to standard output and exit code of 256 is set. */ if (new_xct->name == NULL) { print_exit("ERROR: Malloc failed", 256); } strncpy(new_xct->name, user_name, size_to_malloc); new_xct->name[size_to_malloc] = '\0'; new_xct->amount = amount; new_xct->next = NULL; } /* Finds the previous user to the user that added a xct in order to change * the user's balance and re-organize the list. */ User *prev_user = find_prev_user(group, user_name); /* If no user was found with the same user name in the list of users, -1 is * returned. */ if (prev_user == NULL) { free_dp(new_xct->name); free_dp(new_xct); return -1; /* Checks if the user returned is the head of the list of users, if it is, * the head is changed so that the user can be moved to its correct location. */ } else if (strcmp(prev_user->name, user_name) == 0) { push_xct(group, new_xct); prev_user->balance += amount; sort_user(group, prev_user, user_name); return 0; /* Checks if the user returned is in the middle of the list of users, the * user is remvoed from its current position and placed in the correct * position in the users of list. */ } else { push_xct(group, new_xct); prev_user->next->balance += amount; sort_user(group, prev_user, user_name); return 0; } }