void mix_card_game_classic(Stack *card_game) { /** Mix the card game by taken an random card from the game and reinsert in the game at an random position. **/ int c ; for (c=0 ; c < 32 ; c++) { int cc ; uint8_t get_cards ; ListElt *card_to_rm = list_head(card_game) ; ListElt *card_to_add = list_head(card_game) ; /** Compute an random card taking position. **/ get_cards=get_rand(32) ; while (get_cards == 0) { get_cards=get_rand(32) ; } for (cc=0 ; cc < get_cards ; cc++ ) { card_to_rm=list_next(card_to_rm) ; } /** Taken one card from the game. **/ Card *card_datas = malloc(sizeof(Card)) ; if (list_remove_next((List *) card_game,card_to_rm,(void *) &card_datas) != 0) { /** This should never append. **/ continue ; } usleep(get_rand(25)) ; /** For random utils. **/ /** Compute an random insert position. **/ get_cards=get_rand(32) ; while (get_cards == 0) { get_cards=get_rand(32) ; } for (cc=0 ; cc < get_cards ; cc++ ) { card_to_add=list_next(card_to_add) ; } if (list_insert_next((List *) card_game,card_to_add,card_datas) != 0) { /** This should never append. **/ reinsert_card_in_game((List *) card_game, card_datas) ; } usleep(get_rand(25)) ; /** For random utils. **/ } return ; }
/* * Get DevCB for new registration */ static DevCB* newDevCB(RAW_U8 *devnm ) { DevCB *devcb; devcb = (DevCB*)list_remove_next(&FreeDevCB); if ( devcb == NULL ) { return NULL; /* No space */ } raw_strncpy((char*)devcb->devnm, (char*)devnm, DEVICE_NAME_LENGTH + 1); list_init(&devcb->openq); list_init(&devcb->syncq); list_insert(&devcb->q, &UsedDevCB); return devcb; }
void list_destroy(linked_list_t* list) { void* data; while (list_size(list) > 0) { if (list_remove_next(list, NULL, (void **)&data) == 0 && list->destroy != NULL) { // Free the data (if it needs to be freed) list->destroy(data); } } // Clear the list just in case list->size = 0; list->destroy = NULL; list->head = NULL; list->tail = NULL; }
void list_destroy(linkedList *list) { void * data; /* Loop to remove each element from the list */ while (list_get_size(list) > 0) { if (list_remove_next(list, NULL, (void**)&data) == 0 && list->destroy != NULL) { /* Call the function that the user defines for destroy */ list->destroy(data); } } /* No more operations can be performed on this list now. As a * precaution, the structure should be cleared anyway. */ memset(list, 0, sizeof(linkedList)); return; }
int hashtbl_remove(HashTable *htbl, void **data) { ListElmt *element, *prev; int bucket; bucket = htbl->h(*data) % htbl->buckets; prev = NULL; for (element = list_head(&htbl->table[bucket]); element != NULL; element = list_next(element)) { if (htbl->match(*data, list_data(element))) { if (list_remove_next(&htbl->table[bucket], prev, data) == 0) { htbl->size--; return 0; } else { return -1; } } prev = element; } return -1; }
int run_access (newts_nfref *ref) { List access_list; short changed = FALSE; short redraw = TRUE; int c; int first = 0; int entries; entries = get_access_list (ref, &access_list); while (1) { if (redraw) display_access (&access_list, first, entries); redraw = FALSE; mvprintw (LINES - 2, 0, _("Option: ")); refresh (); c = getch (); switch (c) { case '?': access_help (); /* Fall through. */ case 'r': /* Redraw the screen. */ case '\f': redraw = TRUE; break; case '!': redraw = TRUE; spawn_subshell (); break; case 's': move (LINES - 2, 0); clrtoeol (); printw (_("Sorting...")); refresh (); list_merge_sort (&access_list); redraw = TRUE; break; case 'Q': /* Quit without saving. */ case 'K': list_destroy (&access_list); return 0; case 'q': case 'k': if (changed) { list_merge_sort (&access_list); write_access_list (ref, &access_list); } list_destroy (&access_list); return 0; case '-': case KEY_UP: case KEY_PPAGE: first -= c == KEY_UP ? 1 : (LINES - 6) / 2 - 1; if (first < 0) first = 0; redraw = TRUE; break; case '+': case KEY_DOWN: case KEY_NPAGE: first += c == KEY_DOWN ? 1 : (LINES - 6) / 2 - 1; if (first >= entries - (LINES - 6) / 2) { first = entries - (LINES - 6) - 3; if (first < 0) first = 0; } redraw = TRUE; break; case 'i': /* Insert new entries. */ { char *persistent_error = NULL; short stop = FALSE; while (entries < NPERMS && !stop) { ListNode *node; struct access *access_entry; char *temp, *name, *prompt; int key, i; int y, x; enum newts_access_scopes scope; int scope_is_set = FALSE; int mode; short restart = FALSE; short advice_displayed = FALSE; if (traditional) mvprintw (LINES - 5, 39, _("Entry type: ")); else { clear (); display_access (&access_list, first, entries); if (persistent_error != NULL) { move (LINES - 3, 0); clrtoeol (); printw ("%s", persistent_error); persistent_error = NULL; } move (LINES - 2, 0); clrtoeol (); printw (_("Entry type: ")); } refresh (); if (!traditional) advice_displayed = FALSE; getyx (stdscr, y, x); while (scope_is_set == FALSE) { key = getch (); if (key == '\n' || key == '\r' || key == KEY_ENTER || key == 'q' || key == 'k') { if (traditional && (key == 'k' || key == 'q')) echochar (key); stop = TRUE; break; } switch (key) { case 'u': if (traditional) { echochar (key); move (y, x); } scope = SCOPE_USER; scope_is_set = TRUE; break; case 'g': if (traditional) { echochar (key); move (y, x); } scope = SCOPE_GROUP; scope_is_set = TRUE; break; case 's': if (traditional) { echochar (key); move (y, x); } scope = SCOPE_SYSTEM; scope_is_set = TRUE; break; case KEY_RESIZE: break; case EOF: clear (); display_access (&access_list, first, entries); if (traditional) { if (advice_displayed) mvprintw (LINES - 5, 54, "(u,g,s, q,k,<cr>)"); move (LINES - 5, 51); } else { if (advice_displayed) mvprintw (LINES - 3, 0, _("Please enter 'u', 'g', or 's'; or " "'q', 'k', or <RET> to exit.")); mvprintw (LINES - 2, 0, _("Entry type: ")); } refresh (); break; default: advice_displayed = TRUE; if (traditional) { mvprintw (LINES - 5, 54, "(u,g,s, q,k,<cr>)"); move (LINES - 5, 51); } else { move (LINES - 3, 0); clrtoeol (); printw (_("Please enter 'u', 'g', or 's'; or 'q', " "'k', or <RET> to exit.")); move (LINES - 2, 0); clrtoeol (); printw (_("Entry type: ")); } refresh (); break; } } if (stop) continue; if (traditional) { prompt = newts_nmalloc (strlen (_("Name: ")) + 40, sizeof (char)); strcpy (prompt, " "); strncat (prompt, _("Name: "), strlen (_("Name: ")) + 1); move (LINES - 4, 0); clrtoeol (); } else { move (LINES - 3, 0); clrtoeol (); prompt = newts_strdup (scope == SCOPE_SYSTEM ? _("System name: ") : scope == SCOPE_GROUP ? _("Group name: ") : _("User name: ")); move (LINES - 2, 0); clrtoeol (); } refresh (); temp = gl_getline (prompt); temp[strlen (temp) - 1] = '\0'; newts_free (prompt); if (strlen (temp) == 0) continue; name = newts_strdup (temp); gl_histadd (name); if (scope == SCOPE_USER) { if (strcasecmp (name, "other") != 0) { struct passwd *pw = getpwnam (name); if (pw == NULL) { if (traditional) { move (LINES - 3, 0); clrtoeol (); mvprintw (LINES - 3, 39, _("--No such user--")); } else persistent_error = _("No such user."); continue; } endpwent (); } } if (scope == SCOPE_GROUP) { if (strcasecmp (name, "other") != 0) { struct group *gp = getgrnam (name); if (gp == NULL) { if (traditional) { move (LINES - 3, 0); clrtoeol (); mvprintw (LINES - 3, 39, _("--No such group--")); } else persistent_error = _("No such group."); continue; } endgrent (); } } node = list_head (&access_list); for (i=0; i<entries; i++) { access_entry = (struct access *) list_data (node); if (access_scope (access_entry) == scope && strcmp (access_name (access_entry), name) == 0) { if (traditional) { move (LINES - 3, 0); clrtoeol (); mvprintw (LINES - 3, 39, _("%s entry exists"), name); } else persistent_error = scope == SCOPE_USER ? _("User already exists in " "permission table.") : (scope == SCOPE_GROUP ? _("Group already exists in " "permission table.") : _("System already exists in permission table.")); restart = TRUE; continue; } node = list_next (node); if (node == NULL) continue; } if (restart) continue; { struct access *new_access = access_alloc (); access_set_permissions (new_access, READ | WRITE | REPLY); access_set_scope (new_access, scope); access_set_name (new_access, name); get_mode (new_access, &access_list, first, entries); list_insert_sorted (&access_list, (void *) new_access); } newts_free (name); entries++; redraw = TRUE; changed = TRUE; clear (); display_access (&access_list, first, entries); } if (!traditional) redraw = TRUE; break; } case 'd': /* Delete existing entries. */ { ListNode *node, *prev; struct access *data; int key, number, i; move (LINES - 2, 0); clrtoeol (); if (traditional) printw ("%s", _("Delete entry #: ")); else printw ("%s", _("Delete entry number: ")); key = getch (); while (key != '\n' && key != '\r' && key != KEY_ENTER && (key < '1' || key > '9')) key = getch (); if (key == '\n' || key == '\r' || key == KEY_ENTER) { redraw = TRUE; break; } number = get_number (key, entries); if (number < 0) { redraw = TRUE; break; } if (number > entries || key < '0' || key > '9' || number == 0) { clear (); display_access (&access_list, first, entries); if (traditional) { move (LINES - 1, 0); clrtoeol (); printw ("%s", _("Bad entry")); } else { move (LINES - 3, 0); clrtoeol (); printw ("%s", _("Invalid entry.")); } break; } number--; /* Adjust to base zero. */ prev = NULL; node = list_head (&access_list); for (i=0; i<number; i++) { prev = node; node = list_next (prev); } data = (struct access *) list_data (node); if (data->scope == SCOPE_USER && strcmp (data->name, username) == 0) { clear (); display_access (&access_list, first, entries); if (traditional) { move (LINES - 1, 0); clrtoeol (); printw ("%s", _(" Can't Delete self")); } else { move (LINES - 3, 0); clrtoeol (); printw ("%s", _("Can't delete own entry.")); } break; } list_remove_next (&access_list, prev, NULL); entries--; changed = TRUE; redraw = TRUE; break; } case 'm': /* Modify existing entries. */ { ListNode *node; struct access *existing_entry; int key, number, i; move (LINES - 2, 0); clrtoeol (); if (traditional) printw ("%s", _("Modify entry #: ")); else printw ("%s", _("Modify entry number: ")); key = getch (); while (key != '\n' && key != '\r' && key != KEY_ENTER && (key < '1' || key > '9')) key = getch (); if (key == '\n' || key == '\r' || key == KEY_ENTER) { redraw = TRUE; break; } number = get_number (key, entries); if (number < 0) { redraw = TRUE; break; } if (number > entries || key < '0' || key > '9' || number == 0) { clear (); display_access (&access_list, first, entries); if (traditional) { move (LINES - 1, 0); clrtoeol (); printw ("%s", _("Bad entry")); } else { move (LINES - 3, 0); clrtoeol (); printw ("%s", _("Invalid entry.")); } break; } number--; /* Adjust to base zero. */ node = list_head (&access_list); for (i=0; i<number; i++) { node = list_next (node); } existing_entry = (struct access *) list_data (node); get_mode (existing_entry, &access_list, first, entries); changed = TRUE; redraw = TRUE; break; } case '\004': list_destroy (&access_list); return QUITNOSEQ; case 'z': list_destroy (&access_list); return QUITSEQ; default: beep (); break; } } return 0; }
int stack_pop(Stack *stack, void **data) { return list_remove_next((List *) stack,NULL,data) ; }