// Tests DHCPDISCOVER options void test1() { struct options opts; memset(&opts, 0, sizeof(struct options)); parse_options(test1_opts, sizeof(test1_opts), &opts); ASSERT_EQ(MESSAGE_DHCPDISCOVER, opts.message_type); ASSERT_EQ(1500, opts.max_size); char hostname[] = { "lozenge" }; ASSERT_EQ(0, strcmp(hostname, opts.hostname)); ASSERT_EQ(7776000, opts.lease_time); ASSERT_EQ(1, opts.client_id_type); ASSERT_EQ(6, opts.client_id_length); uint8_t mac[] = { 0x00, 0x0a, 0x095, 0xbe, 0x88, 0xc6 }; ASSERT_EQ(0, memcmp(mac, opts.client_id, 6)); uint8_t params[] = { DHCP_SUBNET_MASK, DHCP_ROUTER, DHCP_DNS, DHCP_DOMAIN }; ASSERT_EQ(4, opts.param_size); ASSERT_EQ(0, memcmp(params, opts.param_list, 4)); ASSERT_EQ(0, opts.server_id); ASSERT_EQ(0, opts.subnet); ASSERT_EQ(0, opts.router_num); ASSERT_EQ(0, opts.dns_num); ASSERT_EQ(0, strlen(opts.domain)); uint8_t buf[1024]; uint16_t size = 1024; write_options(&opts, buf, &size); ASSERT_EQ(size, sizeof(test1_output)); ASSERT_ARRAY_EQ(test1_output, buf, size); }
// Tests DHCPOFFER options void test4() { struct options opts; memset(&opts, 0, sizeof(struct options)); parse_options(test4_opts, sizeof(test4_opts), &opts); ASSERT_EQ(MESSAGE_DHCPACK, opts.message_type); uint8_t id[] = { 10, 0, 1, 1 }; ASSERT_EQ(0, memcmp(id, &opts.server_id, 4)); uint8_t subnet[] = { 255, 255, 255, 0 }; ASSERT_EQ(0, memcmp(subnet, &opts.subnet, 4)); ASSERT_EQ(14400, opts.lease_time); ASSERT_EQ(1, opts.router_num); ASSERT_EQ(0, memcmp(id, &opts.router, 4)); ASSERT_EQ(1, opts.dns_num); ASSERT_EQ(0, memcmp(id, &opts.dns, 4)); char domain[] = { "thebends.org" }; ASSERT_EQ(0, strcmp(domain, opts.domain)); // Options NOT in the response ASSERT_EQ(0, opts.max_size); ASSERT_EQ(0, strlen(opts.hostname)); ASSERT_EQ(0, opts.client_id_type); ASSERT_EQ(0, opts.client_id_length); ASSERT_EQ(0, opts.param_size); uint8_t buf[1024]; uint16_t size = 1024; write_options(&opts, buf, &size); ASSERT_ARRAY_EQ(test4_output, buf, size); ASSERT_EQ(size, sizeof(test4_output)); }
/* Reads contents of the filename file as an info file and updates it with the * state of current instance. */ static void update_info_file(const char filename[]) { /* TODO: refactor this function update_info_file() */ FILE *fp; char **cmds_list; int ncmds_list = -1; char **ft = NULL, **fx = NULL, **fv = NULL, **cmds = NULL, **marks = NULL; char **lh = NULL, **rh = NULL, **cmdh = NULL, **srch = NULL, **regs = NULL; int *lhp = NULL, *rhp = NULL, *bt = NULL, *bmt = NULL; char **prompt = NULL, **filter = NULL, **trash = NULL; char **bmarks = NULL; int nft = 0, nfx = 0, nfv = 0, ncmds = 0, nmarks = 0, nlh = 0, nrh = 0; int ncmdh = 0, nsrch = 0, nregs = 0, nprompt = 0, nfilter = 0, ntrash = 0; int nbmarks = 0; char **dir_stack = NULL; int ndir_stack = 0; char *non_conflicting_marks; if(cfg.vifm_info == 0) return; cmds_list = list_udf(); while(cmds_list[++ncmds_list] != NULL); non_conflicting_marks = strdup(valid_marks); if((fp = os_fopen(filename, "r")) != NULL) { size_t nlhp = 0UL, nrhp = 0UL, nbt = 0UL, nbmt = 0UL; char *line = NULL, *line2 = NULL, *line3 = NULL, *line4 = NULL; while((line = read_vifminfo_line(fp, line)) != NULL) { const char type = line[0]; const char *const line_val = line + 1; if(type == LINE_TYPE_COMMENT || type == '\0') continue; if(type == LINE_TYPE_FILETYPE) { if((line2 = read_vifminfo_line(fp, line2)) != NULL) { if(!ft_assoc_exists(&filetypes, line_val, line2)) { nft = add_to_string_array(&ft, nft, 2, line_val, line2); } } } else if(type == LINE_TYPE_XFILETYPE) { if((line2 = read_vifminfo_line(fp, line2)) != NULL) { if(!ft_assoc_exists(&xfiletypes, line_val, line2)) { nfx = add_to_string_array(&fx, nfx, 2, line_val, line2); } } } else if(type == LINE_TYPE_FILEVIEWER) { if((line2 = read_vifminfo_line(fp, line2)) != NULL) { if(!ft_assoc_exists(&fileviewers, line_val, line2)) { nfv = add_to_string_array(&fv, nfv, 2, line_val, line2); } } } else if(type == LINE_TYPE_COMMAND) { if(line_val[0] == '\0') continue; if((line2 = read_vifminfo_line(fp, line2)) != NULL) { int i; const char *p = line_val; for(i = 0; i < ncmds_list; i += 2) { int cmp = strcmp(cmds_list[i], p); if(cmp < 0) continue; if(cmp == 0) p = NULL; break; } if(p == NULL) continue; ncmds = add_to_string_array(&cmds, ncmds, 2, line_val, line2); } } else if(type == LINE_TYPE_LWIN_HIST || type == LINE_TYPE_RWIN_HIST) { if(line_val[0] == '\0') continue; if((line2 = read_vifminfo_line(fp, line2)) != NULL) { const int pos = read_optional_number(fp); if(type == LINE_TYPE_LWIN_HIST) { process_hist_entry(&lwin, line_val, line2, pos, &lh, &nlh, &lhp, &nlhp); } else { process_hist_entry(&rwin, line_val, line2, pos, &rh, &nrh, &rhp, &nrhp); } } } else if(type == LINE_TYPE_MARK) { const char mark = line_val[0]; if(line_val[1] != '\0') { LOG_ERROR_MSG("Expected end of line, but got: %s", line_val + 1); } if((line2 = read_vifminfo_line(fp, line2)) != NULL) { if((line3 = read_vifminfo_line(fp, line3)) != NULL) { const int timestamp = read_optional_number(fp); const char mark_str[] = { mark, '\0' }; if(!char_is_one_of(valid_marks, mark)) { continue; } if(is_mark_older(mark, timestamp)) { char *const pos = strchr(non_conflicting_marks, mark); if(pos != NULL) { nmarks = add_to_string_array(&marks, nmarks, 3, mark_str, line2, line3); nbt = add_to_int_array(&bt, nbt, timestamp); *pos = '\xff'; } } } } } else if(type == LINE_TYPE_BOOKMARK) { if((line2 = read_vifminfo_line(fp, line2)) != NULL) { if((line3 = read_vifminfo_line(fp, line3)) != NULL) { long timestamp; if(read_number(line3, ×tamp) && bmark_is_older(line_val, timestamp)) { nbmarks = add_to_string_array(&bmarks, nbmarks, 2, line_val, line2); nbmt = add_to_int_array(&bmt, nbmt, timestamp); } } } } else if(type == LINE_TYPE_TRASH) { if((line2 = read_vifminfo_line(fp, line2)) != NULL) { char *const trash_name = convert_old_trash_path(line_val); if(exists_in_trash(trash_name) && !is_in_trash(trash_name)) { ntrash = add_to_string_array(&trash, ntrash, 2, trash_name, line2); } free(trash_name); } } else if(type == LINE_TYPE_CMDLINE_HIST) { if(!hist_contains(&cfg.cmd_hist, line_val)) { ncmdh = add_to_string_array(&cmdh, ncmdh, 1, line_val); } } else if(type == LINE_TYPE_SEARCH_HIST) { if(!hist_contains(&cfg.search_hist, line_val)) { nsrch = add_to_string_array(&srch, nsrch, 1, line_val); } } else if(type == LINE_TYPE_PROMPT_HIST) { if(!hist_contains(&cfg.prompt_hist, line_val)) { nprompt = add_to_string_array(&prompt, nprompt, 1, line_val); } } else if(type == LINE_TYPE_FILTER_HIST) { if(!hist_contains(&cfg.filter_hist, line_val)) { nfilter = add_to_string_array(&filter, nfilter, 1, line_val); } } else if(type == LINE_TYPE_DIR_STACK) { if((line2 = read_vifminfo_line(fp, line2)) != NULL) { if((line3 = read_vifminfo_line(fp, line3)) != NULL) { if((line4 = read_vifminfo_line(fp, line4)) != NULL) { ndir_stack = add_to_string_array(&dir_stack, ndir_stack, 4, line_val, line2, line3 + 1, line4); } } } } else if(type == LINE_TYPE_REG) { if(regs_exists(line_val[0])) { continue; } nregs = add_to_string_array(®s, nregs, 1, line); } } free(line); free(line2); free(line3); free(line4); fclose(fp); } if((fp = os_fopen(filename, "w")) != NULL) { fprintf(fp, "# You can edit this file by hand, but it's recommended not to " "do that.\n"); if(cfg.vifm_info & VIFMINFO_OPTIONS) { write_options(fp); } if(cfg.vifm_info & VIFMINFO_FILETYPES) { write_assocs(fp, "Filetypes", LINE_TYPE_FILETYPE, &filetypes, nft, ft); write_assocs(fp, "X Filetypes", LINE_TYPE_XFILETYPE, &xfiletypes, nfx, fx); write_assocs(fp, "Fileviewers", LINE_TYPE_FILEVIEWER, &fileviewers, nfv, fv); } if(cfg.vifm_info & VIFMINFO_COMMANDS) { write_commands(fp, cmds_list, cmds, ncmds); } if(cfg.vifm_info & VIFMINFO_MARKS) { write_marks(fp, non_conflicting_marks, marks, bt, nmarks); } if(cfg.vifm_info & VIFMINFO_BOOKMARKS) { write_bmarks(fp, bmarks, bmt, nbmarks); } if(cfg.vifm_info & VIFMINFO_TUI) { write_tui_state(fp); } if((cfg.vifm_info & VIFMINFO_DHISTORY) && cfg.history_len > 0) { write_view_history(fp, &lwin, "Left", LINE_TYPE_LWIN_HIST, nlh, lh, lhp); write_view_history(fp, &rwin, "Right", LINE_TYPE_RWIN_HIST, nrh, rh, rhp); } if(cfg.vifm_info & VIFMINFO_CHISTORY) { write_history(fp, "Command line", LINE_TYPE_CMDLINE_HIST, MIN(ncmdh, cfg.history_len - cfg.cmd_hist.pos), cmdh, &cfg.cmd_hist); } if(cfg.vifm_info & VIFMINFO_SHISTORY) { write_history(fp, "Search", LINE_TYPE_SEARCH_HIST, nsrch, srch, &cfg.search_hist); } if(cfg.vifm_info & VIFMINFO_PHISTORY) { write_history(fp, "Prompt", LINE_TYPE_PROMPT_HIST, nprompt, prompt, &cfg.prompt_hist); } if(cfg.vifm_info & VIFMINFO_FHISTORY) { write_history(fp, "Local filter", LINE_TYPE_FILTER_HIST, nfilter, filter, &cfg.filter_hist); } if(cfg.vifm_info & VIFMINFO_REGISTERS) { write_registers(fp, regs, nregs); } if(cfg.vifm_info & VIFMINFO_DIRSTACK) { write_dir_stack(fp, dir_stack, ndir_stack); } write_trash(fp, trash, ntrash); if(cfg.vifm_info & VIFMINFO_STATE) { write_general_state(fp); } if(cfg.vifm_info & VIFMINFO_CS) { fputs("\n# Color scheme:\n", fp); fprintf(fp, "c%s\n", cfg.cs.name); } fclose(fp); } free_string_array(ft, nft); free_string_array(fv, nfv); free_string_array(fx, nfx); free_string_array(cmds, ncmds); free_string_array(marks, nmarks); free_string_array(cmds_list, ncmds_list); free_string_array(lh, nlh); free_string_array(rh, nrh); free(lhp); free(rhp); free(bt); free(bmt); free_string_array(cmdh, ncmdh); free_string_array(srch, nsrch); free_string_array(regs, nregs); free_string_array(prompt, nprompt); free_string_array(filter, nfilter); free_string_array(trash, ntrash); free_string_array(bmarks, nbmarks); free_string_array(dir_stack, ndir_stack); free(non_conflicting_marks); }
static int add_swap( const char *what, struct mntent *me, bool noauto, bool nofail) { _cleanup_free_ char *name = NULL, *unit = NULL; _cleanup_fclose_ FILE *f = NULL; int r; assert(what); assert(me); if (access("/proc/swaps", F_OK) < 0) { log_info("Swap not supported, ignoring fstab swap entry for %s.", what); return 0; } if (detect_container() > 0) { log_info("Running in a container, ignoring fstab swap entry for %s.", what); return 0; } r = unit_name_from_path(what, ".swap", &name); if (r < 0) return log_error_errno(r, "Failed to generate unit name: %m"); unit = strjoin(arg_dest, "/", name); if (!unit) return log_oom(); f = fopen(unit, "wxe"); if (!f) return log_error_errno(errno, errno == EEXIST ? "Failed to create swap unit file %s, as it already exists. Duplicate entry in /etc/fstab?" : "Failed to create unit file %s: %m", unit); fputs_unlocked("# Automatically generated by systemd-fstab-generator\n\n" "[Unit]\n" "SourcePath=/etc/fstab\n" "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n" "[Swap]\n", f); r = write_what(f, what); if (r < 0) return r; r = write_options(f, me->mnt_opts); if (r < 0) return r; r = fflush_and_check(f); if (r < 0) return log_error_errno(r, "Failed to write unit file %s: %m", unit); /* use what as where, to have a nicer error message */ r = generator_write_timeouts(arg_dest, what, what, me->mnt_opts, NULL); if (r < 0) return r; if (!noauto) { r = generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET, nofail ? "wants" : "requires", name); if (r < 0) return r; } return 0; }
int gehash_dump(gehash_t * the_table, const char fname []) { int ii, jj, xx; int i, scroll_counter = 0; FILE * fp = f_subr_open(fname, "wb"); int maximum_bucket_size = 0; if (!fp) { SUBREADprintf ("Table file `%s' is not able to open.\n", fname); return -1; } if(the_table->version_number == SUBINDEX_VER2) { fwrite("2subindx",1,8,fp); write_options(fp, the_table); } fwrite(& (the_table -> current_items ), sizeof(long long int), 1, fp); fwrite(& (the_table -> buckets_number), sizeof(int), 1, fp); print_in_box(80,0,0,"Save current index block... "); for (i=0; i<the_table -> buckets_number; i++) { struct gehash_bucket * current_bucket = &(the_table -> buckets[i]); if(current_bucket -> current_items > maximum_bucket_size) maximum_bucket_size = current_bucket -> current_items; } #define SORT_LANE_NUMBER 16 short * sort_space_new_key[SORT_LANE_NUMBER]; gehash_data_t * sort_space_data [SORT_LANE_NUMBER]; int items_in_sort[SORT_LANE_NUMBER] ; int items_in_merge[SORT_LANE_NUMBER] ; if(the_table->version_number > SUBINDEX_VER0) { for(xx=0;xx<SORT_LANE_NUMBER;xx++) { sort_space_new_key[xx] = (short *)malloc(sizeof(short)*(maximum_bucket_size/SORT_LANE_NUMBER+2)); sort_space_data[xx] = (gehash_data_t *)malloc(sizeof(gehash_data_t)*(maximum_bucket_size/SORT_LANE_NUMBER+2)); } } for (i=0; i<the_table -> buckets_number; i++) { struct gehash_bucket * current_bucket = &(the_table -> buckets[i]); gehash_data_t tmp_val=0; if(i % (the_table -> buckets_number/10) == 0) print_window_scrolling_bar("", 1.0*i/the_table -> buckets_number, 74, &scroll_counter); if(current_bucket -> current_items>=1) { if(the_table->version_number == SUBINDEX_VER0) { for(ii=0;ii<current_bucket -> current_items -1; ii++) { gehash_key_t tmp_key; for (jj = ii+1; jj < current_bucket -> current_items; jj++) { if (current_bucket -> item_keys[ii] > current_bucket -> item_keys[jj]) { tmp_key = current_bucket -> item_keys[ii]; current_bucket -> item_keys[ii] = current_bucket -> item_keys[jj]; current_bucket -> item_keys[jj] = tmp_key; tmp_val = current_bucket -> item_values[ii]; current_bucket -> item_values[ii] = current_bucket -> item_values[jj]; current_bucket -> item_values[jj] = tmp_val; } } } } else { if(0){ for(ii=0;ii<current_bucket -> current_items -1; ii++) { short tmp_key; for (jj = ii+1; jj < current_bucket -> current_items; jj++) { if (current_bucket -> new_item_keys[ii] > current_bucket -> new_item_keys[jj]) { tmp_key = current_bucket -> new_item_keys[ii]; current_bucket -> new_item_keys[ii] = current_bucket -> new_item_keys[jj]; current_bucket -> new_item_keys[jj] = tmp_key; tmp_val = current_bucket -> item_values[ii]; current_bucket -> item_values[ii] = current_bucket -> item_values[jj]; current_bucket -> item_values[jj] = tmp_val; } } } } if(1){ memset(items_in_sort, 0, sizeof(int)*SORT_LANE_NUMBER); for(ii=0;ii<current_bucket -> current_items;ii++) { int sort_lane_no = ii%SORT_LANE_NUMBER; int xx_th_item = items_in_sort[sort_lane_no] ++; sort_space_new_key[sort_lane_no][xx_th_item] = current_bucket -> new_item_keys[ii]; sort_space_data[sort_lane_no][xx_th_item] = current_bucket -> item_values[ii]; } for(xx=0;xx<SORT_LANE_NUMBER;xx++) { for(ii=0;ii<items_in_sort[xx]-1; ii++) { for(jj = ii+1; jj < items_in_sort[xx]; jj++) { short tmp_key; if(sort_space_new_key[xx][ii] > sort_space_new_key[xx][jj]) { tmp_key = sort_space_new_key[xx][ii]; sort_space_new_key[xx][ii] = sort_space_new_key[xx][jj]; sort_space_new_key[xx][jj] = tmp_key; tmp_val = sort_space_data[xx][ii]; sort_space_data[xx][ii]=sort_space_data[xx][jj]; sort_space_data[xx][jj]=tmp_val; } } } } memset(items_in_merge, 0, sizeof(int)*SORT_LANE_NUMBER); for(ii=0;ii<current_bucket -> current_items;ii++) { int tmp_key=0x7fffffff; int selected_lane = 0; for(xx=0;xx<SORT_LANE_NUMBER;xx++) { int ii_in_xx = items_in_merge[xx]; if(ii_in_xx >= items_in_sort[xx]) continue; if(tmp_key>sort_space_new_key[xx][ii_in_xx]) { selected_lane=xx; tmp_key = sort_space_new_key[xx][ii_in_xx]; } } assert(tmp_key<0x10000); current_bucket -> new_item_keys[ii] = (short)tmp_key; current_bucket -> item_values[ii] = sort_space_data[selected_lane][items_in_merge[selected_lane]]; items_in_merge[selected_lane]++; //printf("V [%d] [%d] =%d\n",i , ii, tmp_key); } } } } fwrite(& (current_bucket -> current_items), sizeof(int), 1, fp); fwrite(& (current_bucket -> space_size), sizeof(int), 1, fp); if(the_table->version_number == SUBINDEX_VER0) fwrite(current_bucket -> item_keys, sizeof(gehash_key_t), current_bucket -> current_items, fp); else fwrite(current_bucket -> new_item_keys, sizeof(short), current_bucket -> current_items, fp); fwrite(current_bucket -> item_values, sizeof(gehash_data_t), current_bucket -> current_items, fp); } if(the_table->version_number > SUBINDEX_VER0) { for(xx=0;xx<SORT_LANE_NUMBER;xx++) { free(sort_space_new_key[xx]); free(sort_space_data[xx]); } } fwrite(&(the_table -> is_small_table), sizeof(char), 1, fp); fclose(fp); print_in_box(80,0,0,""); return 0; }
static INT_PTR CALLBACK options_window_proc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { (void)lParam; switch (uMsg) { case WM_INITDIALOG: { SendDlgItemMessage (hDlg, IDC_ENABLE_SMIME, BM_SETCHECK, !!opt.enable_smime, 0L); SendDlgItemMessage (hDlg, IDC_ENCRYPT_DEFAULT, BM_SETCHECK, !!opt.encrypt_default, 0L); SendDlgItemMessage (hDlg, IDC_SIGN_DEFAULT, BM_SETCHECK, !!opt.sign_default, 0L); #ifndef MIME_SEND SendDlgItemMessage (hDlg, IDC_MIME_UI, BM_SETCHECK, !!opt.mime_ui, 0L); #endif enable_disable_opts (hDlg); set_labels (hDlg); ShowWindow (GetDlgItem (hDlg, IDC_GPG_OPTIONS), opt.enable_debug ? SW_SHOW : SW_HIDE); } return 1; case WM_LBUTTONDOWN: { return 1; } case WM_COMMAND: switch (LOWORD (wParam)) { case IDOK: { opt.enable_smime = !!SendDlgItemMessage (hDlg, IDC_ENABLE_SMIME, BM_GETCHECK, 0, 0L); opt.encrypt_default = !!SendDlgItemMessage (hDlg, IDC_ENCRYPT_DEFAULT, BM_GETCHECK, 0, 0L); opt.sign_default = !!SendDlgItemMessage (hDlg, IDC_SIGN_DEFAULT, BM_GETCHECK, 0, 0L); #ifndef MIME_SEND int mime_ui_old = opt.mime_ui; opt.mime_ui = !!SendDlgItemMessage (hDlg, IDC_MIME_UI, BM_GETCHECK, 0, 0L); if (opt.mime_ui != mime_ui_old) { MessageBox (NULL, _("Changing the interface requires a restart of Outlook."), _("GpgOL"), MB_ICONINFORMATION|MB_OK); } #endif write_options (); EndDialog (hDlg, TRUE); break; } case IDC_GPG_CONF: engine_start_confdialog (hDlg); break; case IDC_GPG_OPTIONS: config_dialog_box (hDlg); break; } case WM_SYSCOMMAND: switch (LOWORD (wParam)) { case SC_CLOSE: EndDialog (hDlg, TRUE); } break; } return 0; }
static int add_swap( const char *what, struct mntent *me, MountpointFlags flags) { _cleanup_free_ char *name = NULL; _cleanup_fclose_ FILE *f = NULL; int r; assert(what); assert(me); if (access("/proc/swaps", F_OK) < 0) { log_info("Swap not supported, ignoring fstab swap entry for %s.", what); return 0; } if (detect_container() > 0) { log_info("Running in a container, ignoring fstab swap entry for %s.", what); return 0; } r = unit_name_from_path(what, ".swap", &name); if (r < 0) return log_error_errno(r, "Failed to generate unit name: %m"); r = generator_open_unit_file(arg_dest, "/etc/fstab", name, &f); if (r < 0) return r; fputs("# Automatically generated by systemd-fstab-generator\n\n" "[Unit]\n" "SourcePath=/etc/fstab\n" "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n" "[Swap]\n", f); r = write_what(f, what); if (r < 0) return r; r = write_options(f, me->mnt_opts); if (r < 0) return r; r = fflush_and_check(f); if (r < 0) return log_error_errno(r, "Failed to write unit file %s: %m", name); /* use what as where, to have a nicer error message */ r = generator_write_timeouts(arg_dest, what, what, me->mnt_opts, NULL); if (r < 0) return r; if (flags & MAKEFS) { r = generator_hook_up_mkswap(arg_dest, what); if (r < 0) return r; } if (flags & GROWFS) /* TODO: swap devices must be wiped and recreated */ log_warning("%s: growing swap devices is currently unsupported.", what); if (!(flags & NOAUTO)) { r = generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET, (flags & NOFAIL) ? "wants" : "requires", name); if (r < 0) return r; } return 0; }