/******************************************************************************* * Description : * Argurments : * Return value : * Modify : * warning : *******************************************************************************/ static void list_destroy(ListHandle handle) { if(handle) { if(handle->list) { list_remove_all(handle); } handle->link_only = 0; g_free(handle); } }
void cpu_remove_all() { struct list_link *link = cpu_instances; struct cpu_instance *instance; while ((instance = list_get_next(&link))) if (instance->cpu->deinit) instance->cpu->deinit(instance); list_remove_all(&cpu_instances); }
void channel_close(struct channel_t *ch) { debug("channel_close: begin\n"); if ( ! ch->closed ) { disable_irq(); FOR_LIST_BEGIN(it, task, struct task_t *, &ch->input_tasks); msg_close(task->msg); list_remove_all(&task->output_channels); task_make_active(task); FOR_LIST_END(); list_remove_all(&ch->input_tasks); FOR_LIST_BEGIN(it, task, struct task_t *, &ch->output_tasks); msg_close(task->msg); list_remove_all(&task->input_channels); task_make_active(task); FOR_LIST_END(); list_remove_all(&ch->output_tasks); enable_irq(); }
void anon_octs_delete(anon_octs_t *a) { if (! a) { return; } lh_free(a->hash_table); list_remove_all(&(a->list)); free(a); }
/** * This function listens for input from STDIN and tries to match it to a * pattern that will trigger different actions. */ void *cmdline(void *arg) { pthread_detach(pthread_self()); (void) arg; char buffer[1024]; while (1) { memset(buffer, '\0', 1024); printf("> "); fflush(stdout); fgets(buffer, 1024, stdin); if (strncasecmp(buffer, "users", 5) == 0 || strncasecmp(buffer, "online", 6) == 0 || strncasecmp(buffer, "clients", 7) == 0) { list_print(l); continue; } else if (strncasecmp(buffer, "exit", 4) == 0 || strncasecmp(buffer, "quit", 4) == 0) { raise(SIGINT); break; } else if ( strncasecmp(buffer, "help", 4) == 0 ) { printf("------------------------ HELP ------------------------\n"); printf("| To display information about the online users, |\n"); printf("| type: 'users', 'online', or 'clients'. |\n"); printf("| |\n"); printf("| To send a message to a specific user from the |\n"); printf("| server type: 'send <IP> <SOCKET> <MESSAGE>' or |\n"); printf("| 'write <IP> <SOCKET> <MESSAGE>'. |\n"); printf("| |\n"); printf("| To send a message to all users from the server |\n"); printf("| type: 'sendall <MESSAGE>' or 'writeall |\n"); printf("| <MESSAGE>'. |\n"); printf("| |\n"); printf("| To kick a user from the server and close the |\n"); printf("| socket connection type: 'kick <IP> <SOCKET>' |\n"); printf("| or 'close <IP> <SOCKET>'. |\n"); printf("| |\n"); printf("| To kick all users from the server and close |\n"); printf("| all socket connections type: 'kickall' or |\n"); printf("| 'closeall'. |\n"); printf("| |\n"); printf("| To quit the server type: 'quit' or 'exit'. |\n"); printf("------------------------------------------------------\n"); fflush(stdout); continue; } else if ( strncasecmp(buffer, "kickall", 7) == 0 || strncasecmp(buffer, "closeall", 8) == 0) { list_remove_all(l); } else if ( strncasecmp(buffer, "kick", 4) == 0 || strncasecmp(buffer, "close", 5) == 0) { char *token = strtok(buffer, " "), *addr, *sock; if (token != NULL) { token = strtok(NULL, " "); if (token == NULL) { printf("The command was executed without parameters. Type " "'help' to see how to execute the command properly." "\n"); fflush(stdout); continue; } else { addr = token; } token = strtok(NULL, ""); if (token == NULL) { printf("The command was executed with too few parameters. Type " "'help' to see how to execute the command properly." "\n"); fflush(stdout); continue; } else { sock = token; } ws_client *n = list_get(l, addr, strtol(sock, (char **) NULL, 10)); if (n == NULL) { printf("The client that was supposed to receive the " "message, was not found in the userlist.\n"); fflush(stdout); continue; } ws_closeframe(n, CLOSE_SHUTDOWN); } } else if ( strncasecmp(buffer, "sendall", 7) == 0 || strncasecmp(buffer, "writeall", 8) == 0) { char *token = strtok(buffer, " "); ws_connection_close status; if (token != NULL) { token = strtok(NULL, ""); if (token == NULL) { printf("The command was executed without parameters. Type " "'help' to see how to execute the command properly." "\n"); fflush(stdout); continue; } else { ws_message *m = message_new(); m->len = strlen(token); char *temp = malloc( sizeof(char)*(m->len+1) ); if (temp == NULL) { raise(SIGINT); break; } memset(temp, '\0', (m->len+1)); memcpy(temp, token, m->len); m->msg = temp; temp = NULL; if ( (status = encodeMessage(m)) != CONTINUE) { message_free(m); free(m); raise(SIGINT); break;; } list_multicast_all(l, m); message_free(m); free(m); } } } else if ( strncasecmp(buffer, "send", 4) == 0 || strncasecmp(buffer, "write", 5) == 0) { char *token = strtok(buffer, " "), *addr, *sock, *msg; ws_connection_close status; if (token != NULL) { token = strtok(NULL, " "); if (token == NULL) { printf("The command was executed without parameters. Type " "'help' to see how to execute the command properly." "\n"); fflush(stdout); continue; } else { addr = token; } token = strtok(NULL, " "); if (token == NULL) { printf("The command was executed with too few parameters. Type " "'help' to see how to execute the command properly." "\n"); fflush(stdout); continue; } else { sock = token; } token = strtok(NULL, ""); if (token == NULL) { printf("The command was executed with too few parameters. Type " "'help' to see how to execute the command properly." "\n"); fflush(stdout); continue; } else { msg = token; } ws_client *n = list_get(l, addr, strtol(sock, (char **) NULL, 10)); if (n == NULL) { printf("The client that was supposed to receive the " "message, was not found in the userlist.\n"); fflush(stdout); continue; } ws_message *m = message_new(); m->len = strlen(msg); char *temp = malloc( sizeof(char)*(m->len+1) ); if (temp == NULL) { raise(SIGINT); break; } memset(temp, '\0', (m->len+1)); memcpy(temp, msg, m->len); m->msg = temp; temp = NULL; if ( (status = encodeMessage(m)) != CONTINUE) { message_free(m); free(m); raise(SIGINT); break;; } list_multicast_one(l, n, m); message_free(m); free(m); } } else { printf("To see functions available type: 'help'.\n"); fflush(stdout); continue; } } pthread_exit((void *) EXIT_SUCCESS); }
// this is where the magic happens int main(int argc, const char *argv[]) { WINDOW *menu_win; node_t *curr; int len, ch, count, highlight = 1; char directions[] = "Use arrow keys to navigate, press enter to select"; char insert_msg[] = "Enter the element you'd like to insert: "; char remove_msg[] = "Enter the element you'd like to remove: "; char count_msg[] = "Enter the element you'd like to count: "; // create a new node list list_t *node_list = new_list(); // initialize the ncurses session initscr(); clear(); noecho(); cbreak(); curs_set(0); getmaxyx(stdscr, stdscr_rows, stdscr_cols); mvprintw(2, 2, "%s", directions); // initialize the menu window menu_win = newwin(10, 50, ((stdscr_rows-10)/2), ((stdscr_cols-50)/2)); getmaxyx(menu_win, menu_win_rows, menu_win_cols); keypad(menu_win, TRUE); print_menu(menu_win, highlight); refresh(); // enter continuous loop to let user use the menu while (1) { switch(ch = wgetch(menu_win)) { case KEY_UP: if (highlight == 1) highlight = n_choices; else --highlight; break; case KEY_DOWN: if (highlight == n_choices) highlight = 1; else ++highlight; break; case 10: switch (highlight) { case 1: /* print */ if (node_list->len) { len = node_list->len; curr = node_list->head->next; mvprintw(stdscr_rows - 2, 2, "List elements:"); clrtoeol(); while (len--) { printw(" %d", curr->val); curr = curr->next; } } else { mvprintw(stdscr_rows - 2, 2, "The list is empty"); clrtoeol(); } break; case 2: /* insert one */ list_insert(node_list, new_node(get_input(insert_msg))); mvprintw(stdscr_rows - 2, 2, "Element Inserted!"); clrtoeol(); break; case 3: /* remove one */ curr = list_find(node_list, get_input(remove_msg)); if (curr) { list_remove(node_list, curr); mvprintw(stdscr_rows - 2, 2, "Element Removed!"); } else { mvprintw(stdscr_rows - 2, 2, "Element Not Found!"); } clrtoeol(); break; case 4: /* remove each */ curr = list_find(node_list, get_input(remove_msg)); if (curr) { list_remove_each(node_list, curr); mvprintw(stdscr_rows - 2, 2, "All Matching Elements Removed!"); } else { mvprintw(stdscr_rows - 2, 2, "Element Not Found!"); } clrtoeol(); break; case 5: /* remove all */ list_remove_all(node_list); mvprintw(stdscr_rows - 2, 2, "All Elements Removed!"); clrtoeol(); break; case 6: /* count elements */ mvprintw(stdscr_rows - 2, 2, "List length: %d", node_list->len); clrtoeol(); break; case 7: /* count occurrences */ count = list_count_each(node_list, get_input(count_msg)); mvprintw(stdscr_rows - 2, 2, "Occurrences: %d", count); clrtoeol(); break; case 8: /* exit */ list_destroy(node_list); endwin(); return 0; } } print_menu(menu_win, highlight); refresh(); } } /* end main() */
int test_list() { puts("starting tests"); puts("##########################################"); puts("starting linked list tests"); puts("##########################################"); int value = 0; struct List *list = list_create(); puts("empty list created"); if (list_length(list) != 0) { printf("list_length of empty list should be zero\n"); return 0; } puts("list_length ok"); // Insert value 101 and test functions list_insert(list, 0, 101); if (list_length(list) != 1) { printf("list_length should be 1\n"); return 0; } if (list_get(list, 0, &value) == 0) { printf("Error in list_get (1)\n"); return 0; } if (value != 101) { printf("list_get should return value 101\n"); return 0; } // Insert value 202 and test functions list_insert(list, 0, 202); if (list_length(list) != 2) { printf("list_length should return 2\n"); return 0; } if (list_get(list, 0, &value) == 0) { printf("Error in list_length (2)\n"); return 0; } if (value != 202) { printf("list_get should return 202\n"); return 0; } puts("list_get ok"); // Test remove function if (list_remove(list, 1) == 0) { printf("Error in list_remove\n"); return 0; } if (list_length(list) != 1) { printf("list_length should return 1 (after remove)\n"); return 0; } if (list_remove(list, 1) != 0) { printf("Error in list_remove\n"); return 0; } if (list_length(list) != 1) { printf("list_length should return 1 (after remove)\n"); return 0; } if (list_remove(list, 0) == 0) { printf("Error in list_remove\n"); return 0; } if (list_length(list) != 0) { printf("list_length should return 0 (after remove)\n"); return 0; } if (list_remove(list, 0) != 0) { printf("Error in list_remove\n"); return 0; } if (list_length(list) != 0) { printf("list_length should return 0 (after remove)\n"); return 0; } puts("list_remove ok"); // Test pop function if (list_pop(list, &value) != 0) { printf("Error in list_pop\n"); return 0; } list_append(list, 202); if (list_pop(list, &value) == 0) { printf("Error in list_pop\n"); return 0; } if (value != 202) { printf("list_pop should return 202\n"); return 0; } if (list_length(list) != 0) { printf("list_length should return 0 (after pop)\n"); return 0; } puts("list_pop ok"); // test list_append() list_append(list, -5); list_append(list, 0); list_append(list, 15); if (list_length(list) != 3) { printf("list_length should return 0 (after pop)\n"); return 0; } if (list_get(list, 0, &value) != 1) { printf("Error in list_append\n"); return 0; } if (value != -5) { printf("list_get should return -5\n"); return 0; } if (list_get(list, 1, &value) != 1) { printf("Error in list_append\n"); return 0; } if (value != 0) { printf("list_get should return 0\n"); return 0; } if (list_get(list, 2, &value) != 1) { printf("Error in list_append\n"); return 0; } if (value != 15) { printf("list_get should return 15\n"); return 0; } if (list_pop(list, &value) == 0) { printf("Error in list_pop\n"); return 0; } if (list_pop(list, &value) == 0) { printf("Error in list_pop\n"); return 0; } if (list_pop(list, &value) == 0) { printf("Error in list_pop\n"); return 0; } if (list_get(list, 0, &value) != 0) { printf("Error in list_get\n"); return 0; } puts("list_append ok"); // test list_prepend list_prepend(list, -5); list_prepend(list, 0); list_prepend(list, 15); if (list_length(list) != 3) { printf("list_length should return 0 (after pop)\n"); return 0; } if (list_get(list, 0, &value) != 1) { printf("Error in list_append\n"); return 0; } if (value != 15) { printf("list_get should return 15\n"); return 0; } if (list_get(list, 1, &value) != 1) { printf("Error in list_append\n"); return 0; } if (value != 0) { printf("list_get should return 0\n"); return 0; } if (list_get(list, 2, &value) != 1) { printf("Error in list_append\n"); return 0; } if (value != -5) { printf("list_get should return -5\n"); return 0; } puts("list_prepend ok"); // test list insert list_insert(list, -5, 0); if (list_length(list) != 4) { printf("list_length should return 4\n"); return 0; } if (list_get(list, 0, &value) != 1) { printf("Error in list_append\n"); return 0; } if (value != 0) { printf("list_get should return 0\n"); return 0; } list_insert(list, 2, 100); if (list_length(list) != 5) { printf("list_length should return 5\n"); return 0; } if (list_get(list, 2, &value) != 1) { printf("Error in list_append\n"); return 0; } if (value != 100) { printf("list_get should return 100\n"); return 0; } list_insert(list, 10, 500); if (list_length(list) != 6) { printf("list_length should return 6\n"); return 0; } if (list_get(list, 5, &value) != 1) { printf("Error in list_append\n"); return 0; } if (value != 500) { printf("list_get should return 500\n"); return 0; } puts("list_insert ok"); // test insert sorted for (int i = 0; i<6; i++) list_remove(list, 0); for (int i = 0; i<5; i++) list_append(list, i); list_append(list, 6); if (list_length(list) != 6) { printf("list_length should return 6\n"); return 0; } list_insert_sorted(list, -1); list_insert_sorted(list, 5); list_insert_sorted(list, 7); for (int i = -1; i<8; i++) { list_get(list, i+1, &value); if (value != i) printf("error in list insert sorted\n"); } // test print and print reversed puts("list_insert_sorted ok"); puts("print current list, should be sorted"); list_print(list); puts("printing reversed list"); list_print_reverse(list); puts("check print and print_reversed for yourself!"); // test list remove all for (int i = 0; i<9; i++) list_remove(list, 0); for (int i = 0; i<5; i++) list_append(list, 5); list_remove_all(list, 5); if (list_length(list) != 0) { printf("list_length should return 0 (list remove all doesn't work\n"); return 0; } for (int i = 0; i<9; i++) list_remove(list, 0); for (int i = 0; i<5; i++) list_append(list, 5); list_insert(list, -1, 0); list_remove_all(list, 5); if (list_length(list) != 1) { printf("list_length should return 1 (list remove all doesn't work\n"); return 0; } for (int i = 0; i<9; i++) list_remove(list, 0); for (int i = 0; i<5; i++) list_append(list, 5); list_insert(list, 3, 0); list_remove_all(list, 5); if (list_length(list) != 1) { printf("list_length should return 1 (list remove all doesn't work\n"); return 0; } for (int i = 0; i<9; i++) list_remove(list, 0); for (int i = 0; i<5; i++) list_append(list, 5); list_insert(list, 10, 0); list_remove_all(list, 5); if (list_length(list) != 1) { printf("list_length should return 1 (list remove all doesn't work\n"); return 0; } puts("list_remove_all ok"); puts("##########################################"); puts("all tests of linked lists completed"); puts("##########################################"); puts("------------------------------------------"); list_delete(list); return 1; }
/* * generate anonymized strings preserving lexicographic-order * * only strings between start and end (including start, excluding end) * will be processed * set end to null to process list to the end * prev_length - length of prefixes already processed. Hence all * strings in the range must be longer or of equal * length. Furthermore, all strings in the range are expected to have * identical prefixes of prev_length. * aprefix - anonymized prefix (first prev_length chars) */ static int generate_lex_anonymizations(anon_octs_t *a, size_t prev_length, const char* aprefix, struct node *start, struct node *end) { char* str; /* prefix up to min_length */ char* astr; /* astr - anonymized str */ char* prefix; /* prefix of prev_length - same for all strings in group */ //char* aprefix; /* anonymized (hash of) prefix */ //char* middle; /* part of string between prev_length and min_length */ char* amiddle; /* anonymized (hash of) middle */ struct node *p, *q; /* nodes in list */ struct node *start2; /* recursively process this part of the list */ size_t min_length; /* minimum string length in our part of list */ int count; /* number of unique prefixes (of min_length) */ struct node* hashlist = NULL; /* stores generated amiddle's */ struct node *hp; /* nodes in hash list */ struct hash_node* node = NULL; /* lhash table node */ int i; assert(a); if (!start) return 0; assert(aprefix || prev_length > 0); /* find min length */ min_length = strlen(start->data); for (p = start; p && p!=end; p = p->next) { int tmp = strlen(p->data); if (tmp < min_length) { min_length = tmp; } } assert(min_length > prev_length); /* count unique prefixes of min_length (after position prev_length) */ count = 0; for (p = start, q = NULL; p && p!=end; q = p, p = p->next) { if (q) { if (strncmp(p->data+prev_length, q->data+prev_length, min_length-prev_length)) { count++; } } else { /* first element in list */ count++; } } /* produce hashlist (amiddle) */ for (i=0; i<count; i++) { do { amiddle = (char*) malloc(min_length-prev_length+1); amiddle = generate_random_string(amiddle, min_length-prev_length); } while (list_insert(&hashlist,amiddle)==1); } /* assign anon. strings to real strings and store them in lhash table */ str = (char*) malloc(min_length+1); astr = (char*) malloc(min_length+1); assert(str); assert(astr); hp = hashlist; int group_size = 0; /* size of last group * excluding min_lenght element (if it exists) */ int is_diff = 0; /* is current string (p) different from previous one (q) * up to min_length? */ int was_minlength = 0; /* if last group contained (==started with) * a string of min_length * - determines if we need to allocate new str, astr */ start2 = start; for (p = start, q = NULL; p && p!=end; q = p, p = p->next) { /* fprintf(stderr, "assigning %s (hp: %s)...\n", p->data, (hp)?hp->data:"NULL"); */ assert(strlen(p->data) >= min_length); /* check if p is different from q up to first min_length chars */ is_diff = 0; if (q) { if (strncmp(p->data+prev_length, q->data+prev_length, min_length-prev_length)) { is_diff = 1; } else { group_size++; } } else { /* first item in list */ is_diff = 1; } if (is_diff) { if (q) { /* don't call for first item in list */ /* anonymize the previous group */ if (group_size > 0) { assert(strlen(start2->data) > min_length); generate_lex_anonymizations(a, min_length, astr, start2, p); } if (was_minlength) { str = (char*) malloc(min_length+1); astr = (char*) malloc(min_length+1); assert(str); assert(astr); } } start2 = p; /* prepare str, astr */ strncpy(str, p->data, min_length); str[min_length] = '\0'; /* aprefix generated earlier and passed as a function argument */ strncpy(astr, aprefix, prev_length); assert(hp); assert(hp->data); strncpy(astr+prev_length, hp->data, min_length-prev_length); astr[min_length] = '\0'; if (strlen(p->data) == min_length) { /* store (str, astr) in lhash */ node = (struct hash_node*) malloc(sizeof(struct hash_node)); assert(node); node->data = str; node->hash = astr; /* fprintf(stderr, "storing in hash table [%s --> %s]\n", str, astr); */ lh_insert(a->hash_table, node); /* omit this (min_length) element from recursion */ start2 = p->next; was_minlength = 1; group_size = 0; } else { /* don't need to store (str, astr) in lhash */ was_minlength = 0; group_size = 1; } /* advance to next node in hashlist */ hp = hp->next; } /* else do nothing */ } if (start2 && group_size > 0) { assert(strlen(start2->data) > min_length); generate_lex_anonymizations(a, min_length, astr, start2, end); } if (was_minlength) { str = (char*) malloc(min_length+1); astr = (char*) malloc(min_length+1); assert(str); assert(astr); } /* we don't need the list of used strings anymore */ //list_remove_all(&(a->list)); list_remove_all(&hashlist); free(str); free(astr); return 0; }
void i_list_delete(i_list* l) { dc_del_cons(l->cons); free(l); list_remove_all(l->head); }