void proto_init(struct bot *b) { bot = b; Link *invite_link = hashmap_get("INVITE", b->handlers); if (!invite_link) { invite_link = calloc(1, sizeof(Link)); hashmap_set("INVITE", invite_link, b->handlers); } push_val(invite_link, (void*)irc_invite); Link *chanjoin_link = hashmap_get("332", b->handlers); if (!chanjoin_link) { chanjoin_link = calloc(1, sizeof(Link)); hashmap_set("332", chanjoin_link, b->handlers); } push_val(chanjoin_link, (void*)irc_chan_join); Link *privmsg_link = hashmap_get("PRIVMSG", b->handlers); if (!privmsg_link) { privmsg_link = calloc(1, sizeof(Link)); hashmap_set("PRIVMSG", privmsg_link, b->handlers); } push_val(privmsg_link, (void*)irc_privmsg); }
/* * spawn_shell - Creates a new shell session * [int forkno] - Index of the shell session * [char * user] - User logging in. It could be null, for example, on startup */ void spawn_shell(int forkno, char * user){ int shellpid; if(!(shellpid = fork())) { /* Redirect IO */ grab_io(fd_slave); /* Call shell/login session */ if(forkno > 0) { if(user == NULL) { char * tokens[] = {"/bin/login", "-q", NULL}; execv(tokens[0], tokens); } else { char * tokens[] = {"/bin/login", "-q", "-u", user, NULL}; execv(tokens[0], tokens); } } else { char * tokens[] = {"/bin/login", "-u", "root", NULL}; execv(tokens[0], tokens); } } else { char forkno_str[10]; sprintf(forkno_str, "%d", forkno); hashmap_set(shellpid_hash, forkno_str, (void*)shellpid); if(user) hashmap_set(multishell_sessions, user, (void*)shellpid); } }
/* * Get an icon from the cache, or if it is not in the cache, * load it - or cache the generic icon if we can not find an * appropriate matching icon on the filesystem. */ static sprite_t * icon_get(char * name) { if (!strcmp(name,"")) { /* If a window doesn't have an icon set, return the generic icon */ return hashmap_get(icon_cache, "generic"); } /* Check the icon cache */ sprite_t * icon = hashmap_get(icon_cache, name); if (!icon) { /* We don't have an icon cached for this identifier, try search */ int i = 0; char path[100]; while (icon_directories[i]) { /* Check each path... */ sprintf(path, "%s/%s.png", icon_directories[i], name); if (access(path, R_OK) == 0) { /* And if we find one, cache it */ icon = malloc(sizeof(sprite_t)); load_sprite_png(icon, path); hashmap_set(icon_cache, name, icon); return icon; } i++; } /* If we've exhausted our search paths, just return the generic icon */ icon = hashmap_get(icon_cache, "generic"); hashmap_set(icon_cache, name, icon); } /* We have an icon, return it */ return icon; }
confreader_t * confreader_load(const char * file) { confreader_t * out = malloc(sizeof(confreader_t)); out->sections = hashmap_create(10); FILE * f = fopen(file, "r"); hashmap_t * current_section = hashmap_create(10); current_section->hash_val_free = free_hashmap; hashmap_set(out->sections, "", current_section); if (!f) { /* File does not exist, no configuration values, but continue normally. */ return out; } char tmp[1024]; char tmp2[1024]; char eq[2]; while (!feof(f)) { char c = fgetc(f); tmp[0] = '\0'; eq[0] = '\0'; tmp2[0] = '\0'; if (c == ';') { fscanf(f, " %[^\n]", tmp); while (!feof(f) && fgetc(f) != '\n'); } else if (c == '\n' || c == EOF) { continue; } else if (c == '[') { fscanf(f, " %[^]] ", tmp); while (!feof(f) && fgetc(f) != '\n'); current_section = hashmap_create(10); hashmap_set(out->sections, tmp, current_section); } else { ungetc(c, f); fscanf(f, " %[^=]%1[=]%[^=\n]", tmp, eq, tmp2); while (!feof(f) && fgetc(f) != '\n'); if (strcmp(eq, "=")) { continue; } hashmap_set(current_section, tmp, strdup(tmp2)); } } fclose(f); return out; }
void hashmap_t() { struct hashmap *h = hashmap_new(); hashmap_set(h, "toto", "truc"); hashmap_set(h, "otto", "machin"); assert(0 == strcmp("truc", (char *) hashmap_get(h, "toto"))); assert(0 == strcmp("machin", (char *) hashmap_get(h, "otto"))); hashmap_free(&h, NULL); assert(h == NULL); fprintf(stderr, "hashmap_t: OK\n"); }
int decode(char * inputStr, hashmap_t * map) { char * pch, * tmp, * key, * value; char *saveptr1, *saveptr2; /* split input string */ pch = strtok_r (inputStr," ", &saveptr1); while (pch != NULL) { /* url decode the part */ tmp = url_decode(pch); key = strtok_r (tmp,":", &saveptr2); value = strtok_r (NULL,":", &saveptr2); /* put it into hashmap */ if (hashmap_set(map,key,value) < 0) { fprintf(stderr,"can't set decode string into hashmap (%s/%s)\n",key,value); return -1; } /* continue */ pch = strtok_r (NULL," ", &saveptr1); } return 0; }
static void _init_caches(void) { icon_cache_16 = hashmap_create(10); { /* Generic fallback icon */ sprite_t * app_icon = malloc(sizeof(sprite_t)); load_sprite(app_icon, "/usr/share/icons/16/applications-generic.bmp"); app_icon->alpha = ALPHA_EMBEDDED; hashmap_set(icon_cache_16, "generic", app_icon); } icon_cache_48 = hashmap_create(10); { /* Generic fallback icon */ sprite_t * app_icon = malloc(sizeof(sprite_t)); load_sprite(app_icon, "/usr/share/icons/48/applications-generic.bmp"); app_icon->alpha = ALPHA_EMBEDDED; hashmap_set(icon_cache_48, "generic", app_icon); } }
void hashmap_add_list(hashmap_t* hashmap, list_value_t* lt) { hashmap_node_t* tmp_node; while(lt!=NULL) { tmp_node = hashmap_node_create(hashmap_hash(lt->key), lt); hashmap_set(hashmap, tmp_node); lt=lt->next; } }
/* * Test that we can get and set key/value pairs. */ char * test_get_set() { int rc = hashmap_set(map, &test1, &expect1); mu_assert(rc == 1, "hashmap_test: failed to set &test1."); bstring result = hashmap_get(map, &test1); mu_assert(result == &expect1, "Wrong value for test1."); rc = hashmap_set(map, &test2, &expect2); mu_assert(rc == 1, "hashmap_test: failed to set test2."); result = hashmap_get(map, &test2); mu_assert(result == &expect2, "hashmap_test: wrong value for test2."); rc = hashmap_set(map, &test3, &expect3); mu_assert(rc == 1, "hashmap_test: failed to set test3."); result = hashmap_get(map, &test3); mu_assert(result == &expect3, "hashmap_test: wrong value for test3."); return NULL; }
void dcache_set(struct _fs_instance_t *instance, struct _dentry_t* pdentry, const char * name, struct _dentry_t *dentry) { if (!instance->fs->unique_inode) return; struct key_t *key = kmalloc(sizeof(struct key_t)); key->instance = instance; key->dentry = pdentry; key->name = name; hashmap_set(map, (struct hashmap_key_t*)key, (struct hashmap_value_t*)dentry); }
char * shmon_callback(shm_packet_t * packet) { shmon_packet_t * shmon_packet = (shmon_packet_t*)packet->dat; switch(shmon_packet->cmd) { case SHMON_CTRL_GRAB_PID: { /* Allocate shell and return its pid */ /* Read the user from shared memory */ char * user = shmon_packet->username; /* See if he is already logged in (through the hashmap) */ if(hashmap_contains(multishell_sessions, user)) { /* return the user's pid instead of the new one (if he's logged in) */ return respond_to_client((int)hashmap_get(multishell_sessions, user)); } else { /* the user is not logged in, spawn new shell */ spawn_shell(shells_forked, user); return respond_to_client(get_pid_from_hash(shells_forked++)); } break; } case SHMON_CTRL_ADD_USR: { /* Add a user to the hashmap */ /* Read the user from shared memory */ char * user = shmon_packet->username; int pid = atoi(shmon_packet->cmd_dat); hashmap_set(multishell_sessions, user, (void*)pid); break; } case SHMON_CTRL_GET_USR: { /* Get a user from the hashmap */ /* Grab process pid from user IF he exists */ /* Read the user from shared memory */ char * user = shmon_packet->username; if(hashmap_contains(multishell_sessions, user)) return respond_to_client((int)hashmap_get(multishell_sessions, user)); else return respond_to_client(-1); break; } case SHMON_CTRL_REDIR: { /* Sends packet to the destination process, causing that process to 'wake up'. */ char packet_dest[SHM_MAX_ID_SIZE]; sprintf(packet_dest, SHMON_CLIENT_LOGIN_FORMAT, atoi(shmon_packet->cmd_dat)); char shm_msg[16] = SHMON_MSG_PROC_WAKE; shm_packet_t * pack = create_packet(shmon_server, packet_dest, SHM_DEV_CLIENT, shm_msg, strlen(shm_msg)); shman_send_to_network_clear(shmon_server, pack, 0); free(pack); break; } } char * ret = malloc(SHM_MAX_PACKET_DAT_SIZE); ret[0] = 1; /* just tells it is finished really. The client won't be expecting data to return, so it's not important what number we put here */ return ret; }
static elf_t * open_object(const char * path) { if (!path) { _main_obj->loaded = 1; return _main_obj; } if (hashmap_has(objects_map, (void*)path)) { elf_t * object = hashmap_get(objects_map, (void*)path); object->loaded = 1; return object; } char * file = find_lib(path); if (!file) return NULL; FILE * f = fopen(file, "r"); free(file); if (!f) { return NULL; } elf_t * object = calloc(1, sizeof(elf_t)); hashmap_set(objects_map, (void*)path, object); if (!object) { return NULL; } object->file = f; size_t r = fread(&object->header, sizeof(Elf32_Header), 1, object->file); if (!r) { free(object); return NULL; } if (object->header.e_ident[0] != ELFMAG0 || object->header.e_ident[1] != ELFMAG1 || object->header.e_ident[2] != ELFMAG2 || object->header.e_ident[3] != ELFMAG3) { free(object); return NULL; } object->dependencies = list_create(); return object; }
int main(void){ xenon_stack_item val; val.type = x_integer; val.data.anint = 10; hashmap *hm = hashmap_init(100); hashmap_set(hm, "a", val); xenon_stack_item *sb = hashmap_get(hm, "a"); if(sb != NULL){ printf("'a' = %ld\n", sb->data.anint); } hashmap_free(hm); }
int longestConsecutive(int* nums, int numsSize) { struct I2iHashTable* table = hashmap_create(numsSize); int i, max = 0; for (i = 0 ; i < numsSize ; i++) { if (hashmap_get(table, nums[i]) != NULL) { continue; } struct I2iHashNode * pre = hashmap_get(table, nums[i]-1); struct I2iHashNode * nxt = hashmap_get(table, nums[i]+1); int newlen = (pre != NULL ? pre->value : 0) + (nxt != NULL ? nxt->value : 0) + 1; if (pre != NULL) { hashmap_set(table, nums[i]-pre->value, newlen); } if (nxt != NULL) { hashmap_set(table, nums[i]+nxt->value, newlen); } hashmap_set(table, nums[i], newlen); if (newlen > max) { max = newlen; } } return max; }
ttk_window_t * ttk_window_new(char * title, uint16_t width, uint16_t height) { ttk_window_t * new_win = malloc(sizeof(ttk_window_t)); new_win->title = strdup(title); new_win->width = width; new_win->height = height; new_win->off_x = decor_left_width; new_win->off_y = decor_top_height; new_win->core_window = yutani_window_create(yctx, new_win->width + decor_width(), new_win->height + decor_height()); yutani_window_move(yctx, new_win->core_window, TTK_DEFAULT_X, TTK_DEFAULT_Y); assert(new_win->core_window && "Oh dear, I've failed to allocate a new window from the server. This is terrible."); /* XXX icon; also need to do this if we change the title later */ yutani_window_advertise(yctx, new_win->core_window, new_win->title); new_win->core_context = init_graphics_yutani_double_buffer(new_win->core_window); draw_fill(new_win->core_context, rgb(TTK_BACKGROUND_DEFAULT)); ttk_window_draw(new_win); hashmap_set(ttk_wids_to_windows, (void*)new_win->core_window->wid, new_win); }
void redis_pool_init(char *host, int port) { char key[64] = {}; if (!g_hmap){ g_hmap = hashmap_open(); assert(g_hmap != NULL); } struct cnt_pool *pool = calloc(1, sizeof(struct cnt_pool)); cnt_init ( pool, 20000, new_connect, del_connect ); sprintf(key, "%s:%d", host, port); x_printf(D, "pool addr is %p\n", pool); hashmap_set(g_hmap, key, strlen(key), &pool, sizeof(uintptr_t *)); uintptr_t *cite = NULL; cnt_pull ( pool, &cite, host, port ); printf("|-------%s redis pool init------->%s\n", key, (cite)?" OK!":" FAIL!"); assert (cite); cnt_push ( pool, &cite ); x_printf(D, "cite is %p\n", cite); return; }
int maxPoints(struct Point* points, int pointsSize) { int maxp = 0, i, j; for (i = 0 ; i < pointsSize ; i++) { struct HashTable * table = hashmap_create(pointsSize); int dup = 0; int max = 0; for (j = i+1 ; j < pointsSize ; j++) { if (points[i].x == points[j].x && points[i].y == points[j].y) { dup++; continue; } int dx = points[j].x - points[i].x; int dy = points[j].y - points[i].y; double k = (dx != 0 ? dy * 1.0 / dx : INT_MAX); struct HashNode * node = hashmap_get(table, k); if (node != NULL) node->value++; else node = hashmap_set(table, k, 1); max = max > node->value ? max : node->value; } maxp = maxp > 1 + max + dup ? maxp : 1 + max + dup; } return maxp; }
static void object_find_copy_relocations(elf_t * object) { size_t i = 0; for (uintptr_t x = 0; x < object->header.e_shentsize * object->header.e_shnum; x += object->header.e_shentsize) { Elf32_Shdr shdr; fseek(object->file, object->header.e_shoff + x, SEEK_SET); fread(&shdr, object->header.e_shentsize, 1, object->file); if (shdr.sh_type == 9) { Elf32_Rel * table = (Elf32_Rel *)(shdr.sh_addr + object->base); while ((uintptr_t)table - ((uintptr_t)shdr.sh_addr + object->base) < shdr.sh_size) { unsigned char type = ELF32_R_TYPE(table->r_info); if (type == 5) { unsigned int symbol = ELF32_R_SYM(table->r_info); Elf32_Sym * sym = &object->dyn_symbol_table[symbol]; char * symname = (char *)((uintptr_t)object->dyn_string_table + sym->st_name); hashmap_set(glob_dat, symname, (void *)table->r_offset); } table++; } } } }
void * parent_killshells(void * garbage) { while(!exit_application) { /* Kill the children! Kill them all! */ int shellcount = get_shellno_count(); for(int i=0;i < shellcount;i++) { int child = get_pid_from_hash(i); if(waitpid(child, NULL, WNOHANG) > 0){ /* One of them died */ char forkno_str[10]; sprintf(forkno_str,"%d", i); hashmap_remove(shellpid_hash, forkno_str); char * user = get_username_from_pid(child); hashmap_remove(multishell_sessions, user); free(user); /* Update the other shellno's onward: */ for(int j = i;j < shellcount - 1;j++){ char forkno_next_str[10]; sprintf(forkno_str, "%d", j); sprintf(forkno_next_str, "%d", (j+1)); hashmap_set(shellpid_hash, forkno_str, hashmap_get(shellpid_hash,forkno_next_str)); } /* And finally, delete the last one */ char forkno_last[10]; sprintf(forkno_last, "%d", shellcount-1); hashmap_remove(shellpid_hash, forkno_last); shells_forked--; } } } /* Really shouldn't reach this point */ return NULL; pthread_exit(0); }
static void add_client_to_list(shm_t * client_dev) { if(!hashmap_contains(shm_client_list, client_dev->unique_id)) return; hashmap_set(shm_client_list, client_dev->unique_id, client_dev); }
int room_jion(struct room *r, struct client *c) { if (r && c) { return hashmap_set(r->clients, (void *) c, NULL); } return -1; }
static int object_relocate(elf_t * object) { if (object->dyn_symbol_table) { Elf32_Sym * table = object->dyn_symbol_table; size_t i = 0; while (i < object->dyn_symbol_table_size) { char * symname = (char *)((uintptr_t)object->dyn_string_table + table->st_name); if (!hashmap_has(dumb_symbol_table, symname)) { if (table->st_shndx) { hashmap_set(dumb_symbol_table, symname, (void*)(table->st_value + object->base)); } } else { if (table->st_shndx) { //table->st_value = (uintptr_t)hashmap_get(dumb_symbol_table, symname); } } table++; i++; } } size_t i = 0; for (uintptr_t x = 0; x < object->header.e_shentsize * object->header.e_shnum; x += object->header.e_shentsize) { Elf32_Shdr shdr; fseek(object->file, object->header.e_shoff + x, SEEK_SET); fread(&shdr, object->header.e_shentsize, 1, object->file); if (shdr.sh_type == 9) { Elf32_Rel * table = (Elf32_Rel *)(shdr.sh_addr + object->base); while ((uintptr_t)table - ((uintptr_t)shdr.sh_addr + object->base) < shdr.sh_size) { unsigned int symbol = ELF32_R_SYM(table->r_info); unsigned char type = ELF32_R_TYPE(table->r_info); Elf32_Sym * sym = &object->dyn_symbol_table[symbol]; char * symname = NULL; uintptr_t x = sym->st_value + object->base; if (need_symbol_for_type(type) || (type == 5)) { symname = (char *)((uintptr_t)object->dyn_string_table + sym->st_name); } if ((sym->st_shndx == 0) && need_symbol_for_type(type) || (type == 5)) { if (symname && hashmap_has(dumb_symbol_table, symname)) { x = (uintptr_t)hashmap_get(dumb_symbol_table, symname); } else { TRACE_LD("Symbol not found: %s", symname); x = 0x0; } } /* Relocations, symbol lookups, etc. */ switch (type) { case 6: /* GLOB_DAT */ if (symname && hashmap_has(glob_dat, symname)) { x = (uintptr_t)hashmap_get(glob_dat, symname); } case 7: /* JUMP_SLOT */ memcpy((void *)(table->r_offset + object->base), &x, sizeof(uintptr_t)); break; case 1: /* 32 */ x += *((ssize_t *)(table->r_offset + object->base)); memcpy((void *)(table->r_offset + object->base), &x, sizeof(uintptr_t)); break; case 2: /* PC32 */ x += *((ssize_t *)(table->r_offset + object->base)); x -= (table->r_offset + object->base); memcpy((void *)(table->r_offset + object->base), &x, sizeof(uintptr_t)); break; case 8: /* RELATIVE */ x = object->base; x += *((ssize_t *)(table->r_offset + object->base)); memcpy((void *)(table->r_offset + object->base), &x, sizeof(uintptr_t)); break; case 5: /* COPY */ memcpy((void *)(table->r_offset + object->base), (void *)x, sym->st_size); break; default: TRACE_LD("Unknown relocation type: %d", type); } table++; } } } return 0; }
int main(int argc, char * argv[]) { char * file = argv[1]; size_t arg_offset = 1; if (!strcmp(argv[1], "-e")) { arg_offset = 3; file = argv[2]; } char * trace_ld_env = getenv("LD_DEBUG"); if (trace_ld_env && (!strcmp(trace_ld_env,"1") || !strcmp(trace_ld_env,"yes"))) { __trace_ld = 1; } dumb_symbol_table = hashmap_create(10); glob_dat = hashmap_create(10); objects_map = hashmap_create(10); ld_exports_t * ex = ld_builtin_exports; while (ex->name) { hashmap_set(dumb_symbol_table, ex->name, ex->symbol); ex++; } elf_t * main_obj = open_object(file); _main_obj = main_obj; if (!main_obj) { fprintf(stderr, "%s: error: failed to open object '%s'.\n", argv[0], file); return 1; } size_t main_size = object_calculate_size(main_obj); uintptr_t end_addr = object_load(main_obj, 0x0); object_postload(main_obj); object_find_copy_relocations(main_obj); hashmap_t * libs = hashmap_create(10); list_t * ctor_libs = list_create(); list_t * init_libs = list_create(); TRACE_LD("Loading dependencies."); node_t * item; while (item = list_pop(main_obj->dependencies)) { while (end_addr & 0xFFF) { end_addr++; } char * lib_name = item->value; if (!strcmp(lib_name, "libg.so")) goto nope; elf_t * lib = open_object(lib_name); if (!lib) { fprintf(stderr, "Failed to load dependency '%s'.\n", lib_name); return 1; } hashmap_set(libs, lib_name, lib); TRACE_LD("Loading %s at 0x%x", lib_name, end_addr); end_addr = object_load(lib, end_addr); object_postload(lib); TRACE_LD("Relocating %s", lib_name); object_relocate(lib); fclose(lib->file); /* Execute constructors */ if (lib->ctors) { list_insert(ctor_libs, lib); } if (lib->init) { list_insert(init_libs, lib); } nope: free(item); } TRACE_LD("Relocating main object"); object_relocate(main_obj); TRACE_LD("Placing heap at end"); while (end_addr & 0xFFF) { end_addr++; } char * ld_no_ctors = getenv("LD_DISABLE_CTORS"); if (ld_no_ctors && (!strcmp(ld_no_ctors,"1") || !strcmp(ld_no_ctors,"yes"))) { TRACE_LD("skipping ctors because LD_DISABLE_CTORS was set"); } else { foreach(node, ctor_libs) { elf_t * lib = node->value; if (lib->ctors) { TRACE_LD("Executing ctors..."); for (size_t i = 0; i < lib->ctors_size; i += sizeof(uintptr_t)) { TRACE_LD(" 0x%x()", lib->ctors[i]); lib->ctors[i](); } } } }
int main (int argc, char ** argv) { yctx = yutani_init(); int width = yctx->display_width; int height = yctx->display_height; sprite_t * wallpaper_tmp = malloc(sizeof(sprite_t)); char f_name[512]; sprintf(f_name, "%s/.wallpaper.png", getenv("HOME")); FILE * f = fopen(f_name, "r"); if (f) { fclose(f); load_sprite_png(wallpaper_tmp, f_name); } else { load_sprite_png(wallpaper_tmp, "/usr/share/wallpaper.png"); } /* Initialize hashmap for icon cache */ icon_cache = hashmap_create(10); { /* Generic fallback icon */ sprite_t * app_icon = malloc(sizeof(sprite_t)); load_sprite_png(app_icon, "/usr/share/icons/48/applications-generic.png"); hashmap_set(icon_cache, "generic", app_icon); } sprintf(f_name, "%s/.desktop", getenv("HOME")); f = fopen(f_name, "r"); if (!f) { f = fopen("/etc/default.desktop", "r"); } read_applications(f); /* Load applications */ uint32_t i = 0; while (applications[i].icon) { applications[i].icon_sprite = icon_get(applications[i].icon); ++i; } float x = (float)width / (float)wallpaper_tmp->width; float y = (float)height / (float)wallpaper_tmp->height; int nh = (int)(x * (float)wallpaper_tmp->height); int nw = (int)(y * (float)wallpaper_tmp->width);; wallpaper = create_sprite(width, height, ALPHA_OPAQUE); gfx_context_t * tmp = init_graphics_sprite(wallpaper); if (nw > width) { draw_sprite_scaled(tmp, wallpaper_tmp, (width - nw) / 2, 0, nw, height); } else { draw_sprite_scaled(tmp, wallpaper_tmp, 0, (height - nh) / 2, width, nh); } free(tmp); win_width = width; win_height = height; wina = yutani_window_create(yctx, width, height); assert(wina); yutani_set_stack(yctx, wina, YUTANI_ZORDER_BOTTOM); ctx = init_graphics_yutani_double_buffer(wina); init_shmemfonts(); redraw_apps(); yutani_flip(yctx, wina); while (_continue) { yutani_msg_t * m = yutani_poll(yctx); waitpid(-1, NULL, WNOHANG); if (m) { switch (m->type) { case YUTANI_MSG_WINDOW_MOUSE_EVENT: wallpaper_check_click((struct yutani_msg_window_mouse_event *)m->data); break; case YUTANI_MSG_SESSION_END: _continue = 0; break; } free(m); } } yutani_close(yctx, wina); return 0; }
static void add_server_to_network(shm_t * server_dev) { list_t * network_list = list_create(); list_insert(network_list, server_dev); /* the first node of this network list will always be the server itself */ hashmap_set(shm_network, server_dev->shm_key, network_list); servers_online++; }