int read_and_print_results(const int fd, const pid_t pids[], const int expected) { FILE* f; result_node *list; char line[1024]; int list_size; list = NULL; f = fdopen(fd, "r"); /* * read all of the results from pipe. they are handily newline delimited, * so fgets does the job of splitting them up. */ while( (fgets(line, 1024, f)) != NULL) list = list_insert_sorted(list, line); /* * if the count doesn't match, one or more children didn't write their * results for some unknown reason. add them as failures. */ list_size = list_count(list); if(list_count(list) < expected) { int i; /* iterate through our known pids */ for(i = 0; i < expected; i++) { int j; char spid[24]; int found = 0; sprintf(spid, "%d", pids[i]); /* and search the list to see if this pid is present. */ for(j = 0; j < list_size; j++) { if(list_find(list, spid) != NULL) { found = 1; break; } } /* didn't find it. add it to the results as a failure. */ if(found == 0) { char fail[30]; sprintf(fail, "%s %s\n", spid, kFailure); list = list_insert_sorted(list, fail); } } } list_print(list); list_free(list); return list_size; }
/* Add source to download (copies source). */ as_bool as_download_add_source (ASDownload *dl, ASSource *source) { List *l; ASDownConn *conn; /* Sources can be added in any state but we need to make sure there are * no duplicates */ for (l = dl->conns; l; l = l->next) { conn = l->data; if (as_source_equal (conn->source, source)) { AS_DBG_1 ("Source \"%s\" already added.", as_source_str (source)); return FALSE; } } /* create new connection and add it */ if (!(conn = as_downconn_create (source, conn_state_cb, conn_data_cb))) return FALSE; /* point udata1 to download, we will use udata2 for the chunk */ conn->udata1 = dl; /* Insert sorted. */ dl->conns = list_insert_sorted (dl->conns, (CompareFunc) conn_cmp_func, conn); /* check if the source should be used now */ if (dl->state == DOWNLOAD_ACTIVE) download_maintain (dl); return TRUE; }
/** * Connects an event handler function to an event. If the event handler is enabled, * this function will be called anytime the event_raise() function is called * for this event. * * @attention Under no circumstances the event handler function should block for a long time * because no other events can be processed as long as an event handler is running! * * @note The event handlers with the highest priority values are called first. * In order to use this functionality properly a convention for this event should * be made. If there is no convention, you should use 0 as priority. * * @param name name of the event * @param priority a priority value * @param handler pointer to the event handler function * @param user_data data which will be passed to the event handler function * @param handler_state the initial state of this event handler * * @returns the id of the new event handler */ int event_connect(char *name, int priority, EventHandler handler, void *user_data, EventHandlerState handler_state) { EventInfo *event; // Check whether the passed name is too long for our struct. if (strlen(name) > EVENT_NAME_MAX_LENGTH - 1) return 0; // Get the event the caller want to connect to. If we can't find it, we create a new one. event = _event_get_event(name); if (!event) { event = (EventInfo *)malloc(sizeof(EventInfo)); strcpy(event->name, name); event->l_event_handlers = NULL; event->raise_count = 0; l_events = list_append(l_events, event); } // Initialize a new struct containing information about the event handler. EventHandlerInfo *handler_info = (EventHandlerInfo *)malloc(sizeof(EventHandlerInfo)); handler_info->id = next_id++; handler_info->priority = priority; handler_info->event = event; handler_info->state = handler_state; handler_info->handler = handler; handler_info->user_data = user_data; // Insert the handler into the list. event->l_event_handlers = list_insert_sorted(event->l_event_handlers, handler_info, _event_insert_handler); // Return the generated ID. return handler_info->id; }
list_item_t* list_insert_sorted_new(list_t* list, void* data, int (*compare)(const void*, const void*)) { list_item_t* item = list_alloc_item(data); list_insert_sorted(list, data, compare); return item; }
static void cb_copy_curve(void *d, const void *s, UNUSED(void *user)) { const NdbCCurve *src = s; NdbCCurve *dst = d; NdbCKey *key; unsigned int i; dst->id = src->id; strcpy(dst->name, src->name); dst->dimensions = src->dimensions; dst->keys = dynarr_new_copy(src->keys, NULL, NULL); /* Keys are trivially copyable. */ dst->curve = NULL; for(i = 0; (key = dynarr_index(dst->keys, i)) != NULL; i++) dst->curve = list_insert_sorted(dst->curve, key, cb_key_compare); }
/* Remove connection from current position in connection list and move it to * new correct position according to conn_cmp_func. O(2n). */ static void conn_reinsert (ASDownload *dl, ASDownConn *conn) { List *link; /* Find link. */ link = list_find (dl->conns, conn); assert (link); /* Remove entry. */ dl->conns = list_remove_link (dl->conns, link); /* Reinsert at correct pos. */ dl->conns = list_insert_sorted (dl->conns, (CompareFunc) conn_cmp_func, conn); #ifdef VERIFY_CONN_LIST verify_connections (dl); #endif }
NdbCKey * nodedb_c_key_create(NdbCCurve *curve, uint32 key_id, real64 pos, const real64 *value, const uint32 *pre_pos, const real64 *pre_value, const uint32 *post_pos, const real64 *post_value) { NdbCKey *key; int insert = 0, sort = 0, i; if(curve->keys == NULL) { curve->keys = dynarr_new(sizeof *key, 8); dynarr_set_default_func(curve->keys, cb_key_default, NULL); } if(curve->keys == NULL) return NULL; if(key_id == ~0u) { /* If creating a new key, make sure it's not clobbering an existing position. Search. */ List *old; if((old = list_find_sorted(curve->curve, &pos, cb_key_compare_find)) != NULL) key = list_data(old); /* Just re-use the same slot, since we're not changing position. */ else { key = dynarr_append(curve->keys, NULL, NULL); insert = 1; } } else { key = dynarr_set(curve->keys, key_id, NULL); sort = 1; /* We might re-sort. Turned off if new pos equals old, below. */ } if(key == NULL) return NULL; key->id = key_id; if(sort) sort = key->pos != pos; /* Only resort if new position differs from the one we're reusing. */ key->pos = pos; for(i = 0; i < curve->dimensions; i++) { key->value[i] = value[i]; key->pre.pos[i] = pre_pos[i]; key->pre.value[i] = pre_value[i]; key->post.pos[i] = post_pos[i]; key->post.value[i] = post_value[i]; } if(insert) { printf("inserting key in list\n"); curve->curve = list_insert_sorted(curve->curve, key, cb_key_compare); } else if(sort) { List *nl = NULL, *iter; printf("resorting list\n"); /* This is considered a rare event, so we can be a bit expensive. */ nl = list_insert_sorted(nl, key, cb_key_compare); /* Get the new one in there. */ for(iter = curve->curve; iter != NULL; iter = list_next(iter)) nl = list_insert_sorted(nl, list_data(iter), cb_key_compare); list_destroy(curve->curve); curve->curve = nl; printf("key list resorted\n"); } return key; }
int main(int argc, char *argv[]) { List *list; FILE *fp; char word[64]; size_t count = 0, god_count = 0; ListItem *item, *prev; char *ptr; // initialize lists: list = list_new(str_equal, &free, NULL); assert(list != NULL); assert(list_empty(list) == true); // put words from bible into list: if((fp = fopen("bible.txt", "r"))) { // count words: while(fscanf(fp, "%s", word) > 0) { list_append(list, strdup(word)); ++count; } } assert(list_count(list) == count); assert(list_empty(list) == false); // iterate through list: item = list_head(list); while(item) { --count; item = list_item_next(item); } assert(count == 0); item = list_tail(list); while(item) { ++count; item = list_item_prev(item); } assert(count == list_count(list)); // remove word: item = list_head(list); while(item) { if(!strcmp(list_item_get_data(item), "God")) { ++god_count; } item = list_item_next(item); } list_remove_by_data(list, "God", true); assert(list_count(list) == count - god_count); // lookup & replace words: item = list_find(list, NULL, (void *)"God"); assert(item == NULL); assert(list_contains(list, "God") == false); item = list_find(list, NULL, (void *)"she"); assert(item != NULL); assert(!strcmp("she", (char *)list_item_get_data(item))); list_item_free_data(list, item); list_item_set_data(item, strdup("Eris")); item = list_find(list, item, (void *)"she"); assert(item != NULL); assert(!strcmp("she", (char *)list_item_get_data(item))); item = list_find(list, NULL, (void *)"Eris"); assert(!strcmp("Eris", (char *)list_item_get_data(item))); assert(list_contains(list, "Eris") == true); // clear list: list_clear(list); assert(list_empty(list) == true); assert(list_count(list) == 0); // insert sorted: list_insert_sorted(list, strdup("d"), str_compare); list_insert_sorted(list, strdup("b"), str_compare); list_insert_sorted(list, strdup("c"), str_compare); item = list_prepend(list, strdup("e")); list_reorder(list, item, str_compare); item = list_append(list, strdup("a")); list_reorder(list, item, str_compare); item = list_head(list); prev = NULL; while(item) { if(prev) { assert(strcmp((char *)list_item_get_data(prev), (char *)list_item_get_data(item)) < 0); } prev = item; item = list_item_next(item); } // pop: count = 0; while((ptr = list_pop(list))) { ++count; free(ptr); } assert(count == 5); list_destroy(list); }
int main(void) { data_t *datap = NULL; list_t *Lptr = NULL; /***** Lptr = list_construct(trte_compare, trte_route_rec_cleanup); // create one item to test list_insert datap = (data_t *) calloc(1,sizeof(data_t)); datap->src_ip_addr = 555; datap->dest_ip_addr = 555; list_insert(Lptr, datap, LISTPOS_HEAD); datap = NULL; // add a second item to head of the list datap = (data_t *) calloc(1,sizeof(data_t)); datap->src_ip_addr = 444; datap->dest_ip_addr = 444; list_insert(Lptr, datap, LISTPOS_HEAD); datap = NULL; // add a 3rd item to tail of the list datap = (data_t *) calloc(1,sizeof(data_t)); datap->src_ip_addr = 777; datap->dest_ip_addr = 777; list_insert(Lptr, datap, LISTPOS_TAIL); datap = NULL; // add a 4th item datap = (data_t *) calloc(1,sizeof(data_t)); datap->src_ip_addr = 500; datap->dest_ip_addr = 500; list_insert(Lptr, datap, LISTPOS_TAIL); datap = NULL; // add a 4th item, sorted datap = (data_t *) calloc(1,sizeof(data_t)); datap->src_ip_addr = 333; datap->dest_ip_addr = 333; list_insert_sorted(Lptr, datap); datap = NULL; // test list_access with one item in list datap = list_access(Lptr, LISTPOS_HEAD); printf("Should find 555 and found (%d)\n\n", datap->src_ip_addr); datap = NULL; // find all three and print datap = list_access(Lptr, 0); printf("Second test\nPosition 0 should find 555 and found (%d)\n", datap->src_ip_addr); datap = list_access(Lptr, 1); printf("Position 1 should find 555 and found (%d)\n", datap->src_ip_addr); datap = list_access(Lptr, 2); printf("Position 2 should find 777 and found (%d)\n", datap->src_ip_addr); datap = list_access(Lptr, 3); printf("Position 3 should find 777 and found (%d)\n", datap->src_ip_addr); datap = list_access(Lptr, 3); printf("Position 3 should find 777 and found (%d)\n", datap->src_ip_addr); //Next try to use list_debug_print printf("\nTest of list print\n\n"); list_debug_print(Lptr); */ // Uncomment this section to test list_elem_find /********************************************************************* data_t template; int my_index = -999; template.src_ip_addr = 444; template.dest_ip_addr = 444; data_t *foundp = list_elem_find(Lptr, &template, &my_index); printf("\nTest of list elem find\n"); if (foundp != NULL) printf("looked for %d and found %d at index %d\n", template.src_ip_addr, foundp->src_ip_addr, my_index); else printf("looked for %d and did not find \n", template.src_ip_addr); foundp = NULL; */ //list_destruct(Lptr); // End of tests with unsorted list // Uncomment this section to try some tests on a sorted list /*********************************************************************/ list_t *Lsortptr = list_construct(trte_compare, trte_route_rec_cleanup); //Lsortptr->list_sorted_state = -7654321; datap = (data_t *) calloc(1,sizeof(data_t)); datap->src_ip_addr = 23; datap->dest_ip_addr = 23; list_insert_sorted(Lsortptr, datap); datap = NULL; // add a second item datap = (data_t *) calloc(1,sizeof(data_t)); datap->src_ip_addr = 7; datap->dest_ip_addr = 7; list_insert_sorted(Lsortptr, datap); datap = NULL; // add a third item datap = (data_t *) calloc(1,sizeof(data_t)); datap->src_ip_addr = 15; datap->dest_ip_addr = 15; list_insert_sorted(Lsortptr, datap); datap = NULL; // add a fourth item datap = (data_t *) calloc(1,sizeof(data_t)); datap->src_ip_addr = 8; datap->dest_ip_addr = 8; list_insert_sorted(Lsortptr, datap); datap = NULL; // add a fifth item datap = (data_t *) calloc(1,sizeof(data_t)); datap->src_ip_addr = 28; datap->dest_ip_addr = 28; list_insert_sorted(Lsortptr, datap); datap = NULL; //print list_debug_print(Lsortptr); // remove an item data_t *temp = list_remove(Lsortptr, 3); //print list_debug_print(Lsortptr); // add a sixth item datap = (data_t *) calloc(1,sizeof(data_t)); datap->src_ip_addr = 2; datap->dest_ip_addr = 2; list_insert_sorted(Lsortptr, datap); datap = NULL; list_reverse(Lsortptr); //print list_debug_print(Lsortptr); list_destruct(Lsortptr); return 0; }
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; }
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 main(int argc, char* argv[]){ if(argc > 3) { printf("You may only enter a filename and an option.\necho sort tail tail-remove\nExiting...\n"); exit(-1); } else if (argc < 3) { printf("You must enter both a filename and an option.\necho sort tail tail-remove\nExiting...\n"); exit(-1); } // Open the supplied input file read only FILE *input_file = fopen(argv[1], "r"); if(input_file == NULL) { perror("Please enter a different filename"); exit(-1); } // Echo the file if(strcmp(argv[2], "echo") == 0) { char echo_buf[41]; while(read_line(input_file, echo_buf, 41)) { printf("%s\n", echo_buf); memset(echo_buf, '\0', 41); } } // Put the file in a list and visit-print the list else if(strcmp(argv[2], "tail") == 0) { list_t list; list_init(&list, list_item_compare_string, string_data_delete); char *tail_buf = malloc(41*sizeof(char)); if(tail_buf == NULL) { perror("malloc failed to create new buffer."); exit(-1); } while(read_line(input_file, tail_buf, 41)) { list_insert_tail(&list, tail_buf); tail_buf = malloc(41*sizeof(char)); if(tail_buf == NULL) { perror("malloc failed to create new buffer."); exit(-1); } } // Free the last buffer created when we looped last free(tail_buf); list_visit_items(&list, print_data_string); cleanup_list(&list); } // Insert the file into a sorted list ** Does not seem to sort correctly** else if(strcmp(argv[2], "sort") == 0) { list_t list; list_init(&list, list_item_compare_string, string_data_delete); char *sort_buf = malloc(41*sizeof(char)); if(sort_buf == NULL) { perror("malloc failed to create new buffer."); exit(-1); } while(read_line(input_file, sort_buf, 41)) { list_insert_sorted(&list, sort_buf); sort_buf = malloc(41*sizeof(char)); if(sort_buf == NULL) { perror("malloc failed to create new buffer."); exit(-1); } } // Free last buffer created as it is unused free(sort_buf); list_visit_items(&list, print_data_string); cleanup_list(&list); } // Insert the file into a list in order and then remove 3 head nodes until it is empty else if(strcmp(argv[2], "tail-remove") == 0) { printf("Running tail-remove\n"); list_t list; list_init(&list, list_item_compare_string, string_data_delete); char *rem_buf = malloc(41*sizeof(char)); if(rem_buf == NULL) { perror("malloc failed to create new buffer."); exit(-1); } while(read_line(input_file, rem_buf, 41)) { list_insert_tail(&list, rem_buf); rem_buf = malloc(41*sizeof(char)); } // Free last buffer created free(rem_buf); unsigned int three_sets = (list.length)/3; unsigned int i; list_visit_items(&list, print_data_string); for(i = 0; i < three_sets; i++) { list_remove_head(&list); list_remove_head(&list); list_remove_head(&list); printf("----------------------------------------\n"); list_visit_items(&list, print_data_string); } cleanup_list(&list); printf("----------------------------------------\n"); list_visit_items(&list, print_data_string); } fclose(input_file); return 0; };