static int add_watch(const char* path, watch_node* parent) { int wd = inotify_add_watch(inotify_fd, path, IN_MODIFY | IN_ATTRIB | IN_CREATE | IN_DELETE | IN_MOVE | IN_DELETE_SELF); if (wd < 0) { if (errno == ENOSPC) { limit_reached = true; } userlog(LOG_ERR, "inotify_add_watch(%s): %s", path, strerror(errno)); return ERR_CONTINUE; } else { userlog(LOG_DEBUG, "watching %s: %d", path, wd); } watch_node* node = table_get(watches, wd); if (node != NULL) { if (node->wd != wd || strcmp(node->name, path) != 0) { char buf1[PATH_MAX], buf2[PATH_MAX]; const char* normalized1 = realpath(node->name, buf1); const char* normalized2 = realpath(path, buf2); if (normalized1 == NULL || normalized2 == NULL || strcmp(normalized1, normalized2) != 0) { userlog(LOG_ERR, "table error: collision at %d (new %s, existing %s)", wd, path, node->name); return ERR_ABORT; } else { userlog(LOG_WARNING, "intersection at %d: (new %s, existing %s, real %s)", wd, path, node->name, normalized1); return ERR_IGNORE; } } return wd; } node = malloc(sizeof(watch_node)); CHECK_NULL(node); node->name = strdup(path); CHECK_NULL(node->name); node->wd = wd; node->parent = parent; node->kids = NULL; if (parent != NULL) { if (parent->kids == NULL) { parent->kids = array_create(DEFAULT_SUBDIR_COUNT); CHECK_NULL(parent->kids); } CHECK_NULL(array_push(parent->kids, node)); } if (table_put(watches, wd, node) == NULL) { userlog(LOG_ERR, "table error: unable to put (%d:%s)", wd, path); return ERR_ABORT; } return wd; }
static int walk_tree(int path_len, watch_node* parent, bool recursive, array* mounts) { for (int j=0; j<array_size(mounts); j++) { char* mount = array_get(mounts, j); if (strncmp(path_buf, mount, strlen(mount)) == 0) { userlog(LOG_DEBUG, "watch path '%s' crossed mount point '%s' - skipping", path_buf, mount); return ERR_IGNORE; } } DIR* dir = NULL; if (recursive) { if ((dir = opendir(path_buf)) == NULL) { if (errno == EACCES || errno == ENOENT || errno == ENOTDIR) { userlog(LOG_DEBUG, "opendir(%s): %d", path_buf, errno); return ERR_IGNORE; } else { userlog(LOG_ERR, "opendir(%s): %s", path_buf, strerror(errno)); return ERR_CONTINUE; } } } int id = add_watch(path_len, parent); if (dir == NULL) { return id; } else if (id < 0) { closedir(dir); return id; } path_buf[path_len] = '/'; struct dirent* entry; while ((entry = readdir(dir)) != NULL) { if (entry->d_type != DT_DIR || strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } int name_len = strlen(entry->d_name); memcpy(path_buf + path_len + 1, entry->d_name, name_len + 1); int subdir_id = walk_tree(path_len + 1 + name_len, table_get(watches, id), recursive, mounts); if (subdir_id < 0 && subdir_id != ERR_IGNORE) { rm_watch(id, true); id = subdir_id; break; } } closedir(dir); return id; }
void print_columns(struct Table *table) { struct Vector* vector = table_get(table, 0); for(int i = 0; i < vector->length; i++) { printf("'%s' ", vector->data[i]); } printf("\n"); }
static int dict_filter(lua_State *L) { if(!g_dict) { return luaL_error(L, "need open first"); } Table* dict = g_dict; luaL_checktype(L, 1, LUA_TTABLE); size_t len = lua_rawlen(L,1); size_t i,j; int flag = 0; for(i=1;i<=len;) { TableNode *node = NULL; int step = 0; for(j=i;j<=len;j++) { lua_rawgeti(L, 1, j); uint32_t rune = (uint32_t)lua_tointeger(L, -1); lua_pop(L, 1); if(node == NULL) { node = table_get(dict, rune); } else { node = table_get(node->value, rune); } if(node && node->flag == 'o') step = j - i + 1; if(!(node && node->value)) break; } if(step > 0) { for(j=0;j<step;j++) { lua_pushinteger(L, '*'); lua_rawseti(L, 1, i+j); } flag = 1; i = i + step; } else { i++; } } lua_pushboolean(L, flag); return 1; }
int table_put(Pair *table, int *top, char *name, int value) { Pair *pair = table_get(table, *top, name); if (pair == NULL) { pair = &table[*top]; pair->value = value; pair->name = strdup(name); (*top)++; return 1; } return 0; }
UINT16 translate(char *label, char *op, char *param) { Pair *op1 = table_get(opTable, opTop, op); if (op == NULL) { printf("Error: op(%s) not found!\n", op); exit(0); } UINT16 code = 0; if (op1->value == WORD) { sscanf(param, "%hu", &code); } else { Pair *sym = table_get(symTable, symTop, param); if (sym == NULL) { printf("Error: symbol(%s) not found!\n", param); exit(0); } UINT16 paddress = sym->value; code = (op1->value << 12)|paddress; } return code; }
int hashFunc(obj * key_obj){ if(key_obj->type == 's' || key_obj->type == 'n') return key_obj->hash; obj * h_fun = table_get(key_obj->dict, m_hash); if (h_fun != NULL) { obj *key = call_func(key_obj, h_fun, 0, NULL); if(key != NULL) { return (int) key->integer; } } return (long)key_obj; }
int php3_init_request_info(void *conf) { const char *buf; TLS_VARS; GLOBAL(request_info).current_user = NULL; GLOBAL(request_info).current_user_length = 0; /* see above for why this has to be estrdup()'d */ GLOBAL(request_info).filename = estrdup(GLOBAL(php3_rqst)->filename); GLOBAL(request_info).request_method = GLOBAL(php3_rqst)->method; GLOBAL(request_info).query_string = GLOBAL(php3_rqst)->args; GLOBAL(request_info).content_type = table_get(GLOBAL(php3_rqst)->subprocess_env, "CONTENT_TYPE"); buf = table_get(GLOBAL(php3_rqst)->subprocess_env, "CONTENT_LENGTH"); GLOBAL(request_info).content_length = (buf ? atoi(buf) : 0); GLOBAL(request_info).cookies = table_get(GLOBAL(php3_rqst)->subprocess_env, "HTTP_COOKIE"); return SUCCESS; }
static int walk_tree(const char* path, watch_node* parent, array* ignores) { if (is_ignored(path, ignores)) { return ERR_IGNORE; } DIR* dir = opendir(path); if (dir == NULL) { if (errno == EACCES) { return ERR_IGNORE; } else if (errno == ENOTDIR) { // flat root return add_watch(path, parent); } userlog(LOG_ERR, "opendir(%s): %s", path, strerror(errno)); return ERR_CONTINUE; } int id = add_watch(path, parent); if (id < 0) { closedir(dir); return id; } struct dirent* entry; char subdir[PATH_MAX]; strcpy(subdir, path); if (subdir[strlen(subdir) - 1] != '/') { strcat(subdir, "/"); } char* p = subdir + strlen(subdir); while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } strcpy(p, entry->d_name); if (!is_directory(entry, subdir)) { continue; } int subdir_id = walk_tree(subdir, table_get(watches, id), ignores); if (subdir_id < 0 && subdir_id != ERR_IGNORE) { rm_watch(id, true); id = subdir_id; break; } } closedir(dir); return id; }
static int filter_word(lua_State *L) { Table* dict = check_crab_obj(L, 1); luaL_checktype(L, 2, LUA_TTABLE); size_t len = lua_rawlen(L, 2); size_t i,j; int flag = 0; for(i=1;i<=len;) { TableNode *node = NULL; int step = 0; for(j=i;j<=len;j++) { lua_rawgeti(L, 2, j); uint32_t rune = lua_tounsigned(L, -1); lua_pop(L, 1); if(node == NULL) { node = table_get(dict, rune); } else { node = table_get((Table*)node->value, rune); } if(node && node->flag == 'o') step = j - i + 1; if(!(node && node->value)) break; } if(step > 0) { for(j=0;j<step;j++) { lua_pushinteger(L, '*'); lua_rawseti(L, 2, i+j); } flag = 1; i = i + step; } else { i++; } } lua_pushboolean(L, flag); return 1; }
int main() { struct table *shapes; int loops, planecount, pointcount, x, y, z; /* Get number of bulks to process. */ scanf("%d", &loops); while(loops--) { /* allocate the shapes table and create the * initial shape. */ shapes = table_new(shape_comparator); struct shape *newshape = shape_alloc(); table_add(shapes, newshape); /* Get the plane count. */ scanf("%d", &planecount); while(planecount--) { /* Create a new plane and add it to the plane table inside * the shape. */ struct plane *newplane = plane_alloc(); table_add(newshape->planes, newplane); /* Get the point count. */ scanf("%d", &pointcount); while(pointcount--) { /* Add point to the shape. */ scanf("%d %d %d", &x, &y, &z); struct point *newpoint = point_alloc(x, y, z); if (!table_add(newshape->points, newpoint)) { int loc; table_find(newshape->points, newpoint, &loc); free(newpoint); newpoint = table_get(newshape->points, loc); } /* Add mapping between plane and point. */ table_add(newshape->ppmaps, ppmap_alloc(newplane, newpoint)); } /* Add the plane to the planes table inside shape. */ table_add(newshape->planes, newplane); } resolve_planes(shapes); resolve_shapes(shapes); coallece_planes(shapes); } }
Array * page_rank(Table * inbound, unsigned int order, float alpha, float convergence, unsigned int max_times) { register int t = 0, k, i; register float norm2 = 1.0, prod_scalar, x; unsigned int total_size = order; Array * vector = initial(total_size); Array * new_vector = initial(total_size); Array * tmp; x = 1.0 - alpha; while (t < max_times && norm2 >= convergence) { t++; norm2 = 0.0; prod_scalar = 0.0; for (k = 0; k < total_size; k++) { if (is_sink(inbound, k)) prod_scalar += array_get(vector, k) * alpha; } prod_scalar += x; for (k = 0; k < total_size; k++) { array_set(new_vector, k, prod_scalar); if (!is_sink(inbound, k)) { tmp = table_get(inbound, k); for (i = 0; i < array_len(tmp); i++) { array_incr(new_vector, k, array_get(vector, (int)array_get(tmp, i)) * alpha); } } norm2 += (array_get(new_vector, k) - array_get(vector, k)) * (array_get(new_vector, k) - array_get(vector, k)); } t++; norm2 = sqrt((double)norm2) / total_size; array_copy(vector, new_vector); } array_delete(new_vector); return normalize(vector); }
/* * constructs a default set for configuration variables * * conf_preset() cannot be implemented with conf_set() since they differ in handling an omitted * section name. conf_section() cannot affect conf_preset() because the former is not callable * before the latter. Thus, conf_preset() can safely assume that a section name is given properly * whenever necessary. On the other hand, because conf_set() is affected by conf_section(), a * variable name without a section name and a period should be recognized as referring to the * current section, not to the global section. * * TODO: * - some adjustment on arguments to table_new() is necessary; * - considering changes to the format of a configuration file as a program to accept it is * upgraded, making it a recoverable error to encounter a non-preset section or variable name * would be useful; this enables an old version of the program to accept a new configuration * file with diagnostics. */ int (conf_preset)(const conf_t *tab, int ctrl) { size_t len; char *pbuf, *sec, *var; const char *hkey; table_t *t; struct valnode_t *pnode; assert(!section); assert(tab); control = ctrl; section = table_new(0, NULL, NULL); errcode = CONF_ERR_OK; for (; tab->var; tab++) { assert(tab->defval); assert(tab->type == CONF_TYPE_BOOL || tab->type == CONF_TYPE_INT || tab->type == CONF_TYPE_UINT || tab->type == CONF_TYPE_REAL || tab->type == CONF_TYPE_STR); len = strlen(tab->var); pbuf = strcpy(MEM_ALLOC(len+1 + strlen(tab->defval)+1), tab->var); if (sep(pbuf, &sec, &var) != CONF_ERR_OK) { MEM_FREE(pbuf); break; /* sep sets errcode */ } if (!sec) /* means global section in this case */ sec = ""; /* lower modifies sec only when necessary */ hkey = hash_string((control & CONF_OPT_CASE)? sec: lower(sec)); t = table_get(section, hkey); if (!t) { /* new section */ t = table_new(0, NULL, NULL); table_put(section, hkey, t); } MEM_NEW(pnode); pnode->type = tab->type; pnode->freep = pbuf; pnode->val = strcpy(pbuf + len+1, tab->defval); table_put(t, hash_string((control & CONF_OPT_CASE)? var: lower(var)), pnode); } preset = 1; return errcode; }
/* * This function implements the "target = object[key]" syntax in the language. * The semantics depend on the type of target, but the idea is that the sub-element with specified * @key in @object will be put in in @target */ kj_internal void vm_get_property(vm_t *vm, value_t *target, value_t const *object, value_t const *key) { switch (object->type) { case KJ_TYPE_TABLE: value_set(target, vm->allocator, table_get(&object->table->table, key)); break; case KJ_TYPE_STRING: // #todo add short string optimization value_new_string(target, vm->allocator, 1); target->string->data[0] = object->string->data[value_to_int(key)]; target->string->data[1] = '\0'; break; default: vm_throw(vm, "invalid get operation performed on value of type %s.", VALUE_TYPE_STRING[object->type]); } }
void ws(char *name, FILE *fp) { table_t table ; char buf[128]; char *word; int *count; void **ar; int i; table = NULL; word = NULL; ar = NULL; memset(buf, 0, sizeof(buf)); table = table_new(4096, cmpstring, hashcode); while(getword(fp, buf, sizeof(buf))) { word = (char *) mem_calloc(1, strlen(buf) + 1); assert(word); strcpy(word, buf); count = table_get(table, word); if(count) { (*count)++; }else { count = (int *) mem_calloc(1, sizeof(*count)); assert(count); *count = 1; table_put(table, word, count); } }/*while*/ if(name) printf("%s:\n",name); printf("table has keys:%d\n", table_length(table)); ar = table_to_array(table, NULL); qsort(ar, table_length(table), 2 * sizeof(*ar), cmp); for(i = 0; ar[i]; i += 2) { printf("%d\t%s\n", *(int *)ar[i+1], (char *)ar[i]); } }
static bool process_inotify_event(struct inotify_event* event) { watch_node* node = table_get(watches, event->wd); if (node == NULL) { return true; } bool is_dir = (event->mask & IN_ISDIR) == IN_ISDIR; userlog(LOG_DEBUG, "inotify: wd=%d mask=%d dir=%d name=%s", event->wd, event->mask & (~IN_ISDIR), is_dir, node->path); memcpy(path_buf, node->path, node->path_len + 1); int path_len = node->path_len; if (event->len > 0) { path_buf[node->path_len] = '/'; int name_len = strlen(event->name); memcpy(path_buf + path_len + 1, event->name, name_len + 1); path_len += name_len + 1; } if (is_dir && event->mask & (IN_CREATE | IN_MOVED_TO)) { int result = walk_tree(path_len, node, true, NULL); if (result < 0 && result != ERR_IGNORE && result != ERR_CONTINUE) { return false; } } if (is_dir && event->mask & (IN_DELETE | IN_MOVED_FROM)) { for (int i=0; i<array_size(node->kids); i++) { watch_node* kid = array_get(node->kids, i); if (kid != NULL && strncmp(path_buf, kid->path, kid->path_len) == 0) { rm_watch(kid->wd, false); array_put(node->kids, i, NULL); break; } } } if (callback != NULL) { (*callback)(path_buf, event->mask); } return true; }
static bool process_inotify_event(struct inotify_event* event) { watch_node* node = table_get(watches, event->wd); if (node == NULL) { return true; } userlog(LOG_DEBUG, "inotify: wd=%d mask=%d dir=%d name=%s", event->wd, event->mask & (~IN_ISDIR), (event->mask & IN_ISDIR) != 0, node->name); char path[PATH_MAX]; strcpy(path, node->name); if (event->len > 0) { if (path[strlen(path) - 1] != '/') { strcat(path, "/"); } strcat(path, event->name); } if ((event->mask & IN_CREATE || event->mask & IN_MOVED_TO) && event->mask & IN_ISDIR) { int result = walk_tree(path, node, NULL); if (result < 0 && result != ERR_IGNORE) { return false; } } if ((event->mask & IN_DELETE || event->mask & IN_MOVED_FROM) && event->mask & IN_ISDIR) { for (int i=0; i<array_size(node->kids); i++) { watch_node* kid = array_get(node->kids, i); if (kid != NULL && strcmp(kid->name, path) == 0) { rm_watch(kid->wd, false); array_put(node->kids, i, NULL); break; } } } if (callback != NULL) { (*callback)(path, event->mask); } return true; }
static void parse_login(const char *bytes, size_t len, struct user *user, int kq) { if (user->name != NULL) { cmderr(user, ULOGGED, kq); return; } if (*bytes == '#') { cmderr(user, USHARP, kq); return; } if (illegal_name(bytes, len, user, kq, UEMPTY, UILLEGAL)) return; if (table_get(users, bytes, len)) { cmderr(user, UUSED, kq); return; } user->name = malloc_or_die(len); bcopy(bytes, user->name, len); user->namelen = len; table_put(users, user->name, user->namelen, user); ok(user, kq); }
void wf(const char *name, FILE *fp) { table_t table = table_new(0, NULL, NULL); char buf[BUFSIZ]; while (getword(fp, buf, sizeof(buf), first, rest)) { const char *word; int i, *count; for (i=0; buf[i] != '\0'; i++) buf[i] = tolower(buf[i]); word = atom_string(buf); count = table_get(table, word); if (count) { (*count) ++; } else { count = (int *)zalloc(sizeof (*count)); *count = 1; table_put(table, word, count); } } if (name) { printf("%s:\n", name); } /* print the words */ int i; void **array = table_to_array(table, NULL); qsort(array, table_length(table), 2*sizeof(*array), cmp); for (i = 0; array[i]; i+=2) { printf("%d\t%s\n", *(int *)array[i+1], (char *)array[i]); } zfree(array); /* destroy the table */ table_free(&table, vfree); }
void test_bridge_table_id_iter(void) { dp_bridge_iter_t iter; uint8_t ids[] = { 0, 5, 10 }; uint8_t id; int i; lagopus_result_t rv; iter = NULL; TEST_ASSERT_EQUAL(dp_bridge_table_id_iter_create(bridge_name, &iter), LAGOPUS_RESULT_OK); TEST_ASSERT_NOT_NULL(iter); for (i = 0; i < sizeof(ids) / sizeof(ids[0]); i++) { TEST_ASSERT_NOT_NULL(table_get(bridge->flowdb, ids[i])); } i = 0; while ((rv = dp_bridge_table_id_iter_get(iter, &id)) == LAGOPUS_RESULT_OK) { TEST_ASSERT_NOT_EQUAL(i, sizeof(ids) / sizeof(ids[0])); TEST_ASSERT_EQUAL(id, ids[i]); i++; } TEST_ASSERT_EQUAL(rv, LAGOPUS_RESULT_EOF); dp_bridge_table_id_iter_destroy(iter); }
const char* menu_current_entry(menu* pMenu) { const char *str; table_get(pMenu->m_aEntries, pMenu->m_iCursorPos, &str); return str; }
int main(int argc, char *argv[]) { FILE *fp; char buffer[512]; size_t i, j, r; unsigned int d = 0; uint64_t s, e, a, ri, si, ai, sr, rg, sg, ag, sd, ng; char **t; struct ck_ht_stat st; r = 20; s = 8; srand(time(NULL)); if (argc < 2) { ck_error("Usage: ck_ht <dictionary> [<repetitions> <initial size>]\n"); } if (argc >= 3) r = atoi(argv[2]); if (argc >= 4) s = (uint64_t)atoi(argv[3]); keys = malloc(sizeof(char *) * keys_capacity); assert(keys != NULL); fp = fopen(argv[1], "r"); assert(fp != NULL); while (fgets(buffer, sizeof(buffer), fp) != NULL) { buffer[strlen(buffer) - 1] = '\0'; keys[keys_length++] = strdup(buffer); assert(keys[keys_length - 1] != NULL); if (keys_length == keys_capacity) { t = realloc(keys, sizeof(char *) * (keys_capacity *= 2)); assert(t != NULL); keys = t; } } t = realloc(keys, sizeof(char *) * keys_length); assert(t != NULL); keys = t; table_init(); for (i = 0; i < keys_length; i++) d += table_insert(keys[i]) == false; ck_ht_stat(&ht, &st); fprintf(stderr, "# %zu entries stored, %u duplicates, %" PRIu64 " probe.\n", table_count(), d, st.probe_maximum); fprintf(stderr, "# reverse_insertion serial_insertion random_insertion serial_replace reverse_get serial_get random_get serial_remove negative_get\n\n"); a = 0; for (j = 0; j < r; j++) { if (table_reset() == false) { ck_error("ERROR: Failed to reset hash table.\n"); } s = rdtsc(); for (i = keys_length; i > 0; i--) d += table_insert(keys[i - 1]) == false; e = rdtsc(); a += e - s; } ri = a / (r * keys_length); a = 0; for (j = 0; j < r; j++) { if (table_reset() == false) { ck_error("ERROR: Failed to reset hash table.\n"); } s = rdtsc(); for (i = 0; i < keys_length; i++) d += table_insert(keys[i]) == false; e = rdtsc(); a += e - s; } si = a / (r * keys_length); a = 0; for (j = 0; j < r; j++) { keys_shuffle(keys); if (table_reset() == false) { ck_error("ERROR: Failed to reset hash table.\n"); } s = rdtsc(); for (i = 0; i < keys_length; i++) d += table_insert(keys[i]) == false; e = rdtsc(); a += e - s; } ai = a / (r * keys_length); a = 0; for (j = 0; j < r; j++) { s = rdtsc(); for (i = 0; i < keys_length; i++) table_replace(keys[i]); e = rdtsc(); a += e - s; } sr = a / (r * keys_length); table_reset(); for (i = 0; i < keys_length; i++) table_insert(keys[i]); a = 0; for (j = 0; j < r; j++) { s = rdtsc(); for (i = keys_length; i > 0; i--) { if (table_get(keys[i - 1]) == NULL) { ck_error("ERROR: Unexpected NULL value.\n"); } } e = rdtsc(); a += e - s; } rg = a / (r * keys_length); a = 0; for (j = 0; j < r; j++) { s = rdtsc(); for (i = 0; i < keys_length; i++) { if (table_get(keys[i]) == NULL) { ck_error("ERROR: Unexpected NULL value.\n"); } } e = rdtsc(); a += e - s; } sg = a / (r * keys_length); a = 0; for (j = 0; j < r; j++) { keys_shuffle(keys); s = rdtsc(); for (i = 0; i < keys_length; i++) { if (table_get(keys[i]) == NULL) { ck_error("ERROR: Unexpected NULL value.\n"); } } e = rdtsc(); a += e - s; } ag = a / (r * keys_length); a = 0; for (j = 0; j < r; j++) { s = rdtsc(); for (i = 0; i < keys_length; i++) table_remove(keys[i]); e = rdtsc(); a += e - s; for (i = 0; i < keys_length; i++) table_insert(keys[i]); } sd = a / (r * keys_length); a = 0; for (j = 0; j < r; j++) { s = rdtsc(); for (i = 0; i < keys_length; i++) { table_get("\x50\x03\x04\x05\x06\x10"); } e = rdtsc(); a += e - s; } ng = a / (r * keys_length); printf("%zu " "%" PRIu64 " " "%" PRIu64 " " "%" PRIu64 " " "%" PRIu64 " " "%" PRIu64 " " "%" PRIu64 " " "%" PRIu64 " " "%" PRIu64 " " "%" PRIu64 "\n", keys_length, ri, si, ai, sr, rg, sg, ag, sd, ng); return 0; }
/* {{{ sapi_apache_read_cookies */ static char *sapi_apache_read_cookies(TSRMLS_D) { return (char *) table_get(((request_rec *) SG(server_context))->subprocess_env, "HTTP_COOKIE"); }
/** * Query for a value * @param table Pointer to the hash_table * @param key entry key * * @return 1 if found, 0 if not */ int table_has(hash_table *table, const wchar_t *key) { return (table_get(table, key) != NULL); }
int main(int argc, char **argv) { int r, op = 'p', method = 'c'; const char *path; if (argc < 2) { fprintf(stderr, "usage: kv dbpath [put|get] [col|table|hash|pat|ql] [value_size]\n"); return -1; } path = *argv[1] ? argv[1] : NULL; if (argc > 2) { op = *argv[2]; } if (argc > 3) { method = *argv[3]; } if (argc > 4) { value_size = atoi(argv[4]); } if (argc > 5) { nloops = atoi(argv[5]); } if (grn_init()) { fprintf(stderr, "grn_init() failed\n"); return -1; } if (grn_ctx_init(&ctx, (method == 'q' ? GRN_CTX_USE_QL|GRN_CTX_BATCH_MODE : 0))) { fprintf(stderr, "grn_ctx_init() failed\n"); return -1; } srand(0); if (method == 'h' || method == 'p') { switch (method) { case 'h' : r = (op == 'p') ? hash_put(path) : hash_get(path); break; case 'p' : r = (op == 'p') ? pat_put(path) : pat_get(path); break; default : r = -1; fprintf(stderr, "invalid method\n"); break; } } else { if (path) { db = grn_db_open(&ctx, path); } if (!db) { db = grn_db_create(&ctx, path, NULL); } if (!db) { fprintf(stderr, "db initialize failed\n"); return -1; } value_type = grn_type_create(&ctx, "<value_type>", 12, 0, value_size); switch (method) { case 'q' : r = (op == 'p') ? ql_put() : ql_get(); break; case 'c' : r = (op == 'p') ? column_put() : column_get(); break; case 't' : r = (op == 'p') ? table_put() : table_get(); //r = (op == 'p') ? table_put_allocate() : table_get(); //r = (op == 'p') ? table_put2() : table_get(); break; default : r = -1; fprintf(stderr, "invalid method\n"); break; } if (grn_obj_close(&ctx, db)) { fprintf(stderr, "grn_obj_close() failed\n"); return -1; } } if (grn_ctx_fin(&ctx)) { fprintf(stderr, "grn_ctx_fin() failed\n"); return -1; } if (grn_fin()) { fprintf(stderr, "grn_fin() failed\n"); return -1; } return r; }
/** * Extract one request header. A header can span multiple lines, in * which case they will be folded into one before parsing is attempted. * * @param connp * @return HTP_OK or HTP_ERROR */ int htp_process_request_header_apache_2_2(htp_connp_t *connp) { bstr *tempstr = NULL; unsigned char *data = NULL; size_t len = 0; // Create new header structure htp_header_t *h = calloc(1, sizeof (htp_header_t)); if (h == NULL) return HTP_ERROR; // Ensure we have the necessary header data in a single buffer if (connp->in_header_line_index + 1 == connp->in_header_line_counter) { // Single line htp_header_line_t *hl = list_get(connp->in_tx->request_header_lines, connp->in_header_line_index); if (hl == NULL) { // Internal error htp_log(connp, HTP_LOG_MARK, HTP_LOG_ERROR, 0, "Process request header (Apache 2.2): Internal error"); free(h); return HTP_ERROR; } data = (unsigned char *) bstr_ptr(hl->line); len = bstr_len(hl->line); hl->header = h; } else { // Multiple lines (folded) int i = 0; for (i = connp->in_header_line_index; i < connp->in_header_line_counter; i++) { htp_header_line_t *hl = list_get(connp->in_tx->request_header_lines, i); if (hl == NULL) { // Internal error htp_log(connp, HTP_LOG_MARK, HTP_LOG_ERROR, 0, "Process request header (Apache 2.2): Internal error"); free(h); return HTP_ERROR; } len += bstr_len(hl->line); } tempstr = bstr_alloc(len); if (tempstr == NULL) { htp_log(connp, HTP_LOG_MARK, HTP_LOG_ERROR, 0, "Process request header (Apache 2.2): Failed to allocate bstring of %d bytes", len); free(h); return HTP_ERROR; } for (i = connp->in_header_line_index; i < connp->in_header_line_counter; i++) { htp_header_line_t *hl = list_get(connp->in_tx->request_header_lines, i); if (hl == NULL) { // Internal error htp_log(connp, HTP_LOG_MARK, HTP_LOG_ERROR, 0, "Process request header (Apache 2.2): Internal error"); bstr_free(tempstr); free(h); return HTP_ERROR; } char *data = bstr_ptr(hl->line); size_t len = bstr_len(hl->line); htp_chomp((unsigned char *)data, &len); bstr_add_mem_noex(tempstr, data, len); hl->header = h; } data = (unsigned char *) bstr_ptr(tempstr); } // Now try to parse the header if (htp_parse_request_header_apache_2_2(connp, h, data, len) != HTP_OK) { // Note: downstream responsible for error logging if (tempstr != NULL) { free(tempstr); } free(h); return HTP_ERROR; } // Do we already have a header with the same name? htp_header_t *h_existing = table_get(connp->in_tx->request_headers, h->name); if (h_existing != NULL) { // TODO Do we want to keep a list of the headers that are // allowed to be combined in this way? // Add to existing header h_existing->value = bstr_expand(h_existing->value, bstr_len(h_existing->value) + 2 + bstr_len(h->value)); bstr_add_mem_noex(h_existing->value, ", ", 2); bstr_add_str_noex(h_existing->value, h->value); // The header is no longer needed if (h->name != NULL) free(h->name); if (h->value != NULL) free(h->value); free(h); // Keep track of same-name headers h_existing->flags |= HTP_FIELD_REPEATED; } else { // Add as a new header table_add(connp->in_tx->request_headers, h->name, h); } if (tempstr != NULL) { free(tempstr); } return HTP_OK; }
int test_get( void ) { ASSERT_EQUAL(value, table_get(&test_table, L"test")); return 0; }
ssize_t is_column(struct Table* table, char* column) { struct Vector* vector = table_get(table, 0); return vector_find(vector, column); }
static int add_watch(int path_len, watch_node* parent) { int wd = inotify_add_watch(inotify_fd, path_buf, EVENT_MASK); if (wd < 0) { if (errno == EACCES || errno == ENOENT) { userlog(LOG_DEBUG, "inotify_add_watch(%s): %s", path_buf, strerror(errno)); return ERR_IGNORE; } else if (errno == ENOSPC) { userlog(LOG_WARNING, "inotify_add_watch(%s): %s", path_buf, strerror(errno)); watch_limit_reached(); return ERR_CONTINUE; } else { userlog(LOG_ERR, "inotify_add_watch(%s): %s", path_buf, strerror(errno)); return ERR_ABORT; } } else { userlog(LOG_DEBUG, "watching %s: %d", path_buf, wd); } watch_node* node = table_get(watches, wd); if (node != NULL) { if (node->wd != wd) { userlog(LOG_ERR, "table error: corruption at %d:%s / %d:%s)", wd, path_buf, node->wd, node->path); return ERR_ABORT; } else if (strcmp(node->path, path_buf) != 0) { char buf1[PATH_MAX], buf2[PATH_MAX]; const char* normalized1 = realpath(node->path, buf1); const char* normalized2 = realpath(path_buf, buf2); if (normalized1 == NULL || normalized2 == NULL || strcmp(normalized1, normalized2) != 0) { userlog(LOG_ERR, "table error: collision at %d (new %s, existing %s)", wd, path_buf, node->path); return ERR_ABORT; } else { userlog(LOG_INFO, "intersection at %d: (new %s, existing %s, real %s)", wd, path_buf, node->path, normalized1); return ERR_IGNORE; } } return wd; } node = malloc(sizeof(watch_node) + path_len + 1); CHECK_NULL(node, ERR_ABORT); memcpy(node->path, path_buf, path_len + 1); node->path_len = path_len; node->wd = wd; node->parent = parent; node->kids = NULL; if (parent != NULL) { if (parent->kids == NULL) { parent->kids = array_create(DEFAULT_SUBDIR_COUNT); CHECK_NULL(parent->kids, ERR_ABORT); } CHECK_NULL(array_push(parent->kids, node), ERR_ABORT); } if (table_put(watches, wd, node) == NULL) { userlog(LOG_ERR, "table error: unable to put (%d:%s)", wd, path_buf); return ERR_ABORT; } return wd; }
int main() { table_t tab = table_new(1024, cmp, dictGenHashFunction); table_put(tab, "append", "cmd_append"); table_put(tab, "bitcount", "cmd_bitcount"); table_put(tab, "brpop", "cmd_brpop"); table_put(tab, "brpoplpush", "cmd_brpoplpush"); table_put(tab, "decr", "cmd_decr"); table_put(tab, "decrby", "cmd_decrby"); table_put(tab, "del", "cmd_del"); table_put(tab, "exists", "cmd_exists"); table_put(tab, "get", "cmd_get"); table_put(tab, "getbit", "cmd_getbit"); table_put(tab, "getrange", "cmd_getrange"); table_put(tab, "incr", "cmd_incr"); table_put(tab, "incrby", "cmd_incrby"); table_put(tab, "keys", "cmd_keys"); table_put(tab, "lindex", "cmd_lindex"); table_put(tab, "linsert", "cmd_linsert"); table_put(tab, "llen", "cmd_llen"); table_put(tab, "lpop", "cmd_lpop"); table_put(tab, "lpush", "cmd_lpush"); table_put(tab, "lpushx", "cmd_lpushx"); table_put(tab, "lrange", "cmd_lrange"); table_put(tab, "lrem", "cmd_lrem"); table_put(tab, "lset", "cmd_lset"); table_put(tab, "ltrim", "cmd_ltrim"); table_put(tab, "mget", "cmd_mget"); table_put(tab, "msetnx", "cmd_msetnx"); table_put(tab, "randomkey", "cmd_randomkey"); table_put(tab, "rename", "cmd_rename"); table_put(tab, "rpop", "cmd_rpop"); table_put(tab, "rpoplpush", "cmd_rpoplpush"); table_put(tab, "rpush", "cmd_rpush"); table_put(tab, "rpushx", "cmd_rpushx"); table_put(tab, "set", "cmd_set"); table_put(tab, "setbit", "cmd_setbit"); table_put(tab, "setrange", "cmd_setrange"); table_put(tab, "strlen", "cmd_strlen"); table_put(tab, "type", "cmd_type"); table_map(tab, apply); printf("len = %d\n", table_length(tab)); char key[32]; scanf("%s", key); printf("%s\n", (char *)table_get(tab, key)); printf("remove set\n"); table_remove(tab, "set"); table_map(tab, apply); printf("len = %d\n", table_length(tab)); scanf("%s", key); printf("%s\n", (char *)table_get(tab, key)); table_free(&tab); return 0; }