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)); }
END_TEST START_TEST (test_make_fwd) { UT_string str; utstring_init(&str); zfwd_rule_t rule; rule.port = htons(80); rule.fwd_ip = 0x04030201; rule.fwd_port = htons(83); zclient_rules_make_fwd(&str, PROTO_TCP, &rule); fail_if(0 != strcmp(utstring_body(&str), "fwd.tcp.80.1.2.3.4:83"), "make fwd str fail"); utstring_clear(&str); zclient_rules_make_fwd(&str, PROTO_UDP, &rule); fail_if(0 != strcmp(utstring_body(&str), "fwd.udp.80.1.2.3.4:83"), "make fwd str fail"); utstring_clear(&str); rule.fwd_port = 0; zclient_rules_make_fwd(&str, PROTO_UDP, &rule); fail_if(0 != strcmp(utstring_body(&str), "fwd.udp.80.1.2.3.4"), "make fwd str fail"); utstring_done(&str); }
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; }
END_TEST START_TEST (test_make_ports) { UT_string str; utstring_init(&str); uint16_t ports[] = {htons(53), htons(80), htons(443)}; size_t count = sizeof(ports) / sizeof(ports[0]); zclient_rules_make_ports(&str, PROTO_TCP, ACCESS_ALLOW, ports, count); fail_if(0 != strcmp(utstring_body(&str), "ports.allow.tcp.53.80.443"), "make ports str fail"); utstring_clear(&str); zclient_rules_make_ports(&str, PROTO_UDP, ACCESS_ALLOW, ports, count); fail_if(0 != strcmp(utstring_body(&str), "ports.allow.udp.53.80.443"), "make ports str fail"); utstring_clear(&str); zclient_rules_make_ports(&str, PROTO_TCP, ACCESS_DENY, ports, count); fail_if(0 != strcmp(utstring_body(&str), "ports.deny.tcp.53.80.443"), "make ports str fail"); utstring_clear(&str); zclient_rules_make_ports(&str, PROTO_UDP, ACCESS_DENY, ports, count); fail_if(0 != strcmp(utstring_body(&str), "ports.deny.udp.53.80.443"), "make ports str fail"); utstring_done(&str); }
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); }
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; }
int main() { UT_string *s,*t; char V_TestStr[] = "There are two needle\0s in this \0haystack with needle\0s."; char V_NeedleStr[] = "needle\0s"; long *V_KMP_Table; long V_FindPos; size_t V_StartPos; size_t V_FindCnt; utstring_new(s); utstring_new(t); utstring_bincpy(s, V_TestStr, sizeof(V_TestStr)-1); printf("\"%s\" len=%u\n", utstring_body(s), utstring_len(s)); utstring_bincpy(t, V_NeedleStr, sizeof(V_NeedleStr)-1); printf("\"%s\" len=%u\n", utstring_body(t), utstring_len(t)); V_KMP_Table = (long *)malloc(sizeof(long) * (utstring_len(t) + 1)); if (V_KMP_Table != NULL) { _utstring_BuildTable(utstring_body(t), utstring_len(t), V_KMP_Table); V_FindCnt = 0; V_FindPos = 0; V_StartPos = 0; do { V_FindPos = _utstring_find(utstring_body(s) + V_StartPos, utstring_len(s) - V_StartPos, utstring_body(t), utstring_len(t), V_KMP_Table); if (V_FindPos >= 0) { V_FindPos += V_StartPos; V_FindCnt++; V_StartPos = V_FindPos + 1; } printf("utstring_find()=%ld\n", V_FindPos); } while (V_FindPos >= 0); printf("FindCnt=%u\n", V_FindCnt); free(V_KMP_Table); } else { printf("malloc() failed...\n"); } utstring_free(s); utstring_free(t); return 0; }
//////////////////////////////////////////////////////////////// // // output_read // // This function reads a fastq record from one file and writes // it to another. It assumes the header in the first line of // record has already been stored in fwd. The last 3 lines of // the record are read from input and the record is written // unaltered to output. A read will also be written to // mates_output if mates_output is not null. If rev is not null // this read will be written to mates_output with reversed // sequence and quality. // void output_read(UT_string *fwd, UT_string *rev, UT_string *tmp, FILE *input, FILE *output, FILE *mates_output, int fmt_fasta) { if (mates_output) { if (rev) { utstring_clear(rev); utstring_concat(rev, fwd); // head ss_get_utstring(input, tmp); // seq if (output) { utstring_concat(fwd, tmp); } ss_trunc_utstring(tmp, 1); // remove newline ss_rev_utstring(tmp); ss_strcat_utstring(tmp, "\n"); // add newline back utstring_concat(rev, tmp); if (!fmt_fasta) { ss_get_utstring(input, tmp); // extra if (output) { utstring_concat(fwd, tmp); } utstring_concat(rev, tmp); ss_get_utstring(input, tmp); // qual if (output) { utstring_concat(fwd, tmp); } ss_trunc_utstring(tmp, 1); // remove newline ss_rev_utstring(tmp); ss_strcat_utstring(tmp, "\n"); // add newline back utstring_concat(rev, tmp); } fputs(utstring_body(rev), mates_output); if (output) { fputs(utstring_body(fwd), output); } } else { ss_get_cat_utstring(input, fwd); if (!fmt_fasta) { ss_get_cat_utstring(input, fwd); ss_get_cat_utstring(input, fwd); } fputs(utstring_body(fwd), mates_output); if (output) { fputs(utstring_body(fwd), output); } } } else { ss_get_cat_utstring(input, fwd); if (!fmt_fasta) { ss_get_cat_utstring(input, fwd); ss_get_cat_utstring(input, fwd); } fputs(utstring_body(fwd), output); } }
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); }
END_TEST START_TEST (test_make_bw) { UT_string str; utstring_init(&str); zclient_rules_make_bw(&str, 524288, DIR_DOWN); fail_if(0 != strcmp(utstring_body(&str), "bw.4096KBit.down"), "make bw down str fail"); utstring_clear(&str); zclient_rules_make_bw(&str, 524288, DIR_UP); fail_if(0 != strcmp(utstring_body(&str), "bw.4096KBit.up"), "make bw up str fail"); utstring_done(&str); }
int o_bin_shutdown(orientdb *o, orientdb_con *c, struct timeval *timeout, int nonblocking) { UT_string *s = NULL; int i; debug_note(o, ORIENT_INFO, "executing binary method: SHUTDOWN\n"); i = get_con_fd(o, c); if (i == -1) return -1; // send the operation we want to execute to the server i = o_sendrequest(o, c, O_SHUTDOWN); i = o_sendcredentials(o, c, ORIENT_ADMIN); 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"); utstring_new(s); i = o_getexceptions(o, c, timeout, s); debug_note(o, ORIENT_INFO, "E: %s\n", utstring_body(s)); utstring_free(s); return -1; } 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; }
END_TEST START_TEST (test_make_p2p_policer) { UT_string str; utstring_init(&str); crules_make_p2p_policy(&str, 0); fail_if(0 != strcmp(utstring_body(&str), "p2p_policy.0"), "make p2p_policy str fail"); utstring_clear(&str); crules_make_p2p_policy(&str, 1); fail_if(0 != strcmp(utstring_body(&str), "p2p_policy.1"), "make p2p_policy str fail"); utstring_done(&str); }
void append_to_client_buf(UT_string *f) { assert(utarray_len(cfg.outbufs) > 0); UT_string **s=NULL; size_t l,least,c; char *b; int i=0,lx; b = utstring_body(f); l = utstring_len(f); switch(cfg.mode) { case fan: // send to ALL clients while ( (s=(UT_string**)utarray_next(cfg.outbufs,s))) { utstring_bincpy(*s,b,l); } break; case round_robin: // send to ONE client while ( (s=(UT_string**)utarray_next(cfg.outbufs,s))) { c = utstring_len(*s); if ((i==0) || (c < least)) {least=c; lx=i;} i++; } s = (UT_string**)utarray_eltptr(cfg.outbufs,lx); utstring_bincpy(*s,b,l); break; } }
/* 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)); } } }
static int JsonParser_internalData(struct ParserInternal *pi) { if (pi->quote_begin) { pi->error = JSON_ERR_QUOTE; return JSON_NOK; } if (utstring_len(pi->key) > 0 || utstring_len(pi->value) > 0) { if (pi->elemData) { pi->elemData(pi->parser, utstring_body(pi->key), utstring_body(pi->value) ); } } JsonParser_internalReset(pi); return 0; }
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); }
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)); }
// 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 set_to_binary(void *set, UT_string *bin) { uint32_t l, u, a,b,c,d, abcd; uint16_t s; uint8_t g; double h; utstring_clear(bin); l=0; utstring_bincpy(bin,&l,sizeof(l)); // placeholder for size prefix int rc=-1,i=0,*t; kv_t *kv, kvdef; char **k=NULL,**def; while( (k=(char**)utarray_next(output_keys,k))) { kv = kv_get(set,*k); t = (int*)utarray_eltptr(output_types,i); assert(t); def = (char**)utarray_eltptr(output_defaults,i); assert(def); if (kv==NULL) { /* no such key */ kv=&kvdef; if (*def) {kv->val=*def; kv->vlen=strlen(*def);} /* default */ else if (*t == str) {kv->val=NULL; kv->vlen=0;} /* zero len string */ else { fprintf(stderr,"required key %s not present in spool frame\n", *k); goto done; } } switch(*t) { case d64: h=atof(kv->val); utstring_bincpy(bin,&h,sizeof(h)); break; case i8: g=atoi(kv->val); utstring_bincpy(bin,&g,sizeof(g)); break; case i16: s=atoi(kv->val); utstring_bincpy(bin,&s,sizeof(s)); break; case i32: u=atoi(kv->val); utstring_bincpy(bin,&u,sizeof(u)); break; case str: l=kv->vlen; utstring_bincpy(bin,&l,sizeof(l)); /* length prefix */ utstring_bincpy(bin,kv->val,kv->vlen); /* string itself */ break; case ipv4: if ((sscanf(kv->val,"%u.%u.%u.%u",&a,&b,&c,&d) != 4) || (a > 255 || b > 255 || c > 255 || d > 255)) { fprintf(stderr,"invalid IP for key %s: %s\n",*k,kv->val); goto done; } abcd = (a << 24) | (b << 16) | (c << 8) | d; abcd = htonl(abcd); utstring_bincpy(bin,&abcd,sizeof(abcd)); break; default: assert(0); break; } i++; } uint32_t len = utstring_len(bin); len -= sizeof(len); // length does not include itself char *length_prefix = utstring_body(bin); memcpy(length_prefix, &len, sizeof(len)); rc = 0; done: return rc; }
static luarest_status verify_application(application** apps, const char* appName, UT_string* path) { int ret; lua_State* ls = luaL_newstate(); application* app; luaL_openlibs(ls); luaopen_luarestlibs(ls); ret = luaL_loadfile(ls, utstring_body(path)); if (ret != 0) { printf("Couldn't load file: %s\n", lua_tostring(ls, -1)); lua_close(ls); return(LUAREST_ERROR); } ret = lua_pcall(ls, 0, 0, 0); if (ret != 0) { printf("Couldn't execute LUA Script %s\n", lua_tostring(ls, -1)); lua_close(ls); return(LUAREST_ERROR); } lua_getglobal(ls, "luarest_init"); if (lua_isfunction(ls, -1) == 0) { printf("Couln'd find 'luarest_init' table in LUA script!\n"); lua_close(ls); return(LUAREST_ERROR); } app = (application*)lua_newuserdata(ls, sizeof(application)); luaL_getmetatable(ls, LUA_USERDATA_APPLICATION); lua_setmetatable(ls, -2); app->s = NULL; utstring_new(app->name); utstring_printf(app->name, appName); if (lua_pcall(ls, 1, 0, 0) != 0) { printf("Error calling luarest_init: %s\n!", lua_tostring(ls, -1)); lua_close(ls); return(LUAREST_ERROR); } app->lua_state = ls; HASH_ADD_KEYPTR(hh, *apps, utstring_body(app->name), utstring_len(app->name), app); return(LUAREST_SUCCESS); }
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; }
int main() { UT_string *s; utstring_new(s); utstring_printf(s, "hello world!" ); printf("%s\n", utstring_body(s)); utstring_free(s); return 0; }
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)); }
int main() { UT_string *err; utstring_new(err); void *c60; c60 = c60_client_init_fromfile("config.txt",err); if (!c60) fprintf(stderr,"init failed: %s",utstring_body(err)); c60_send(c60, "recipient", "hello!", 7); c60_client_close(c60); return 0; }
END_TEST START_TEST (test_make_identity) { UT_string str; utstring_init(&str); zclient_rules_make_identity(&str, 31337, "ABABA"); fail_if(0 != strcmp(utstring_body(&str), "identity.31337.ABABA"), "make identity str fail"); utstring_done(&str); }
static PyObject* binaryplist_encode(PyObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = {"obj", "unique", "debug", "convert_nulls", "max_recursion", "object_hook", "as_ascii", NULL}; PyObject *newobj = NULL; PyObject *oinput = NULL; PyObject *ounique = NULL; PyObject *odebug = NULL; PyObject *orecursion = NULL; binaryplist_encoder encoder; memset(&encoder, 0, sizeof(binaryplist_encoder)); encoder.convert_nulls = Py_False; encoder.as_ascii = Py_False; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOOOOO", kwlist, &oinput, &ounique, &odebug, &(encoder.convert_nulls), &orecursion, &(encoder.object_hook), &(encoder.as_ascii))) { return NULL; } if (encoder.object_hook && !PyCallable_Check(encoder.object_hook)) { PyErr_SetString(PLIST_Error, "object_hook is not callable"); return NULL; } encoder.ref_table = PyDict_New(); encoder.objects = PyList_New(0); utstring_new(encoder.output); if (!ounique || (ounique && PyObject_IsTrue(ounique))) { /* default to True */ encoder.dounique = 1; encoder.uniques = PyDict_New(); } if (odebug && PyObject_IsTrue(odebug)) { encoder.debug = 1; } if (orecursion && PyInt_Check(orecursion)) { encoder.max_recursion = PyInt_AsLong(orecursion); } else { encoder.max_recursion = 1024*16; } if (encoder_encode_object(&encoder, oinput) == BINARYPLIST_OK && encoder_write(&encoder) == BINARYPLIST_OK) { newobj = PyString_FromStringAndSize(utstring_body(encoder.output), utstring_len(encoder.output)); } Py_DECREF(encoder.ref_table); Py_DECREF(encoder.objects); Py_XDECREF(encoder.uniques); utstring_free(encoder.output); return newobj; }
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; }