static const lang_code_t *_lang_code_get ( const char *code, size_t len ) { int i; char tmp[4]; if (lang_codes_code2b == NULL) { const lang_code_t *c = lang_codes; lang_codes_code2b = (lang_code_lookup_t *)calloc(1, sizeof(lang_code_lookup_t)); lang_codes_code1 = (lang_code_lookup_t *)calloc(1, sizeof(lang_code_lookup_t)); lang_codes_code2t = (lang_code_lookup_t *)calloc(1, sizeof(lang_code_lookup_t)); while (c->code2b) { _lang_code_lookup_add(lang_codes_code2b, c, _lang_code2b_cmp); if (c->code1) _lang_code_lookup_add(lang_codes_code1, c, _lang_code1_cmp); if (c->code2t) _lang_code_lookup_add(lang_codes_code2t, c, _lang_code2t_cmp); c++; } } if (code && *code && len) { /* Extract the code (lowercase) */ i = 0; while (i < 3 && *code && len) { if (*code == ';' || *code == ',' || *code == '-') break; if (*code != ' ') tmp[i++] = *code | 0x20; // |0x20 = lower case code++; len--; } tmp[i] = '\0'; /* Convert special case (qaa..qtz) */ if (*tmp == 'q') { if (tmp[1] >= 'a' && tmp[1] <= 'z' && tmp[2] >= 'a' && tmp[2] <= 'z') { tmp[1] = 'a'; tmp[2] = 'a'; } } /* Search */ if (i) { lang_code_lookup_element_t sample, *element; lang_code_t lang_code; lang_code.code1 = tmp; lang_code.code2b = tmp; lang_code.code2t = tmp; sample.lang_code = &lang_code; element = RB_FIND(lang_codes_code2b, &sample, link, _lang_code2b_cmp); if (element != NULL) return element->lang_code; element = RB_FIND(lang_codes_code1, &sample, link, _lang_code1_cmp); if (element != NULL) return element->lang_code; element = RB_FIND(lang_codes_code2t, &sample, link, _lang_code2t_cmp); if (element != NULL) return element->lang_code; } } return &lang_codes[0]; }
struct options_entry * options_find(struct options *oo, const char *name) { struct options_entry *o, p; p.name = (char *)name; o = RB_FIND(options_tree, &oo->tree, &p); while (o == NULL) { oo = oo->parent; if (oo == NULL) break; o = RB_FIND(options_tree, &oo->tree, &p); } return (o); }
static enum cmd_retval cmd_bind_key_mode_table(struct cmd *self, struct cmdq_item *item, key_code key) { struct args *args = self->args; const char *tablename; const struct mode_key_table *mtab; struct mode_key_binding *mbind, mtmp; enum mode_key_cmd cmd; tablename = args_get(args, 't'); if ((mtab = mode_key_findtable(tablename)) == NULL) { cmdq_error(item, "unknown key table: %s", tablename); return (CMD_RETURN_ERROR); } cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]); if (cmd == MODEKEY_NONE) { cmdq_error(item, "unknown command: %s", args->argv[1]); return (CMD_RETURN_ERROR); } if (args->argc != 2) { cmdq_error(item, "no argument allowed"); return (CMD_RETURN_ERROR); } mtmp.key = key; if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) == NULL) { mbind = xmalloc(sizeof *mbind); mbind->key = mtmp.key; RB_INSERT(mode_key_tree, mtab->tree, mbind); } mbind->cmd = cmd; return (CMD_RETURN_NORMAL); }
int api_exec ( const char *subsystem, htsmsg_t *args, htsmsg_t **resp ) { api_hook_t h; api_link_t *ah, skel; const char *op; /* Args and response must be set */ if (!args || !resp || !subsystem) return EINVAL; // Note: there is no locking while checking the hook tree, its assumed // this is all setup during init (if this changes the code will // need updating) h.ah_subsystem = subsystem; skel.hook = &h; ah = RB_FIND(&api_hook_tree, &skel, link, ah_cmp); if (!ah) { tvhwarn("api", "failed to find subsystem [%s]", subsystem); return ENOSYS; // TODO: is this really the right error code? } /* Extract method */ op = htsmsg_get_str(args, "method"); if (!op) op = htsmsg_get_str(args, "op"); // Note: this is not required (so no final validation) /* Execute */ return ah->hook->ah_callback(ah->hook->ah_opaque, op, args, resp); }
struct tupid_tree *tupid_tree_search(struct tupid_entries *root, tupid_t tupid) { struct tupid_tree tt = { .tupid = tupid, }; return RB_FIND(tupid_entries, root, &tt); }
ssize_t intlconv_utf8( char *dst, size_t dst_size, const char *dst_charset_id, const char *src_utf8 ) { intlconv_cache_t templ, *ic; char **inbuf, **outbuf; size_t inbuf_left, outbuf_left; ssize_t res; if (dst_charset_id == NULL) { strncpy(dst, src_utf8, dst_size); dst[dst_size - 1] = '\0'; return strlen(dst); } templ.ic_charset_id = (char *)dst_charset_id; pthread_mutex_lock(&intlconv_lock); if (intlconv_last_ic && strcmp(intlconv_last_ic->ic_charset_id, dst_charset_id) == 0) { ic = intlconv_last_ic; goto found; } ic = RB_FIND(&intlconv_all, &templ, ic_link, intlconv_cmp); if (!ic) { iconv_t c = iconv_open(dst_charset_id, "UTF-8"); if ((iconv_t)-1 == c) { pthread_mutex_unlock(&intlconv_lock); return -EIO; } ic = malloc(sizeof(*ic)); if (ic == NULL) { pthread_mutex_unlock(&intlconv_lock); return -ENOMEM; } ic->ic_charset_id = strdup(dst_charset_id); if (ic->ic_charset_id == NULL) { pthread_mutex_unlock(&intlconv_lock); free(ic); iconv_close(c); return -ENOMEM; } ic->ic_handle = c; RB_INSERT_SORTED(&intlconv_all, ic, ic_link, intlconv_cmp); } found: inbuf = (char **)&src_utf8; inbuf_left = strlen(src_utf8); outbuf = &dst; outbuf_left = dst_size; res = iconv(ic->ic_handle, inbuf, &inbuf_left, outbuf, &outbuf_left); if (res == -1) { res = -errno; } else { intlconv_last_ic = ic; } pthread_mutex_unlock(&intlconv_lock); if (res >= 0) res = dst_size - outbuf_left; return res; }
struct evcpe_dns_entry *evcpe_dns_cache_find(struct evcpe_dns_cache *cache, const char *name) { struct evcpe_dns_entry find; find.name = (char *)name; return RB_FIND(evcpe_dns_cache, cache, &find); }
char * ct_getloginbyuid(uid_t uid) { struct passwd *passwd; struct ct_login_cache *entry, search; search.lc_uid = uid; entry = RB_FIND(ct_login_cache_tree, &ct_login_cache, &search); if (entry != NULL) { return entry->lc_name; } /* if the cache gets too big, dump all entries and refill. */ if (ct_login_cache_size > MAX_LC_CACHE_SIZE) { ct_cleanup_login_cache(); } /* yes, this even caches negative entries */ ct_login_cache_size++; entry = e_calloc(1, sizeof(*entry)); entry->lc_uid = uid; passwd = getpwuid(uid); if (passwd) entry->lc_name = e_strdup(passwd->pw_name); else entry->lc_name = NULL; /* entry not found cache NULL */ RB_INSERT(ct_login_cache_tree, &ct_login_cache, entry); return entry->lc_name; }
struct pfi_kif * pfi_kif_get(const char *kif_name) { struct pfi_kif *kif; struct pfi_kif_cmp s; bzero(&s, sizeof(s)); strlcpy(s.pfik_name, kif_name, sizeof(s.pfik_name)); if ((kif = RB_FIND(pfi_ifhead, &pfi_ifs, (struct pfi_kif *)&s)) != NULL) return (kif); /* create new one */ if ((kif = malloc(sizeof(*kif), PFI_MTYPE, M_NOWAIT|M_ZERO)) == NULL) return (NULL); strlcpy(kif->pfik_name, kif_name, sizeof(kif->pfik_name)); #ifdef __NetBSD__ /* time_second is not valid yet */ kif->pfik_tzero = (time_second > 7200) ? time_second : 0; #else kif->pfik_tzero = time_second; #endif /* !__NetBSD__ */ TAILQ_INIT(&kif->pfik_dynaddrs); RB_INSERT(pfi_ifhead, &pfi_ifs, kif); return (kif); }
int cmd_bind_key_table(struct cmd *self, struct cmd_ctx *ctx, int key) { struct args *args = self->args; const char *tablename; const struct mode_key_table *mtab; struct mode_key_binding *mbind, mtmp; enum mode_key_cmd cmd; tablename = args_get(args, 't'); if ((mtab = mode_key_findtable(tablename)) == NULL) { ctx->error(ctx, "unknown key table: %s", tablename); return (-1); } cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]); if (cmd == MODEKEY_NONE) { ctx->error(ctx, "unknown command: %s", args->argv[1]); return (-1); } mtmp.key = key; mtmp.mode = !!args_has(args, 'c'); if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) { mbind->cmd = cmd; return (0); } mbind = xmalloc(sizeof *mbind); mbind->key = mtmp.key; mbind->mode = mtmp.mode; mbind->cmd = cmd; RB_INSERT(mode_key_tree, mtab->tree, mbind); return (0); }
int flexran_agent_destroy_channel(int channel_id) { int i, j; /*Check to see if channel exists*/ struct flexran_agent_channel_s *e = NULL; struct flexran_agent_channel_s search; memset(&search, 0, sizeof(struct flexran_agent_channel_s)); e = RB_FIND(flexran_agent_channel_map, &channel_instance.flexran_agent_head, &search); if (e == NULL) { return -1; } /*Unregister the channel from all agents*/ for (i = 0; i < NUM_MAX_ENB; i++) { for (j = 0; j < FLEXRAN_AGENT_MAX; j++) { if (agent_channel[i][j] != NULL) { if (agent_channel[i][j]->channel_id == e->channel_id) { agent_channel[i][j] == NULL; } } } } /*Remove the channel from the tree and free memory*/ RB_REMOVE(flexran_agent_channel_map, &channel_instance.flexran_agent_head, e); e->release(e); free(e); return 0; }
struct domain * wl_find(const gchar *search, struct domain_list *wl) { int i; struct domain *d = NULL, dfind; gchar *s = NULL; if (search == NULL || wl == NULL) return (NULL); if (strlen(search) < 2) return (NULL); if (search[0] != '.') s = g_strdup_printf(".%s", search); else s = g_strdup(search); for (i = strlen(s) - 1; i >= 0; i--) { if (s[i] == '.') { dfind.d = &s[i]; d = RB_FIND(domain_list, wl, &dfind); if (d) goto done; } } done: if (s) g_free(s); return (d); }
int cmd_unbind_key_table(struct cmd *self, struct cmd_ctx *ctx, int key) { struct args *args = self->args; const char *tablename; const struct mode_key_table *mtab; struct mode_key_binding *mbind, mtmp; tablename = args_get(args, 't'); if ((mtab = mode_key_findtable(tablename)) == NULL) { ctx->error(ctx, "unknown key table: %s", tablename); return (-1); } if (key == KEYC_NONE) { while (!RB_EMPTY(mtab->tree)) { mbind = RB_ROOT(mtab->tree); RB_REMOVE(mode_key_tree, mtab->tree, mbind); xfree(mbind); } return (0); } mtmp.key = key; mtmp.mode = !!args_has(args, 'c'); if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) { RB_REMOVE(mode_key_tree, mtab->tree, mbind); xfree(mbind); } return (0); }
/* * Remove any files not in ``keepfiles'' from cachedir. * * This is best effort and returns no errors if we fail to remove a file. */ void ctfile_cache_trim_aliens(const char *cachedir, struct ctfile_list_tree *keepfiles) { struct ctfile_list_file sfile; struct dirent *dp; DIR *dirp; CNDBG(CT_LOG_CTFILE, "triming files not found on server"); if ((dirp = opendir(cachedir)) == NULL) return; while ((dp = readdir(dirp)) != NULL) { /* ignore . and .. */ if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) continue; strlcpy(sfile.mlf_name, dp->d_name, sizeof(sfile.mlf_name)); if ((RB_FIND(ctfile_list_tree, keepfiles, &sfile)) == NULL) { CNDBG(CT_LOG_CTFILE, "Trimming %s from ctfile cache: " "not found on server", dp->d_name); (void)ctfile_cache_remove(dp->d_name, cachedir); } } closedir(dirp); CNDBG(CT_LOG_CTFILE, "done"); return; }
/* * UDP specific implementation of receive. */ static void net2_udpsocket_recv(void *udps_ptr, struct net2_dgram_rx *rx) { struct net2_udpsocket *udps = udps_ptr; struct net2_conn_receive*r; struct net2_conn_p2p *c, search; if ((r = net2_malloc(sizeof(*r))) == NULL) return; /* Drop packet. */ r->buf = rx->data; rx->data = NULL; r->error = rx->error; search.np2p_remote = (struct sockaddr*)&rx->addr; search.np2p_remotelen = rx->addrlen; c = RB_FIND(net2_udpsocket_conns, &udps->conns, &search); if (c != NULL) net2_connection_recv(&c->np2p_conn, r); else { /* Unrecognized connection. */ /* XXX Implement new connection callback. */ if (r->buf) net2_buffer_free(r->buf); net2_free(r); } }
static jni_subscription_t * jni_sub_find(int id) { jni_subscription_t skel; skel.js_id = id; return RB_FIND(&jni_subscriptions, &skel, js_link, js_cmp); }
/* Get language element */ lang_str_ele_t *lang_str_get2 ( lang_str_t *ls, const char *lang ) { int i; const char **langs; lang_str_ele_t skel, *e = NULL; if (!ls) return NULL; /* Check config/requested langs */ if ((langs = lang_code_split(lang))) { i = 0; while (langs[i]) { skel.lang = langs[i]; if ((e = RB_FIND(ls, &skel, link, _lang_cmp))) break; i++; } free(langs); } /* Use first available */ if (!e) e = RB_FIRST(ls); /* Return */ return e; }
epggrab_ota_mux_t *epggrab_ota_find_mux ( mpegts_mux_t *mm ) { epggrab_ota_mux_t *om, tmp; tmp.om_mux_uuid = mm->mm_id.in_uuid; om = RB_FIND(&epggrab_ota_all, &tmp, om_global_link, om_id_cmp); return om; }
void *tato_tree_int_find(TatoTreeInt *thiss, unsigned int key) { TatoTreeIntNode node_scanned; node_scanned.key = key; TatoTreeIntNode *node = RB_FIND(TatoTreeInt_, thiss, &node_scanned); return (node) ? node->value : NULL; }
enum mode_key_cmd mode_key_lookup(struct mode_key_data *mdata, int key, const char **arg) { struct mode_key_binding *mbind, mtmp; mtmp.key = key; mtmp.mode = mdata->mode; if ((mbind = RB_FIND(mode_key_tree, mdata->tree, &mtmp)) == NULL) { if (mdata->mode != 0) return (MODEKEY_NONE); return (MODEKEY_OTHER); } switch (mbind->cmd) { case MODEKEYEDIT_SWITCHMODE: case MODEKEYEDIT_SWITCHMODEAPPEND: case MODEKEYEDIT_SWITCHMODEAPPENDLINE: case MODEKEYEDIT_SWITCHMODEBEGINLINE: case MODEKEYEDIT_SWITCHMODECHANGELINE: case MODEKEYEDIT_SWITCHMODESUBSTITUTE: case MODEKEYEDIT_SWITCHMODESUBSTITUTELINE: mdata->mode = 1 - mdata->mode; /* FALLTHROUGH */ default: if (arg != NULL) *arg = mbind->arg; return (mbind->cmd); } }
/* * Checks to see if an IP is client or server by finding it in the tree * returns SERVER or CLIENT. * if mode = UNKNOWN, then abort on unknowns * if mode = CLIENT, then unknowns become clients * if mode = SERVER, then unknowns become servers */ int check_ip_tree(const int mode, const unsigned long ip) { struct tree_type *node = NULL, *finder = NULL; finder = new_tree(); finder->ip = ip; node = RB_FIND(data_tree, &treeroot, finder); if (node == NULL && mode == UNKNOWN) errx(1, "%s (%lu) is an unknown system... aborting.!\n" "Try a different auto mode (-n router|client|server)", libnet_addr2name4(ip, RESOLVE), ip); #ifdef DEBUG if (node->type == SERVER) { dbg(1, "Server: %s", libnet_addr2name4(ip, RESOLVE)); } else if (node->type == CLIENT) { dbg(1, "Client: %s", libnet_addr2name4(ip, RESOLVE)); } else { dbg(1, "Unknown: %s", libnet_addr2name4(ip, RESOLVE)); } #endif /* return node type if we found the node, else return the default (mode) */ if (node != NULL) { return (node->type); } else { return mode; } }
enum cmd_retval cmd_unbind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, key_code key) { struct args *args = self->args; const char *tablename; const struct mode_key_table *mtab; struct mode_key_binding *mbind, mtmp; tablename = args_get(args, 't'); if ((mtab = mode_key_findtable(tablename)) == NULL) { cmdq_error(cmdq, "unknown key table: %s", tablename); return (CMD_RETURN_ERROR); } if (key == KEYC_UNKNOWN) { while (!RB_EMPTY(mtab->tree)) { mbind = RB_ROOT(mtab->tree); RB_REMOVE(mode_key_tree, mtab->tree, mbind); free(mbind); } return (CMD_RETURN_NORMAL); } mtmp.key = key; mtmp.mode = !!args_has(args, 'c'); if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) { RB_REMOVE(mode_key_tree, mtab->tree, mbind); free(mbind); } return (CMD_RETURN_NORMAL); }
void add_tree_first_ipv6(const u_char *data) { tcpr_tree_t *newnode = NULL, *findnode; ipv6_hdr_t ip6_hdr; assert(data); /* * first add/find the source IP/client */ newnode = new_tree(); /* prevent issues with byte alignment, must memcpy */ memcpy(&ip6_hdr, (data + TCPR_ETH_H), TCPR_IPV6_H); /* copy over the source ip, and values to gurantee this a client */ newnode->family = AF_INET6; newnode->u.ip6 = ip6_hdr.ip_src; newnode->type = DIR_CLIENT; newnode->client_cnt = 1000; findnode = RB_FIND(tcpr_data_tree_s, &treeroot, newnode); /* if we didn't find it, add it to the tree, else free it */ if (findnode == NULL) { RB_INSERT(tcpr_data_tree_s, &treeroot, newnode); } else { safe_free(newnode); } /* * now add/find the destination IP/server */ newnode = new_tree(); memcpy(&ip6_hdr, (data + TCPR_ETH_H), TCPR_IPV6_H); newnode->family = AF_INET6; newnode->u.ip6 = ip6_hdr.ip_dst; newnode->type = DIR_SERVER; newnode->server_cnt = 1000; findnode = RB_FIND(tcpr_data_tree_s, &treeroot, newnode); if (findnode == NULL) { RB_INSERT(tcpr_data_tree_s, &treeroot, newnode); } else { safe_free(newnode); } }
void add_tree(const unsigned long ip, const u_char * data) { struct tree_type *node = NULL, *newnode = NULL; newnode = packet2tree(data); if (newnode->type == UNKNOWN) { /* couldn't figure out if packet was client or server */ dbg(2, "%s (%lu) unknown client/server", libnet_addr2name4(newnode->ip, RESOLVE), newnode->ip); } /* try to find a simular entry in the tree */ node = RB_FIND(data_tree, &treeroot, newnode); #ifdef DEBUG if (debug > 2) tree_printnode("add_tree", node); #endif /* new entry required */ if (node == NULL) { /* increment counters */ if (newnode->type == SERVER) { newnode->server_cnt++; } else if (newnode->type == CLIENT) { newnode->client_cnt++; } /* insert it in */ RB_INSERT(data_tree, &treeroot, newnode); } else { /* we found something, so update it */ dbg(2, " node: %p\nnewnode: %p", node, newnode); #ifdef DEBUG if (debug > 2) tree_printnode("update node", node); #endif /* increment counter */ if (newnode->type == SERVER) { node->server_cnt++; } else if (newnode->type == CLIENT) { /* temp debug code */ node->client_cnt++; } /* didn't insert it, so free it */ free(newnode); } dbg(2, "------- START NEXT -------"); #ifdef DEBUG if (debug > 2) tree_print(&treeroot); #endif }
int linkchk(FTSENT *p) { static struct links_entry *free_list = NULL; static int stop_allocating = 0; struct links_entry ltmp, *le; struct stat *st; st = p->fts_statp; ltmp.ino = st->st_ino; ltmp.dev = st->st_dev; le = RB_FIND(ltree, &links, <mp); if (le != NULL) { /* * Save memory by releasing an entry when we've seen * all of it's links. */ if (--le->links <= 0) { RB_REMOVE(ltree, &links, le); /* Recycle this node through the free list */ if (stop_allocating) { free(le); } else { le->fnext = free_list; free_list = le; } } return (1); } if (stop_allocating) return (0); /* Add this entry to the links cache. */ if (free_list != NULL) { /* Pull a node from the free list if we can. */ le = free_list; free_list = le->fnext; } else /* Malloc one if we have to. */ le = malloc(sizeof(struct links_entry)); if (le == NULL) { stop_allocating = 1; warnx("No more memory for tracking hard links"); return (0); } le->dev = st->st_dev; le->ino = st->st_ino; le->links = st->st_nlink - 1; le->fnext = NULL; RB_INSERT(ltree, &links, le); return (0); }
struct CACHEINFO_DEPS *depcache_find_byhash(struct DEPCACHE *depcache, hash_t hashid) { struct CACHEINFO_DEPS tempinfo; if(!depcache) return NULL; tempinfo.hashid = hashid; return RB_FIND(CACHEINFO_DEPS_RB, &depcache->nodetree, &tempinfo); }
/* Find a flag in the arguments tree. */ struct args_entry * args_find(struct args *args, u_char ch) { struct args_entry entry; entry.flag = ch; return (RB_FIND(args_tree, &args->tree, &entry)); }
struct environ_entry * environ_find(struct environ *env, const char *name) { struct environ_entry envent; envent.name = (char *) name; return (RB_FIND(environ, env, &envent)); }
/* Find a job and return it. */ struct job * job_get(struct jobs *jobs, const char *cmd) { struct job job; job.cmd = (char *) cmd; return (RB_FIND(jobs, jobs, &job)); }
struct options_entry * options_find1(struct options *oo, const char *name) { struct options_entry p; p.name = (char *)name; return (RB_FIND(options_tree, &oo->tree, &p)); }