int main() { int i; UT_string *t; UT_vector v; utvector_init(&v, utstring_mm); UT_string s; utstring_init(&s); for(i=0; i<16; i++) { utstring_printf(&s, "."); utvector_push(&v, &s); } dump(&v); t = (UT_string*)utvector_head(&v); printf("head: %s %s\n", t?"non-null":"null", t?utstring_body(t):"-"); t = (UT_string*)utvector_tail(&v); printf("tail: %s %s\n", t?"non-null":"null", t?utstring_body(t):"-"); for(i=0; i<16; i++) { printf("shift\n"); utvector_shift(&v); t = (UT_string*)utvector_head(&v); printf("len: %d, head: %s %s\n", utvector_len(&v), t?"non-null":"null", t?utstring_body(t):"-"); } printf("extend\n"); t= (UT_string*)utvector_extend(&v); utstring_printf(t,"extended"); t = (UT_string*)utvector_head(&v); printf("len: %d, head: %s %s\n", utvector_len(&v), t?"non-null":"null", t?utstring_body(t):"-"); utvector_fini(&v); utstring_done(&s); return 0; }
static int pkg_repo_binary_build_search_query(UT_string *sql, match_t match, pkgdb_field field, pkgdb_field sort) { const char *how = NULL; const char *what = NULL; const char *orderby = NULL; how = pkg_repo_binary_search_how(match); switch (field) { case FIELD_NONE: what = NULL; break; case FIELD_ORIGIN: what = "origin"; break; case FIELD_NAME: what = "name"; break; case FIELD_NAMEVER: what = "name || '-' || version"; break; case FIELD_COMMENT: what = "comment"; break; case FIELD_DESC: what = "desc"; break; } if (what != NULL && how != NULL) utstring_printf(sql, how, what); switch (sort) { case FIELD_NONE: orderby = NULL; break; case FIELD_ORIGIN: orderby = " ORDER BY origin"; break; case FIELD_NAME: orderby = " ORDER BY name"; break; case FIELD_NAMEVER: orderby = " ORDER BY name, version"; break; case FIELD_COMMENT: orderby = " ORDER BY comment"; break; case FIELD_DESC: orderby = " ORDER BY desc"; break; } if (orderby != NULL) utstring_printf(sql, "%s", orderby); return (EPKG_OK); }
static void get_gw_data(char *username, char *device, JsonNode *last) { JsonNode *array; static char *types[] = { "batt", "ext", "status", NULL }; char **t, *js; static UT_string *ts = NULL, *u = NULL, *d = NULL; if (last == NULL || last->tag != JSON_OBJECT) return; for (t = types; t && *t; t++) { utstring_renew(u); utstring_renew(d); utstring_printf(u, "%s", username); utstring_printf(d, "%s", device); lowercase(UB(u)); lowercase(UB(d)); utstring_renew(ts); utstring_printf(ts, "%s/last/%s/%s/%s.json", STORAGEDIR, UB(u), UB(d), *t); /* Read file into JSON array and append to `last' object */ if ((js = slurp_file(UB(ts), TRUE)) != NULL) { if ((array = json_decode(js)) != NULL) { json_append_member(last, *t, array); } free(js); } } }
/* report to all configured destinations */ void report_status(pmtr_t *cfg) { int rc; time_t now = time(NULL); /* construct msg */ utstring_clear(cfg->s); utstring_printf(cfg->s, "report %s\n", cfg->report_id); job_t *j = NULL; while ( (j=(job_t*)utarray_next(cfg->jobs,j))) { if (j->respawn == 0) continue; /* don't advertise one-time jobs */ utstring_printf(cfg->s, "%s %c %u %d %s\n", j->name, j->disabled?'d':'e', (unsigned)(now - j->start_ts), (int)j->pid, *((char**)utarray_front(&j->cmdv))); } /* send to all dests */ int *fd=NULL; while ( (fd=(int*)utarray_next(cfg->report,fd))) { rc = write(*fd,utstring_body(cfg->s),utstring_len(cfg->s)); if (rc < 0 && errno != ECONNREFUSED) syslog(LOG_INFO,"write error: %s", strerror(errno)); if (rc >= 0 && rc < utstring_len(cfg->s)) { syslog(LOG_INFO,"incomplete write %d/%d", rc, utstring_len(cfg->s)); } } }
JsonNode *lister(char *user, char *device, time_t s_lo, time_t s_hi, int reverse) { JsonNode *json = json_mkobject(); UT_string *path = NULL; char *bp; utstring_renew(path); for (bp = user; bp && *bp; bp++) { if (isupper(*bp)) *bp = tolower(*bp); } for (bp = device; bp && *bp; bp++) { if (isupper(*bp)) *bp = tolower(*bp); } if (!user && !device) { utstring_printf(path, "%s/rec", STORAGEDIR); ls(UB(path), json); } else if (!device) { utstring_printf(path, "%s/rec/%s", STORAGEDIR, user); ls(UB(path), json); } else { utstring_printf(path, "%s/rec/%s/%s", STORAGEDIR, user, device); lsscan(UB(path), s_lo, s_hi, json, reverse); } return (json); }
// TODO: finish to test this one int o_bin_dbcreate(orientdb *o, orientdb_con *c, struct timeval *timeout, int nonblocking, const char *dbname, int dbtype) { UT_string *s; int i; debug_note(o, ORIENT_INFO, "executing binary method: DB_CREATE\n"); i = get_con_fd(o, c); if (i == -1) return -1; // check the dbname and dbtype parameters if ((dbname == NULL) || (strlen(dbname) == 0) || ((dbtype != O_DBTYPE_LOCAL) && (dbtype != O_DBTYPE_MEMORY))) return -1; utstring_new(s); // send the operation we want to execute to the server i = o_sendrequest(o, c, O_DB_CREATE); // send db name utstring_printf(s, dbname); i = o_putstring(o, c, s); utstring_clear(s); switch (dbtype) { case O_DBTYPE_LOCAL: utstring_printf(s, "local"); break; case O_DBTYPE_MEMORY: utstring_printf(s, "memory"); break; default: break; } // send db type i = o_putstring(o, c, s); utstring_clear(s); /* i = o_sendcredentials(o, c, ORIENT_USER); */ i = o_flushsendbuffer(o, c, timeout); i = o_getresponse(o, c, timeout); if (i == 1) { /* EXCEPTION!! */ debug_note(o, ORIENT_INFO, "received an exception\n"); i = o_getexceptions(o, c, timeout, s); debug_note(o, ORIENT_INFO, "E: %s\n", utstring_body(s)); utstring_free(s); return -1; } utstring_free(s); return 0; }
int packing_append_tree(struct packing *pack, const char *treepath, const char *newroot) { FTS *fts = NULL; FTSENT *fts_e = NULL; size_t treelen; UT_string *sb; char *paths[2] = { __DECONST(char *, treepath), NULL }; treelen = strlen(treepath); fts = fts_open(paths, FTS_PHYSICAL | FTS_XDEV, NULL); if (fts == NULL) goto cleanup; utstring_new(sb); while ((fts_e = fts_read(fts)) != NULL) { switch(fts_e->fts_info) { case FTS_D: case FTS_DEFAULT: case FTS_F: case FTS_SL: case FTS_SLNONE: /* Entries not within this tree are irrelevant. */ if (fts_e->fts_pathlen <= treelen) break; utstring_clear(sb); /* Strip the prefix to obtain the target path */ if (newroot) /* Prepend a root if one is specified */ utstring_printf(sb, "%s", newroot); /* +1 = skip trailing slash */ utstring_printf(sb, "%s", fts_e->fts_path + treelen + 1); packing_append_file_attr(pack, fts_e->fts_name, utstring_body(sb), NULL, NULL, 0, 0); break; case FTS_DC: case FTS_DNR: case FTS_ERR: case FTS_NS: /* XXX error cases, check fts_e->fts_errno and * bubble up the call chain */ break; default: break; } } utstring_free(sb); cleanup: fts_close(fts); return EPKG_OK; }
short o_bin_dataclusteradd(orientdb *o, orientdb_con *c, struct timeval *timeout, int nonblocking, const char *type, const char *name, const char *filename, int initialsize) { UT_string *s; int i; short x; debug_note(o, ORIENT_INFO, "executing binary method: DATACLUSTER_ADD\n"); i = get_con_fd(o, c); if (i == -1) return -1; utstring_new(s); // send the operation we want to execute to the server i = o_sendrequest(o, c, O_DATACLUSTER_ADD); // send cluster type utstring_printf(s, type); i = o_putstring(o, c, s); utstring_clear(s); // send cluster name utstring_printf(s, name); i = o_putstring(o, c, s); utstring_clear(s); if (strcmp(type, O_CLUSTER_PHYSICAL) == 0) { // the physical cluster need filename and initialsize, while memory and logical do not need them. // send cluster filename utstring_printf(s, filename); i = o_putstring(o, c, s); utstring_clear(s); // send cluster initialsize in bytes i = o_putint(o, c, initialsize); } i = o_flushsendbuffer(o, c, timeout); i = o_getresponse(o, c, timeout); if (i == 1) { /* EXCEPTION!! */ debug_note(o, ORIENT_INFO, "received an exception\n"); i = o_getexceptions(o, c, timeout, s); debug_note(o, ORIENT_INFO, "E: %s\n", utstring_body(s)); utstring_free(s); return -1; } // get the new cluster id x = o_getshort(o, c, timeout); utstring_free(s); return x; }
int64_t pkg_repo_binary_stat(struct pkg_repo *repo, pkg_stats_t type) { sqlite3 *sqlite = PRIV_GET(repo); sqlite3_stmt *stmt = NULL; int64_t stats = 0; UT_string *sql = NULL; int ret; utstring_new(sql); switch(type) { case PKG_STATS_LOCAL_COUNT: goto out; break; case PKG_STATS_LOCAL_SIZE: goto out; break; case PKG_STATS_REMOTE_UNIQUE: utstring_printf(sql, "SELECT COUNT(id) FROM main.packages;"); break; case PKG_STATS_REMOTE_COUNT: utstring_printf(sql, "SELECT COUNT(id) FROM main.packages;"); break; case PKG_STATS_REMOTE_SIZE: utstring_printf(sql, "SELECT SUM(pkgsize) FROM main.packages;"); break; case PKG_STATS_REMOTE_REPOS: goto out; break; } pkg_debug(4, "binary_repo: running '%s'", utstring_body(sql)); ret = sqlite3_prepare_v2(sqlite, utstring_body(sql), -1, &stmt, NULL); if (ret != SQLITE_OK) { ERROR_SQLITE(sqlite, utstring_body(sql)); goto out; } while (sqlite3_step(stmt) != SQLITE_DONE) { stats = sqlite3_column_int64(stmt, 0); } out: utstring_free(sql); if (stmt != NULL) sqlite3_finalize(stmt); return (stats); }
int main() { int i; UT_string *t; UT_vector v; utvector_init(&v, utvector_utstring); UT_string s; utstring_init(&s); for(i=0; i<16; i++) { utstring_printf(&s, "."); utvector_push(&v, &s); } dump(&v); t = (UT_string*)utvector_head(&v); printf("head: %s %s\n", t?"non-null":"null", t?utstring_body(t):"-"); t = (UT_string*)utvector_tail(&v); printf("tail: %s %s\n", t?"non-null":"null", t?utstring_body(t):"-"); printf("extend\n"); t = (UT_string*)utvector_extend(&v); utstring_bincpy(t, "hello", 5); dump(&v); t = (UT_string*)utvector_head(&v); printf("head: %s %s\n", t?"non-null":"null", t?utstring_body(t):"-"); t = (UT_string*)utvector_tail(&v); printf("tail: %s %s\n", t?"non-null":"null", t?utstring_body(t):"-"); utvector_fini(&v); utstring_done(&s); return 0; }
char *mtex2MML_combine_row_data(UT_array **environment_data_stack) { /* if no information was provided, give a standard sizing */ if (utarray_len(*environment_data_stack) == 0) { const char* s = "rowspacing=\"0.5ex\" rowlines=\"none\""; char* c = (char*)malloc(strlen(s) + 1); strcpy(c, s); return c; } envdata_t *row_data_elem = (envdata_t*) utarray_front(*environment_data_stack); char *row_spacing_data = row_data_elem->rowspacing, *row_lines_data = row_data_elem->rowlines, *row_attr; UT_string *row_attr_data; utstring_new(row_attr_data); /* combine the row spacing and row lines data */ utstring_printf(row_attr_data, "%s%s\" %s\"", "rowspacing=\"", row_spacing_data, row_lines_data); row_attr = string_dup(utstring_body(row_attr_data)); utarray_erase(*environment_data_stack, 0, 1); utstring_free(row_attr_data); return row_attr; }
char *mtex2MML_remove_excess_pipe_chars(char *string) { UT_string *columnalign; utstring_new(columnalign); char *dupe = string_dup(string); char *token = strtok(dupe, " "); char *attr_columnalign; while (token != NULL) { if (strncmp(token, "s", 1) != 0 && strncmp(token, "d", 1) != 0) { utstring_printf(columnalign, "%s ", token); } token = strtok(NULL, " "); } attr_columnalign = string_dup(utstring_body(columnalign)); free(dupe); utstring_free(columnalign); if (strlen(attr_columnalign) > 0) { mtex2MML_remove_last_char(attr_columnalign); /* remove the final space */ } return attr_columnalign; }
struct pkg_repo_it * pkg_repo_binary_require(struct pkg_repo *repo, const char *provide) { sqlite3_stmt *stmt; sqlite3 *sqlite = PRIV_GET(repo); UT_string *sql = NULL; int ret; const char basesql[] = "" "SELECT p.id, p.origin, p.name, p.version, p.comment, " "p.name as uniqueid, " "p.prefix, p.desc, p.arch, p.maintainer, p.www, " "p.licenselogic, p.flatsize, p.pkgsize, " "p.cksum, p.manifestdigest, p.path AS repopath, '%s' AS dbname " "FROM packages AS p INNER JOIN pkg_requires AS ps ON " "p.id = ps.package_id " "WHERE ps.require_id = (SELECT id FROM requires WHERE require=?1);"; utstring_new(sql); utstring_printf(sql, basesql, repo->name); pkg_debug(4, "Pkgdb: running '%s'", utstring_body(sql)); ret = sqlite3_prepare_v2(sqlite, utstring_body(sql), -1, &stmt, NULL); if (ret != SQLITE_OK) { ERROR_SQLITE(sqlite, utstring_body(sql)); utstring_free(sql); return (NULL); } utstring_free(sql); sqlite3_bind_text(stmt, 1, provide, -1, SQLITE_TRANSIENT); return (pkg_repo_binary_it_new(repo, stmt, PKGDB_IT_FLAG_ONCE)); }
luarest_status invoke_application(application* apps, UT_string* url, luarest_method m, luarest_response* res_code, luarest_content_type* con_type, UT_string* res_buf) { application* app = NULL; service *service; char* pch = NULL; char* tmp = utstring_body(url); UT_string* app_name; UT_string* key; utstring_new(app_name); pch = strchr(++tmp, '/'); if (pch == NULL) { return(LUAREST_ERROR); } utstring_bincpy(app_name, tmp, pch-tmp); HASH_FIND(hh, apps, utstring_body(app_name), utstring_len(app_name), app); utstring_free(app_name); if (app == NULL) { return(LUAREST_ERROR); } utstring_new(key); utstring_printf(key, "M%d#P%s", m, pch); HASH_FIND(hh, app->s, utstring_body(key), utstring_len(key), service); if (service == NULL) { return(LUAREST_ERROR); } invoke_lua(app->lua_state, service->callback_ref, res_code, con_type, res_buf); return(LUAREST_SUCCESS); }
/* addr is like "udp://127.0.0.1:3333". * only one listener can be set up currently. * we set up a UDP socket file descriptor bound to the port, * during the main loop we get SIGIO on incoming datagram */ void set_listen(parse_t *ps, char *addr) { in_addr_t local_ip; int rc = -1, port, flags; if (parse_spec(ps->cfg, ps->em, addr, &local_ip, &port, NULL)) goto done; if (ps->cfg->test_only) return; /* syntax looked ok */ int fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd == -1) {rc = -2; goto done;} /* set close-on-exec flag for the descriptor so our jobs don't inherit it */ flags = fcntl(fd, F_GETFD); flags |= FD_CLOEXEC; if (fcntl(fd, F_SETFD, flags) == -1) {rc = -3; goto done;} /* specify the local address and port */ struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_addr.s_addr = local_ip; sin.sin_port = htons(port); /* bind our socket to it */ if (bind(fd, (struct sockaddr*)&sin, sizeof(sin)) == -1) { close(fd); rc = -4; utstring_printf(ps->em,"can't bind to %s: %s", addr, strerror(errno)); goto done; } /* enable SIGIO to our pid whenever it becomes readable */ int fl = fcntl(fd, F_GETFL); fl |= O_ASYNC | O_NONBLOCK; fcntl(fd, F_SETFL, fl); fcntl(fd, F_SETOWN, getpid()); /* success */ utarray_push_back(ps->cfg->listen, &fd); rc = 0; done: if (rc == -1) utstring_printf(ps->em, " (at line %d)", ps->line); if (rc == -2) utstring_printf(ps->em,"can't open file descriptor"); if (rc == -3) utstring_printf(ps->em,"can't set close-on-exec"); if (rc == -4) { /* ps->em already set */ } if (rc < 0) ps->rc = -1; }
/* PLAYING WITH EXCEPTIONS */ int o_getexceptions(orientdb *o, orientdb_con *c, struct timeval *timeout, UT_string *str) { int i,len=0; utstring_clear(str); debug_note(o, ORIENT_DEBUG, "get a exception\n"); // if i it's == 1 it follows an exception with a cause (2 strings), else the exception is over while ((i = o_getbyte(o, c, timeout)) == 1) { if (len > 0) { utstring_printf(str, " | "); len += 3;} // read the first string len += o_getstring(o, c, timeout, str); utstring_printf(str, ": "); len += 2; // read the second string (it get automatically concatenated due to utstring_printf() macro in utstring.h) len += o_getstring(o, c, timeout, str); } return len; }
static char* fd_str(struct client_socket *socket) { struct fd_socket *self = (struct fd_socket*)socket->self; UT_string s; utstring_init(&s); utstring_printf(&s, "%s:%d", inet_ntoa(self->addr.sin_addr), self->addr.sin_port); return utstring_body(&s); }
char *bindump(char *buf, long buflen) { static UT_string *out = NULL; int i, ch; utstring_renew(out); for (i = 0; i < buflen; i++) { ch = buf[i]; if (isprint(ch)) { utstring_printf(out, "%c", ch); } else { utstring_printf(out, " %02X ", ch & 0xFF); } } return (UB(out)); }
struct pkg_repo_it * pkg_repo_binary_query(struct pkg_repo *repo, const char *pattern, match_t match) { sqlite3 *sqlite = PRIV_GET(repo); sqlite3_stmt *stmt = NULL; UT_string *sql = NULL; const char *comp = NULL; int ret; char basesql[BUFSIZ] = "" "SELECT id, origin, name, name as uniqueid, version, comment, " "prefix, desc, arch, maintainer, www, " "licenselogic, flatsize, pkgsize, " "cksum, manifestdigest, path AS repopath, '%s' AS dbname " "FROM packages AS p"; if (match != MATCH_ALL && (pattern == NULL || pattern[0] == '\0')) return (NULL); utstring_new(sql); comp = pkgdb_get_pattern_query(pattern, match); if (comp && comp[0]) strlcat(basesql, comp, sizeof(basesql)); utstring_printf(sql, basesql, repo->name); utstring_printf(sql, "%s", " ORDER BY name;"); pkg_debug(4, "Pkgdb: running '%s' query for %s", utstring_body(sql), pattern == NULL ? "all": pattern); ret = sqlite3_prepare_v2(sqlite, utstring_body(sql), utstring_len(sql), &stmt, NULL); if (ret != SQLITE_OK) { ERROR_SQLITE(sqlite, utstring_body(sql)); utstring_free(sql); return (NULL); } utstring_free(sql); if (match != MATCH_ALL && match != MATCH_CONDITION) sqlite3_bind_text(stmt, 1, pattern, -1, SQLITE_TRANSIENT); return (pkg_repo_binary_it_new(repo, stmt, PKGDB_IT_FLAG_ONCE)); }
int main() { UT_string *s; utstring_new(s); utstring_printf(s, "hello world!" ); printf("%s\n", utstring_body(s)); utstring_free(s); return 0; }
int rates_cmd(void *cp, cp_arg_t *arg, void *data) { utstring_renew(CF.s); unsigned i,hits; char *when; for(i=0; i<CF.frames_1h->num_buckets; i++) { hits = *(unsigned*)(bkt(CF.frames_1h,i)->data); when = asctime(localtime(&(bkt(CF.frames_1h,i)->start))); utstring_printf(CF.s, " %u frames: %s", hits, when); } cp_add_reply(cp,utstring_body(CF.s),utstring_len(CF.s)); }
int main(int argc, char * argv[]) { int opt,verbose=0; long dirmax=0; /* input spool */ char *dir=NULL,unit,*sz="10GB"; UT_string *s; utstring_new(s); while ( (opt = getopt(argc, argv, "v+s:")) != -1) { switch (opt) { default: usage(argv[0]); break; case 'v': verbose++; break; case 's': sz = strdup(optarg); switch (sscanf(sz, "%ld%c", &dirmax, &unit)) { case 2: /* check unit */ switch (unit) { case 't': case 'T': break; case 'g': case 'G': break; case 'm': case 'M': break; case 'k': case 'K': break; case '\r': case '\n': case ' ': case '\t': break; default: usage(argv[0]); break; } case 1: /* just a number in bytes */ break; default: usage(argv[0]); break; } break; } } if (optind >= argc) usage(argv[0]); if (!dirmax) usage(argv[0]); kv_spool_options.dir_max = dirmax; while (optind < argc) { dir = argv[optind++]; utstring_clear(s); utstring_printf(s,"%s/limits", dir); char *p = utstring_body(s); FILE *f = fopen(p, "w"); if (f == NULL) { fprintf(stderr,"cannot open %s: %s\n", p, strerror(errno)); continue; } fprintf(f, "%s", sz); fclose(f); sp_attrition(dir); } done: utstring_free(s); return 0; }
void nbt_record_tag(struct nbt_tag *tag, off_t pos, uint32_t count, UT_vector /* of nbt_stack_frame */ *nbt_stack, UT_vector /* of struct nbt_record */ *records) { /* do not record each item of a list; we record the list itself */ nbt_stack_frame *top; top = (nbt_stack_frame*)utvector_tail(nbt_stack); if (top && (top->tag.type == TAG_List)) return; /* record the tag. prepend stack tags to "fully-qualify" the name */ struct nbt_record *r = (struct nbt_record*)utvector_extend(records); r->tag = *tag; r->pos = pos; r->count = count; nbt_stack_frame *f = NULL; while ( (f = (nbt_stack_frame*)utvector_next(nbt_stack,f))) { utstring_printf(&r->fqname, "%.*s.", (int)f->tag.len, f->tag.name); } utstring_printf(&r->fqname, "%.*s", (int)tag->len, tag->name); }
struct pkg_repo_it * pkg_repo_binary_search(struct pkg_repo *repo, const char *pattern, match_t match, pkgdb_field field, pkgdb_field sort) { sqlite3 *sqlite = PRIV_GET(repo); sqlite3_stmt *stmt = NULL; UT_string *sql = NULL; int ret; const char *multireposql = "" "SELECT id, origin, name, version, comment, " "prefix, desc, arch, maintainer, www, " "licenselogic, flatsize, pkgsize, " "cksum, path AS repopath, '%1$s' AS dbname, '%2$s' AS repourl " "FROM packages "; if (pattern == NULL || pattern[0] == '\0') return (NULL); utstring_new(sql); utstring_printf(sql, multireposql, repo->name, repo->url); /* close the UNIONs and build the search query */ utstring_printf(sql, "%s", "WHERE "); pkg_repo_binary_build_search_query(sql, match, field, sort); utstring_printf(sql, "%s", ";"); pkg_debug(4, "Pkgdb: running '%s'", utstring_body(sql)); ret = sqlite3_prepare_v2(sqlite, utstring_body(sql), -1, &stmt, NULL); if (ret != SQLITE_OK) { ERROR_SQLITE(sqlite, utstring_body(sql)); utstring_free(sql); return (NULL); } utstring_free(sql); sqlite3_bind_text(stmt, 1, pattern, -1, SQLITE_TRANSIENT); return (pkg_repo_binary_it_new(repo, stmt, PKGDB_IT_FLAG_ONCE)); }
static void lsscan(char *pathpat, time_t s_lo, time_t s_hi, JsonNode *obj, int reverse) { struct dirent **namelist; int i, n; JsonNode *jarr = json_mkarray(); static UT_string *path = NULL; if (obj == NULL || obj->tag != JSON_OBJECT) return; utstring_renew(path); /* Set global t_ values */ t_lo = s_lo; t_hi = s_hi; if ((n = scandir(pathpat, &namelist, filter_filename, cmp)) < 0) { json_append_member(obj, "error", json_mkstring("Cannot lsscan requested directory")); return; } if (reverse) { for (i = n - 1; i >= 0; i--) { utstring_clear(path); utstring_printf(path, "%s/%s", pathpat, namelist[i]->d_name); json_append_element(jarr, json_mkstring(UB(path))); free(namelist[i]); } } else { for (i = 0; i < n; i++) { utstring_clear(path); utstring_printf(path, "%s/%s", pathpat, namelist[i]->d_name); json_append_element(jarr, json_mkstring(UB(path))); free(namelist[i]); } } free(namelist); json_append_member(obj, "results", jarr); }
/** * LUA syntax: application.register(method, url, callback) * * Return: boolean true on success * */ static int l_register(lua_State* state) { service* s; application* a = (application*)luaL_checkudata(state, 1, LUA_USERDATA_APPLICATION); int method = luaL_checkint(state, 2); const char* url = luaL_checkstring(state, 3); int ref = luaL_ref(state, LUA_REGISTRYINDEX); s = (service*)malloc(sizeof(service)); utstring_new(s->key); utstring_printf(s->key, "M%d#P%s", method, url); switch (method) { case 1: s->method = HTTP_METHOD_GET; break; case 2: s->method = HTTP_METHOD_POST; break; case 3: s->method = HTTP_METHOD_PUT; break; case 4: s->method = HTTP_METHOD_DELETE; break; case 5: s->method = HTTP_METHOD_OPTION; break; case 6: s->method = HTTP_METHOD_HEAD; break; } utstring_new(s->path); utstring_printf(s->path, url); s->callback_ref = ref; HASH_ADD_KEYPTR(hh, a->s, utstring_body(s->key), utstring_len(s->key), s); return(1); }
/** * prepare the given user, its password and its role to be used for new connections */ int o_prepare_user(orientdb *o, int role, const char *username, const char *password) { OM_DEFINE_OBJECT(o_handler,oh); // Checking o parameter if (o == NULL) return O_WRONG_PARAM; oh = get_oh(o); debug_note(o, ORIENT_DEBUG, "preparing for setting user and password\n"); // TODO: a would have nice feature: check for local security policies (pass is too short, it is not robust enough, user is banned, etc...) // checking which type of authentication role we are going to set up switch (role) { case ORIENT_ADMIN: debug_note(o, ORIENT_INFO, "setting user \"%s\" (ADMIN) and its password\n", username); OM_MUTEX_LOCK(o_prepared, oprep); utstring_clear(oh->admin->username); utstring_clear(oh->admin->password); utstring_printf(oh->admin->username, username); utstring_printf(oh->admin->password, password); OM_MUTEX_UNLOCK(o_prepared, oprep); break; case ORIENT_USER: debug_note(o, ORIENT_INFO, "setting user \"%s\" (USER) and its password\n", username); OM_MUTEX_LOCK(o_prepared, oprep); utstring_clear(oh->user->username); utstring_clear(oh->user->password); utstring_printf(oh->user->username, username); utstring_printf(oh->user->password, password); OM_MUTEX_UNLOCK(o_prepared, oprep); break; default: debug_note(o, ORIENT_CRITICAL, "unknown role was given setting user \"%s\" and its password\n", username); return O_WRONG_ROLE; break; } return O_OK; }
static int parse_spec(pmtr_t *cfg, UT_string *em, char *spec, in_addr_t *addr, int *port, char **iface) { char *proto = spec, *colon, *host, *at; struct hostent *h; int hlen, rc=-1; if (iface) *iface = NULL; if (strncmp(proto, "udp://", 6)) goto done; host = &spec[6]; if ( !(colon = strrchr(spec, ':'))) goto done; *port = atoi(colon+1); if ((*port < 0) || (*port > 65535)) goto done; if ( (at = strrchr(spec, '@')) != NULL) { // trailing @eth2 if (iface) *iface = at+1; } /* stop here if syntax checking */ if (cfg->test_only) return 0; /* dns lookup. */ *colon = '\0'; h = gethostbyname(host); hlen = strlen(host); *colon = ':'; if (!h) { utstring_printf(em, "lookup [%.*s]: %s", hlen, host, hstrerror(h_errno)); rc = -2; goto done; } *addr = ((struct in_addr*)h->h_addr)->s_addr; rc = 0; /* success */ done: if (rc == -1) utstring_printf(em,"required format: udp://1.2.3.4:5678[@eth2]"); return rc; }
/* ** The following code executes when a syntax error first occurs. */ static void yy_syntax_error( yyParser *yypParser, /* The parser */ int yymajor, /* The major type of the error token */ YYMINORTYPE yyminor /* The minor type of the error token */ ){ ParseARG_FETCH; #define TOKEN (yyminor.yy0) #line 10 "cfg.y" utstring_printf(ps->em, "error in %s line %d ", ps->cfg->file, ps->line); ps->rc=-1; #line 927 "cfg.c" ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ }
void monitorhook(struct udata *userdata, time_t now, char *topic) { // struct udata *ud = (struct udata *)userdata; /* TODO: add monitor hook to a "monitor" key in LMDB ? */ char mpath[BUFSIZ]; static UT_string *us = NULL; utstring_renew(us); utstring_printf(us, "%ld %s\n", now, topic); snprintf(mpath, sizeof(mpath), "%s/monitor", STORAGEDIR); safewrite(mpath, UB(us)); }