void webcgi_init(char *query) { int nel; char *q, *end, *name, *value; if (htab.table) hdestroy_r(&htab); if (query == NULL) return; // cprintf("query = %s\n", query); end = query + strlen(query); q = query; nel = 1; while (strsep(&q, "&;")) { nel++; } hcreate_r(nel, &htab); for (q = query; q < end; ) { value = q; q += strlen(q) + 1; unescape(value); name = strsep(&value, "="); if (value) webcgi_set(name, value); } }
void init_cgi(char *query) { int len, nel; char *q, *name, *value; /* Clear variables */ if (!query) { hdestroy_r(&htab); return; } /* Parse into individual assignments */ q = query; len = strlen(query); nel = 1; while (strsep(&q, "&;")) nel++; hcreate_r(nel, &htab); for (q = query; q < (query + len);) { /* Unescape each assignment */ unescape(name = value = q); /* Skip to next assignment */ for (q += strlen(q); q < (query + len) && !*q; q++); /* Assign variable */ name = strsep(&value, "="); if (value) { // printf("set_cgi: name=%s, value=%s.\n", name , value); // N12 test set_cgi(name, value); } } }
void hash_destroy(struct hsearch_data * table) { check(table, "hash_destroy: table is NULL -> fail to destroy it"); hdestroy_r(table); error: return; }
void destroy_symtab(struct symtab *symtab) { if (!symtab) return; if (symtab->strs) free(symtab->strs); if (symtab->symbols) free(symtab->symbols); if (symtab->hash_table) { hdestroy_r(symtab->hash_table); free(symtab->hash_table); } free(symtab); }
void hdestroy(void) { /* Destroy global hash table if present. */ if (global_hashtable_initialized) { hdestroy_r(&global_hashtable); global_hashtable_initialized = false; } }
int main(int argc, char *argv[]) { int err, i; struct ibc_opts ibc_opts; struct inotify_event *evt; char buf[BUF_LEN], output[FILEPATH_BUF_SZ]; const char *fp; memset(&ibc, 0, sizeof(struct ibc)); err = hcreate_r(HTAB_SIZE, &ibc.htab); if (err == 0) { perror("hcreate_r"); goto hcreate_error; } err = parse_opts(&ibc_opts, argc, argv); if (err == -1) { goto parsing_error; } for (i = 0; i < argc; i++) memset(argv[i], 0, strlen(argv[i])); err = ibc.fd = inotify_init(); if (err == -1) { perror("inotify_init"); goto inotify_init_error; } err = add_watches(&ibc_opts); if (err == -1) { goto add_watches_error; } while(read(ibc.fd, buf, BUF_LEN) > 0) { evt = (struct inotify_event *) buf; fp = get_inotify_event_path(evt->wd, evt->name); if (fp) { snprintf(output, FILEPATH_BUF_SZ, "%s/%s", ibc_opts.output_dir, evt->name); cp(output, fp); } } add_watches_error: close(ibc.fd); inotify_init_error: free_opts(&ibc_opts); parsing_error: hcreate_error: hdestroy_r(&ibc.htab); return err; }
/** * free AIDE_CONTEXT */ void freeAideContext(AIDE_CONTEXT *ctx) { /* check */ if (ctx == NULL) { LOG(LOG_ERR, "ctx is NULL\n"); return; } DEBUG("freeAideContext %p \n", ctx); // DEBUG("aide_md_table_size = %d\n", ctx->aide_md_table_size); // DEBUG("aide_in_table_size = %d\n", ctx->aide_in_table_size); /* hash tables */ hdestroy_r(ctx->aide_md_table); hdestroy_r(ctx->aide_in_table); xfree(ctx->aide_md_table); xfree(ctx->aide_in_table); #ifdef CONFIG_SQLITE if (ctx->sqlite_db != NULL) { /* close DB */ sqlite3_close(ctx->sqlite_db); } #endif /* free metadata chain */ if (ctx->start != NULL) { freeAideMetadata(ctx->start); } /* free ignore list */ if (ctx->ignore_name_start != NULL) { // DEBUG("free tx->ignore_name_start\n"); freeAideIgnoreList(ctx->ignore_name_start); } xfree(ctx); return; }
void hashtable_destroy(hashtable *hash) { /* CRITICAL SECTION */ pthread_mutex_lock(&hash_mutex); if (hash) { hdestroy_r(hash); } pthread_mutex_unlock(&hash_mutex); /* CRITICAL SECTION */ return ; }
int ewf_hashtable_destroy( ewf_hashtable_t ** htab ) { if ( *htab == NULL ) { nbu_log_error( "hash table pointer is NULL" ); return EWF_ERROR; } hdestroy_r( *htab ); free( *htab ); nbu_log_debug( "hash table has been destroyed" ); return EWF_SUCCESS; }
static void sc_map_destroy() { // this frees all of the nodes' ep so we don't have to below hdestroy_r(&sc_map_htab); struct sc_map_entry *next = sc_map_entries->list; struct sc_map_entry *p = NULL; while (next != NULL) { p = next; next = p->next; free(p->e->key); free(p->e->data); free(p->e); free(p); } free(sc_map_entries); }
void init_cgi(char *query) { int len, nel; char *q, *name, *value; htab_count = 0; //cprintf("\nIn init_cgi(), query = %s\n", query); /* Clear variables */ if (!query) { hdestroy_r(&htab); return; } /* Parse into individual assignments */ q = query; len = strlen(query); nel = 1; while (strsep(&q, "&;")) nel++; hcreate_r(nel, &htab); //cprintf("\nIn init_cgi(), nel = %d\n", nel); for (q = query; q < (query + len);) { /* Unescape each assignment */ unescape(name = value = q); /* Skip to next assignment */ for (q += strlen(q); q < (query + len) && !*q; q++) ; /* Assign variable */ name = strsep(&value, "="); if (value) set_cgi(name, value); } //cprintf("\nIn init_cgi(), AFTER PROCESS query = %s\n", query); }
void free_hash_alignment(hash_alignment_block aln){ if(aln == NULL) return; free(aln->data); ENTRY *ret_val; // ENTRY search; int hc=0; for(int i = 0; i < aln->size; ++i){ ENTRY search={aln->species[i],NULL}; hc = hsearch_r(search,FIND,&ret_val,aln->sequences); if(hc == 0){ fprintf(stderr,"Failed to read hash table: %s\n", strerror(errno)); exit(1); } if(ret_val != NULL){ free(ret_val->key); free_sequence(ret_val->data); // free(ret_val); } }free(aln->species); hdestroy_r(aln->sequences); free(aln->sequences); free(aln); }
int himport_r(struct hsearch_data *htab, const char *env, size_t size, const char sep, int flag, int crlf_is_lf, int nvars, char * const vars[]) { char *data, *sp, *dp, *name, *value; char *localvars[nvars]; int i; /* Test for correct arguments. */ if (htab == NULL) { __set_errno(EINVAL); return 0; } /* we allocate new space to make sure we can write to the array */ if ((data = malloc(size + 1)) == NULL) { debug("himport_r: can't malloc %lu bytes\n", (ulong)size + 1); __set_errno(ENOMEM); return 0; } memcpy(data, env, size); data[size] = '\0'; dp = data; /* make a local copy of the list of variables */ if (nvars) memcpy(localvars, vars, sizeof(vars[0]) * nvars); if ((flag & H_NOCLEAR) == 0) { /* Destroy old hash table if one exists */ debug("Destroy Hash Table: %p table = %p\n", htab, htab->table); if (htab->table) hdestroy_r(htab); } /* * Create new hash table (if needed). The computation of the hash * table size is based on heuristics: in a sample of some 70+ * existing systems we found an average size of 39+ bytes per entry * in the environment (for the whole key=value pair). Assuming a * size of 8 per entry (= safety factor of ~5) should provide enough * safety margin for any existing environment definitions and still * allow for more than enough dynamic additions. Note that the * "size" argument is supposed to give the maximum environment size * (CONFIG_ENV_SIZE). This heuristics will result in * unreasonably large numbers (and thus memory footprint) for * big flash environments (>8,000 entries for 64 KB * environment size), so we clip it to a reasonable value. * On the other hand we need to add some more entries for free * space when importing very small buffers. Both boundaries can * be overwritten in the board config file if needed. */ if (!htab->table) { int nent = CONFIG_ENV_MIN_ENTRIES + size / 8; if (nent > CONFIG_ENV_MAX_ENTRIES) nent = CONFIG_ENV_MAX_ENTRIES; debug("Create Hash Table: N=%d\n", nent); if (hcreate_r(nent, htab) == 0) { free(data); return 0; } } if (!size) { free(data); return 1; /* everything OK */ } if(crlf_is_lf) { /* Remove Carriage Returns in front of Line Feeds */ unsigned ignored_crs = 0; for(;dp < data + size && *dp; ++dp) { if(*dp == '\r' && dp < data + size - 1 && *(dp+1) == '\n') ++ignored_crs; else *(dp-ignored_crs) = *dp; } size -= ignored_crs; dp = data; } /* Parse environment; allow for '\0' and 'sep' as separators */ do { ENTRY e, *rv; /* skip leading white space */ while (isblank(*dp)) ++dp; /* skip comment lines */ if (*dp == '#') { while (*dp && (*dp != sep)) ++dp; ++dp; continue; } /* parse name */ for (name = dp; *dp != '=' && *dp && *dp != sep; ++dp) ; /* deal with "name" and "name=" entries (delete var) */ if (*dp == '\0' || *(dp + 1) == '\0' || *dp == sep || *(dp + 1) == sep) { if (*dp == '=') *dp++ = '\0'; *dp++ = '\0'; /* terminate name */ debug("DELETE CANDIDATE: \"%s\"\n", name); if (!drop_var_from_set(name, nvars, localvars)) continue; if (hdelete_r(name, htab, flag) == 0) debug("DELETE ERROR ##############################\n"); continue; } *dp++ = '\0'; /* terminate name */ /* parse value; deal with escapes */ for (value = sp = dp; *dp && (*dp != sep); ++dp) { if ((*dp == '\\') && *(dp + 1)) ++dp; *sp++ = *dp; } *sp++ = '\0'; /* terminate value */ ++dp; if (*name == 0) { debug("INSERT: unable to use an empty key\n"); __set_errno(EINVAL); free(data); return 0; } /* Skip variables which are not supposed to be processed */ if (!drop_var_from_set(name, nvars, localvars)) continue; /* enter into hash table */ e.key = name; e.data = value; hsearch_r(e, ENTER, &rv, htab, flag); if (rv == NULL) printf("himport_r: can't insert \"%s=%s\" into hash table\n", name, value); debug("INSERT: table %p, filled %d/%d rv %p ==> name=\"%s\" value=\"%s\"\n", htab, htab->filled, htab->size, rv, name, value); } while ((dp < data + size) && *dp); /* size check needed for text */ /* without '\0' termination */ debug("INSERT: free(data = %p)\n", data); free(data); /* process variables which were not considered */ for (i = 0; i < nvars; i++) { if (localvars[i] == NULL) continue; /* * All variables which were not deleted from the variable list * were not present in the imported env * This could mean two things: * a) if the variable was present in current env, we delete it * b) if the variable was not present in current env, we notify * it might be a typo */ if (hdelete_r(localvars[i], htab, flag) == 0) printf("WARNING: '%s' neither in running nor in imported env!\n", localvars[i]); else printf("WARNING: '%s' not in imported env, deleting it!\n", localvars[i]); } debug("INSERT: done\n"); return 1; /* everything OK */ }
int himport_r(struct hsearch_data *htab, const char *env, size_t size, const char sep, int flag) { char *data, *sp, *dp, *name, *value; /* Test for correct arguments. */ if (htab == NULL) { __set_errno(EINVAL); return 0; } /* we allocate new space to make sure we can write to the array */ if ((data = malloc(size)) == NULL) { debug("himport_r: can't malloc %d bytes\n", size); __set_errno(ENOMEM); return 0; } memcpy(data, env, size); dp = data; if ((flag & H_NOCLEAR) == 0) { /* Destroy old hash table if one exists */ debug("Destroy Hash Table: %p table = %p\n", htab, htab->table); if (htab->table) hdestroy_r(htab); } /* * Create new hash table (if needed). The computation of the hash * table size is based on heuristics: in a sample of some 70+ * existing systems we found an average size of 39+ bytes per entry * in the environment (for the whole key=value pair). Assuming a * size of 8 per entry (= safety factor of ~5) should provide enough * safety margin for any existing environment definitions and still * allow for more than enough dynamic additions. Note that the * "size" argument is supposed to give the maximum enviroment size * (CONFIG_ENV_SIZE). This heuristics will result in * unreasonably large numbers (and thus memory footprint) for * big flash environments (>8,000 entries for 64 KB * envrionment size), so we clip it to a reasonable value. * On the other hand we need to add some more entries for free * space when importing very small buffers. Both boundaries can * be overwritten in the board config file if needed. */ if (!htab->table) { int nent = CONFIG_ENV_MIN_ENTRIES + size / 8; if (nent > CONFIG_ENV_MAX_ENTRIES) nent = CONFIG_ENV_MAX_ENTRIES; debug("Create Hash Table: N=%d\n", nent); if (hcreate_r(nent, htab) == 0) { free(data); return 0; } } /* Parse environment; allow for '\0' and 'sep' as separators */ do { ENTRY e, *rv; /* skip leading white space */ while ((*dp == ' ') || (*dp == '\t')) ++dp; /* skip comment lines */ if (*dp == '#') { while (*dp && (*dp != sep)) ++dp; ++dp; continue; } /* parse name */ for (name = dp; *dp != '=' && *dp && *dp != sep; ++dp) ; /* deal with "name" and "name=" entries (delete var) */ if (*dp == '\0' || *(dp + 1) == '\0' || *dp == sep || *(dp + 1) == sep) { if (*dp == '=') *dp++ = '\0'; *dp++ = '\0'; /* terminate name */ debug("DELETE CANDIDATE: \"%s\"\n", name); if (hdelete_r(name, htab) == 0) debug("DELETE ERROR ##############################\n"); continue; } *dp++ = '\0'; /* terminate name */ /* parse value; deal with escapes */ for (value = sp = dp; *dp && (*dp != sep); ++dp) { if ((*dp == '\\') && *(dp + 1)) ++dp; *sp++ = *dp; } *sp++ = '\0'; /* terminate value */ ++dp; /* enter into hash table */ e.key = name; e.data = value; hsearch_r(e, ENTER, &rv, htab); if (rv == NULL) { printf("himport_r: can't insert \"%s=%s\" into hash table\n", name, value); return 0; } debug("INSERT: table %p, filled %d/%d rv %p ==> name=\"%s\" value=\"%s\"\n", htab, htab->filled, htab->size, rv, name, value); } while ((dp < data + size) && *dp); /* size check needed for text */ /* without '\0' termination */ debug("INSERT: free(data = %p)\n", data); free(data); debug("INSERT: done\n"); return 1; /* everything OK */ }
void hdestroy (void) { hdestroy_r(the_global_hsearch_data); }
/***************************************************************************** 函 数 名 : subdesc_free 功能描述 : 释放SUB_ROOT结构内存, 同时释放root本身 输入参数 : root 结构指针 输出参数 : 无 返 回 值 : 无 调用函数 : 被调函数 : ============================================================================ 修改历史 : 1.日 期 : 2008年8月26日 修改内容 : 新生成函数 *****************************************************************************/ void subdesc_free(SUB_ROOT * root) { int i, j; /*释放hash内存*/ if(NULL != root->_hashtable) { /*销毁hash表*/ hdestroy_r((struct hsearch_data *)(root->_hashtable)); free(root->_hashtable); } /*pre_treatment*/ if(root->pre_treatment.length >0 && root->pre_treatment.prefunctions != NULL) { for(i=0;i<root->pre_treatment.length;i++) { SUB_PREFUNCTION * p = &(root->pre_treatment.prefunctions[i]); subdesc_xmlFree((unsigned char*)p->func_so_file); subdesc_xmlFree((unsigned char*)p->func_name); } free(root->pre_treatment.prefunctions); } /*all_action*/ if(root->action_list.length >0 && root->action_list.actions != NULL) { for(i=0;i<root->action_list.length;i++) { SUB_ACTION * p = &(root->action_list.actions[i]); subdesc_xmlFree((unsigned char*)p->attr_name); subdesc_xmlFree((unsigned char*)p->attr_type); subdesc_xmlFree((unsigned char*)p->act_url); subdesc_xmlFree((unsigned char*)p->act_url_type); subdesc_xmlFree((unsigned char*)p->act_msg_key); subdesc_xmlFree((unsigned char*)p->act_fun_name); subdesc_xmlFree((unsigned char*)p->act_fun_file); subdesc_xmlFree((unsigned char*)p->act_fun_para); subdesc_xmlFree((unsigned char*)p->act_window); /*return_para*/ if(root->action_list.actions[i].return_para_len > 0 && root->action_list.actions[i].return_para != NULL) { for(j=0;j<root->action_list.actions[i].return_para_len;j++) { SUB_ACTION_PARAM * pp = &(root->action_list.actions[i].return_para[j]); subdesc_xmlFree((unsigned char*)pp->attr_name); subdesc_xmlFree((unsigned char*)pp->attr_type); subdesc_xmlFree((unsigned char*)pp->attr_request_name); subdesc_xmlFree((unsigned char*)pp->attr_ws_method_name); subdesc_xmlFree((unsigned char*)pp->attr_value); } free(root->action_list.actions[i].return_para); } } free(root->action_list.actions); } /*all_function*/ if(root->function_list.length >0 && root->function_list.functions != NULL) { for(i=0;i<root->function_list.length;i++) { SUB_FUNCTION * p = &(root->function_list.functions[i]); subdesc_xmlFree((unsigned char*)p->attr_name); subdesc_xmlFree((unsigned char*)p->attr_type); subdesc_xmlFree((unsigned char*)p->func_ws_method); subdesc_xmlFree((unsigned char*)p->func_ws_file); subdesc_xmlFree((unsigned char*)p->func_succ_act); subdesc_xmlFree((unsigned char*)p->func_fail_act); subdesc_xmlFree((unsigned char*)p->func_name); subdesc_xmlFree((unsigned char*)p->func_so_file); subdesc_xmlFree((unsigned char*)p->func_ws_no_ha); subdesc_xmlFree((unsigned char*)p->func_ws_no_vsm); } free(root->function_list.functions); } /*config_item_list*/ if(root->config_item_list.length >0 && root->config_item_list.parameters != NULL) { for(i=0;i<root->config_item_list.length;i++) { SUB_PARAMETER * p = &(root->config_item_list.parameters[i]); subdesc_xmlFree((unsigned char*)p->attr_name); subdesc_xmlFree((unsigned char*)p->attr_type); subdesc_xmlFree((unsigned char*)p->ci_source_name); subdesc_xmlFree((unsigned char*)p->ci_ws_name); subdesc_xmlFree((unsigned char*)p->ci_type); subdesc_xmlFree((unsigned char*)p->ci_checkbox); subdesc_xmlFree((unsigned char*)p->ci_min); subdesc_xmlFree((unsigned char*)p->ci_max); subdesc_xmlFree((unsigned char*)p->ci_err_key); subdesc_xmlFree((unsigned char*)p->ci_const_val); } free(root->config_item_list.parameters); } /*execution_list*/ if(root->execution_list.length >0 && root->execution_list.clues != NULL) { for(i=0;i<root->execution_list.length;i++) { SUB_CLUE * p = &(root->execution_list.clues[i]); subdesc_xmlFree((unsigned char*)p->exe_path); } free(root->execution_list.clues); } /*other*/ subdesc_xmlFree((unsigned char*)root->result.fail_result); subdesc_xmlFree((unsigned char*)root->result.succ_result); free(root); }
void jrst_filter_finalize (void) { hdestroy_r (&classes); hdestroy_r (&methods); }
/* * MAIN */ int main(int argc, char *argv[]) { int sock_fd; int err; int optval; int conn; struct sockaddr_in *addr, *client_addr; socklen_t client_addr_size; struct hsearch_data *htab; pthread_t thread; thdata *thread_data = malloc(sizeof(thdata)); // Create hashmap storage htab = calloc(1, sizeof(struct hsearch_data)); if (hcreate_r(10000, htab) == -1) { printf("Error on hcreate\n"); } // Create socket sock_fd = socket(AF_INET, SOCK_STREAM, 0); if (sock_fd == -1) { printf("Error creating socket: %d\n", errno); } // Allow address reuse optval = 1; err = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int)); if (err == -1) { printf("Error setting SO_REUSEADDR on socket: %d\n", errno); } // bind addr = calloc(1, sizeof(struct sockaddr_in)); addr->sin_family = AF_INET; addr->sin_port = htons(11211); // htons: Convert to network byte order addr->sin_addr.s_addr = INADDR_ANY; err = bind(sock_fd, (struct sockaddr *)addr, sizeof(struct sockaddr)); free(addr); if (err == -1) { printf("bind error: %d\n", errno); } err = listen(sock_fd, 1); if (err == -1) { printf("listen error: %d\n", errno); } if (is_single(argc, argv)) { client_addr = malloc(sizeof(struct sockaddr_in)); client_addr_size = sizeof(struct sockaddr); conn = accept(sock_fd, (struct sockaddr *)client_addr, &client_addr_size); free(client_addr); thread_data->conn = conn; thread_data->htab = htab; handle_conn(thread_data); close(conn); } else { while (1) { client_addr = malloc(sizeof(struct sockaddr_in)); client_addr_size = sizeof(struct sockaddr); conn = accept( sock_fd, (struct sockaddr *)client_addr, &client_addr_size); free(client_addr); thread_data->conn = conn; thread_data->htab = htab; pthread_create( &thread, NULL, handle_conn, thread_data); } } close(sock_fd); free(thread_data); hdestroy_r(htab); free(htab); return 0; }
int main (int argc, char *argv[]) { char line[1000000]; FILE *in, *out; int line_length; int total_num_root_entities; int total_num_sub_entities; int num_entities; unsigned long *temp; int index; int ret; int roots, subs; int json = 1; if (hcreate_r (200000, &htab) == 0) { perror ("Failed to create hash"); exit (1); } struct entity *entities = 0; in = fopen ("in.txt", "r"); out = fopen ("out.txt", "w"); num_entities = -1; while (1 == fscanf (in, "%[^\n]%n\n", line, &line_length)) { //read one line char *word; struct entity *focal_root = 0; //Used when the Root Entity already exists char *ptr; { char *root = strtok_r (line, ",", &ptr); int i = 0; // First check whether the entry already exists: focal_root = find_entity (entities, num_entities, root); if (focal_root == NULL) { // Initialise this Root Entity: focal_root = add_ent (root); fprintf (out, "%s\n", root); total_num_root_entities++; } } for (; word = strtok_r (NULL, ",", &ptr);) { struct entity *entity = 0; //First check whether the entity already exists : entity = find_entity (entities, num_entities, word); if (entity == NULL) { //Initialise this Sub Entity: entity = add_ent (word); } // Now link the Sub Entity to the focal_root_entity using the index of the entity arrays : add_link (focal_root, entity); add_link (entity, focal_root); // Echo the word to the o/p file : fprintf (out, "%s\n", word); } // End for pos } fclose (out); fclose (in); // Now print out the entire set of Entities: if (json) printf ("[\n"); { int i; roots = subs = 0; for (i = 0; i <= num_entities; i++) { int j; if (entities[i].num_links >= 0) { if (!json) printf ("Root Entity '%s' discovered with %d sub links\n", entities[i].entity_name, entities[i].num_links); roots++; for (j = 0; j <= entities[i].num_links; j++) { if (((i > 0) || (j > 0)) && (json)) printf (","); if (!json) printf ("Sub Entity is %s\n", entities[entities[i].links[j]].entity_name); if (json) printf ("{\n \"source\" : \"%s\",\n \"target\" : \"%s\",\n \"type\" : \"suit\"\n}\n", entities[i].entity_name, entities[entities[i].links[j]].entity_name); subs++; } printf ("\n"); } } } if (json) printf ("]\n"); if (!json) { printf ("The number of root entities found were %d and the number of subs found were %d\n", roots, subs); printf ("The total number of Entities are %d\n", num_entities); printf ("The total number of Root Entities are %d\n", total_num_root_entities); printf ("The total number of Sub Entities are %d\n", total_num_sub_entities); } hdestroy_r (&htab); return (0); // fwrite the array of structs out to save them: out = fopen ("entities.bin", "wb"); ret = fwrite (entities, sizeof (entities), 1, out); fclose (out); // fread the array of structs in test: in = fopen ("entities.bin", "rb"); ret = fread (entities, sizeof (entities), 1, in); fclose (in); return 0; }
/* void __hdestroy (void) */ void hdestroy (void) { hdestroy_r (&htab); }