From 2243a41e9d46230b9e27b9c38b6b7addf202e5f3 Mon Sep 17 00:00:00 2001 From: adam Date: Mon, 22 Apr 2013 01:08:28 +0700 Subject: [PATCH] #12 winport branch merged + applying changes to linux, some tests need debug --- tcejdb/Makefile.in | 2 +- tcejdb/bson.c | 4 +- tcejdb/bson.h | 1 + tcejdb/configure.ac | 6 +- tcejdb/ejdb.c | 56 +- tcejdb/myconf.h | 5 +- tcejdb/nix/platform.c | 10 + tcejdb/nix/platform.h | 21 + tcejdb/platform.c | 8 + tcejdb/tcadb.c | 14 +- tcejdb/tcamgr.c | 1664 +++++++++++++++--------------- tcejdb/tcamttest.c | 826 ++++++++------- tcejdb/tcatest.c | 24 +- tcejdb/tcawmgr.c | 4 +- tcejdb/tcbdb.c | 56 +- tcejdb/tcbmgr.c | 16 +- tcejdb/tcbmttest.c | 80 +- tcejdb/tcbtest.c | 84 +- tcejdb/tcfdb.c | 40 +- tcejdb/tcfdb.h | 2 +- tcejdb/tcfmgr.c | 1293 ++++++++++++----------- tcejdb/tcfmttest.c | 2031 ++++++++++++++++++------------------ tcejdb/tcftest.c | 44 +- tcejdb/tchdb.c | 68 +- tcejdb/tchmgr.c | 10 +- tcejdb/tchmttest.c | 62 +- tcejdb/tchtest.c | 62 +- tcejdb/tctdb.c | 18 +- tcejdb/tctmgr.c | 26 +- tcejdb/tctmttest.c | 20 +- tcejdb/tcttest.c | 24 +- tcejdb/tcucodec.c | 2257 ++++++++++++++++++++--------------------- tcejdb/tcumttest.c | 16 +- tcejdb/tcutest.c | 18 +- tcejdb/tcutil.c | 18 +- 35 files changed, 4409 insertions(+), 4481 deletions(-) create mode 100644 tcejdb/nix/platform.c create mode 100644 tcejdb/nix/platform.h diff --git a/tcejdb/Makefile.in b/tcejdb/Makefile.in index 8997bb0..2d02868 100644 --- a/tcejdb/Makefile.in +++ b/tcejdb/Makefile.in @@ -844,6 +844,6 @@ tokyocabinet_all.c : myconf.c tcutil.c md5.c tchdb.c tcbdb.c tcfdb.c tctdb.c tca tokyocabinet_all.o : myconf.h tcutil.h tchdb.h tcbdb.h tcfdb.h tctdb.h tcadb.h -platform.o : basedefs.h platform.c win32/platform.c +platform.o : basedefs.h platform.c win32/platform.c nix/platform.c # END OF FILE diff --git a/tcejdb/bson.c b/tcejdb/bson.c index f34cc7f..333d3ee 100644 --- a/tcejdb/bson.c +++ b/tcejdb/bson.c @@ -1274,7 +1274,7 @@ void bson_numstr(char *str, int64_t i) { if (i >= 0 && i < 1000) memcpy(str, bson_numstrs[i], 4); else - sprintf(str, "%" PRIdMAX "", (long long int) i); + sprintf(str, "%" PRIdMAX "", (int64_t) i); } #pragma GCC diagnostic push @@ -1285,7 +1285,7 @@ int bson_numstrn(char *str, int maxbuf, int64_t i) { memcpy(str, bson_numstrs[i], 4); return strlen(bson_numstrs[i]); } else { - return snprintf(str, maxbuf, "%" PRIdMAX "", (long long int) i); + return snprintf(str, maxbuf, "%" PRIdMAX "", (int64_t) i); } } #pragma GCC diagnostic pop diff --git a/tcejdb/bson.h b/tcejdb/bson.h index 774605d..132f653 100644 --- a/tcejdb/bson.h +++ b/tcejdb/bson.h @@ -25,6 +25,7 @@ #include #include #include +#include #define BSON_IS_NUM_TYPE(atype) (atype == BSON_INT || atype == BSON_LONG || atype == BSON_DOUBLE) diff --git a/tcejdb/configure.ac b/tcejdb/configure.ac index b4e4154..bbd01b3 100644 --- a/tcejdb/configure.ac +++ b/tcejdb/configure.ac @@ -39,7 +39,7 @@ MYPCFILES="tcejdb.pc" MYCFLAGS="$MYCFLAGS -std=gnu99 -Wall -fsigned-char -O2 -Wfatal-errors" MYCPPFLAGS="-I. -I$HOME/include -I\$(INCLUDEDIR) -I/usr/local/include" MYCPPFLAGS="$MYCPPFLAGS -D_UNICODE -DNDEBUG -D_GNU_SOURCE=1 -D_REENTRANT -D__EXTENSIONS__" -MYLDFLAGS="-L. -L\$(LIBDIR) -L$HOME/lib -L/usr/local/lib -L/usr/lib -Wl,-Bstatic" +MYLDFLAGS="-L. -L\$(LIBDIR) -L$HOME/lib -L/usr/local/lib -L/usr/lib" MYCMDLDFLAGS="" MYRUNPATH="\$(LIBDIR)" MYLDLIBPATHENV="LD_LIBRARY_PATH" @@ -351,13 +351,13 @@ case $host_os in AC_CHECK_LIB(pcre, pcre_compile, [], AC_MSG_ERROR([pcre lib is required])) AC_CHECK_LIB(pcreposix, regcomp, [], AC_MSG_ERROR([pcreposix lib is required])) AC_CHECK_LIB(pthreadGC2, main, [], AC_MSG_ERROR([pthreadGC2 lubrary is required])) - MYLDFLAGS="-static-libgcc -static-libstdc++ $MYLDFLAGS" + MYLDFLAGS="-static-libgcc -static-libstdc++ $MYLDFLAGS -Wl,-Bstatic" MYHEADERFILES="$MYHEADERFILES win32/platform.h" MYCPPFLAGS="$MYCPPFLAGS -DPCRE_STATIC -DPTW32_STATIC_LIB" ;; *) + MYHEADERFILES="$MYHEADERFILES nix/platform.h" AC_CHECK_HEADER(regex.h, true, AC_MSG_ERROR([regex.h is required])) - AC_CHECK_LIB(regex, regcomp, [], AC_MSG_ERROR([regex lib is required])) AC_CHECK_LIB(pthread, main, [], AC_MSG_ERROR([pthread lubrary is required])) MYCFLAGS="$MYCFLAGS -fPIC" ;; diff --git a/tcejdb/ejdb.c b/tcejdb/ejdb.c index 7fd63bf..f81ba49 100644 --- a/tcejdb/ejdb.c +++ b/tcejdb/ejdb.c @@ -97,7 +97,7 @@ static bool _updatebsonidx(EJCOLL *jcoll, const bson_oid_t *oid, const bson *bs, static bool _metasetopts(EJDB *jb, const char *colname, EJCOLLOPTS *opts); static bool _metagetopts(EJDB *jb, const char *colname, EJCOLLOPTS *opts); static bson* _metagetbson(EJDB *jb, const char *colname, int colnamesz, const char *mkey); -static bson* _metagetbson2(EJCOLL *jcoll, const char *mkey) __attribute__ ((unused)); +static bson* _metagetbson2(EJCOLL *jcoll, const char *mkey) __attribute__((unused)); static bool _metasetbson(EJDB *jb, const char *colname, int colnamesz, const char *mkey, bson *val, bool merge, bool mergeoverwrt); static bool _metasetbson2(EJCOLL *jcoll, const char *mkey, bson *val, bool merge, bool mergeoverwrt); @@ -130,7 +130,7 @@ extern const char *utf8proc_errmsg(ssize_t errcode); static const bool yes = true; static const bool no = false; - const char* ejdberrmsg(int ecode) { +const char* ejdberrmsg(int ecode) { if (ecode > -6 && ecode < 0) { //Hook for negative error codes of utf8proc library return utf8proc_errmsg(ecode); } @@ -155,7 +155,7 @@ static const bool no = false; } } - bool ejdbisvalidoidstr(const char *oid) { +bool ejdbisvalidoidstr(const char *oid) { if (!oid) { return false; } @@ -167,12 +167,12 @@ static const bool no = false; } /* Get the last happened error code of a database object. */ - int ejdbecode(EJDB *jb) { +int ejdbecode(EJDB *jb) { assert(jb && jb->metadb); return tctdbecode(jb->metadb); } - EJDB* ejdbnew(void) { +EJDB* ejdbnew(void) { EJDB *jb; TCCALLOC(jb, 1, sizeof (*jb)); jb->metadb = tctdbnew(); @@ -186,7 +186,7 @@ static const bool no = false; return jb; } - void ejdbdel(EJDB *jb) { +void ejdbdel(EJDB *jb) { assert(jb && jb->metadb); if (JBISOPEN(jb)) ejdbclose(jb); for (int i = 0; i < jb->cdbsnum; ++i) { @@ -204,7 +204,7 @@ static const bool no = false; TCFREE(jb); } - bool ejdbclose(EJDB *jb) { +bool ejdbclose(EJDB *jb) { JBENSUREOPENLOCK(jb, true, false); bool rv = true; for (int i = 0; i < jb->cdbsnum; ++i) { @@ -222,11 +222,11 @@ static const bool no = false; return rv; } - bool ejdbisopen(EJDB *jb) { +bool ejdbisopen(EJDB *jb) { return JBISOPEN(jb); } - bool ejdbopen(EJDB *jb, const char *path, int mode) { +bool ejdbopen(EJDB *jb, const char *path, int mode) { assert(jb && path); assert(jb->metadb); if (!JBLOCKMETHOD(jb, true)) return false; @@ -258,7 +258,7 @@ finish: return rv; } - EJCOLL* ejdbgetcoll(EJDB *jb, const char *colname) { +EJCOLL* ejdbgetcoll(EJDB *jb, const char *colname) { assert(colname); EJCOLL *coll = NULL; JBENSUREOPENLOCK(jb, false, NULL); @@ -267,7 +267,7 @@ finish: return coll; } - TCLIST* ejdbgetcolls(EJDB *jb) { +TCLIST* ejdbgetcolls(EJDB *jb) { assert(jb); EJCOLL *coll = NULL; JBENSUREOPENLOCK(jb, false, NULL); @@ -280,7 +280,7 @@ finish: return ret; } - EJCOLL* ejdbcreatecoll(EJDB *jb, const char *colname, EJCOLLOPTS *opts) { +EJCOLL* ejdbcreatecoll(EJDB *jb, const char *colname, EJCOLLOPTS *opts) { assert(colname); EJCOLL *coll = ejdbgetcoll(jb, colname); if (coll) { @@ -309,7 +309,7 @@ finish: return coll; } - bool ejdbrmcoll(EJDB *jb, const char *colname, bool unlinkfile) { +bool ejdbrmcoll(EJDB *jb, const char *colname, bool unlinkfile) { assert(colname); JBENSUREOPENLOCK(jb, true, false); bool rv = true; @@ -359,11 +359,11 @@ finish: } /* Save/Update BSON */ - bool ejdbsavebson(EJCOLL *jcoll, bson *bs, bson_oid_t *oid) { +bool ejdbsavebson(EJCOLL *jcoll, bson *bs, bson_oid_t *oid) { return ejdbsavebson2(jcoll, bs, oid, false); } - bool ejdbsavebson2(EJCOLL *jcoll, bson *bs, bson_oid_t *oid, bool merge) { +bool ejdbsavebson2(EJCOLL *jcoll, bson *bs, bson_oid_t *oid, bool merge) { assert(jcoll); if (!bs || bs->err || !bs->finished) { _ejdbsetecode(jcoll->jb, JBEINVALIDBSON, __FILE__, __LINE__, __func__); @@ -379,7 +379,7 @@ finish: return rv; } - bool ejdbrmbson(EJCOLL *jcoll, bson_oid_t *oid) { +bool ejdbrmbson(EJCOLL *jcoll, bson_oid_t *oid) { assert(jcoll && oid); if (!JBISOPEN(jcoll->jb)) { _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__); @@ -406,7 +406,7 @@ finish: return rv; } - bson* ejdbloadbson(EJCOLL *jcoll, const bson_oid_t *oid) { +bson* ejdbloadbson(EJCOLL *jcoll, const bson_oid_t *oid) { assert(jcoll && oid); if (!JBISOPEN(jcoll->jb)) { _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__); @@ -437,7 +437,7 @@ finish: return ret; } - EJQ* ejdbcreatequery(EJDB *jb, bson *qobj, bson *orqobjs, int orqobjsnum, bson *hints) { +EJQ* ejdbcreatequery(EJDB *jb, bson *qobj, bson *orqobjs, int orqobjsnum, bson *hints) { assert(jb); if (!qobj || qobj->err || !qobj->finished) { _ejdbsetecode(jb, JBEINVALIDBSON, __FILE__, __LINE__, __func__); @@ -481,12 +481,12 @@ error: return NULL; } - void ejdbquerydel(EJQ *q) { +void ejdbquerydel(EJQ *q) { _qrydel(q, true); } /** Set index */ - bool ejdbsetindex(EJCOLL *jcoll, const char *fpath, int flags) { +bool ejdbsetindex(EJCOLL *jcoll, const char *fpath, int flags) { assert(jcoll && fpath); bool rv = true; bson *imeta = NULL; @@ -641,7 +641,7 @@ finish: return rv; } - uint32_t ejdbupdate(EJCOLL *jcoll, bson *qobj, bson *orqobjs, int orqobjsnum, bson *hints, TCXSTR *log) { +uint32_t ejdbupdate(EJCOLL *jcoll, bson *qobj, bson *orqobjs, int orqobjsnum, bson *hints, TCXSTR *log) { assert(jcoll); uint32_t count = 0; EJQ *q = ejdbcreatequery(jcoll->jb, qobj, orqobjs, orqobjsnum, hints); @@ -653,7 +653,7 @@ finish: return count; } - TCLIST* ejdbqryexecute(EJCOLL *jcoll, const EJQ *q, uint32_t *count, int qflags, TCXSTR *log) { +TCLIST* ejdbqryexecute(EJCOLL *jcoll, const EJQ *q, uint32_t *count, int qflags, TCXSTR *log) { assert(jcoll && q && q->qobjlist); if (!JBISOPEN(jcoll->jb)) { _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__); @@ -670,7 +670,7 @@ finish: return res; } - bool ejdbsyncoll(EJCOLL *jcoll) { +bool ejdbsyncoll(EJCOLL *jcoll) { assert(jcoll); if (!JBISOPEN(jcoll->jb)) { _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__); @@ -683,7 +683,7 @@ finish: return rv; } - bool ejdbsyncdb(EJDB *jb) { +bool ejdbsyncdb(EJDB *jb) { assert(jb); JBENSUREOPENLOCK(jb, true, false); bool rv = true; @@ -699,7 +699,7 @@ finish: return rv; } - bool ejdbtranbegin(EJCOLL *jcoll) { +bool ejdbtranbegin(EJCOLL *jcoll) { assert(jcoll); if (!JBISOPEN(jcoll->jb)) { _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__); @@ -726,7 +726,7 @@ finish: return true; } - bool ejdbtrancommit(EJCOLL *jcoll) { +bool ejdbtrancommit(EJCOLL *jcoll) { assert(jcoll); if (!JBISOPEN(jcoll->jb)) { _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__); @@ -745,7 +745,7 @@ finish: return !err; } - bool ejdbtranabort(EJCOLL *jcoll) { +bool ejdbtranabort(EJCOLL *jcoll) { assert(jcoll); if (!JBISOPEN(jcoll->jb)) { _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__); @@ -764,7 +764,7 @@ finish: return !err; } - bool ejdbtranstatus(EJCOLL *jcoll, bool *txactive) { +bool ejdbtranstatus(EJCOLL *jcoll, bool *txactive) { assert(jcoll && txactive); if (!JBISOPEN(jcoll->jb)) { _ejdbsetecode(jcoll->jb, TCEINVALID, __FILE__, __LINE__, __func__); diff --git a/tcejdb/myconf.h b/tcejdb/myconf.h index e77ac75..3d0a771 100644 --- a/tcejdb/myconf.h +++ b/tcejdb/myconf.h @@ -212,6 +212,9 @@ #define GET_STDIN_HANDLE() GetStdHandle(STD_INPUT_HANDLE) #define CLOSEFH(_fd) (CloseHandle(_fd)) #else +#ifdef __unix +#include "nix/platform.h" +#endif #include #include #include @@ -221,7 +224,7 @@ #define GET_STDOUT_HANDLE() (1) #define GET_STDERR_HANDLE() (2) #define GET_STDIN_HANDLE() (0) -#define CLOSEFH(_fd) (close(_fd) != -1) +#define CLOSEFH(_fd) (closefd(_fd)) #define sysconf_SC_CLK_TCK sysconf(_SC_CLK_TCK) #endif diff --git a/tcejdb/nix/platform.c b/tcejdb/nix/platform.c new file mode 100644 index 0000000..aeb60a3 --- /dev/null +++ b/tcejdb/nix/platform.c @@ -0,0 +1,10 @@ +#include "platform.h" +#include "../tcutil.h" +#include "../myconf.h" + +#include + +bool closefd(int fd) { + return (close(fd) != -1); +} + diff --git a/tcejdb/nix/platform.h b/tcejdb/nix/platform.h new file mode 100644 index 0000000..64525c6 --- /dev/null +++ b/tcejdb/nix/platform.h @@ -0,0 +1,21 @@ +/* + * File: platform.h + * Author: adam + * + * Created on April 21, 2013, 10:27 PM + */ + +#ifndef PLATFORM_H +#define PLATFORM_H + +#include "basedefs.h" +#include + +EJDB_EXTERN_C_START + +bool closefd(int fd); + +EJDB_EXTERN_C_END + +#endif /* PLATFORM_H */ + diff --git a/tcejdb/platform.c b/tcejdb/platform.c index 20fd85a..70c696a 100644 --- a/tcejdb/platform.c +++ b/tcejdb/platform.c @@ -1,3 +1,11 @@ #ifdef _WIN32 #include "win32/platform.c" #endif + +#ifdef __unix +#include "nix/platform.c" +#endif + + + + diff --git a/tcejdb/tcadb.c b/tcejdb/tcadb.c index e680991..793f56f 100644 --- a/tcejdb/tcadb.c +++ b/tcejdb/tcadb.c @@ -446,7 +446,7 @@ bool tcadbput(TCADB *adb, const void *kbuf, int ksiz, const void *vbuf, int vsiz break; case ADBOTDB: if (ksiz < 1) { - ksiz = sprintf(numbuf, "%" PRIdMAX "", (long long) tctdbgenuid(adb->tdb)); + ksiz = sprintf(numbuf, "%" PRIdMAX "", (int64_t) tctdbgenuid(adb->tdb)); kbuf = numbuf; } if (!tctdbput2(adb->tdb, kbuf, ksiz, vbuf, vsiz)) err = true; @@ -520,7 +520,7 @@ bool tcadbputkeep(TCADB *adb, const void *kbuf, int ksiz, const void *vbuf, int break; case ADBOTDB: if (ksiz < 1) { - ksiz = sprintf(numbuf, "%" PRIdMAX "", (long long) tctdbgenuid(adb->tdb)); + ksiz = sprintf(numbuf, "%" PRIdMAX "", (int64_t) tctdbgenuid(adb->tdb)); kbuf = numbuf; } if (!tctdbputkeep2(adb->tdb, kbuf, ksiz, vbuf, vsiz)) err = true; @@ -590,7 +590,7 @@ bool tcadbputcat(TCADB *adb, const void *kbuf, int ksiz, const void *vbuf, int v break; case ADBOTDB: if (ksiz < 1) { - ksiz = sprintf(numbuf, "%" PRIdMAX "", (long long) tctdbgenuid(adb->tdb)); + ksiz = sprintf(numbuf, "%" PRIdMAX "", (int64_t) tctdbgenuid(adb->tdb)); kbuf = numbuf; } if (!tctdbputcat2(adb->tdb, kbuf, ksiz, vbuf, vsiz)) err = true; @@ -929,7 +929,7 @@ int tcadbaddint(TCADB *adb, const void *kbuf, int ksiz, int num) { break; case ADBOTDB: if (ksiz < 1) { - ksiz = sprintf(numbuf, "%" PRIdMAX "", (long long) tctdbgenuid(adb->tdb)); + ksiz = sprintf(numbuf, "%" PRIdMAX "", (int64_t) tctdbgenuid(adb->tdb)); kbuf = numbuf; } rv = tctdbaddint(adb->tdb, kbuf, ksiz, num); @@ -991,7 +991,7 @@ double tcadbadddouble(TCADB *adb, const void *kbuf, int ksiz, double num) { break; case ADBOTDB: if (ksiz < 1) { - ksiz = sprintf(numbuf, "%" PRIdMAX "", (long long) tctdbgenuid(adb->tdb)); + ksiz = sprintf(numbuf, "%" PRIdMAX "", (int64_t) tctdbgenuid(adb->tdb)); kbuf = numbuf; } rv = tctdbadddouble(adb->tdb, kbuf, ksiz, num); @@ -1235,7 +1235,7 @@ bool tcadbcopy(TCADB *adb, const char *path) { case ADBONDB: if (*path == '@') { char tsbuf[TCNUMBUFSIZ]; - sprintf(tsbuf, "%" PRIuMAX "", (unsigned long long) (tctime() * 1000000)); + sprintf(tsbuf, "%" PRIuMAX "", (uint64_t) (tctime() * 1000000)); const char *args[2]; args[0] = path + 1; args[1] = tsbuf; @@ -3207,7 +3207,7 @@ TCLIST *tcadbmisc(TCADB *adb, const char *name, const TCLIST *args) { } else if (!strcmp(name, "genuid")) { rv = tclistnew2(1); char numbuf[TCNUMBUFSIZ]; - int nsiz = sprintf(numbuf, "%" PRIdMAX "", (long long) tctdbgenuid(adb->tdb)); + int nsiz = sprintf(numbuf, "%" PRIdMAX "", (int64_t) tctdbgenuid(adb->tdb)); TCLISTPUSH(rv, numbuf, nsiz); } else { rv = NULL; diff --git a/tcejdb/tcamgr.c b/tcejdb/tcamgr.c index 73d5008..b8daace 100644 --- a/tcejdb/tcamgr.c +++ b/tcejdb/tcamgr.c @@ -21,7 +21,7 @@ /* global variables */ -const char *g_progname; // program name +const char *g_progname; // program name /* function prototypes */ @@ -33,7 +33,7 @@ static char *strtozsv(const char *str, int sep, int *sp); static int printdata(const char *ptr, int size, bool px, int sep); static void setskeltran(ADBSKEL *skel); static bool mapbdbproc(void *map, const char *kbuf, int ksiz, const char *vbuf, int vsiz, - void *op); + void *op); static int runcreate(int argc, char **argv); static int runinform(int argc, char **argv); static int runput(int argc, char **argv); @@ -47,7 +47,7 @@ static int runversion(int argc, char **argv); static int proccreate(const char *name); static int procinform(const char *name); static int procput(const char *name, const char *kbuf, int ksiz, const char *vbuf, int vsiz, - int dmode); + int dmode); static int procout(const char *name, const char *kbuf, int ksiz); static int procget(const char *name, const char *kbuf, int ksiz, int sep, bool px, bool pz); static int proclist(const char *name, int sep, int max, bool pv, bool px, const char *fmstr); @@ -56,970 +56,950 @@ static int procmisc(const char *name, const char *func, const TCLIST *args, int static int procmap(const char *name, const char *dest, const char *fmstr); static int procversion(void); - /* main routine */ -int main(int argc, char **argv){ - g_progname = argv[0]; - if(argc < 2) usage(); - int rv = 0; - if(!strcmp(argv[1], "create")){ - rv = runcreate(argc, argv); - } else if(!strcmp(argv[1], "inform")){ - rv = runinform(argc, argv); - } else if(!strcmp(argv[1], "put")){ - rv = runput(argc, argv); - } else if(!strcmp(argv[1], "out")){ - rv = runout(argc, argv); - } else if(!strcmp(argv[1], "get")){ - rv = runget(argc, argv); - } else if(!strcmp(argv[1], "list")){ - rv = runlist(argc, argv); - } else if(!strcmp(argv[1], "optimize")){ - rv = runoptimize(argc, argv); - } else if(!strcmp(argv[1], "misc")){ - rv = runmisc(argc, argv); - } else if(!strcmp(argv[1], "map")){ - rv = runmap(argc, argv); - } else if(!strcmp(argv[1], "version") || !strcmp(argv[1], "--version")){ - rv = runversion(argc, argv); - } else { - usage(); - } - return rv; +int main(int argc, char **argv) { + g_progname = argv[0]; + if (argc < 2) usage(); + int rv = 0; + if (!strcmp(argv[1], "create")) { + rv = runcreate(argc, argv); + } else if (!strcmp(argv[1], "inform")) { + rv = runinform(argc, argv); + } else if (!strcmp(argv[1], "put")) { + rv = runput(argc, argv); + } else if (!strcmp(argv[1], "out")) { + rv = runout(argc, argv); + } else if (!strcmp(argv[1], "get")) { + rv = runget(argc, argv); + } else if (!strcmp(argv[1], "list")) { + rv = runlist(argc, argv); + } else if (!strcmp(argv[1], "optimize")) { + rv = runoptimize(argc, argv); + } else if (!strcmp(argv[1], "misc")) { + rv = runmisc(argc, argv); + } else if (!strcmp(argv[1], "map")) { + rv = runmap(argc, argv); + } else if (!strcmp(argv[1], "version") || !strcmp(argv[1], "--version")) { + rv = runversion(argc, argv); + } else { + usage(); + } + return rv; } - /* print the usage and exit */ -static void usage(void){ - fprintf(stderr, "%s: the command line utility of the abstract database API\n", g_progname); - fprintf(stderr, "\n"); - fprintf(stderr, "usage:\n"); - fprintf(stderr, " %s create name\n", g_progname); - fprintf(stderr, " %s inform name\n", g_progname); - fprintf(stderr, " %s put [-sx] [-sep chr] [-dk|-dc|-dai|-dad] name key value\n", g_progname); - fprintf(stderr, " %s out [-sx] [-sep chr] name key\n", g_progname); - fprintf(stderr, " %s get [-sx] [-sep chr] [-px] [-pz] name key\n", g_progname); - fprintf(stderr, " %s list [-sep chr] [-m num] [-pv] [-px] [-fm str] name\n", g_progname); - fprintf(stderr, " %s optimize name [params]\n", g_progname); - fprintf(stderr, " %s misc [-sx] [-sep chr] [-px] name func [arg...]\n", g_progname); - fprintf(stderr, " %s map [-fm str] name dest\n", g_progname); - fprintf(stderr, " %s version\n", g_progname); - fprintf(stderr, "\n"); - exit(1); +static void usage(void) { + fprintf(stderr, "%s: the command line utility of the abstract database API\n", g_progname); + fprintf(stderr, "\n"); + fprintf(stderr, "usage:\n"); + fprintf(stderr, " %s create name\n", g_progname); + fprintf(stderr, " %s inform name\n", g_progname); + fprintf(stderr, " %s put [-sx] [-sep chr] [-dk|-dc|-dai|-dad] name key value\n", g_progname); + fprintf(stderr, " %s out [-sx] [-sep chr] name key\n", g_progname); + fprintf(stderr, " %s get [-sx] [-sep chr] [-px] [-pz] name key\n", g_progname); + fprintf(stderr, " %s list [-sep chr] [-m num] [-pv] [-px] [-fm str] name\n", g_progname); + fprintf(stderr, " %s optimize name [params]\n", g_progname); + fprintf(stderr, " %s misc [-sx] [-sep chr] [-px] name func [arg...]\n", g_progname); + fprintf(stderr, " %s map [-fm str] name dest\n", g_progname); + fprintf(stderr, " %s version\n", g_progname); + fprintf(stderr, "\n"); + exit(1); } - /* get the character of separation string */ -static int sepstrtochr(const char *str){ - if(!strcmp(str, "\\t")) return '\t'; - if(!strcmp(str, "\\r")) return '\r'; - if(!strcmp(str, "\\n")) return '\n'; - return *(unsigned char *)str; +static int sepstrtochr(const char *str) { + if (!strcmp(str, "\\t")) return '\t'; + if (!strcmp(str, "\\r")) return '\r'; + if (!strcmp(str, "\\n")) return '\n'; + return *(unsigned char *) str; } - /* encode a string as a zero separaterd string */ -static char *strtozsv(const char *str, int sep, int *sp){ - int size = strlen(str); - char *buf = tcmemdup(str, size); - for(int i = 0; i < size; i++){ - if(buf[i] == sep) buf[i] = '\0'; - } - *sp = size; - return buf; +static char *strtozsv(const char *str, int sep, int *sp) { + int size = strlen(str); + char *buf = tcmemdup(str, size); + for (int i = 0; i < size; i++) { + if (buf[i] == sep) buf[i] = '\0'; + } + *sp = size; + return buf; } - /* print error information */ -static void printerr(TCADB *adb){ - const char *path = tcadbpath(adb); - fprintf(stderr, "%s: %s: error\n", g_progname, path ? path : "-"); +static void printerr(TCADB *adb) { + const char *path = tcadbpath(adb); + fprintf(stderr, "%s: %s: error\n", g_progname, path ? path : "-"); } - /* print record data */ -static int printdata(const char *ptr, int size, bool px, int sep){ - int len = 0; - while(size-- > 0){ - if(px){ - if(len > 0) putchar(' '); - len += printf("%02X", *(unsigned char *)ptr); - } else if(sep > 0){ - if(*ptr == '\0'){ - putchar(sep); - } else { - putchar(*ptr); - } - len++; - } else { - putchar(*ptr); - len++; +static int printdata(const char *ptr, int size, bool px, int sep) { + int len = 0; + while (size-- > 0) { + if (px) { + if (len > 0) putchar(' '); + len += printf("%02X", *(unsigned char *) ptr); + } else if (sep > 0) { + if (*ptr == '\0') { + putchar(sep); + } else { + putchar(*ptr); + } + len++; + } else { + putchar(*ptr); + len++; + } + ptr++; } - ptr++; - } - return len; + return len; } - /* set the transparent skeleton database */ -static void setskeltran(ADBSKEL *skel){ - memset(skel, 0, sizeof(*skel)); - skel->opq = tcadbnew(); - skel->del = (void (*)(void *))tcadbdel; - skel->open = (bool (*)(void *, const char *))tcadbopen; - skel->close = (bool (*)(void *))tcadbclose; - skel->put = (bool (*)(void *, const void *, int, const void *, int))tcadbput; - skel->putkeep = (bool (*)(void *, const void *, int, const void *, int))tcadbputkeep; - skel->putcat = (bool (*)(void *, const void *, int, const void *, int))tcadbputcat; - skel->out = (bool (*)(void *, const void *, int))tcadbout; - skel->get = (void *(*)(void *, const void *, int, int *))tcadbget; - skel->vsiz = (int (*)(void *, const void *, int))tcadbvsiz; - skel->iterinit = (bool (*)(void *))tcadbiterinit; - skel->iternext = (void *(*)(void *, int *))tcadbiternext; - skel->fwmkeys = (TCLIST *(*)(void *, const void *, int, int))tcadbfwmkeys; - skel->addint = (int (*)(void *, const void *, int, int))tcadbaddint; - skel->adddouble = (double (*)(void *, const void *, int, double))tcadbadddouble; - skel->sync = (bool (*)(void *))tcadbsync; - skel->optimize = (bool (*)(void *, const char *))tcadboptimize; - skel->vanish = (bool (*)(void *))tcadbvanish; - skel->copy = (bool (*)(void *, const char *))tcadbcopy; - skel->tranbegin = (bool (*)(void *))tcadbtranbegin; - skel->trancommit = (bool (*)(void *))tcadbtrancommit; - skel->tranabort = (bool (*)(void *))tcadbtranabort; - skel->path = (const char *(*)(void *))tcadbpath; - skel->rnum = (uint64_t (*)(void *))tcadbrnum; - skel->size = (uint64_t (*)(void *))tcadbsize; - skel->misc = (TCLIST *(*)(void *, const char *, const TCLIST *))tcadbmisc; - skel->putproc = - (bool (*)(void *, const void *, int, const void *, int, TCPDPROC, void *))tcadbputproc; - skel->foreach = (bool (*)(void *, TCITER, void *))tcadbforeach; +static void setskeltran(ADBSKEL *skel) { + memset(skel, 0, sizeof (*skel)); + skel->opq = tcadbnew(); + skel->del = (void (*)(void *))tcadbdel; + skel->open = (bool(*)(void *, const char *))tcadbopen; + skel->close = (bool(*)(void *))tcadbclose; + skel->put = (bool(*)(void *, const void *, int, const void *, int))tcadbput; + skel->putkeep = (bool(*)(void *, const void *, int, const void *, int))tcadbputkeep; + skel->putcat = (bool(*)(void *, const void *, int, const void *, int))tcadbputcat; + skel->out = (bool(*)(void *, const void *, int))tcadbout; + skel->get = (void *(*)(void *, const void *, int, int *))tcadbget; + skel->vsiz = (int (*)(void *, const void *, int))tcadbvsiz; + skel->iterinit = (bool(*)(void *))tcadbiterinit; + skel->iternext = (void *(*)(void *, int *))tcadbiternext; + skel->fwmkeys = (TCLIST * (*)(void *, const void *, int, int))tcadbfwmkeys; + skel->addint = (int (*)(void *, const void *, int, int))tcadbaddint; + skel->adddouble = (double (*)(void *, const void *, int, double))tcadbadddouble; + skel->sync = (bool(*)(void *))tcadbsync; + skel->optimize = (bool(*)(void *, const char *))tcadboptimize; + skel->vanish = (bool(*)(void *))tcadbvanish; + skel->copy = (bool(*)(void *, const char *))tcadbcopy; + skel->tranbegin = (bool(*)(void *))tcadbtranbegin; + skel->trancommit = (bool(*)(void *))tcadbtrancommit; + skel->tranabort = (bool(*)(void *))tcadbtranabort; + skel->path = (const char *(*)(void *))tcadbpath; + skel->rnum = (uint64_t(*)(void *))tcadbrnum; + skel->size = (uint64_t(*)(void *))tcadbsize; + skel->misc = (TCLIST * (*)(void *, const char *, const TCLIST *))tcadbmisc; + skel->putproc = + (bool(*)(void *, const void *, int, const void *, int, TCPDPROC, void *))tcadbputproc; + skel->foreach = (bool(*)(void *, TCITER, void *))tcadbforeach; } - /* mapping function */ static bool mapbdbproc(void *map, const char *kbuf, int ksiz, const char *vbuf, int vsiz, - void *op){ - bool err = false; - if(!tcadbmapbdbemit(map, kbuf, ksiz, vbuf, vsiz)) err = true; - return !err; + void *op) { + bool err = false; + if (!tcadbmapbdbemit(map, kbuf, ksiz, vbuf, vsiz)) err = true; + return !err; } - /* parse arguments of create command */ -static int runcreate(int argc, char **argv){ - char *name = NULL; - for(int i = 2; i < argc; i++){ - if(!name && argv[i][0] == '-'){ - usage(); - } else if(!name){ - name = argv[i]; - } else { - usage(); +static int runcreate(int argc, char **argv) { + char *name = NULL; + for (int i = 2; i < argc; i++) { + if (!name && argv[i][0] == '-') { + usage(); + } else if (!name) { + name = argv[i]; + } else { + usage(); + } } - } - if(!name) usage(); - int rv = proccreate(name); - return rv; + if (!name) usage(); + int rv = proccreate(name); + return rv; } - /* parse arguments of inform command */ -static int runinform(int argc, char **argv){ - char *name = NULL; - for(int i = 2; i < argc; i++){ - if(!name && argv[i][0] == '-'){ - usage(); - } else if(!name){ - name = argv[i]; - } else { - usage(); +static int runinform(int argc, char **argv) { + char *name = NULL; + for (int i = 2; i < argc; i++) { + if (!name && argv[i][0] == '-') { + usage(); + } else if (!name) { + name = argv[i]; + } else { + usage(); + } } - } - if(!name) usage(); - name = tcsprintf("%s#mode=r", name); - int rv = procinform(name); - tcfree(name); - return rv; + if (!name) usage(); + name = tcsprintf("%s#mode=r", name); + int rv = procinform(name); + tcfree(name); + return rv; } - /* parse arguments of put command */ -static int runput(int argc, char **argv){ - char *name = NULL; - char *key = NULL; - char *value = NULL; - int dmode = 0; - bool sx = false; - int sep = -1; - for(int i = 2; i < argc; i++){ - if(!name && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-dk")){ - dmode = -1; - } else if(!strcmp(argv[i], "-dc")){ - dmode = 1; - } else if(!strcmp(argv[i], "-dai")){ - dmode = 10; - } else if(!strcmp(argv[i], "-dad")){ - dmode = 11; - } else if(!strcmp(argv[i], "-sx")){ - sx = true; - } else if(!strcmp(argv[i], "-sep")){ - if(++i >= argc) usage(); - sep = sepstrtochr(argv[i]); - } else { - usage(); - } - } else if(!name){ - name = argv[i]; - } else if(!key){ - key = argv[i]; - } else if(!value){ - value = argv[i]; +static int runput(int argc, char **argv) { + char *name = NULL; + char *key = NULL; + char *value = NULL; + int dmode = 0; + bool sx = false; + int sep = -1; + for (int i = 2; i < argc; i++) { + if (!name && argv[i][0] == '-') { + if (!strcmp(argv[i], "-dk")) { + dmode = -1; + } else if (!strcmp(argv[i], "-dc")) { + dmode = 1; + } else if (!strcmp(argv[i], "-dai")) { + dmode = 10; + } else if (!strcmp(argv[i], "-dad")) { + dmode = 11; + } else if (!strcmp(argv[i], "-sx")) { + sx = true; + } else if (!strcmp(argv[i], "-sep")) { + if (++i >= argc) usage(); + sep = sepstrtochr(argv[i]); + } else { + usage(); + } + } else if (!name) { + name = argv[i]; + } else if (!key) { + key = argv[i]; + } else if (!value) { + value = argv[i]; + } else { + usage(); + } + } + if (!name || !key || !value) usage(); + char *kbuf, *vbuf; + int ksiz, vsiz; + if (sx) { + kbuf = tchexdecode(key, &ksiz); + vbuf = tchexdecode(value, &vsiz); + } else if (sep > 0) { + kbuf = strtozsv(key, sep, &ksiz); + vbuf = strtozsv(value, sep, &vsiz); } else { - usage(); + ksiz = strlen(key); + kbuf = tcmemdup(key, ksiz); + vsiz = strlen(value); + vbuf = tcmemdup(value, vsiz); } - } - if(!name || !key || !value) usage(); - char *kbuf, *vbuf; - int ksiz, vsiz; - if(sx){ - kbuf = tchexdecode(key, &ksiz); - vbuf = tchexdecode(value, &vsiz); - } else if(sep > 0){ - kbuf = strtozsv(key, sep, &ksiz); - vbuf = strtozsv(value, sep, &vsiz); - } else { - ksiz = strlen(key); - kbuf = tcmemdup(key, ksiz); - vsiz = strlen(value); - vbuf = tcmemdup(value, vsiz); - } - int rv = procput(name, kbuf, ksiz, vbuf, vsiz, dmode); - tcfree(vbuf); - tcfree(kbuf); - return rv; + int rv = procput(name, kbuf, ksiz, vbuf, vsiz, dmode); + tcfree(vbuf); + tcfree(kbuf); + return rv; } - /* parse arguments of out command */ -static int runout(int argc, char **argv){ - char *name = NULL; - char *key = NULL; - bool sx = false; - int sep = -1; - for(int i = 2; i < argc; i++){ - if(!name && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-sx")){ - sx = true; - } else if(!strcmp(argv[i], "-sep")){ - if(++i >= argc) usage(); - sep = sepstrtochr(argv[i]); - } else { - usage(); - } - } else if(!name){ - name = argv[i]; - } else if(!key){ - key = argv[i]; +static int runout(int argc, char **argv) { + char *name = NULL; + char *key = NULL; + bool sx = false; + int sep = -1; + for (int i = 2; i < argc; i++) { + if (!name && argv[i][0] == '-') { + if (!strcmp(argv[i], "-sx")) { + sx = true; + } else if (!strcmp(argv[i], "-sep")) { + if (++i >= argc) usage(); + sep = sepstrtochr(argv[i]); + } else { + usage(); + } + } else if (!name) { + name = argv[i]; + } else if (!key) { + key = argv[i]; + } else { + usage(); + } + } + if (!name || !key) usage(); + int ksiz; + char *kbuf; + if (sx) { + kbuf = tchexdecode(key, &ksiz); + } else if (sep > 0) { + kbuf = strtozsv(key, sep, &ksiz); } else { - usage(); + ksiz = strlen(key); + kbuf = tcmemdup(key, ksiz); } - } - if(!name || !key) usage(); - int ksiz; - char *kbuf; - if(sx){ - kbuf = tchexdecode(key, &ksiz); - } else if(sep > 0){ - kbuf = strtozsv(key, sep, &ksiz); - } else { - ksiz = strlen(key); - kbuf = tcmemdup(key, ksiz); - } - int rv = procout(name, kbuf, ksiz); - tcfree(kbuf); - return rv; + int rv = procout(name, kbuf, ksiz); + tcfree(kbuf); + return rv; } - /* parse arguments of get command */ -static int runget(int argc, char **argv){ - char *name = NULL; - char *key = NULL; - bool sx = false; - int sep = -1; - bool px = false; - bool pz = false; - for(int i = 2; i < argc; i++){ - if(!name && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-sx")){ - sx = true; - } else if(!strcmp(argv[i], "-sep")){ - if(++i >= argc) usage(); - sep = sepstrtochr(argv[i]); - } else if(!strcmp(argv[i], "-px")){ - px = true; - } else if(!strcmp(argv[i], "-pz")){ - pz = true; - } else { - usage(); - } - } else if(!name){ - name = argv[i]; - } else if(!key){ - key = argv[i]; - } else { - usage(); +static int runget(int argc, char **argv) { + char *name = NULL; + char *key = NULL; + bool sx = false; + int sep = -1; + bool px = false; + bool pz = false; + for (int i = 2; i < argc; i++) { + if (!name && argv[i][0] == '-') { + if (!strcmp(argv[i], "-sx")) { + sx = true; + } else if (!strcmp(argv[i], "-sep")) { + if (++i >= argc) usage(); + sep = sepstrtochr(argv[i]); + } else if (!strcmp(argv[i], "-px")) { + px = true; + } else if (!strcmp(argv[i], "-pz")) { + pz = true; + } else { + usage(); + } + } else if (!name) { + name = argv[i]; + } else if (!key) { + key = argv[i]; + } else { + usage(); + } } - } - if(!name || !key) usage(); - int ksiz; - char *kbuf; - if(sx){ - kbuf = tchexdecode(key, &ksiz); - } else if(sep > 0){ - kbuf = strtozsv(key, sep, &ksiz); - } else { - ksiz = strlen(key); - kbuf = tcmemdup(key, ksiz); - } - name = tcsprintf("%s#mode=r", name); - int rv = procget(name, kbuf, ksiz, sep, px, pz); - tcfree(name); - tcfree(kbuf); - return rv; + if (!name || !key) usage(); + int ksiz; + char *kbuf; + if (sx) { + kbuf = tchexdecode(key, &ksiz); + } else if (sep > 0) { + kbuf = strtozsv(key, sep, &ksiz); + } else { + ksiz = strlen(key); + kbuf = tcmemdup(key, ksiz); + } + name = tcsprintf("%s#mode=r", name); + int rv = procget(name, kbuf, ksiz, sep, px, pz); + tcfree(name); + tcfree(kbuf); + return rv; } - /* parse arguments of list command */ -static int runlist(int argc, char **argv){ - char *name = NULL; - int sep = -1; - int max = -1; - bool pv = false; - bool px = false; - char *fmstr = NULL; - for(int i = 2; i < argc; i++){ - if(!name && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-sep")){ - if(++i >= argc) usage(); - sep = sepstrtochr(argv[i]); - } else if(!strcmp(argv[i], "-m")){ - if(++i >= argc) usage(); - max = tcatoix(argv[i]); - } else if(!strcmp(argv[i], "-pv")){ - pv = true; - } else if(!strcmp(argv[i], "-px")){ - px = true; - } else if(!strcmp(argv[i], "-fm")){ - if(++i >= argc) usage(); - fmstr = argv[i]; - } else { - usage(); - } - } else if(!name){ - name = argv[i]; - } else { - usage(); +static int runlist(int argc, char **argv) { + char *name = NULL; + int sep = -1; + int max = -1; + bool pv = false; + bool px = false; + char *fmstr = NULL; + for (int i = 2; i < argc; i++) { + if (!name && argv[i][0] == '-') { + if (!strcmp(argv[i], "-sep")) { + if (++i >= argc) usage(); + sep = sepstrtochr(argv[i]); + } else if (!strcmp(argv[i], "-m")) { + if (++i >= argc) usage(); + max = tcatoix(argv[i]); + } else if (!strcmp(argv[i], "-pv")) { + pv = true; + } else if (!strcmp(argv[i], "-px")) { + px = true; + } else if (!strcmp(argv[i], "-fm")) { + if (++i >= argc) usage(); + fmstr = argv[i]; + } else { + usage(); + } + } else if (!name) { + name = argv[i]; + } else { + usage(); + } } - } - if(!name) usage(); - name = tcsprintf("%s#mode=r", name); - int rv = proclist(name, sep, max, pv, px, fmstr); - tcfree(name); - return rv; + if (!name) usage(); + name = tcsprintf("%s#mode=r", name); + int rv = proclist(name, sep, max, pv, px, fmstr); + tcfree(name); + return rv; } - /* parse arguments of optimize command */ -static int runoptimize(int argc, char **argv){ - char *name = NULL; - char *params = NULL; - for(int i = 2; i < argc; i++){ - if(!name && argv[i][0] == '-'){ - usage(); - } else if(!name){ - name = argv[i]; - } else if(!params){ - params = argv[i]; - } else { - usage(); +static int runoptimize(int argc, char **argv) { + char *name = NULL; + char *params = NULL; + for (int i = 2; i < argc; i++) { + if (!name && argv[i][0] == '-') { + usage(); + } else if (!name) { + name = argv[i]; + } else if (!params) { + params = argv[i]; + } else { + usage(); + } } - } - if(!name) usage(); - int rv = procoptimize(name, params); - return rv; + if (!name) usage(); + int rv = procoptimize(name, params); + return rv; } - /* parse arguments of misc command */ -static int runmisc(int argc, char **argv){ - char *name = NULL; - char *func = NULL; - TCLIST *args = tcmpoollistnew(tcmpoolglobal()); - bool sx = false; - int sep = -1; - bool px = false; - for(int i = 2; i < argc; i++){ - if(!name && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-sx")){ - sx = true; - } else if(!strcmp(argv[i], "-sep")){ - if(++i >= argc) usage(); - sep = sepstrtochr(argv[i]); - } else if(!strcmp(argv[i], "-px")){ - px = true; - } else { - usage(); - } - } else if(!name){ - name = argv[i]; - } else if(!func){ - func = argv[i]; - } else { - if(sx){ - int size; - char *buf = tchexdecode(argv[i], &size); - tclistpush(args, buf, size); - tcfree(buf); - } else if(sep > 0){ - int size; - char *buf = strtozsv(argv[i], sep, &size); - tclistpush(args, buf, size); - tcfree(buf); - } else { - tclistpush2(args, argv[i]); - } +static int runmisc(int argc, char **argv) { + char *name = NULL; + char *func = NULL; + TCLIST *args = tcmpoollistnew(tcmpoolglobal()); + bool sx = false; + int sep = -1; + bool px = false; + for (int i = 2; i < argc; i++) { + if (!name && argv[i][0] == '-') { + if (!strcmp(argv[i], "-sx")) { + sx = true; + } else if (!strcmp(argv[i], "-sep")) { + if (++i >= argc) usage(); + sep = sepstrtochr(argv[i]); + } else if (!strcmp(argv[i], "-px")) { + px = true; + } else { + usage(); + } + } else if (!name) { + name = argv[i]; + } else if (!func) { + func = argv[i]; + } else { + if (sx) { + int size; + char *buf = tchexdecode(argv[i], &size); + tclistpush(args, buf, size); + tcfree(buf); + } else if (sep > 0) { + int size; + char *buf = strtozsv(argv[i], sep, &size); + tclistpush(args, buf, size); + tcfree(buf); + } else { + tclistpush2(args, argv[i]); + } + } } - } - if(!name || !func) usage(); - int rv = procmisc(name, func, args, sep, px); - return rv; + if (!name || !func) usage(); + int rv = procmisc(name, func, args, sep, px); + return rv; } - /* parse arguments of map command */ -static int runmap(int argc, char **argv){ - char *name = NULL; - char *dest = NULL; - char *fmstr = NULL; - for(int i = 2; i < argc; i++){ - if(!name && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-fm")){ - if(++i >= argc) usage(); - fmstr = argv[i]; - } else { - usage(); - } - } else if(!name){ - name = argv[i]; - } else if(!dest){ - dest = argv[i]; - } else { - usage(); +static int runmap(int argc, char **argv) { + char *name = NULL; + char *dest = NULL; + char *fmstr = NULL; + for (int i = 2; i < argc; i++) { + if (!name && argv[i][0] == '-') { + if (!strcmp(argv[i], "-fm")) { + if (++i >= argc) usage(); + fmstr = argv[i]; + } else { + usage(); + } + } else if (!name) { + name = argv[i]; + } else if (!dest) { + dest = argv[i]; + } else { + usage(); + } } - } - if(!name || !dest) usage(); - name = tcsprintf("%s#mode=r", name); - int rv = procmap(name, dest, fmstr); - tcfree(name); - return rv; + if (!name || !dest) usage(); + name = tcsprintf("%s#mode=r", name); + int rv = procmap(name, dest, fmstr); + tcfree(name); + return rv; } - /* parse arguments of version command */ -static int runversion(int argc, char **argv){ - int rv = procversion(); - return rv; +static int runversion(int argc, char **argv) { + int rv = procversion(); + return rv; } - /* perform create command */ -static int proccreate(const char *name){ - TCADB *adb = tcadbnew(); - ADBSKEL skel; - if(*name == '@'){ - setskeltran(&skel); - if(!tcadbsetskel(adb, &skel)){ - printerr(adb); - skel.del(skel.opq); - tcadbdel(adb); - return 1; +static int proccreate(const char *name) { + TCADB *adb = tcadbnew(); + ADBSKEL skel; + if (*name == '@') { + setskeltran(&skel); + if (!tcadbsetskel(adb, &skel)) { + printerr(adb); + skel.del(skel.opq); + tcadbdel(adb); + return 1; + } + name++; + } else if (*name == '%') { + if (!tcadbsetskelmulti(adb, 8)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + name++; + } + if (!tcadbopen(adb, name)) { + printerr(adb); + tcadbdel(adb); + return 1; } - name++; - } else if(*name == '%'){ - if(!tcadbsetskelmulti(adb, 8)){ - printerr(adb); - tcadbdel(adb); - return 1; + bool err = false; + if (!tcadbclose(adb)) { + printerr(adb); + err = true; } - name++; - } - if(!tcadbopen(adb, name)){ - printerr(adb); tcadbdel(adb); - return 1; - } - bool err = false; - if(!tcadbclose(adb)){ - printerr(adb); - err = true; - } - tcadbdel(adb); - return err ? 1 : 0; + return err ? 1 : 0; } - /* perform inform command */ -static int procinform(const char *name){ - TCADB *adb = tcadbnew(); - ADBSKEL skel; - if(*name == '@'){ - setskeltran(&skel); - if(!tcadbsetskel(adb, &skel)){ - printerr(adb); - skel.del(skel.opq); - tcadbdel(adb); - return 1; +static int procinform(const char *name) { + TCADB *adb = tcadbnew(); + ADBSKEL skel; + if (*name == '@') { + setskeltran(&skel); + if (!tcadbsetskel(adb, &skel)) { + printerr(adb); + skel.del(skel.opq); + tcadbdel(adb); + return 1; + } + name++; + } else if (*name == '%') { + if (!tcadbsetskelmulti(adb, 8)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + name++; } - name++; - } else if(*name == '%'){ - if(!tcadbsetskelmulti(adb, 8)){ - printerr(adb); - tcadbdel(adb); - return 1; + if (!tcadbopen(adb, name)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + bool err = false; + const char *path = tcadbpath(adb); + if (!path) path = "(unknown)"; + printf("path: %s\n", path); + const char *type = "(unknown)"; + switch (tcadbomode(adb)) { + case ADBOVOID: type = "not opened"; + break; + case ADBOMDB: type = "on-memory hash database"; + break; + case ADBONDB: type = "on-memory tree database"; + break; + case ADBOHDB: type = "hash database"; + break; + case ADBOBDB: type = "B+ tree database"; + break; + case ADBOFDB: type = "fixed-length database"; + break; + case ADBOTDB: type = "table database"; + break; + case ADBOSKEL: type = "skeleton database"; + break; + } + printf("database type: %s\n", type); + printf("record number: %" PRIuMAX "\n", tcadbrnum(adb)); + printf("size: %" PRIuMAX "\n", tcadbsize(adb)); + if (!tcadbclose(adb)) { + printerr(adb); + err = true; } - name++; - } - if(!tcadbopen(adb, name)){ - printerr(adb); tcadbdel(adb); - return 1; - } - bool err = false; - const char *path = tcadbpath(adb); - if(!path) path = "(unknown)"; - printf("path: %s\n", path); - const char *type = "(unknown)"; - switch(tcadbomode(adb)){ - case ADBOVOID: type = "not opened"; break; - case ADBOMDB: type = "on-memory hash database"; break; - case ADBONDB: type = "on-memory tree database"; break; - case ADBOHDB: type = "hash database"; break; - case ADBOBDB: type = "B+ tree database"; break; - case ADBOFDB: type = "fixed-length database"; break; - case ADBOTDB: type = "table database"; break; - case ADBOSKEL: type = "skeleton database"; break; - } - printf("database type: %s\n", type); - printf("record number: %" PRIuMAX "\n", (unsigned long long)tcadbrnum(adb)); - printf("size: %" PRIuMAX "\n", (unsigned long long)tcadbsize(adb)); - if(!tcadbclose(adb)){ - printerr(adb); - err = true; - } - tcadbdel(adb); - return err ? 1 : 0; + return err ? 1 : 0; } - /* perform put command */ static int procput(const char *name, const char *kbuf, int ksiz, const char *vbuf, int vsiz, - int dmode){ - TCADB *adb = tcadbnew(); - ADBSKEL skel; - if(*name == '@'){ - setskeltran(&skel); - if(!tcadbsetskel(adb, &skel)){ - printerr(adb); - skel.del(skel.opq); - tcadbdel(adb); - return 1; - } - name++; - } else if(*name == '%'){ - if(!tcadbsetskelmulti(adb, 8)){ - printerr(adb); - tcadbdel(adb); - return 1; + int dmode) { + TCADB *adb = tcadbnew(); + ADBSKEL skel; + if (*name == '@') { + setskeltran(&skel); + if (!tcadbsetskel(adb, &skel)) { + printerr(adb); + skel.del(skel.opq); + tcadbdel(adb); + return 1; + } + name++; + } else if (*name == '%') { + if (!tcadbsetskelmulti(adb, 8)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + name++; } - name++; - } - if(!tcadbopen(adb, name)){ - printerr(adb); - tcadbdel(adb); - return 1; - } - bool err = false; - int inum; - double dnum; - switch(dmode){ - case -1: - if(!tcadbputkeep(adb, kbuf, ksiz, vbuf, vsiz)){ - printerr(adb); - err = true; - } - break; - case 1: - if(!tcadbputcat(adb, kbuf, ksiz, vbuf, vsiz)){ - printerr(adb); - err = true; - } - break; - case 10: - inum = tcadbaddint(adb, kbuf, ksiz, tcatoi(vbuf)); - if(inum == INT_MIN){ - printerr(adb); - err = true; - } else { - printf("%d\n", inum); - } - break; - case 11: - dnum = tcadbadddouble(adb, kbuf, ksiz, tcatof(vbuf)); - if(isnan(dnum)){ + if (!tcadbopen(adb, name)) { printerr(adb); + tcadbdel(adb); + return 1; + } + bool err = false; + int inum; + double dnum; + switch (dmode) { + case -1: + if (!tcadbputkeep(adb, kbuf, ksiz, vbuf, vsiz)) { + printerr(adb); + err = true; + } + break; + case 1: + if (!tcadbputcat(adb, kbuf, ksiz, vbuf, vsiz)) { + printerr(adb); + err = true; + } + break; + case 10: + inum = tcadbaddint(adb, kbuf, ksiz, tcatoi(vbuf)); + if (inum == INT_MIN) { + printerr(adb); + err = true; + } else { + printf("%d\n", inum); + } + break; + case 11: + dnum = tcadbadddouble(adb, kbuf, ksiz, tcatof(vbuf)); + if (isnan(dnum)) { + printerr(adb); + err = true; + } else { + printf("%.6f\n", dnum); + } + break; + default: + if (!tcadbput(adb, kbuf, ksiz, vbuf, vsiz)) { + printerr(adb); + err = true; + } + break; + } + if (!tcadbclose(adb)) { + if (!err) printerr(adb); err = true; - } else { - printf("%.6f\n", dnum); - } - break; - default: - if(!tcadbput(adb, kbuf, ksiz, vbuf, vsiz)){ - printerr(adb); - err = true; - } - break; - } - if(!tcadbclose(adb)){ - if(!err) printerr(adb); - err = true; - } - tcadbdel(adb); - return err ? 1 : 0; + } + tcadbdel(adb); + return err ? 1 : 0; } - /* perform out command */ -static int procout(const char *name, const char *kbuf, int ksiz){ - TCADB *adb = tcadbnew(); - ADBSKEL skel; - if(*name == '@'){ - setskeltran(&skel); - if(!tcadbsetskel(adb, &skel)){ - printerr(adb); - skel.del(skel.opq); - tcadbdel(adb); - return 1; +static int procout(const char *name, const char *kbuf, int ksiz) { + TCADB *adb = tcadbnew(); + ADBSKEL skel; + if (*name == '@') { + setskeltran(&skel); + if (!tcadbsetskel(adb, &skel)) { + printerr(adb); + skel.del(skel.opq); + tcadbdel(adb); + return 1; + } + name++; + } else if (*name == '%') { + if (!tcadbsetskelmulti(adb, 8)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + name++; + } + if (!tcadbopen(adb, name)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + bool err = false; + if (!tcadbout(adb, kbuf, ksiz)) { + printerr(adb); + err = true; } - name++; - } else if(*name == '%'){ - if(!tcadbsetskelmulti(adb, 8)){ - printerr(adb); - tcadbdel(adb); - return 1; + if (!tcadbclose(adb)) { + if (!err) printerr(adb); + err = true; } - name++; - } - if(!tcadbopen(adb, name)){ - printerr(adb); tcadbdel(adb); - return 1; - } - bool err = false; - if(!tcadbout(adb, kbuf, ksiz)){ - printerr(adb); - err = true; - } - if(!tcadbclose(adb)){ - if(!err) printerr(adb); - err = true; - } - tcadbdel(adb); - return err ? 1 : 0; + return err ? 1 : 0; } - /* perform get command */ -static int procget(const char *name, const char *kbuf, int ksiz, int sep, bool px, bool pz){ - TCADB *adb = tcadbnew(); - ADBSKEL skel; - if(*name == '@'){ - setskeltran(&skel); - if(!tcadbsetskel(adb, &skel)){ - printerr(adb); - skel.del(skel.opq); - tcadbdel(adb); - return 1; +static int procget(const char *name, const char *kbuf, int ksiz, int sep, bool px, bool pz) { + TCADB *adb = tcadbnew(); + ADBSKEL skel; + if (*name == '@') { + setskeltran(&skel); + if (!tcadbsetskel(adb, &skel)) { + printerr(adb); + skel.del(skel.opq); + tcadbdel(adb); + return 1; + } + name++; + } else if (*name == '%') { + if (!tcadbsetskelmulti(adb, 8)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + name++; } - name++; - } else if(*name == '%'){ - if(!tcadbsetskelmulti(adb, 8)){ - printerr(adb); - tcadbdel(adb); - return 1; + if (!tcadbopen(adb, name)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + bool err = false; + int vsiz; + char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz); + if (vbuf) { + printdata(vbuf, vsiz, px, sep); + if (!pz) putchar('\n'); + tcfree(vbuf); + } else { + printerr(adb); + err = true; + } + if (!tcadbclose(adb)) { + if (!err) printerr(adb); + err = true; } - name++; - } - if(!tcadbopen(adb, name)){ - printerr(adb); tcadbdel(adb); - return 1; - } - bool err = false; - int vsiz; - char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz); - if(vbuf){ - printdata(vbuf, vsiz, px, sep); - if(!pz) putchar('\n'); - tcfree(vbuf); - } else { - printerr(adb); - err = true; - } - if(!tcadbclose(adb)){ - if(!err) printerr(adb); - err = true; - } - tcadbdel(adb); - return err ? 1 : 0; + return err ? 1 : 0; } - /* perform list command */ -static int proclist(const char *name, int sep, int max, bool pv, bool px, const char *fmstr){ - TCADB *adb = tcadbnew(); - ADBSKEL skel; - if(*name == '@'){ - setskeltran(&skel); - if(!tcadbsetskel(adb, &skel)){ - printerr(adb); - skel.del(skel.opq); - tcadbdel(adb); - return 1; - } - name++; - } else if(*name == '%'){ - if(!tcadbsetskelmulti(adb, 8)){ - printerr(adb); - tcadbdel(adb); - return 1; - } - name++; - } - if(!tcadbopen(adb, name)){ - printerr(adb); - tcadbdel(adb); - return 1; - } - bool err = false; - if(fmstr){ - TCLIST *keys = tcadbfwmkeys2(adb, fmstr, max); - for(int i = 0; i < tclistnum(keys); i++){ - int ksiz; - const char *kbuf = tclistval(keys, i, &ksiz); - printdata(kbuf, ksiz, px, sep); - if(pv){ - int vsiz; - char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz); - if(vbuf){ - putchar('\t'); - printdata(vbuf, vsiz, px, sep); - tcfree(vbuf); +static int proclist(const char *name, int sep, int max, bool pv, bool px, const char *fmstr) { + TCADB *adb = tcadbnew(); + ADBSKEL skel; + if (*name == '@') { + setskeltran(&skel); + if (!tcadbsetskel(adb, &skel)) { + printerr(adb); + skel.del(skel.opq); + tcadbdel(adb); + return 1; } - } - putchar('\n'); - } - tclistdel(keys); - } else { - if(!tcadbiterinit(adb)){ - printerr(adb); - err = true; + name++; + } else if (*name == '%') { + if (!tcadbsetskelmulti(adb, 8)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + name++; } - int ksiz; - char *kbuf; - int cnt = 0; - while((kbuf = tcadbiternext(adb, &ksiz)) != NULL){ - printdata(kbuf, ksiz, px, sep); - if(pv){ - int vsiz; - char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz); - if(vbuf){ - putchar('\t'); - printdata(vbuf, vsiz, px, sep); - tcfree(vbuf); + if (!tcadbopen(adb, name)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + bool err = false; + if (fmstr) { + TCLIST *keys = tcadbfwmkeys2(adb, fmstr, max); + for (int i = 0; i < tclistnum(keys); i++) { + int ksiz; + const char *kbuf = tclistval(keys, i, &ksiz); + printdata(kbuf, ksiz, px, sep); + if (pv) { + int vsiz; + char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz); + if (vbuf) { + putchar('\t'); + printdata(vbuf, vsiz, px, sep); + tcfree(vbuf); + } + } + putchar('\n'); + } + tclistdel(keys); + } else { + if (!tcadbiterinit(adb)) { + printerr(adb); + err = true; + } + int ksiz; + char *kbuf; + int cnt = 0; + while ((kbuf = tcadbiternext(adb, &ksiz)) != NULL) { + printdata(kbuf, ksiz, px, sep); + if (pv) { + int vsiz; + char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz); + if (vbuf) { + putchar('\t'); + printdata(vbuf, vsiz, px, sep); + tcfree(vbuf); + } + } + putchar('\n'); + tcfree(kbuf); + if (max >= 0 && ++cnt >= max) break; } - } - putchar('\n'); - tcfree(kbuf); - if(max >= 0 && ++cnt >= max) break; } - } - if(!tcadbclose(adb)){ - if(!err) printerr(adb); - err = true; - } - tcadbdel(adb); - return err ? 1 : 0; + if (!tcadbclose(adb)) { + if (!err) printerr(adb); + err = true; + } + tcadbdel(adb); + return err ? 1 : 0; } - /* perform optimize command */ -static int procoptimize(const char *name, const char *params){ - TCADB *adb = tcadbnew(); - ADBSKEL skel; - if(*name == '@'){ - setskeltran(&skel); - if(!tcadbsetskel(adb, &skel)){ - printerr(adb); - skel.del(skel.opq); - tcadbdel(adb); - return 1; +static int procoptimize(const char *name, const char *params) { + TCADB *adb = tcadbnew(); + ADBSKEL skel; + if (*name == '@') { + setskeltran(&skel); + if (!tcadbsetskel(adb, &skel)) { + printerr(adb); + skel.del(skel.opq); + tcadbdel(adb); + return 1; + } + name++; + } else if (*name == '%') { + if (!tcadbsetskelmulti(adb, 8)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + name++; + } + if (!tcadbopen(adb, name)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + bool err = false; + if (!tcadboptimize(adb, params)) { + printerr(adb); + err = true; } - name++; - } else if(*name == '%'){ - if(!tcadbsetskelmulti(adb, 8)){ - printerr(adb); - tcadbdel(adb); - return 1; + if (!tcadbclose(adb)) { + if (!err) printerr(adb); + err = true; } - name++; - } - if(!tcadbopen(adb, name)){ - printerr(adb); tcadbdel(adb); - return 1; - } - bool err = false; - if(!tcadboptimize(adb, params)){ - printerr(adb); - err = true; - } - if(!tcadbclose(adb)){ - if(!err) printerr(adb); - err = true; - } - tcadbdel(adb); - return err ? 1 : 0; + return err ? 1 : 0; } - /* perform misc command */ -static int procmisc(const char *name, const char *func, const TCLIST *args, int sep, bool px){ - TCADB *adb = tcadbnew(); - ADBSKEL skel; - if(*name == '@'){ - setskeltran(&skel); - if(!tcadbsetskel(adb, &skel)){ - printerr(adb); - skel.del(skel.opq); - tcadbdel(adb); - return 1; +static int procmisc(const char *name, const char *func, const TCLIST *args, int sep, bool px) { + TCADB *adb = tcadbnew(); + ADBSKEL skel; + if (*name == '@') { + setskeltran(&skel); + if (!tcadbsetskel(adb, &skel)) { + printerr(adb); + skel.del(skel.opq); + tcadbdel(adb); + return 1; + } + name++; + } else if (*name == '%') { + if (!tcadbsetskelmulti(adb, 8)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + name++; } - name++; - } else if(*name == '%'){ - if(!tcadbsetskelmulti(adb, 8)){ - printerr(adb); - tcadbdel(adb); - return 1; + if (!tcadbopen(adb, name)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + bool err = false; + TCLIST *res = tcadbmisc(adb, func, args); + if (res) { + for (int i = 0; i < tclistnum(res); i++) { + int rsiz; + const char *rbuf = tclistval(res, i, &rsiz); + printdata(rbuf, rsiz, px, sep); + printf("\n"); + } + tclistdel(res); + } else { + printerr(adb); + err = true; } - name++; - } - if(!tcadbopen(adb, name)){ - printerr(adb); - tcadbdel(adb); - return 1; - } - bool err = false; - TCLIST *res = tcadbmisc(adb, func, args); - if(res){ - for(int i = 0; i < tclistnum(res); i++){ - int rsiz; - const char *rbuf = tclistval(res, i, &rsiz); - printdata(rbuf, rsiz, px, sep); - printf("\n"); + if (!tcadbclose(adb)) { + if (!err) printerr(adb); + err = true; } - tclistdel(res); - } else { - printerr(adb); - err = true; - } - if(!tcadbclose(adb)){ - if(!err) printerr(adb); - err = true; - } - tcadbdel(adb); - return err ? 1 : 0; + tcadbdel(adb); + return err ? 1 : 0; } - /* perform map command */ -static int procmap(const char *name, const char *dest, const char *fmstr){ - TCADB *adb = tcadbnew(); - ADBSKEL skel; - if(*name == '@'){ - setskeltran(&skel); - if(!tcadbsetskel(adb, &skel)){ - printerr(adb); - skel.del(skel.opq); - tcadbdel(adb); - return 1; +static int procmap(const char *name, const char *dest, const char *fmstr) { + TCADB *adb = tcadbnew(); + ADBSKEL skel; + if (*name == '@') { + setskeltran(&skel); + if (!tcadbsetskel(adb, &skel)) { + printerr(adb); + skel.del(skel.opq); + tcadbdel(adb); + return 1; + } + name++; + } else if (*name == '%') { + if (!tcadbsetskelmulti(adb, 8)) { + printerr(adb); + tcadbdel(adb); + return 1; + } + name++; } - name++; - } else if(*name == '%'){ - if(!tcadbsetskelmulti(adb, 8)){ - printerr(adb); - tcadbdel(adb); - return 1; + if (!tcadbopen(adb, name)) { + printerr(adb); + tcadbdel(adb); + return 1; } - name++; - } - if(!tcadbopen(adb, name)){ - printerr(adb); - tcadbdel(adb); - return 1; - } - bool err = false; - TCBDB *bdb = tcbdbnew(); - if(!tcbdbopen(bdb, dest, BDBOWRITER | BDBOCREAT | BDBOTRUNC)){ - printerr(adb); - tcbdbdel(bdb); - tcadbdel(adb); - return 1; - } - if(fmstr){ - TCLIST *keys = tcadbfwmkeys2(adb, fmstr, -1); - if(!tcadbmapbdb(adb, keys, bdb, mapbdbproc, NULL, -1)){ - printerr(adb); - err = true; + bool err = false; + TCBDB *bdb = tcbdbnew(); + if (!tcbdbopen(bdb, dest, BDBOWRITER | BDBOCREAT | BDBOTRUNC)) { + printerr(adb); + tcbdbdel(bdb); + tcadbdel(adb); + return 1; + } + if (fmstr) { + TCLIST *keys = tcadbfwmkeys2(adb, fmstr, -1); + if (!tcadbmapbdb(adb, keys, bdb, mapbdbproc, NULL, -1)) { + printerr(adb); + err = true; + } + tclistdel(keys); + } else { + if (!tcadbmapbdb(adb, NULL, bdb, mapbdbproc, NULL, -1)) { + printerr(adb); + err = true; + } } - tclistdel(keys); - } else { - if(!tcadbmapbdb(adb, NULL, bdb, mapbdbproc, NULL, -1)){ - printerr(adb); - err = true; + if (!tcbdbclose(bdb)) { + printerr(adb); + err = true; } - } - if(!tcbdbclose(bdb)){ - printerr(adb); - err = true; - } - tcbdbdel(bdb); - if(!tcadbclose(adb)){ - printerr(adb); - err = true; - } - tcadbdel(adb); - return err ? 1 : 0; + tcbdbdel(bdb); + if (!tcadbclose(adb)) { + printerr(adb); + err = true; + } + tcadbdel(adb); + return err ? 1 : 0; } - /* perform version command */ -static int procversion(void){ - printf("Tokyo Cabinet version %s (%d:%s) for %s\n", - tcversion, _TC_LIBVER, _TC_FORMATVER, TCSYSNAME); - printf("Copyright (C) 2006-2012 FAL Labs\n"); - return 0; +static int procversion(void) { + printf("Tokyo Cabinet version %s (%d:%s) for %s\n", + tcversion, _TC_LIBVER, _TC_FORMATVER, TCSYSNAME); + printf("Copyright (C) 2006-2012 FAL Labs\n"); + return 0; } diff --git a/tcejdb/tcamttest.c b/tcejdb/tcamttest.c index 355e64a..f4a4e63 100644 --- a/tcejdb/tcamttest.c +++ b/tcejdb/tcamttest.c @@ -21,29 +21,29 @@ #define MULDIVNUM 8 // division number of multiple database #define RECBUFSIZ 48 // buffer for records -typedef struct { // type of structure for write thread - TCADB *adb; - int rnum; - int id; +typedef struct { // type of structure for write thread + TCADB *adb; + int rnum; + int id; } TARGWRITE; -typedef struct { // type of structure for read thread - TCADB *adb; - int rnum; - int id; +typedef struct { // type of structure for read thread + TCADB *adb; + int rnum; + int id; } TARGREAD; -typedef struct { // type of structure for remove thread - TCADB *adb; - int rnum; - int id; +typedef struct { // type of structure for remove thread + TCADB *adb; + int rnum; + int id; } TARGREMOVE; /* global variables */ -const char *g_progname; // program name -unsigned int g_randseed; // random seed -HANDLE g_dbgfd; // debugging output +const char *g_progname; // program name +unsigned int g_randseed; // random seed +HANDLE g_dbgfd; // debugging output /* function prototypes */ @@ -64,484 +64,468 @@ static void *threadwrite(void *targ); static void *threadread(void *targ); static void *threadremove(void *targ); - /* main routine */ -int main(int argc, char **argv){ - g_progname = argv[0]; - const char *ebuf = getenv("TCRNDSEED"); - g_randseed = ebuf ? tcatoix(ebuf) : tctime() * 1000; - srand(g_randseed); - ebuf = getenv("TCDBGFD"); - if (ebuf) { - int debugfd = tcatoix(ebuf); +int main(int argc, char **argv) { + g_progname = argv[0]; + const char *ebuf = getenv("TCRNDSEED"); + g_randseed = ebuf ? tcatoix(ebuf) : tctime() * 1000; + srand(g_randseed); + ebuf = getenv("TCDBGFD"); + if (ebuf) { + int debugfd = tcatoix(ebuf); #ifdef _WIN32 - g_dbgfd = (HANDLE) _get_osfhandle(debugfd); + g_dbgfd = (HANDLE) _get_osfhandle(debugfd); #else - g_dbgfd = debugfd; + g_dbgfd = debugfd; #endif - } - if(argc < 2) usage(); - int rv = 0; - if(!strcmp(argv[1], "write")){ - rv = runwrite(argc, argv); - } else if(!strcmp(argv[1], "read")){ - rv = runread(argc, argv); - } else if(!strcmp(argv[1], "remove")){ - rv = runremove(argc, argv); - } else { - usage(); - } - if(rv != 0){ - printf("FAILED: TCRNDSEED=%u PID=%d", g_randseed, (int)getpid()); - for(int i = 0; i < argc; i++){ - printf(" %s", argv[i]); } - printf("\n\n"); - } - return rv; + if (argc < 2) usage(); + int rv = 0; + if (!strcmp(argv[1], "write")) { + rv = runwrite(argc, argv); + } else if (!strcmp(argv[1], "read")) { + rv = runread(argc, argv); + } else if (!strcmp(argv[1], "remove")) { + rv = runremove(argc, argv); + } else { + usage(); + } + if (rv != 0) { + printf("FAILED: TCRNDSEED=%u PID=%d", g_randseed, (int) getpid()); + for (int i = 0; i < argc; i++) { + printf(" %s", argv[i]); + } + printf("\n\n"); + } + return rv; } - /* print the usage and exit */ -static void usage(void){ - fprintf(stderr, "%s: test cases of the abstract database API of Tokyo Cabinet\n", g_progname); - fprintf(stderr, "\n"); - fprintf(stderr, "usage:\n"); - fprintf(stderr, " %s write name tnum rnum\n", g_progname); - fprintf(stderr, " %s read name tnum\n", g_progname); - fprintf(stderr, " %s remove name tnum\n", g_progname); - fprintf(stderr, "\n"); - exit(1); +static void usage(void) { + fprintf(stderr, "%s: test cases of the abstract database API of Tokyo Cabinet\n", g_progname); + fprintf(stderr, "\n"); + fprintf(stderr, "usage:\n"); + fprintf(stderr, " %s write name tnum rnum\n", g_progname); + fprintf(stderr, " %s read name tnum\n", g_progname); + fprintf(stderr, " %s remove name tnum\n", g_progname); + fprintf(stderr, "\n"); + exit(1); } - /* print formatted information string and flush the buffer */ -static void iprintf(const char *format, ...){ - va_list ap; - va_start(ap, format); - vprintf(format, ap); - fflush(stdout); - va_end(ap); +static void iprintf(const char *format, ...) { + va_list ap; + va_start(ap, format); + vprintf(format, ap); + fflush(stdout); + va_end(ap); } - /* print a character and flush the buffer */ -static void iputchar(int c){ - putchar(c); - fflush(stdout); +static void iputchar(int c) { + putchar(c); + fflush(stdout); } - /* print error message of abstract database */ -static void eprint(TCADB *adb, int line, const char *func){ - const char *path = adb ? tcadbpath(adb) : NULL; - fprintf(stderr, "%s: %s: %d: %s: error\n", g_progname, path ? path : "-", line, func); +static void eprint(TCADB *adb, int line, const char *func) { + const char *path = adb ? tcadbpath(adb) : NULL; + fprintf(stderr, "%s: %s: %d: %s: error\n", g_progname, path ? path : "-", line, func); } - /* print system information */ -static void sysprint(void){ - TCMAP *info = tcsysinfo(); - if(info){ - tcmapiterinit(info); - const char *kbuf; - while((kbuf = tcmapiternext2(info)) != NULL){ - iprintf("sys_%s: %s\n", kbuf, tcmapiterval2(kbuf)); +static void sysprint(void) { + TCMAP *info = tcsysinfo(); + if (info) { + tcmapiterinit(info); + const char *kbuf; + while ((kbuf = tcmapiternext2(info)) != NULL) { + iprintf("sys_%s: %s\n", kbuf, tcmapiterval2(kbuf)); + } + tcmapdel(info); } - tcmapdel(info); - } } - /* set the transparent skeleton database */ -static void setskeltran(ADBSKEL *skel){ - memset(skel, 0, sizeof(*skel)); - skel->opq = tcadbnew(); - skel->del = (void (*)(void *))tcadbdel; - skel->open = (bool (*)(void *, const char *))tcadbopen; - skel->close = (bool (*)(void *))tcadbclose; - skel->put = (bool (*)(void *, const void *, int, const void *, int))tcadbput; - skel->putkeep = (bool (*)(void *, const void *, int, const void *, int))tcadbputkeep; - skel->putcat = (bool (*)(void *, const void *, int, const void *, int))tcadbputcat; - skel->out = (bool (*)(void *, const void *, int))tcadbout; - skel->get = (void *(*)(void *, const void *, int, int *))tcadbget; - skel->vsiz = (int (*)(void *, const void *, int))tcadbvsiz; - skel->iterinit = (bool (*)(void *))tcadbiterinit; - skel->iternext = (void *(*)(void *, int *))tcadbiternext; - skel->fwmkeys = (TCLIST *(*)(void *, const void *, int, int))tcadbfwmkeys; - skel->addint = (int (*)(void *, const void *, int, int))tcadbaddint; - skel->adddouble = (double (*)(void *, const void *, int, double))tcadbadddouble; - skel->sync = (bool (*)(void *))tcadbsync; - skel->optimize = (bool (*)(void *, const char *))tcadboptimize; - skel->vanish = (bool (*)(void *))tcadbvanish; - skel->copy = (bool (*)(void *, const char *))tcadbcopy; - skel->tranbegin = (bool (*)(void *))tcadbtranbegin; - skel->trancommit = (bool (*)(void *))tcadbtrancommit; - skel->tranabort = (bool (*)(void *))tcadbtranabort; - skel->path = (const char *(*)(void *))tcadbpath; - skel->rnum = (uint64_t (*)(void *))tcadbrnum; - skel->size = (uint64_t (*)(void *))tcadbsize; - skel->misc = (TCLIST *(*)(void *, const char *, const TCLIST *))tcadbmisc; - skel->putproc = - (bool (*)(void *, const void *, int, const void *, int, TCPDPROC, void *))tcadbputproc; - skel->foreach = (bool (*)(void *, TCITER, void *))tcadbforeach; +static void setskeltran(ADBSKEL *skel) { + memset(skel, 0, sizeof (*skel)); + skel->opq = tcadbnew(); + skel->del = (void (*)(void *))tcadbdel; + skel->open = (bool(*)(void *, const char *))tcadbopen; + skel->close = (bool(*)(void *))tcadbclose; + skel->put = (bool(*)(void *, const void *, int, const void *, int))tcadbput; + skel->putkeep = (bool(*)(void *, const void *, int, const void *, int))tcadbputkeep; + skel->putcat = (bool(*)(void *, const void *, int, const void *, int))tcadbputcat; + skel->out = (bool(*)(void *, const void *, int))tcadbout; + skel->get = (void *(*)(void *, const void *, int, int *))tcadbget; + skel->vsiz = (int (*)(void *, const void *, int))tcadbvsiz; + skel->iterinit = (bool(*)(void *))tcadbiterinit; + skel->iternext = (void *(*)(void *, int *))tcadbiternext; + skel->fwmkeys = (TCLIST * (*)(void *, const void *, int, int))tcadbfwmkeys; + skel->addint = (int (*)(void *, const void *, int, int))tcadbaddint; + skel->adddouble = (double (*)(void *, const void *, int, double))tcadbadddouble; + skel->sync = (bool(*)(void *))tcadbsync; + skel->optimize = (bool(*)(void *, const char *))tcadboptimize; + skel->vanish = (bool(*)(void *))tcadbvanish; + skel->copy = (bool(*)(void *, const char *))tcadbcopy; + skel->tranbegin = (bool(*)(void *))tcadbtranbegin; + skel->trancommit = (bool(*)(void *))tcadbtrancommit; + skel->tranabort = (bool(*)(void *))tcadbtranabort; + skel->path = (const char *(*)(void *))tcadbpath; + skel->rnum = (uint64_t(*)(void *))tcadbrnum; + skel->size = (uint64_t(*)(void *))tcadbsize; + skel->misc = (TCLIST * (*)(void *, const char *, const TCLIST *))tcadbmisc; + skel->putproc = + (bool(*)(void *, const void *, int, const void *, int, TCPDPROC, void *))tcadbputproc; + skel->foreach = (bool(*)(void *, TCITER, void *))tcadbforeach; } - /* parse arguments of write command */ -static int runwrite(int argc, char **argv){ - char *name = NULL; - char *tstr = NULL; - char *rstr = NULL; - for(int i = 2; i < argc; i++){ - if(!name && argv[i][0] == '-'){ - usage(); - } else if(!name){ - name = argv[i]; - } else if(!tstr){ - tstr = argv[i]; - } else if(!rstr){ - rstr = argv[i]; - } else { - usage(); +static int runwrite(int argc, char **argv) { + char *name = NULL; + char *tstr = NULL; + char *rstr = NULL; + for (int i = 2; i < argc; i++) { + if (!name && argv[i][0] == '-') { + usage(); + } else if (!name) { + name = argv[i]; + } else if (!tstr) { + tstr = argv[i]; + } else if (!rstr) { + rstr = argv[i]; + } else { + usage(); + } } - } - if(!name || !tstr || !rstr) usage(); - int tnum = tcatoix(tstr); - int rnum = tcatoix(rstr); - if(tnum < 1 || rnum < 1) usage(); - int rv = procwrite(name, tnum, rnum); - return rv; + if (!name || !tstr || !rstr) usage(); + int tnum = tcatoix(tstr); + int rnum = tcatoix(rstr); + if (tnum < 1 || rnum < 1) usage(); + int rv = procwrite(name, tnum, rnum); + return rv; } - /* parse arguments of read command */ -static int runread(int argc, char **argv){ - char *name = NULL; - char *tstr = NULL; - for(int i = 2; i < argc; i++){ - if(!name && argv[i][0] == '-'){ - usage(); - } else if(!name){ - name = argv[i]; - } else if(!tstr){ - tstr = argv[i]; - } else { - usage(); +static int runread(int argc, char **argv) { + char *name = NULL; + char *tstr = NULL; + for (int i = 2; i < argc; i++) { + if (!name && argv[i][0] == '-') { + usage(); + } else if (!name) { + name = argv[i]; + } else if (!tstr) { + tstr = argv[i]; + } else { + usage(); + } } - } - if(!name || !tstr) usage(); - int tnum = tcatoix(tstr); - if(tnum < 1) usage(); - int rv = procread(name, tnum); - return rv; + if (!name || !tstr) usage(); + int tnum = tcatoix(tstr); + if (tnum < 1) usage(); + int rv = procread(name, tnum); + return rv; } - /* parse arguments of remove command */ -static int runremove(int argc, char **argv){ - char *name = NULL; - char *tstr = NULL; - for(int i = 2; i < argc; i++){ - if(!name && argv[i][0] == '-'){ - usage(); - } else if(!name){ - name = argv[i]; - } else if(!tstr){ - tstr = argv[i]; - } else { - usage(); +static int runremove(int argc, char **argv) { + char *name = NULL; + char *tstr = NULL; + for (int i = 2; i < argc; i++) { + if (!name && argv[i][0] == '-') { + usage(); + } else if (!name) { + name = argv[i]; + } else if (!tstr) { + tstr = argv[i]; + } else { + usage(); + } } - } - if(!name || !tstr) usage(); - int tnum = tcatoix(tstr); - if(tnum < 1) usage(); - int rv = procremove(name, tnum); - return rv; + if (!name || !tstr) usage(); + int tnum = tcatoix(tstr); + if (tnum < 1) usage(); + int rv = procremove(name, tnum); + return rv; } - /* perform write command */ -static int procwrite(const char *name, int tnum, int rnum){ - iprintf("\n seed=%u name=%s tnum=%d rnum=%d\n\n", - g_randseed, name, tnum, rnum); - bool err = false; - double stime = tctime(); - TCADB *adb = tcadbnew(); - ADBSKEL skel; - if(*name == '@'){ - setskeltran(&skel); - if(!tcadbsetskel(adb, &skel)){ - eprint(adb, __LINE__, "tcadbsetskel"); - err = true; - skel.del(skel.opq); +static int procwrite(const char *name, int tnum, int rnum) { + iprintf("\n seed=%u name=%s tnum=%d rnum=%d\n\n", + g_randseed, name, tnum, rnum); + bool err = false; + double stime = tctime(); + TCADB *adb = tcadbnew(); + ADBSKEL skel; + if (*name == '@') { + setskeltran(&skel); + if (!tcadbsetskel(adb, &skel)) { + eprint(adb, __LINE__, "tcadbsetskel"); + err = true; + skel.del(skel.opq); + } + name++; + } else if (*name == '%') { + if (!tcadbsetskelmulti(adb, MULDIVNUM)) { + eprint(adb, __LINE__, "tcadbsetskelmulti"); + err = true; + } + name++; } - name++; - } else if(*name == '%'){ - if(!tcadbsetskelmulti(adb, MULDIVNUM)){ - eprint(adb, __LINE__, "tcadbsetskelmulti"); - err = true; - } - name++; - } - if(!tcadbopen(adb, name)){ - eprint(adb, __LINE__, "tcadbopen"); - err = true; - } - TARGWRITE targs[tnum]; - pthread_t threads[tnum]; - if(tnum == 1){ - targs[0].adb = adb; - targs[0].rnum = rnum; - targs[0].id = 0; - if(threadwrite(targs) != NULL) err = true; - } else { - for(int i = 0; i < tnum; i++){ - targs[i].adb = adb; - targs[i].rnum = rnum; - targs[i].id = i; - if(pthread_create(threads + i, NULL, threadwrite, targs + i) != 0){ - eprint(adb, __LINE__, "pthread_create"); - targs[i].id = -1; + if (!tcadbopen(adb, name)) { + eprint(adb, __LINE__, "tcadbopen"); err = true; - } } - for(int i = 0; i < tnum; i++){ - if(targs[i].id == -1) continue; - void *rv; - if(pthread_join(threads[i], &rv) != 0){ - eprint(adb, __LINE__, "pthread_join"); - err = true; - } else if(rv){ + TARGWRITE targs[tnum]; + pthread_t threads[tnum]; + if (tnum == 1) { + targs[0].adb = adb; + targs[0].rnum = rnum; + targs[0].id = 0; + if (threadwrite(targs) != NULL) err = true; + } else { + for (int i = 0; i < tnum; i++) { + targs[i].adb = adb; + targs[i].rnum = rnum; + targs[i].id = i; + if (pthread_create(threads + i, NULL, threadwrite, targs + i) != 0) { + eprint(adb, __LINE__, "pthread_create"); + targs[i].id = -1; + err = true; + } + } + for (int i = 0; i < tnum; i++) { + if (targs[i].id == -1) continue; + void *rv; + if (pthread_join(threads[i], &rv) != 0) { + eprint(adb, __LINE__, "pthread_join"); + err = true; + } else if (rv) { + err = true; + } + } + } + iprintf("record number: %" PRIuMAX "\n", (uint64_t) tcadbrnum(adb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t) tcadbsize(adb)); + sysprint(); + if (!tcadbclose(adb)) { + eprint(adb, __LINE__, "tcadbclose"); err = true; - } } - } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcadbrnum(adb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcadbsize(adb)); - sysprint(); - if(!tcadbclose(adb)){ - eprint(adb, __LINE__, "tcadbclose"); - err = true; - } - tcadbdel(adb); - iprintf("time: %.3f\n", tctime() - stime); - iprintf("%s\n\n", err ? "error" : "ok"); - return err ? 1 : 0; + tcadbdel(adb); + iprintf("time: %.3f\n", tctime() - stime); + iprintf("%s\n\n", err ? "error" : "ok"); + return err ? 1 : 0; } - /* perform read command */ -static int procread(const char *name, int tnum){ - iprintf("\n seed=%u name=%s tnum=%d\n\n", g_randseed, name, tnum); - bool err = false; - double stime = tctime(); - TCADB *adb = tcadbnew(); - ADBSKEL skel; - if(*name == '@'){ - setskeltran(&skel); - if(!tcadbsetskel(adb, &skel)){ - eprint(adb, __LINE__, "tcadbsetskel"); - err = true; - skel.del(skel.opq); +static int procread(const char *name, int tnum) { + iprintf("\n seed=%u name=%s tnum=%d\n\n", g_randseed, name, tnum); + bool err = false; + double stime = tctime(); + TCADB *adb = tcadbnew(); + ADBSKEL skel; + if (*name == '@') { + setskeltran(&skel); + if (!tcadbsetskel(adb, &skel)) { + eprint(adb, __LINE__, "tcadbsetskel"); + err = true; + skel.del(skel.opq); + } + name++; + } else if (*name == '%') { + if (!tcadbsetskelmulti(adb, MULDIVNUM)) { + eprint(adb, __LINE__, "tcadbsetskelmulti"); + err = true; + } + name++; } - name++; - } else if(*name == '%'){ - if(!tcadbsetskelmulti(adb, MULDIVNUM)){ - eprint(adb, __LINE__, "tcadbsetskelmulti"); - err = true; - } - name++; - } - if(!tcadbopen(adb, name)){ - eprint(adb, __LINE__, "tcadbopen"); - err = true; - } - int rnum = tcadbrnum(adb) / tnum; - TARGREAD targs[tnum]; - pthread_t threads[tnum]; - if(tnum == 1){ - targs[0].adb = adb; - targs[0].rnum = rnum; - targs[0].id = 0; - if(threadread(targs) != NULL) err = true; - } else { - for(int i = 0; i < tnum; i++){ - targs[i].adb = adb; - targs[i].rnum = rnum; - targs[i].id = i; - if(pthread_create(threads + i, NULL, threadread, targs + i) != 0){ - eprint(adb, __LINE__, "pthread_create"); - targs[i].id = -1; + if (!tcadbopen(adb, name)) { + eprint(adb, __LINE__, "tcadbopen"); err = true; - } } - for(int i = 0; i < tnum; i++){ - if(targs[i].id == -1) continue; - void *rv; - if(pthread_join(threads[i], &rv) != 0){ - eprint(adb, __LINE__, "pthread_join"); - err = true; - } else if(rv){ + int rnum = tcadbrnum(adb) / tnum; + TARGREAD targs[tnum]; + pthread_t threads[tnum]; + if (tnum == 1) { + targs[0].adb = adb; + targs[0].rnum = rnum; + targs[0].id = 0; + if (threadread(targs) != NULL) err = true; + } else { + for (int i = 0; i < tnum; i++) { + targs[i].adb = adb; + targs[i].rnum = rnum; + targs[i].id = i; + if (pthread_create(threads + i, NULL, threadread, targs + i) != 0) { + eprint(adb, __LINE__, "pthread_create"); + targs[i].id = -1; + err = true; + } + } + for (int i = 0; i < tnum; i++) { + if (targs[i].id == -1) continue; + void *rv; + if (pthread_join(threads[i], &rv) != 0) { + eprint(adb, __LINE__, "pthread_join"); + err = true; + } else if (rv) { + err = true; + } + } + } + iprintf("record number: %" PRIuMAX "\n", (uint64_t) tcadbrnum(adb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t) tcadbsize(adb)); + sysprint(); + if (!tcadbclose(adb)) { + eprint(adb, __LINE__, "tcadbclose"); err = true; - } } - } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcadbrnum(adb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcadbsize(adb)); - sysprint(); - if(!tcadbclose(adb)){ - eprint(adb, __LINE__, "tcadbclose"); - err = true; - } - tcadbdel(adb); - iprintf("time: %.3f\n", tctime() - stime); - iprintf("%s\n\n", err ? "error" : "ok"); - return err ? 1 : 0; + tcadbdel(adb); + iprintf("time: %.3f\n", tctime() - stime); + iprintf("%s\n\n", err ? "error" : "ok"); + return err ? 1 : 0; } - /* perform remove command */ -static int procremove(const char *name, int tnum){ - iprintf("\n seed=%u name=%s tnum=%d\n\n", g_randseed, name, tnum); - bool err = false; - double stime = tctime(); - TCADB *adb = tcadbnew(); - ADBSKEL skel; - if(*name == '@'){ - setskeltran(&skel); - if(!tcadbsetskel(adb, &skel)){ - eprint(adb, __LINE__, "tcadbsetskel"); - err = true; - skel.del(skel.opq); - } - name++; - } else if(*name == '%'){ - if(!tcadbsetskelmulti(adb, MULDIVNUM)){ - eprint(adb, __LINE__, "tcadbsetskelmulti"); - err = true; +static int procremove(const char *name, int tnum) { + iprintf("\n seed=%u name=%s tnum=%d\n\n", g_randseed, name, tnum); + bool err = false; + double stime = tctime(); + TCADB *adb = tcadbnew(); + ADBSKEL skel; + if (*name == '@') { + setskeltran(&skel); + if (!tcadbsetskel(adb, &skel)) { + eprint(adb, __LINE__, "tcadbsetskel"); + err = true; + skel.del(skel.opq); + } + name++; + } else if (*name == '%') { + if (!tcadbsetskelmulti(adb, MULDIVNUM)) { + eprint(adb, __LINE__, "tcadbsetskelmulti"); + err = true; + } + name++; } - name++; - } - if(!tcadbopen(adb, name)){ - eprint(adb, __LINE__, "tcadbopen"); - err = true; - } - int rnum = tcadbrnum(adb) / tnum; - TARGREMOVE targs[tnum]; - pthread_t threads[tnum]; - if(tnum == 1){ - targs[0].adb = adb; - targs[0].rnum = rnum; - targs[0].id = 0; - if(threadremove(targs) != NULL) err = true; - } else { - for(int i = 0; i < tnum; i++){ - targs[i].adb = adb; - targs[i].rnum = rnum; - targs[i].id = i; - if(pthread_create(threads + i, NULL, threadremove, targs + i) != 0){ - eprint(adb, __LINE__, "pthread_create"); - targs[i].id = -1; + if (!tcadbopen(adb, name)) { + eprint(adb, __LINE__, "tcadbopen"); err = true; - } } - for(int i = 0; i < tnum; i++){ - if(targs[i].id == -1) continue; - void *rv; - if(pthread_join(threads[i], &rv) != 0){ - eprint(adb, __LINE__, "pthread_join"); - err = true; - } else if(rv){ + int rnum = tcadbrnum(adb) / tnum; + TARGREMOVE targs[tnum]; + pthread_t threads[tnum]; + if (tnum == 1) { + targs[0].adb = adb; + targs[0].rnum = rnum; + targs[0].id = 0; + if (threadremove(targs) != NULL) err = true; + } else { + for (int i = 0; i < tnum; i++) { + targs[i].adb = adb; + targs[i].rnum = rnum; + targs[i].id = i; + if (pthread_create(threads + i, NULL, threadremove, targs + i) != 0) { + eprint(adb, __LINE__, "pthread_create"); + targs[i].id = -1; + err = true; + } + } + for (int i = 0; i < tnum; i++) { + if (targs[i].id == -1) continue; + void *rv; + if (pthread_join(threads[i], &rv) != 0) { + eprint(adb, __LINE__, "pthread_join"); + err = true; + } else if (rv) { + err = true; + } + } + } + iprintf("record number: %" PRIuMAX "\n", (uint64_t) tcadbrnum(adb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t) tcadbsize(adb)); + sysprint(); + if (!tcadbclose(adb)) { + eprint(adb, __LINE__, "tcadbclose"); err = true; - } } - } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcadbrnum(adb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcadbsize(adb)); - sysprint(); - if(!tcadbclose(adb)){ - eprint(adb, __LINE__, "tcadbclose"); - err = true; - } - tcadbdel(adb); - iprintf("time: %.3f\n", tctime() - stime); - iprintf("%s\n\n", err ? "error" : "ok"); - return err ? 1 : 0; + tcadbdel(adb); + iprintf("time: %.3f\n", tctime() - stime); + iprintf("%s\n\n", err ? "error" : "ok"); + return err ? 1 : 0; } - /* thread the write function */ -static void *threadwrite(void *targ){ - TCADB *adb = ((TARGWRITE *)targ)->adb; - int rnum = ((TARGWRITE *)targ)->rnum; - int id = ((TARGWRITE *)targ)->id; - bool err = false; - int base = id * rnum; - for(int i = 1; i <= rnum; i++){ - char buf[RECBUFSIZ]; - int len = sprintf(buf, "%08d", base + i + 1); - if(!tcadbput(adb, buf, len, buf, len)){ - eprint(adb, __LINE__, "tcadbput"); - err = true; - break; - } - if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){ - iputchar('.'); - if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); +static void *threadwrite(void *targ) { + TCADB *adb = ((TARGWRITE *) targ)->adb; + int rnum = ((TARGWRITE *) targ)->rnum; + int id = ((TARGWRITE *) targ)->id; + bool err = false; + int base = id * rnum; + for (int i = 1; i <= rnum; i++) { + char buf[RECBUFSIZ]; + int len = sprintf(buf, "%08d", base + i + 1); + if (!tcadbput(adb, buf, len, buf, len)) { + eprint(adb, __LINE__, "tcadbput"); + err = true; + break; + } + if (id == 0 && rnum > 250 && i % (rnum / 250) == 0) { + iputchar('.'); + if (i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); + } } - } - return err ? "error" : NULL; + return err ? "error" : NULL; } - /* thread the read function */ -static void *threadread(void *targ){ - TCADB *adb = ((TARGREAD *)targ)->adb; - int rnum = ((TARGREAD *)targ)->rnum; - int id = ((TARGREAD *)targ)->id; - bool err = false; - int base = id * rnum; - for(int i = 1; i <= rnum && !err; i++){ - char kbuf[RECBUFSIZ]; - int ksiz = sprintf(kbuf, "%08d", base + i + 1); - int vsiz; - char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz); - if(!vbuf){ - eprint(adb, __LINE__, "tcadbget"); - err = true; - } - tcfree(vbuf); - if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){ - iputchar('.'); - if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); +static void *threadread(void *targ) { + TCADB *adb = ((TARGREAD *) targ)->adb; + int rnum = ((TARGREAD *) targ)->rnum; + int id = ((TARGREAD *) targ)->id; + bool err = false; + int base = id * rnum; + for (int i = 1; i <= rnum && !err; i++) { + char kbuf[RECBUFSIZ]; + int ksiz = sprintf(kbuf, "%08d", base + i + 1); + int vsiz; + char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz); + if (!vbuf) { + eprint(adb, __LINE__, "tcadbget"); + err = true; + } + tcfree(vbuf); + if (id == 0 && rnum > 250 && i % (rnum / 250) == 0) { + iputchar('.'); + if (i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); + } } - } - return err ? "error" : NULL; + return err ? "error" : NULL; } - /* thread the remove function */ -static void *threadremove(void *targ){ - TCADB *adb = ((TARGREMOVE *)targ)->adb; - int rnum = ((TARGREMOVE *)targ)->rnum; - int id = ((TARGREMOVE *)targ)->id; - bool err = false; - int base = id * rnum; - for(int i = 1; i <= rnum; i++){ - char kbuf[RECBUFSIZ]; - int ksiz = sprintf(kbuf, "%08d", base + i + 1); - if(!tcadbout(adb, kbuf, ksiz)){ - eprint(adb, __LINE__, "tcadbout"); - err = true; - break; - } - if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){ - iputchar('.'); - if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); +static void *threadremove(void *targ) { + TCADB *adb = ((TARGREMOVE *) targ)->adb; + int rnum = ((TARGREMOVE *) targ)->rnum; + int id = ((TARGREMOVE *) targ)->id; + bool err = false; + int base = id * rnum; + for (int i = 1; i <= rnum; i++) { + char kbuf[RECBUFSIZ]; + int ksiz = sprintf(kbuf, "%08d", base + i + 1); + if (!tcadbout(adb, kbuf, ksiz)) { + eprint(adb, __LINE__, "tcadbout"); + err = true; + break; + } + if (id == 0 && rnum > 250 && i % (rnum / 250) == 0) { + iputchar('.'); + if (i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); + } } - } - return err ? "error" : NULL; + return err ? "error" : NULL; } diff --git a/tcejdb/tcatest.c b/tcejdb/tcatest.c index adfd364..77849d9 100644 --- a/tcejdb/tcatest.c +++ b/tcejdb/tcatest.c @@ -442,8 +442,8 @@ static int procwrite(const char *name, int rnum){ if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcadbrnum(adb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcadbsize(adb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcadbrnum(adb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcadbsize(adb)); sysprint(); if(!tcadbclose(adb)){ eprint(adb, __LINE__, "tcadbclose"); @@ -499,8 +499,8 @@ static int procread(const char *name){ if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcadbrnum(adb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcadbsize(adb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcadbrnum(adb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcadbsize(adb)); sysprint(); if(!tcadbclose(adb)){ eprint(adb, __LINE__, "tcadbclose"); @@ -553,8 +553,8 @@ static int procremove(const char *name){ if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcadbrnum(adb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcadbsize(adb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcadbrnum(adb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcadbsize(adb)); sysprint(); if(!tcadbclose(adb)){ eprint(adb, __LINE__, "tcadbclose"); @@ -608,8 +608,8 @@ static int procrcat(const char *name, int rnum){ if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcadbrnum(adb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcadbsize(adb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcadbrnum(adb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcadbsize(adb)); sysprint(); if(!tcadbclose(adb)){ eprint(adb, __LINE__, "tcadbclose"); @@ -1110,8 +1110,8 @@ static int procmisc(const char *name, int rnum){ eprint(adb, __LINE__, "tcadbforeach"); err = true; } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcadbrnum(adb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcadbsize(adb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcadbrnum(adb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcadbsize(adb)); sysprint(); if(!tcadbclose(adb)){ eprint(adb, __LINE__, "tcadbclose"); @@ -1315,8 +1315,8 @@ static int procwicked(const char *name, int rnum){ eprint(adb, __LINE__, "(validation)"); err = true; } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcadbrnum(adb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcadbsize(adb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcadbrnum(adb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcadbsize(adb)); sysprint(); tcmapdel(map); if(!tcadbclose(adb)){ diff --git a/tcejdb/tcawmgr.c b/tcejdb/tcawmgr.c index 303c12b..1d4c223 100644 --- a/tcejdb/tcawmgr.c +++ b/tcejdb/tcawmgr.c @@ -380,8 +380,8 @@ static void sethtmlheader(PARAMS *params, TCXSTR *obuf, TCADB *db){ /* set the footer of HTML */ static void sethtmlfooter(PARAMS *params, TCXSTR *obuf, TCADB *db){ XP("
\n"); - XP("
record number: %" PRIdMAX "
\n", (long long)tcadbrnum(db)); - XP("
size: %" PRIdMAX "
\n", (long long)tcadbsize(db)); + XP("
record number: %" PRIdMAX "
\n", (int64_t)tcadbrnum(db)); + XP("
size: %" PRIdMAX "
\n", (int64_t)tcadbsize(db)); XP("\n"); XP("\n"); XP("\n"); diff --git a/tcejdb/tcbdb.c b/tcejdb/tcbdb.c index e076630..6b6f4d1 100644 --- a/tcejdb/tcbdb.c +++ b/tcejdb/tcbdb.c @@ -1845,7 +1845,7 @@ static bool tcbdbleafsave(TCBDB *bdb, BDBLEAF *leaf) { } } bool err = false; - step = sprintf(hbuf, "%" PRIxMAX "", (unsigned long long) leaf->id); + step = sprintf(hbuf, "%" PRIxMAX "", (uint64_t) leaf->id); if (ln < 1 && !tchdbout(bdb->hdb, hbuf, step) && tchdbecode(bdb->hdb) != TCENOREC) err = true; if (!leaf->dead && !tchdbput(bdb->hdb, hbuf, step, TCXSTRPTR(rbuf), TCXSTRSIZE(rbuf))) @@ -1873,7 +1873,7 @@ static BDBLEAF *tcbdbleafload(TCBDB *bdb, uint64_t id) { TCDODEBUG(bdb->cnt_loadleaf++); char hbuf[(sizeof (uint64_t) + 1)*3]; int step; - step = sprintf(hbuf, "%" PRIxMAX "", (unsigned long long) id); + step = sprintf(hbuf, "%" PRIxMAX "", (uint64_t) id); char *rbuf = NULL; char wbuf[BDBPAGEBUFSIZ]; const char *rp = NULL; @@ -1991,7 +1991,7 @@ static bool tcbdbleafcheck(TCBDB *bdb, uint64_t id) { if (clk) BDBUNLOCKCACHE(bdb); if (leaf) return true; char hbuf[(sizeof (uint64_t) + 1)*3]; - int step = sprintf(hbuf, "%" PRIxMAX "", (unsigned long long) id); + int step = sprintf(hbuf, "%" PRIxMAX "", (uint64_t) id); return tchdbvsiz(bdb->hdb, hbuf, step) > 0; } @@ -2383,7 +2383,7 @@ static bool tcbdbnodesave(TCBDB *bdb, BDBNODE *node) { TCXSTRCAT(rbuf, ebuf, idx->ksiz); } bool err = false; - step = sprintf(hbuf, "#%" PRIxMAX "", (unsigned long long) (node->id - BDBNODEIDBASE)); + step = sprintf(hbuf, "#%" PRIxMAX "", (uint64_t) (node->id - BDBNODEIDBASE)); if (ln < 1 && !tchdbout(bdb->hdb, hbuf, step) && tchdbecode(bdb->hdb) != TCENOREC) err = true; if (!node->dead && !tchdbput(bdb->hdb, hbuf, step, TCXSTRPTR(rbuf), TCXSTRSIZE(rbuf))) @@ -2411,7 +2411,7 @@ static BDBNODE *tcbdbnodeload(TCBDB *bdb, uint64_t id) { TCDODEBUG(bdb->cnt_loadnode++); char hbuf[(sizeof (uint64_t) + 1)*2]; int step; - step = sprintf(hbuf, "#%" PRIxMAX "", (unsigned long long) (id - BDBNODEIDBASE)); + step = sprintf(hbuf, "#%" PRIxMAX "", (uint64_t) (id - BDBNODEIDBASE)); char *rbuf = NULL; char wbuf[BDBPAGEBUFSIZ]; const char *rp = NULL; @@ -3949,12 +3949,12 @@ void tcbdbprintmeta(TCBDB *bdb) { wp += sprintf(wp, " lmemb=%u", bdb->lmemb); wp += sprintf(wp, " nmemb=%u", bdb->nmemb); wp += sprintf(wp, " opts=%u", bdb->opts); - wp += sprintf(wp, " root=%" PRIxMAX "", (unsigned long long) bdb->root); - wp += sprintf(wp, " first=%" PRIxMAX "", (unsigned long long) bdb->first); - wp += sprintf(wp, " last=%" PRIxMAX "", (unsigned long long) bdb->last); - wp += sprintf(wp, " lnum=%" PRIuMAX "", (unsigned long long) bdb->lnum); - wp += sprintf(wp, " nnum=%" PRIuMAX "", (unsigned long long) bdb->nnum); - wp += sprintf(wp, " rnum=%" PRIuMAX "", (unsigned long long) bdb->rnum); + wp += sprintf(wp, " root=%" PRIxMAX "", (uint64_t) bdb->root); + wp += sprintf(wp, " first=%" PRIxMAX "", (uint64_t) bdb->first); + wp += sprintf(wp, " last=%" PRIxMAX "", (uint64_t) bdb->last); + wp += sprintf(wp, " lnum=%" PRIuMAX "", (uint64_t) bdb->lnum); + wp += sprintf(wp, " nnum=%" PRIuMAX "", (uint64_t) bdb->nnum); + wp += sprintf(wp, " rnum=%" PRIuMAX "", (uint64_t) bdb->rnum); wp += sprintf(wp, " leafc=%p", (void *) bdb->leafc); wp += sprintf(wp, " nodec=%p", (void *) bdb->nodec); wp += sprintf(wp, " cmp=%p", (void *) (intptr_t) bdb->cmp); @@ -3963,21 +3963,21 @@ void tcbdbprintmeta(TCBDB *bdb) { wp += sprintf(wp, " ncnum=%u", bdb->ncnum); wp += sprintf(wp, " lsmax=%u", bdb->lsmax); wp += sprintf(wp, " lschk=%u", bdb->lschk); - wp += sprintf(wp, " capnum=%" PRIuMAX "", (unsigned long long) bdb->capnum); + wp += sprintf(wp, " capnum=%" PRIuMAX "", (uint64_t) bdb->capnum); wp += sprintf(wp, " hist=%p", (void *) bdb->hist); wp += sprintf(wp, " hnum=%d", bdb->hnum); - wp += sprintf(wp, " hleaf=%" PRIuMAX "", (unsigned long long) bdb->hleaf); - wp += sprintf(wp, " lleaf=%" PRIuMAX "", (unsigned long long) bdb->lleaf); + wp += sprintf(wp, " hleaf=%" PRIuMAX "", (uint64_t) bdb->hleaf); + wp += sprintf(wp, " lleaf=%" PRIuMAX "", (uint64_t) bdb->lleaf); wp += sprintf(wp, " tran=%d", bdb->tran); //wp += sprintf(wp, " rbopaque=%p", (void *)bdb->rbopaque); - wp += sprintf(wp, " clock=%" PRIuMAX "", (unsigned long long) bdb->clock); - wp += sprintf(wp, " cnt_saveleaf=%" PRIdMAX "", (long long) bdb->cnt_saveleaf); - wp += sprintf(wp, " cnt_loadleaf=%" PRIdMAX "", (long long) bdb->cnt_loadleaf); - wp += sprintf(wp, " cnt_killleaf=%" PRIdMAX "", (long long) bdb->cnt_killleaf); - wp += sprintf(wp, " cnt_adjleafc=%" PRIdMAX "", (long long) bdb->cnt_adjleafc); - wp += sprintf(wp, " cnt_savenode=%" PRIdMAX "", (long long) bdb->cnt_savenode); - wp += sprintf(wp, " cnt_loadnode=%" PRIdMAX "", (long long) bdb->cnt_loadnode); - wp += sprintf(wp, " cnt_adjnodec=%" PRIdMAX "", (long long) bdb->cnt_adjnodec); + wp += sprintf(wp, " clock=%" PRIuMAX "", (uint64_t) bdb->clock); + wp += sprintf(wp, " cnt_saveleaf=%" PRIdMAX "", (int64_t) bdb->cnt_saveleaf); + wp += sprintf(wp, " cnt_loadleaf=%" PRIdMAX "", (int64_t) bdb->cnt_loadleaf); + wp += sprintf(wp, " cnt_killleaf=%" PRIdMAX "", (int64_t) bdb->cnt_killleaf); + wp += sprintf(wp, " cnt_adjleafc=%" PRIdMAX "", (int64_t) bdb->cnt_adjleafc); + wp += sprintf(wp, " cnt_savenode=%" PRIdMAX "", (int64_t) bdb->cnt_savenode); + wp += sprintf(wp, " cnt_loadnode=%" PRIdMAX "", (int64_t) bdb->cnt_loadnode); + wp += sprintf(wp, " cnt_adjnodec=%" PRIdMAX "", (int64_t) bdb->cnt_adjnodec); *(wp++) = '\n'; tcwrite(dbgfd, buf, wp - buf); } @@ -3995,10 +3995,10 @@ void tcbdbprintleaf(TCBDB *bdb, BDBLEAF *leaf) { char buf[BDBPAGEBUFSIZ]; char *wp = buf; wp += sprintf(wp, "LEAF:"); - wp += sprintf(wp, " id:%" PRIxMAX "", (unsigned long long) leaf->id); + wp += sprintf(wp, " id:%" PRIxMAX "", (uint64_t) leaf->id); wp += sprintf(wp, " size:%u", leaf->size); - wp += sprintf(wp, " prev:%" PRIxMAX "", (unsigned long long) leaf->prev); - wp += sprintf(wp, " next:%" PRIxMAX "", (unsigned long long) leaf->next); + wp += sprintf(wp, " prev:%" PRIxMAX "", (uint64_t) leaf->prev); + wp += sprintf(wp, " next:%" PRIxMAX "", (uint64_t) leaf->next); wp += sprintf(wp, " dirty:%d", leaf->dirty); wp += sprintf(wp, " dead:%d", leaf->dead); wp += sprintf(wp, " rnum:%d", TCPTRLISTNUM(recs)); @@ -4033,8 +4033,8 @@ void tcbdbprintnode(TCBDB *bdb, BDBNODE *node) { char buf[BDBPAGEBUFSIZ]; char *wp = buf; wp += sprintf(wp, "NODE:"); - wp += sprintf(wp, " id:%" PRIxMAX "", (unsigned long long) node->id); - wp += sprintf(wp, " heir:%" PRIxMAX "", (unsigned long long) node->heir); + wp += sprintf(wp, " id:%" PRIxMAX "", (uint64_t) node->id); + wp += sprintf(wp, " heir:%" PRIxMAX "", (uint64_t) node->heir); wp += sprintf(wp, " dirty:%d", node->dirty); wp += sprintf(wp, " dead:%d", node->dead); wp += sprintf(wp, " rnum:%d", TCPTRLISTNUM(idxs)); @@ -4044,7 +4044,7 @@ void tcbdbprintnode(TCBDB *bdb, BDBNODE *node) { wp = buf; BDBIDX *idx = TCPTRLISTVAL(idxs, i); char *ebuf = (char *) idx + sizeof (*idx); - wp += sprintf(wp, " [%" PRIxMAX ":%s]", (unsigned long long) idx->pid, ebuf); + wp += sprintf(wp, " [%" PRIxMAX ":%s]", (uint64_t) idx->pid, ebuf); } *(wp++) = '\n'; tcwrite(dbgfd, buf, wp - buf); diff --git a/tcejdb/tcbmgr.c b/tcejdb/tcbmgr.c index 9877b5c..1053285 100644 --- a/tcejdb/tcbmgr.c +++ b/tcejdb/tcbmgr.c @@ -662,18 +662,18 @@ static int procinform(const char *path, int omode){ printf("\n"); printf("max leaf member: %d\n", tcbdblmemb(bdb)); printf("max node member: %d\n", tcbdbnmemb(bdb)); - printf("leaf number: %" PRIuMAX "\n", (unsigned long long)tcbdblnum(bdb)); - printf("node number: %" PRIuMAX "\n", (unsigned long long)tcbdbnnum(bdb)); - printf("bucket number: %" PRIuMAX "\n", (unsigned long long)tcbdbbnum(bdb)); + printf("leaf number: %" PRIuMAX "\n", (uint64_t)tcbdblnum(bdb)); + printf("node number: %" PRIuMAX "\n", (uint64_t)tcbdbnnum(bdb)); + printf("bucket number: %" PRIuMAX "\n", (uint64_t)tcbdbbnum(bdb)); #ifndef NDEBUG if(bdb->hdb->cnt_writerec >= 0) - printf("used bucket number: %" PRIdMAX "\n", (long long)tcbdbbnumused(bdb)); -#endif + printf("used bucket number: %" PRIdMAX "\n", (int64_t) tcbdbbnumused(bdb)); +#endif printf("alignment: %u\n", tcbdbalign(bdb)); printf("free block pool: %u\n", tcbdbfbpmax(bdb)); - printf("inode number: %" PRIdMAX "\n", (long long)tcbdbinode(bdb)); + printf("inode number: %" PRIdMAX "\n", (int64_t) tcbdbinode(bdb)); char date[48]; tcdatestrwww(tcbdbmtime(bdb), INT_MAX, date); printf("modified time: %s\n", date); @@ -685,8 +685,8 @@ static int procinform(const char *path, int omode){ if(opts & BDBTTCBS) printf(" tcbs"); if(opts & BDBTEXCODEC) printf(" excodec"); printf("\n"); - printf("record number: %" PRIuMAX "\n", (unsigned long long)tcbdbrnum(bdb)); - printf("file size: %" PRIuMAX "\n", (unsigned long long)tcbdbfsiz(bdb)); + printf("record number: %" PRIuMAX "\n", (uint64_t)tcbdbrnum(bdb)); + printf("file size: %" PRIuMAX "\n", (uint64_t)tcbdbfsiz(bdb)); if(!tcbdbclose(bdb)){ if(!err) printerr(bdb); err = true; diff --git a/tcejdb/tcbmttest.c b/tcejdb/tcbmttest.c index c11dd81..b8cc44f 100644 --- a/tcejdb/tcbmttest.c +++ b/tcejdb/tcbmttest.c @@ -203,34 +203,34 @@ static void mprint(TCBDB *bdb){ if(bdb->hdb->cnt_writerec < 0) return; iprintf("max leaf member: %d\n", tcbdblmemb(bdb)); iprintf("max node member: %d\n", tcbdbnmemb(bdb)); - iprintf("leaf number: %" PRIdMAX "\n", (long long)tcbdblnum(bdb)); - iprintf("node number: %" PRIdMAX "\n", (long long)tcbdbnnum(bdb)); - iprintf("bucket number: %" PRIdMAX "\n", (long long)tcbdbbnum(bdb)); - iprintf("used bucket number: %" PRIdMAX "\n", (long long)tcbdbbnumused(bdb)); - iprintf("cnt_saveleaf: %" PRIdMAX "\n", (long long)bdb->cnt_saveleaf); - iprintf("cnt_loadleaf: %" PRIdMAX "\n", (long long)bdb->cnt_loadleaf); - iprintf("cnt_killleaf: %" PRIdMAX "\n", (long long)bdb->cnt_killleaf); - iprintf("cnt_adjleafc: %" PRIdMAX "\n", (long long)bdb->cnt_adjleafc); - iprintf("cnt_savenode: %" PRIdMAX "\n", (long long)bdb->cnt_savenode); - iprintf("cnt_loadnode: %" PRIdMAX "\n", (long long)bdb->cnt_loadnode); - iprintf("cnt_adjnodec: %" PRIdMAX "\n", (long long)bdb->cnt_adjnodec); - iprintf("cnt_writerec: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_writerec); - iprintf("cnt_reuserec: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_reuserec); - iprintf("cnt_moverec: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_moverec); - iprintf("cnt_readrec: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_readrec); - iprintf("cnt_searchfbp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_searchfbp); - iprintf("cnt_insertfbp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_insertfbp); - iprintf("cnt_splicefbp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_splicefbp); - iprintf("cnt_dividefbp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_dividefbp); - iprintf("cnt_mergefbp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_mergefbp); - iprintf("cnt_reducefbp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_reducefbp); - iprintf("cnt_appenddrp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_appenddrp); - iprintf("cnt_deferdrp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_deferdrp); - iprintf("cnt_flushdrp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_flushdrp); - iprintf("cnt_adjrecc: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_adjrecc); - iprintf("cnt_defrag: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_defrag); - iprintf("cnt_shiftrec: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_shiftrec); - iprintf("cnt_trunc: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_trunc); + iprintf("leaf number: %" PRIdMAX "\n", (int64_t)tcbdblnum(bdb)); + iprintf("node number: %" PRIdMAX "\n", (int64_t)tcbdbnnum(bdb)); + iprintf("bucket number: %" PRIdMAX "\n", (int64_t)tcbdbbnum(bdb)); + iprintf("used bucket number: %" PRIdMAX "\n", (int64_t)tcbdbbnumused(bdb)); + iprintf("cnt_saveleaf: %" PRIdMAX "\n", (int64_t)bdb->cnt_saveleaf); + iprintf("cnt_loadleaf: %" PRIdMAX "\n", (int64_t)bdb->cnt_loadleaf); + iprintf("cnt_killleaf: %" PRIdMAX "\n", (int64_t)bdb->cnt_killleaf); + iprintf("cnt_adjleafc: %" PRIdMAX "\n", (int64_t)bdb->cnt_adjleafc); + iprintf("cnt_savenode: %" PRIdMAX "\n", (int64_t)bdb->cnt_savenode); + iprintf("cnt_loadnode: %" PRIdMAX "\n", (int64_t)bdb->cnt_loadnode); + iprintf("cnt_adjnodec: %" PRIdMAX "\n", (int64_t)bdb->cnt_adjnodec); + iprintf("cnt_writerec: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_writerec); + iprintf("cnt_reuserec: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_reuserec); + iprintf("cnt_moverec: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_moverec); + iprintf("cnt_readrec: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_readrec); + iprintf("cnt_searchfbp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_searchfbp); + iprintf("cnt_insertfbp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_insertfbp); + iprintf("cnt_splicefbp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_splicefbp); + iprintf("cnt_dividefbp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_dividefbp); + iprintf("cnt_mergefbp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_mergefbp); + iprintf("cnt_reducefbp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_reducefbp); + iprintf("cnt_appenddrp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_appenddrp); + iprintf("cnt_deferdrp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_deferdrp); + iprintf("cnt_flushdrp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_flushdrp); + iprintf("cnt_adjrecc: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_adjrecc); + iprintf("cnt_defrag: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_defrag); + iprintf("cnt_shiftrec: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_shiftrec); + iprintf("cnt_trunc: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_trunc); } @@ -710,8 +710,8 @@ static int procwrite(const char *path, int tnum, int rnum, int lmemb, int nmemb, } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcbdbrnum(bdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcbdbfsiz(bdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcbdbrnum(bdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ @@ -788,8 +788,8 @@ static int procread(const char *path, int tnum, int xmsiz, int dfunit, int omode } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcbdbrnum(bdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcbdbfsiz(bdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcbdbrnum(bdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ @@ -863,8 +863,8 @@ static int procremove(const char *path, int tnum, int xmsiz, int dfunit, int omo } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcbdbrnum(bdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcbdbfsiz(bdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcbdbrnum(bdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ @@ -975,8 +975,8 @@ static int procwicked(const char *path, int tnum, int rnum, int opts, int omode, if(rnum % 50 > 0) iprintf(" (%08d)\n", rnum); } tcmapdel(map); - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcbdbrnum(bdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcbdbfsiz(bdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcbdbrnum(bdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ @@ -1060,8 +1060,8 @@ static int proctypical(const char *path, int tnum, int rnum, int lmemb, int nmem } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcbdbrnum(bdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcbdbfsiz(bdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcbdbrnum(bdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ @@ -1139,8 +1139,8 @@ static int procrace(const char *path, int tnum, int rnum, int lmemb, int nmemb, } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcbdbrnum(bdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcbdbfsiz(bdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcbdbrnum(bdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ diff --git a/tcejdb/tcbtest.c b/tcejdb/tcbtest.c index c0d998a..d3200f6 100644 --- a/tcejdb/tcbtest.c +++ b/tcejdb/tcbtest.c @@ -166,34 +166,34 @@ static void mprint(TCBDB *bdb){ if(bdb->hdb->cnt_writerec < 0) return; iprintf("max leaf member: %d\n", tcbdblmemb(bdb)); iprintf("max node member: %d\n", tcbdbnmemb(bdb)); - iprintf("leaf number: %" PRIdMAX "\n", (long long)tcbdblnum(bdb)); - iprintf("node number: %" PRIdMAX "\n", (long long)tcbdbnnum(bdb)); - iprintf("bucket number: %" PRIdMAX "\n", (long long)tcbdbbnum(bdb)); - iprintf("used bucket number: %" PRIdMAX "\n", (long long)tcbdbbnumused(bdb)); - iprintf("cnt_saveleaf: %" PRIdMAX "\n", (long long)bdb->cnt_saveleaf); - iprintf("cnt_loadleaf: %" PRIdMAX "\n", (long long)bdb->cnt_loadleaf); - iprintf("cnt_killleaf: %" PRIdMAX "\n", (long long)bdb->cnt_killleaf); - iprintf("cnt_adjleafc: %" PRIdMAX "\n", (long long)bdb->cnt_adjleafc); - iprintf("cnt_savenode: %" PRIdMAX "\n", (long long)bdb->cnt_savenode); - iprintf("cnt_loadnode: %" PRIdMAX "\n", (long long)bdb->cnt_loadnode); - iprintf("cnt_adjnodec: %" PRIdMAX "\n", (long long)bdb->cnt_adjnodec); - iprintf("cnt_writerec: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_writerec); - iprintf("cnt_reuserec: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_reuserec); - iprintf("cnt_moverec: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_moverec); - iprintf("cnt_readrec: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_readrec); - iprintf("cnt_searchfbp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_searchfbp); - iprintf("cnt_insertfbp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_insertfbp); - iprintf("cnt_splicefbp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_splicefbp); - iprintf("cnt_dividefbp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_dividefbp); - iprintf("cnt_mergefbp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_mergefbp); - iprintf("cnt_reducefbp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_reducefbp); - iprintf("cnt_appenddrp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_appenddrp); - iprintf("cnt_deferdrp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_deferdrp); - iprintf("cnt_flushdrp: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_flushdrp); - iprintf("cnt_adjrecc: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_adjrecc); - iprintf("cnt_defrag: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_defrag); - iprintf("cnt_shiftrec: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_shiftrec); - iprintf("cnt_trunc: %" PRIdMAX "\n", (long long)bdb->hdb->cnt_trunc); + iprintf("leaf number: %" PRIdMAX "\n", (int64_t)tcbdblnum(bdb)); + iprintf("node number: %" PRIdMAX "\n", (int64_t)tcbdbnnum(bdb)); + iprintf("bucket number: %" PRIdMAX "\n", (int64_t)tcbdbbnum(bdb)); + iprintf("used bucket number: %" PRIdMAX "\n", (int64_t)tcbdbbnumused(bdb)); + iprintf("cnt_saveleaf: %" PRIdMAX "\n", (int64_t)bdb->cnt_saveleaf); + iprintf("cnt_loadleaf: %" PRIdMAX "\n", (int64_t)bdb->cnt_loadleaf); + iprintf("cnt_killleaf: %" PRIdMAX "\n", (int64_t)bdb->cnt_killleaf); + iprintf("cnt_adjleafc: %" PRIdMAX "\n", (int64_t)bdb->cnt_adjleafc); + iprintf("cnt_savenode: %" PRIdMAX "\n", (int64_t)bdb->cnt_savenode); + iprintf("cnt_loadnode: %" PRIdMAX "\n", (int64_t)bdb->cnt_loadnode); + iprintf("cnt_adjnodec: %" PRIdMAX "\n", (int64_t)bdb->cnt_adjnodec); + iprintf("cnt_writerec: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_writerec); + iprintf("cnt_reuserec: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_reuserec); + iprintf("cnt_moverec: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_moverec); + iprintf("cnt_readrec: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_readrec); + iprintf("cnt_searchfbp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_searchfbp); + iprintf("cnt_insertfbp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_insertfbp); + iprintf("cnt_splicefbp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_splicefbp); + iprintf("cnt_dividefbp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_dividefbp); + iprintf("cnt_mergefbp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_mergefbp); + iprintf("cnt_reducefbp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_reducefbp); + iprintf("cnt_appenddrp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_appenddrp); + iprintf("cnt_deferdrp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_deferdrp); + iprintf("cnt_flushdrp: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_flushdrp); + iprintf("cnt_adjrecc: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_adjrecc); + iprintf("cnt_defrag: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_defrag); + iprintf("cnt_shiftrec: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_shiftrec); + iprintf("cnt_trunc: %" PRIdMAX "\n", (int64_t)bdb->hdb->cnt_trunc); } @@ -848,8 +848,8 @@ static int procwrite(const char *path, int rnum, int lmemb, int nmemb, int bnum, if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcbdbrnum(bdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcbdbfsiz(bdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcbdbrnum(bdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ @@ -942,8 +942,8 @@ static int procread(const char *path, bool mt, TCCMP cmp, int lcnum, int ncnum, if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcbdbrnum(bdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcbdbfsiz(bdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcbdbrnum(bdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ @@ -1022,8 +1022,8 @@ static int procremove(const char *path, bool mt, TCCMP cmp, int lcnum, int ncnum if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcbdbrnum(bdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcbdbfsiz(bdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcbdbrnum(bdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ @@ -1218,8 +1218,8 @@ static int procrcat(const char *path, int rnum, if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcbdbrnum(bdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcbdbfsiz(bdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcbdbrnum(bdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ @@ -1348,8 +1348,8 @@ static int procqueue(const char *path, int rnum, int lmemb, int nmemb, int bnum, break; } tcbdbcurdel(cur); - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcbdbrnum(bdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcbdbfsiz(bdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcbdbrnum(bdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ @@ -2183,8 +2183,8 @@ static int procmisc(const char *path, int rnum, bool mt, int opts, int omode){ err = true; } tcbdbcurdel(cur); - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcbdbrnum(bdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcbdbfsiz(bdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcbdbrnum(bdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ @@ -2573,8 +2573,8 @@ static int procwicked(const char *path, int rnum, bool mt, int opts, int omode){ err = true; } tcbdbcurdel(cur); - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcbdbrnum(bdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcbdbfsiz(bdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcbdbrnum(bdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); tcmapdel(map); diff --git a/tcejdb/tcfdb.c b/tcejdb/tcfdb.c index cd6f6e3..7956e0c 100644 --- a/tcejdb/tcfdb.c +++ b/tcejdb/tcfdb.c @@ -119,7 +119,7 @@ static bool tcfdbvanishimpl(TCFDB *fdb); static bool tcfdbcopyimpl(TCFDB *fdb, const char *path); static bool tcfdbiterjumpimpl(TCFDB *fdb, int64_t id); static bool tcfdbforeachimpl(TCFDB *fdb, TCITER iter, void *op); -static bool tcfdbftruncate(TCFDB *fdb, off_t length); +static bool tcfdbftruncate(TCFDB *fdb, off_t length) __attribute__ ((unused)); static bool tcfdbftruncate2(TCFDB *fdb, off_t length, int opts); EJDB_INLINE bool tcfdblockmethod(TCFDB *fdb, bool wr); @@ -667,7 +667,7 @@ void *tcfdbiternext2(TCFDB *fdb, int *sp) { uint64_t id = tcfdbiternext(fdb); if (id < 1) return NULL; char kbuf[TCNUMBUFSIZ]; - int ksiz = sprintf(kbuf, "%" PRIuMAX "", (unsigned long long) id); + int ksiz = sprintf(kbuf, "%" PRIuMAX "", (uint64_t) id); *sp = ksiz; return tcmemdup(kbuf, ksiz); } @@ -714,7 +714,7 @@ TCLIST *tcfdbrange2(TCFDB *fdb, const void *lbuf, int lsiz, const void *ubuf, in TCLIST *keys = tclistnew2(num); for (int i = 0; i < num; i++) { char kbuf[TCNUMBUFSIZ]; - int ksiz = sprintf(kbuf, "%" PRIuMAX "", (unsigned long long) ids[i]); + int ksiz = sprintf(kbuf, "%" PRIuMAX "", (uint64_t) ids[i]); TCLISTPUSH(keys, kbuf, ksiz); } TCFREE(ids); @@ -794,7 +794,7 @@ TCLIST *tcfdbrange4(TCFDB *fdb, const void *ibuf, int isiz, int max) { TCLIST *keys = tclistnew2(num); for (int i = 0; i < num; i++) { char kbuf[TCNUMBUFSIZ]; - int ksiz = sprintf(kbuf, "%" PRIuMAX "", (unsigned long long) ids[i]); + int ksiz = sprintf(kbuf, "%" PRIuMAX "", (uint64_t) ids[i]); TCLISTPUSH(keys, kbuf, ksiz); } TCFREE(ids); @@ -1589,7 +1589,9 @@ static void tcfdbclear(TCFDB *fdb) { fdb->max = 0; fdb->iter = 0; fdb->map = NULL; +#ifdef _WIN32 fdb->w32hmap = NULL; +#endif fdb->ecode = TCESUCCESS; fdb->fatal = false; fdb->inode = 0; @@ -2700,7 +2702,7 @@ static bool tcfdbcopyimpl(TCFDB *fdb, const char *path) { } if (*path == '@') { char tsbuf[TCNUMBUFSIZ]; - sprintf(tsbuf, "%" PRIuMAX "", (unsigned long long) (tctime() * 1000000)); + sprintf(tsbuf, "%" PRIuMAX "", (uint64_t) (tctime() * 1000000)); const char *args[3]; args[0] = path + 1; args[1] = fdb->path; @@ -2760,7 +2762,7 @@ static bool tcfdbforeachimpl(TCFDB *fdb, TCITER iter, void *op) { FDBUNLOCKSMEM(fdb); if (vbuf) { char kbuf[TCNUMBUFSIZ]; - int ksiz = sprintf(kbuf, "%" PRIuMAX "", (unsigned long long) id); + int ksiz = sprintf(kbuf, "%" PRIuMAX "", (uint64_t) id); if (!iter(kbuf, ksiz, vbuf, vsiz, op)) { break; } @@ -3070,31 +3072,31 @@ void tcfdbprintmeta(TCFDB *fdb) { wp += sprintf(wp, " type=%02X", fdb->type); wp += sprintf(wp, " flags=%02X", fdb->flags); wp += sprintf(wp, " width=%u", fdb->width); - wp += sprintf(wp, " limsiz=%" PRIuMAX "", (unsigned long long) fdb->limsiz); + wp += sprintf(wp, " limsiz=%" PRIuMAX "", (uint64_t) fdb->limsiz); wp += sprintf(wp, " wsiz=%u", fdb->wsiz); wp += sprintf(wp, " rsiz=%u", fdb->rsiz); - wp += sprintf(wp, " limid=%" PRIuMAX "", (unsigned long long) fdb->limid); + wp += sprintf(wp, " limid=%" PRIuMAX "", (uint64_t) fdb->limid); wp += sprintf(wp, " path=%s", fdb->path ? fdb->path : "-"); wp += sprintf(wp, " fd=%d", fdb->fd); wp += sprintf(wp, " omode=%u", fdb->omode); - wp += sprintf(wp, " rnum=%" PRIuMAX "", (unsigned long long) fdb->rnum); - wp += sprintf(wp, " fsiz=%" PRIuMAX "", (unsigned long long) fdb->fsiz); - wp += sprintf(wp, " min=%" PRIuMAX "", (unsigned long long) fdb->min); - wp += sprintf(wp, " max=%" PRIuMAX "", (unsigned long long) fdb->max); - wp += sprintf(wp, " iter=%" PRIuMAX "", (unsigned long long) fdb->iter); + wp += sprintf(wp, " rnum=%" PRIuMAX "", (uint64_t) fdb->rnum); + wp += sprintf(wp, " fsiz=%" PRIuMAX "", (uint64_t) fdb->fsiz); + wp += sprintf(wp, " min=%" PRIuMAX "", (uint64_t) fdb->min); + wp += sprintf(wp, " max=%" PRIuMAX "", (uint64_t) fdb->max); + wp += sprintf(wp, " iter=%" PRIuMAX "", (uint64_t) fdb->iter); wp += sprintf(wp, " map=%p", (void *) fdb->map); wp += sprintf(wp, " ecode=%d", fdb->ecode); wp += sprintf(wp, " fatal=%u", fdb->fatal); - wp += sprintf(wp, " inode=%" PRIuMAX "", (unsigned long long) fdb->inode); - wp += sprintf(wp, " mtime=%" PRIuMAX "", (unsigned long long) fdb->mtime); + wp += sprintf(wp, " inode=%" PRIuMAX "", (uint64_t) fdb->inode); + wp += sprintf(wp, " mtime=%" PRIuMAX "", (uint64_t) fdb->mtime); wp += sprintf(wp, " tran=%d", fdb->tran); wp += sprintf(wp, " walfd=%d", fdb->walfd); - wp += sprintf(wp, " walend=%" PRIuMAX "", (unsigned long long) fdb->walend); + wp += sprintf(wp, " walend=%" PRIuMAX "", (uint64_t) fdb->walend); wp += sprintf(wp, " dbgfd=%d", fdb->dbgfd); #ifndef NDEBUG - wp += sprintf(wp, " cnt_writerec=%" PRIdMAX "", (long long) fdb->cnt_writerec); - wp += sprintf(wp, " cnt_readrec=%" PRIdMAX "", (long long) fdb->cnt_readrec); - wp += sprintf(wp, " cnt_truncfile=%" PRIdMAX "", (long long) fdb->cnt_truncfile); + wp += sprintf(wp, " cnt_writerec=%" PRIdMAX "", (int64_t) fdb->cnt_writerec); + wp += sprintf(wp, " cnt_readrec=%" PRIdMAX "", (int64_t) fdb->cnt_readrec); + wp += sprintf(wp, " cnt_truncfile=%" PRIdMAX "", (int64_t) fdb->cnt_truncfile); #endif *(wp++) = '\n'; tcwrite(dbgfd, buf, wp - buf); diff --git a/tcejdb/tcfdb.h b/tcejdb/tcfdb.h index a53383a..019c722 100644 --- a/tcejdb/tcfdb.h +++ b/tcejdb/tcfdb.h @@ -51,7 +51,7 @@ typedef struct { /* type of structure for a fixed-length database */ void *eckey; /* key for thread specific error code */ char *path; /* path of the database file */ char *rpath; /* real path for locking */ - volatile char *map; /* pointer to the mapped memory */ + char *map; /* pointer to the mapped memory */ volatile int ecode; /* last happened error code */ int wsiz; /* size of the width region */ int rsiz; /* size of each record */ diff --git a/tcejdb/tcfmgr.c b/tcejdb/tcfmgr.c index f577db0..1591124 100644 --- a/tcejdb/tcfmgr.c +++ b/tcejdb/tcfmgr.c @@ -20,8 +20,8 @@ /* global variables */ -const char *g_progname; // program name -HANDLE g_dbgfd; // debugging output +const char *g_progname; // program name +HANDLE g_dbgfd; // debugging output /* function prototypes */ @@ -42,758 +42,739 @@ static int runversion(int argc, char **argv); static int proccreate(const char *path, int width, int64_t limsiz); static int procinform(const char *path, int omode); static int procput(const char *path, const char *kbuf, int ksiz, const char *vbuf, int vsiz, - int omode, int dmode); + int omode, int dmode); static int procout(const char *path, const char *kbuf, int ksiz, int omode); static int procget(const char *path, const char *kbuf, int ksiz, int omode, bool px, bool pz); static int proclist(const char *path, int omode, int max, bool pv, bool px, - const char *rlstr, const char *rustr, const char *ristr); + const char *rlstr, const char *rustr, const char *ristr); static int procoptimize(const char *path, int width, int64_t limsiz, int omode); static int procimporttsv(const char *path, const char *file, int omode, bool sc); static int procversion(void); - /* main routine */ -int main(int argc, char **argv){ - g_progname = argv[0]; - g_dbgfd = INVALID_HANDLE_VALUE; - const char *ebuf = getenv("TCDBGFD"); - if (ebuf) { - int debugfd = tcatoix(ebuf); +int main(int argc, char **argv) { + g_progname = argv[0]; + g_dbgfd = INVALID_HANDLE_VALUE; + const char *ebuf = getenv("TCDBGFD"); + if (ebuf) { + int debugfd = tcatoix(ebuf); #ifdef _WIN32 - g_dbgfd = (HANDLE) _get_osfhandle(debugfd); + g_dbgfd = (HANDLE) _get_osfhandle(debugfd); #else - g_dbgfd = debugfd; + g_dbgfd = debugfd; #endif - } - if(argc < 2) usage(); - int rv = 0; - if(!strcmp(argv[1], "create")){ - rv = runcreate(argc, argv); - } else if(!strcmp(argv[1], "inform")){ - rv = runinform(argc, argv); - } else if(!strcmp(argv[1], "put")){ - rv = runput(argc, argv); - } else if(!strcmp(argv[1], "out")){ - rv = runout(argc, argv); - } else if(!strcmp(argv[1], "get")){ - rv = runget(argc, argv); - } else if(!strcmp(argv[1], "list")){ - rv = runlist(argc, argv); - } else if(!strcmp(argv[1], "optimize")){ - rv = runoptimize(argc, argv); - } else if(!strcmp(argv[1], "importtsv")){ - rv = runimporttsv(argc, argv); - } else if(!strcmp(argv[1], "version") || !strcmp(argv[1], "--version")){ - rv = runversion(argc, argv); - } else { - usage(); - } - return rv; + } + if (argc < 2) usage(); + int rv = 0; + if (!strcmp(argv[1], "create")) { + rv = runcreate(argc, argv); + } else if (!strcmp(argv[1], "inform")) { + rv = runinform(argc, argv); + } else if (!strcmp(argv[1], "put")) { + rv = runput(argc, argv); + } else if (!strcmp(argv[1], "out")) { + rv = runout(argc, argv); + } else if (!strcmp(argv[1], "get")) { + rv = runget(argc, argv); + } else if (!strcmp(argv[1], "list")) { + rv = runlist(argc, argv); + } else if (!strcmp(argv[1], "optimize")) { + rv = runoptimize(argc, argv); + } else if (!strcmp(argv[1], "importtsv")) { + rv = runimporttsv(argc, argv); + } else if (!strcmp(argv[1], "version") || !strcmp(argv[1], "--version")) { + rv = runversion(argc, argv); + } else { + usage(); + } + return rv; } - /* print the usage and exit */ -static void usage(void){ - fprintf(stderr, "%s: the command line utility of the fixed-length database API\n", g_progname); - fprintf(stderr, "\n"); - fprintf(stderr, "usage:\n"); - fprintf(stderr, " %s create path [width [limsiz]]\n", g_progname); - fprintf(stderr, " %s inform [-nl|-nb] path\n", g_progname); - fprintf(stderr, " %s put [-nl|-nb] [-sx] [-dk|-dc|-dai|-dad] path key value\n", g_progname); - fprintf(stderr, " %s out [-nl|-nb] [-sx] path key\n", g_progname); - fprintf(stderr, " %s get [-nl|-nb] [-sx] [-px] [-pz] path key\n", g_progname); - fprintf(stderr, " %s list [-nl|-nb] [-m num] [-pv] [-px] [-rb lkey ukey] [-ri str] path\n", - g_progname); - fprintf(stderr, " %s optimize [-nl|-nb] path [width [limsiz]]\n", g_progname); - fprintf(stderr, " %s importtsv [-nl|-nb] [-sc] path [file]\n", g_progname); - fprintf(stderr, " %s version\n", g_progname); - fprintf(stderr, "\n"); - exit(1); +static void usage(void) { + fprintf(stderr, "%s: the command line utility of the fixed-length database API\n", g_progname); + fprintf(stderr, "\n"); + fprintf(stderr, "usage:\n"); + fprintf(stderr, " %s create path [width [limsiz]]\n", g_progname); + fprintf(stderr, " %s inform [-nl|-nb] path\n", g_progname); + fprintf(stderr, " %s put [-nl|-nb] [-sx] [-dk|-dc|-dai|-dad] path key value\n", g_progname); + fprintf(stderr, " %s out [-nl|-nb] [-sx] path key\n", g_progname); + fprintf(stderr, " %s get [-nl|-nb] [-sx] [-px] [-pz] path key\n", g_progname); + fprintf(stderr, " %s list [-nl|-nb] [-m num] [-pv] [-px] [-rb lkey ukey] [-ri str] path\n", + g_progname); + fprintf(stderr, " %s optimize [-nl|-nb] path [width [limsiz]]\n", g_progname); + fprintf(stderr, " %s importtsv [-nl|-nb] [-sc] path [file]\n", g_progname); + fprintf(stderr, " %s version\n", g_progname); + fprintf(stderr, "\n"); + exit(1); } - /* print error information */ -static void printerr(TCFDB *fdb){ - const char *path = tcfdbpath(fdb); - int ecode = tcfdbecode(fdb); - fprintf(stderr, "%s: %s: %d: %s\n", g_progname, path ? path : "-", ecode, tcfdberrmsg(ecode)); +static void printerr(TCFDB *fdb) { + const char *path = tcfdbpath(fdb); + int ecode = tcfdbecode(fdb); + fprintf(stderr, "%s: %s: %d: %s\n", g_progname, path ? path : "-", ecode, tcfdberrmsg(ecode)); } - /* print record data */ -static int printdata(const char *ptr, int size, bool px){ - int len = 0; - while(size-- > 0){ - if(px){ - if(len > 0) putchar(' '); - len += printf("%02X", *(unsigned char *)ptr); - } else { - putchar(*ptr); - len++; +static int printdata(const char *ptr, int size, bool px) { + int len = 0; + while (size-- > 0) { + if (px) { + if (len > 0) putchar(' '); + len += printf("%02X", *(unsigned char *) ptr); + } else { + putchar(*ptr); + len++; + } + ptr++; } - ptr++; - } - return len; + return len; } - /* read a line from a file descriptor */ -static char *mygetline(FILE *ifp){ - int len = 0; - int blen = 1024; - char *buf = tcmalloc(blen + 1); - bool end = true; - int c; - while((c = fgetc(ifp)) != EOF){ - end = false; - if(c == '\0') continue; - if(blen <= len){ - blen *= 2; - buf = tcrealloc(buf, blen + 1); - } - if(c == '\n' || c == '\r') c = '\0'; - buf[len++] = c; - if(c == '\0') break; - } - if(end){ - tcfree(buf); - return NULL; - } - buf[len] = '\0'; - return buf; +static char *mygetline(FILE *ifp) { + int len = 0; + int blen = 1024; + char *buf = tcmalloc(blen + 1); + bool end = true; + int c; + while ((c = fgetc(ifp)) != EOF) { + end = false; + if (c == '\0') continue; + if (blen <= len) { + blen *= 2; + buf = tcrealloc(buf, blen + 1); + } + if (c == '\n' || c == '\r') c = '\0'; + buf[len++] = c; + if (c == '\0') break; + } + if (end) { + tcfree(buf); + return NULL; + } + buf[len] = '\0'; + return buf; } - /* parse arguments of create command */ -static int runcreate(int argc, char **argv){ - char *path = NULL; - char *wstr = NULL; - char *lstr = NULL; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - usage(); - } else if(!path){ - path = argv[i]; - } else if(!wstr){ - wstr = argv[i]; - } else if(!lstr){ - lstr = argv[i]; - } else { - usage(); - } - } - if(!path) usage(); - int width = wstr ? tcatoix(wstr) : -1; - int64_t limsiz = lstr ? tcatoix(lstr) : -1; - int rv = proccreate(path, width, limsiz); - return rv; +static int runcreate(int argc, char **argv) { + char *path = NULL; + char *wstr = NULL; + char *lstr = NULL; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + usage(); + } else if (!path) { + path = argv[i]; + } else if (!wstr) { + wstr = argv[i]; + } else if (!lstr) { + lstr = argv[i]; + } else { + usage(); + } + } + if (!path) usage(); + int width = wstr ? tcatoix(wstr) : -1; + int64_t limsiz = lstr ? tcatoix(lstr) : -1; + int rv = proccreate(path, width, limsiz); + return rv; } - /* parse arguments of inform command */ -static int runinform(int argc, char **argv){ - char *path = NULL; - int omode = 0; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-nl")){ - omode |= FDBONOLCK; - } else if(!strcmp(argv[i], "-nb")){ - omode |= FDBOLCKNB; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; - } else { - usage(); +static int runinform(int argc, char **argv) { + char *path = NULL; + int omode = 0; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-nl")) { + omode |= FDBONOLCK; + } else if (!strcmp(argv[i], "-nb")) { + omode |= FDBOLCKNB; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } } - } - if(!path) usage(); - int rv = procinform(path, omode); - return rv; + if (!path) usage(); + int rv = procinform(path, omode); + return rv; } - /* parse arguments of put command */ -static int runput(int argc, char **argv){ - char *path = NULL; - char *key = NULL; - char *value = NULL; - int omode = 0; - int dmode = 0; - bool sx = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-nl")){ - omode |= FDBONOLCK; - } else if(!strcmp(argv[i], "-nb")){ - omode |= FDBOLCKNB; - } else if(!strcmp(argv[i], "-dk")){ - dmode = -1; - } else if(!strcmp(argv[i], "-dc")){ - dmode = 1; - } else if(!strcmp(argv[i], "-dai")){ - dmode = 10; - } else if(!strcmp(argv[i], "-dad")){ - dmode = 11; - } else if(!strcmp(argv[i], "-sx")){ - sx = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; - } else if(!key){ - key = argv[i]; - } else if(!value){ - value = argv[i]; +static int runput(int argc, char **argv) { + char *path = NULL; + char *key = NULL; + char *value = NULL; + int omode = 0; + int dmode = 0; + bool sx = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-nl")) { + omode |= FDBONOLCK; + } else if (!strcmp(argv[i], "-nb")) { + omode |= FDBOLCKNB; + } else if (!strcmp(argv[i], "-dk")) { + dmode = -1; + } else if (!strcmp(argv[i], "-dc")) { + dmode = 1; + } else if (!strcmp(argv[i], "-dai")) { + dmode = 10; + } else if (!strcmp(argv[i], "-dad")) { + dmode = 11; + } else if (!strcmp(argv[i], "-sx")) { + sx = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else if (!key) { + key = argv[i]; + } else if (!value) { + value = argv[i]; + } else { + usage(); + } + } + if (!path || !key || !value) usage(); + char *kbuf, *vbuf; + int ksiz, vsiz; + if (sx) { + kbuf = tchexdecode(key, &ksiz); + vbuf = tchexdecode(value, &vsiz); } else { - usage(); - } - } - if(!path || !key || !value) usage(); - char *kbuf, *vbuf; - int ksiz, vsiz; - if(sx){ - kbuf = tchexdecode(key, &ksiz); - vbuf = tchexdecode(value, &vsiz); - } else { - ksiz = strlen(key); - kbuf = tcmemdup(key, ksiz); - vsiz = strlen(value); - vbuf = tcmemdup(value, vsiz); - } - int rv = procput(path, kbuf, ksiz, vbuf, vsiz, omode, dmode); - tcfree(vbuf); - tcfree(kbuf); - return rv; + ksiz = strlen(key); + kbuf = tcmemdup(key, ksiz); + vsiz = strlen(value); + vbuf = tcmemdup(value, vsiz); + } + int rv = procput(path, kbuf, ksiz, vbuf, vsiz, omode, dmode); + tcfree(vbuf); + tcfree(kbuf); + return rv; } - /* parse arguments of out command */ -static int runout(int argc, char **argv){ - char *path = NULL; - char *key = NULL; - int omode = 0; - bool sx = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-nl")){ - omode |= FDBONOLCK; - } else if(!strcmp(argv[i], "-nb")){ - omode |= FDBOLCKNB; - } else if(!strcmp(argv[i], "-sx")){ - sx = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; - } else if(!key){ - key = argv[i]; +static int runout(int argc, char **argv) { + char *path = NULL; + char *key = NULL; + int omode = 0; + bool sx = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-nl")) { + omode |= FDBONOLCK; + } else if (!strcmp(argv[i], "-nb")) { + omode |= FDBOLCKNB; + } else if (!strcmp(argv[i], "-sx")) { + sx = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else if (!key) { + key = argv[i]; + } else { + usage(); + } + } + if (!path || !key) usage(); + int ksiz; + char *kbuf; + if (sx) { + kbuf = tchexdecode(key, &ksiz); } else { - usage(); - } - } - if(!path || !key) usage(); - int ksiz; - char *kbuf; - if(sx){ - kbuf = tchexdecode(key, &ksiz); - } else { - ksiz = strlen(key); - kbuf = tcmemdup(key, ksiz); - } - int rv = procout(path, kbuf, ksiz, omode); - tcfree(kbuf); - return rv; + ksiz = strlen(key); + kbuf = tcmemdup(key, ksiz); + } + int rv = procout(path, kbuf, ksiz, omode); + tcfree(kbuf); + return rv; } - /* parse arguments of get command */ -static int runget(int argc, char **argv){ - char *path = NULL; - char *key = NULL; - int omode = 0; - bool sx = false; - bool px = false; - bool pz = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-nl")){ - omode |= FDBONOLCK; - } else if(!strcmp(argv[i], "-nb")){ - omode |= FDBOLCKNB; - } else if(!strcmp(argv[i], "-sx")){ - sx = true; - } else if(!strcmp(argv[i], "-px")){ - px = true; - } else if(!strcmp(argv[i], "-pz")){ - pz = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; - } else if(!key){ - key = argv[i]; +static int runget(int argc, char **argv) { + char *path = NULL; + char *key = NULL; + int omode = 0; + bool sx = false; + bool px = false; + bool pz = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-nl")) { + omode |= FDBONOLCK; + } else if (!strcmp(argv[i], "-nb")) { + omode |= FDBOLCKNB; + } else if (!strcmp(argv[i], "-sx")) { + sx = true; + } else if (!strcmp(argv[i], "-px")) { + px = true; + } else if (!strcmp(argv[i], "-pz")) { + pz = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else if (!key) { + key = argv[i]; + } else { + usage(); + } + } + if (!path || !key) usage(); + int ksiz; + char *kbuf; + if (sx) { + kbuf = tchexdecode(key, &ksiz); } else { - usage(); - } - } - if(!path || !key) usage(); - int ksiz; - char *kbuf; - if(sx){ - kbuf = tchexdecode(key, &ksiz); - } else { - ksiz = strlen(key); - kbuf = tcmemdup(key, ksiz); - } - int rv = procget(path, kbuf, ksiz, omode, px, pz); - tcfree(kbuf); - return rv; + ksiz = strlen(key); + kbuf = tcmemdup(key, ksiz); + } + int rv = procget(path, kbuf, ksiz, omode, px, pz); + tcfree(kbuf); + return rv; } - /* parse arguments of list command */ -static int runlist(int argc, char **argv){ - char *path = NULL; - int omode = 0; - int max = -1; - bool pv = false; - bool px = false; - char *rlstr = NULL; - char *rustr = NULL; - char *ristr = NULL; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-nl")){ - omode |= FDBONOLCK; - } else if(!strcmp(argv[i], "-nb")){ - omode |= FDBOLCKNB; - } else if(!strcmp(argv[i], "-m")){ - if(++i >= argc) usage(); - max = tcatoix(argv[i]); - } else if(!strcmp(argv[i], "-pv")){ - pv = true; - } else if(!strcmp(argv[i], "-px")){ - px = true; - } else if(!strcmp(argv[i], "-rb")){ - if(++i >= argc) usage(); - rlstr = argv[i]; - if(++i >= argc) usage(); - rustr = argv[i]; - } else if(!strcmp(argv[i], "-ri")){ - if(++i >= argc) usage(); - ristr = argv[i]; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; - } else { - usage(); +static int runlist(int argc, char **argv) { + char *path = NULL; + int omode = 0; + int max = -1; + bool pv = false; + bool px = false; + char *rlstr = NULL; + char *rustr = NULL; + char *ristr = NULL; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-nl")) { + omode |= FDBONOLCK; + } else if (!strcmp(argv[i], "-nb")) { + omode |= FDBOLCKNB; + } else if (!strcmp(argv[i], "-m")) { + if (++i >= argc) usage(); + max = tcatoix(argv[i]); + } else if (!strcmp(argv[i], "-pv")) { + pv = true; + } else if (!strcmp(argv[i], "-px")) { + px = true; + } else if (!strcmp(argv[i], "-rb")) { + if (++i >= argc) usage(); + rlstr = argv[i]; + if (++i >= argc) usage(); + rustr = argv[i]; + } else if (!strcmp(argv[i], "-ri")) { + if (++i >= argc) usage(); + ristr = argv[i]; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } } - } - if(!path) usage(); - int rv = proclist(path, omode, max, pv, px, rlstr, rustr, ristr); - return rv; + if (!path) usage(); + int rv = proclist(path, omode, max, pv, px, rlstr, rustr, ristr); + return rv; } - /* parse arguments of optimize command */ -static int runoptimize(int argc, char **argv){ - char *path = NULL; - char *wstr = NULL; - char *lstr = NULL; - int omode = 0; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-nl")){ - omode |= FDBONOLCK; - } else if(!strcmp(argv[i], "-nb")){ - omode |= FDBOLCKNB; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; - } else if(!wstr){ - wstr = argv[i]; - } else if(!lstr){ - lstr = argv[i]; - } else { - usage(); - } - } - if(!path) usage(); - int width = wstr ? tcatoix(wstr) : -1; - int64_t limsiz = lstr ? tcatoix(lstr) : -1; - int rv = procoptimize(path, width, limsiz, omode); - return rv; +static int runoptimize(int argc, char **argv) { + char *path = NULL; + char *wstr = NULL; + char *lstr = NULL; + int omode = 0; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-nl")) { + omode |= FDBONOLCK; + } else if (!strcmp(argv[i], "-nb")) { + omode |= FDBOLCKNB; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else if (!wstr) { + wstr = argv[i]; + } else if (!lstr) { + lstr = argv[i]; + } else { + usage(); + } + } + if (!path) usage(); + int width = wstr ? tcatoix(wstr) : -1; + int64_t limsiz = lstr ? tcatoix(lstr) : -1; + int rv = procoptimize(path, width, limsiz, omode); + return rv; } - /* parse arguments of importtsv command */ -static int runimporttsv(int argc, char **argv){ - char *path = NULL; - char *file = NULL; - int omode = 0; - bool sc = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-nl")){ - omode |= FDBONOLCK; - } else if(!strcmp(argv[i], "-nb")){ - omode |= FDBOLCKNB; - } else if(!strcmp(argv[i], "-sc")){ - sc = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; - } else if(!file){ - file = argv[i]; - } else { - usage(); +static int runimporttsv(int argc, char **argv) { + char *path = NULL; + char *file = NULL; + int omode = 0; + bool sc = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-nl")) { + omode |= FDBONOLCK; + } else if (!strcmp(argv[i], "-nb")) { + omode |= FDBOLCKNB; + } else if (!strcmp(argv[i], "-sc")) { + sc = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else if (!file) { + file = argv[i]; + } else { + usage(); + } } - } - if(!path) usage(); - int rv = procimporttsv(path, file, omode, sc); - return rv; + if (!path) usage(); + int rv = procimporttsv(path, file, omode, sc); + return rv; } - /* parse arguments of version command */ -static int runversion(int argc, char **argv){ - int rv = procversion(); - return rv; +static int runversion(int argc, char **argv) { + int rv = procversion(); + return rv; } - /* perform create command */ -static int proccreate(const char *path, int width, int64_t limsiz){ - TCFDB *fdb = tcfdbnew(); - if(!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); - if(!tcfdbtune(fdb, width, limsiz)){ - printerr(fdb); - tcfdbdel(fdb); - return 1; - } - if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC)){ - printerr(fdb); +static int proccreate(const char *path, int width, int64_t limsiz) { + TCFDB *fdb = tcfdbnew(); + if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); + if (!tcfdbtune(fdb, width, limsiz)) { + printerr(fdb); + tcfdbdel(fdb); + return 1; + } + if (!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC)) { + printerr(fdb); + tcfdbdel(fdb); + return 1; + } + bool err = false; + if (!tcfdbclose(fdb)) { + printerr(fdb); + err = true; + } tcfdbdel(fdb); - return 1; - } - bool err = false; - if(!tcfdbclose(fdb)){ - printerr(fdb); - err = true; - } - tcfdbdel(fdb); - return err ? 1 : 0; + return err ? 1 : 0; } - /* perform inform command */ -static int procinform(const char *path, int omode){ - TCFDB *fdb = tcfdbnew(); - if(!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); - if(!tcfdbopen(fdb, path, FDBOREADER | omode)){ - printerr(fdb); +static int procinform(const char *path, int omode) { + TCFDB *fdb = tcfdbnew(); + if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); + if (!tcfdbopen(fdb, path, FDBOREADER | omode)) { + printerr(fdb); + tcfdbdel(fdb); + return 1; + } + bool err = false; + const char *npath = tcfdbpath(fdb); + if (!npath) npath = "(unknown)"; + printf("path: %s\n", npath); + const char *type = "(unknown)"; + switch (tcfdbtype(fdb)) { + case TCDBTHASH: type = "hash"; + break; + case TCDBTBTREE: type = "btree"; + break; + case TCDBTFIXED: type = "fixed"; + break; + case TCDBTTABLE: type = "table"; + break; + } + printf("database type: %s\n", type); + uint8_t flags = tcfdbflags(fdb); + printf("additional flags:"); + if (flags & FDBFOPEN) printf(" open"); + if (flags & FDBFFATAL) printf(" fatal"); + printf("\n"); + printf("minimum ID number: %" PRIuMAX "\n", (uint64_t) tcfdbmin(fdb)); + printf("maximum ID number: %" PRIuMAX "\n", (uint64_t) tcfdbmax(fdb)); + printf("width of the value: %u\n", (unsigned int) tcfdbwidth(fdb)); + printf("limit file size: %" PRIuMAX "\n", (uint64_t) tcfdblimsiz(fdb)); + printf("limit ID number: %" PRIuMAX "\n", (uint64_t) tcfdblimid(fdb)); + printf("inode number: %" PRIdMAX "\n", (int64_t) tcfdbinode(fdb)); + char date[48]; + tcdatestrwww(tcfdbmtime(fdb), INT_MAX, date); + printf("modified time: %s\n", date); + printf("record number: %" PRIuMAX "\n", (uint64_t) tcfdbrnum(fdb)); + printf("file size: %" PRIuMAX "\n", (uint64_t) tcfdbfsiz(fdb)); + if (!tcfdbclose(fdb)) { + if (!err) printerr(fdb); + err = true; + } tcfdbdel(fdb); - return 1; - } - bool err = false; - const char *npath = tcfdbpath(fdb); - if(!npath) npath = "(unknown)"; - printf("path: %s\n", npath); - const char *type = "(unknown)"; - switch(tcfdbtype(fdb)){ - case TCDBTHASH: type = "hash"; break; - case TCDBTBTREE: type = "btree"; break; - case TCDBTFIXED: type = "fixed"; break; - case TCDBTTABLE: type = "table"; break; - } - printf("database type: %s\n", type); - uint8_t flags = tcfdbflags(fdb); - printf("additional flags:"); - if(flags & FDBFOPEN) printf(" open"); - if(flags & FDBFFATAL) printf(" fatal"); - printf("\n"); - printf("minimum ID number: %" PRIuMAX "\n", (unsigned long long)tcfdbmin(fdb)); - printf("maximum ID number: %" PRIuMAX "\n", (unsigned long long)tcfdbmax(fdb)); - printf("width of the value: %u\n", (unsigned int)tcfdbwidth(fdb)); - printf("limit file size: %" PRIuMAX "\n", (unsigned long long)tcfdblimsiz(fdb)); - printf("limit ID number: %" PRIuMAX "\n", (unsigned long long)tcfdblimid(fdb)); - printf("inode number: %" PRIdMAX "\n", (long long)tcfdbinode(fdb)); - char date[48]; - tcdatestrwww(tcfdbmtime(fdb), INT_MAX, date); - printf("modified time: %s\n", date); - printf("record number: %" PRIuMAX "\n", (unsigned long long)tcfdbrnum(fdb)); - printf("file size: %" PRIuMAX "\n", (unsigned long long)tcfdbfsiz(fdb)); - if(!tcfdbclose(fdb)){ - if(!err) printerr(fdb); - err = true; - } - tcfdbdel(fdb); - return err ? 1 : 0; + return err ? 1 : 0; } - /* perform put command */ static int procput(const char *path, const char *kbuf, int ksiz, const char *vbuf, int vsiz, - int omode, int dmode){ - TCFDB *fdb = tcfdbnew(); - if(!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); - if(!tcfdbopen(fdb, path, FDBOWRITER | omode)){ - printerr(fdb); - tcfdbdel(fdb); - return 1; - } - bool err = false; - int inum; - double dnum; - switch(dmode){ - case -1: - if(!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz)){ - printerr(fdb); - err = true; - } - break; - case 1: - if(!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz)){ + int omode, int dmode) { + TCFDB *fdb = tcfdbnew(); + if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); + if (!tcfdbopen(fdb, path, FDBOWRITER | omode)) { printerr(fdb); + tcfdbdel(fdb); + return 1; + } + bool err = false; + int inum; + double dnum; + switch (dmode) { + case -1: + if (!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz)) { + printerr(fdb); + err = true; + } + break; + case 1: + if (!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz)) { + printerr(fdb); + err = true; + } + break; + case 10: + inum = tcfdbaddint(fdb, tcfdbkeytoid(kbuf, ksiz), tcatoi(vbuf)); + if (inum == INT_MIN) { + printerr(fdb); + err = true; + } else { + printf("%d\n", inum); + } + break; + case 11: + dnum = tcfdbadddouble(fdb, tcfdbkeytoid(kbuf, ksiz), tcatof(vbuf)); + if (isnan(dnum)) { + printerr(fdb); + err = true; + } else { + printf("%.6f\n", dnum); + } + break; + default: + if (!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)) { + printerr(fdb); + err = true; + } + break; + } + if (!tcfdbclose(fdb)) { + if (!err) printerr(fdb); err = true; - } - break; - case 10: - inum = tcfdbaddint(fdb, tcfdbkeytoid(kbuf, ksiz), tcatoi(vbuf)); - if(inum == INT_MIN){ + } + tcfdbdel(fdb); + return err ? 1 : 0; +} + +/* perform out command */ +static int procout(const char *path, const char *kbuf, int ksiz, int omode) { + TCFDB *fdb = tcfdbnew(); + if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); + if (!tcfdbopen(fdb, path, FDBOWRITER | omode)) { printerr(fdb); - err = true; - } else { - printf("%d\n", inum); - } - break; - case 11: - dnum = tcfdbadddouble(fdb, tcfdbkeytoid(kbuf, ksiz), tcatof(vbuf)); - if(isnan(dnum)){ + tcfdbdel(fdb); + return 1; + } + bool err = false; + if (!tcfdbout2(fdb, kbuf, ksiz)) { printerr(fdb); err = true; - } else { - printf("%.6f\n", dnum); - } - break; - default: - if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){ - printerr(fdb); + } + if (!tcfdbclose(fdb)) { + if (!err) printerr(fdb); err = true; - } - break; - } - if(!tcfdbclose(fdb)){ - if(!err) printerr(fdb); - err = true; - } - tcfdbdel(fdb); - return err ? 1 : 0; -} - - -/* perform out command */ -static int procout(const char *path, const char *kbuf, int ksiz, int omode){ - TCFDB *fdb = tcfdbnew(); - if(!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); - if(!tcfdbopen(fdb, path, FDBOWRITER | omode)){ - printerr(fdb); + } tcfdbdel(fdb); - return 1; - } - bool err = false; - if(!tcfdbout2(fdb, kbuf, ksiz)){ - printerr(fdb); - err = true; - } - if(!tcfdbclose(fdb)){ - if(!err) printerr(fdb); - err = true; - } - tcfdbdel(fdb); - return err ? 1 : 0; + return err ? 1 : 0; } - /* perform get command */ -static int procget(const char *path, const char *kbuf, int ksiz, int omode, bool px, bool pz){ - TCFDB *fdb = tcfdbnew(); - if(!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); - if(!tcfdbopen(fdb, path, FDBOREADER | omode)){ - printerr(fdb); +static int procget(const char *path, const char *kbuf, int ksiz, int omode, bool px, bool pz) { + TCFDB *fdb = tcfdbnew(); + if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); + if (!tcfdbopen(fdb, path, FDBOREADER | omode)) { + printerr(fdb); + tcfdbdel(fdb); + return 1; + } + bool err = false; + int vsiz; + char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz); + if (vbuf) { + printdata(vbuf, vsiz, px); + if (!pz) putchar('\n'); + tcfree(vbuf); + } else { + printerr(fdb); + err = true; + } + if (!tcfdbclose(fdb)) { + if (!err) printerr(fdb); + err = true; + } tcfdbdel(fdb); - return 1; - } - bool err = false; - int vsiz; - char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz); - if(vbuf){ - printdata(vbuf, vsiz, px); - if(!pz) putchar('\n'); - tcfree(vbuf); - } else { - printerr(fdb); - err = true; - } - if(!tcfdbclose(fdb)){ - if(!err) printerr(fdb); - err = true; - } - tcfdbdel(fdb); - return err ? 1 : 0; + return err ? 1 : 0; } - /* perform list command */ static int proclist(const char *path, int omode, int max, bool pv, bool px, - const char *rlstr, const char *rustr, const char *ristr){ - TCFDB *fdb = tcfdbnew(); - if(!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); - if(!tcfdbopen(fdb, path, FDBOREADER | omode)){ - printerr(fdb); - tcfdbdel(fdb); - return 1; - } - bool err = false; - if(rlstr || ristr){ - TCLIST *keys = ristr ? tcfdbrange5(fdb, ristr, max) : tcfdbrange3(fdb, rlstr, rustr, max); - for(int i = 0; i < tclistnum(keys); i++){ - int ksiz; - const char *kbuf = tclistval(keys, i, &ksiz); - printf("%s", kbuf); - if(pv){ - int vsiz; - char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz); - if(vbuf){ - putchar('\t'); - printdata(vbuf, vsiz, px); - tcfree(vbuf); + const char *rlstr, const char *rustr, const char *ristr) { + TCFDB *fdb = tcfdbnew(); + if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); + if (!tcfdbopen(fdb, path, FDBOREADER | omode)) { + printerr(fdb); + tcfdbdel(fdb); + return 1; + } + bool err = false; + if (rlstr || ristr) { + TCLIST *keys = ristr ? tcfdbrange5(fdb, ristr, max) : tcfdbrange3(fdb, rlstr, rustr, max); + for (int i = 0; i < tclistnum(keys); i++) { + int ksiz; + const char *kbuf = tclistval(keys, i, &ksiz); + printf("%s", kbuf); + if (pv) { + int vsiz; + char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz); + if (vbuf) { + putchar('\t'); + printdata(vbuf, vsiz, px); + tcfree(vbuf); + } + } + putchar('\n'); + } + tclistdel(keys); + } else { + if (!tcfdbiterinit(fdb)) { + printerr(fdb); + err = true; + } + int cnt = 0; + uint64_t id; + while ((id = tcfdbiternext(fdb)) > 0) { + printf("%" PRIuMAX "", (uint64_t) id); + if (pv) { + int vsiz; + char *vbuf = tcfdbget(fdb, id, &vsiz); + if (vbuf) { + putchar('\t'); + printdata(vbuf, vsiz, px); + tcfree(vbuf); + } + } + putchar('\n'); + if (max >= 0 && ++cnt >= max) break; } - } - putchar('\n'); } - tclistdel(keys); - } else { - if(!tcfdbiterinit(fdb)){ - printerr(fdb); - err = true; + if (!tcfdbclose(fdb)) { + if (!err) printerr(fdb); + err = true; } - int cnt = 0; - uint64_t id; - while((id = tcfdbiternext(fdb)) > 0){ - printf("%" PRIuMAX "", (unsigned long long)id); - if(pv){ - int vsiz; - char *vbuf = tcfdbget(fdb, id, &vsiz); - if(vbuf){ - putchar('\t'); - printdata(vbuf, vsiz, px); - tcfree(vbuf); - } - } - putchar('\n'); - if(max >= 0 && ++cnt >= max) break; - } - } - if(!tcfdbclose(fdb)){ - if(!err) printerr(fdb); - err = true; - } - tcfdbdel(fdb); - return err ? 1 : 0; + tcfdbdel(fdb); + return err ? 1 : 0; } - /* perform optimize command */ -static int procoptimize(const char *path, int width, int64_t limsiz, int omode){ - TCFDB *fdb = tcfdbnew(); - if(!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); - if(!tcfdbopen(fdb, path, FDBOWRITER | omode)){ - printerr(fdb); +static int procoptimize(const char *path, int width, int64_t limsiz, int omode) { + TCFDB *fdb = tcfdbnew(); + if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); + if (!tcfdbopen(fdb, path, FDBOWRITER | omode)) { + printerr(fdb); + tcfdbdel(fdb); + return 1; + } + bool err = false; + if (!tcfdboptimize(fdb, width, limsiz)) { + printerr(fdb); + err = true; + } + if (!tcfdbclose(fdb)) { + if (!err) printerr(fdb); + err = true; + } tcfdbdel(fdb); - return 1; - } - bool err = false; - if(!tcfdboptimize(fdb, width, limsiz)){ - printerr(fdb); - err = true; - } - if(!tcfdbclose(fdb)){ - if(!err) printerr(fdb); - err = true; - } - tcfdbdel(fdb); - return err ? 1 : 0; + return err ? 1 : 0; } - /* perform importtsv command */ -static int procimporttsv(const char *path, const char *file, int omode, bool sc){ - FILE *ifp = file ? fopen(file, "rb") : stdin; - if(!ifp){ - fprintf(stderr, "%s: could not open\n", file ? file : "(stdin)"); - return 1; - } - TCFDB *fdb = tcfdbnew(); - if(!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); - if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | omode)){ - printerr(fdb); +static int procimporttsv(const char *path, const char *file, int omode, bool sc) { + FILE *ifp = file ? fopen(file, "rb") : stdin; + if (!ifp) { + fprintf(stderr, "%s: could not open\n", file ? file : "(stdin)"); + return 1; + } + TCFDB *fdb = tcfdbnew(); + if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); + if (!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | omode)) { + printerr(fdb); + tcfdbdel(fdb); + if (ifp != stdin) fclose(ifp); + return 1; + } + bool err = false; + char *line; + int cnt = 0; + while (!err && (line = mygetline(ifp)) != NULL) { + char *pv = strchr(line, '\t'); + if (!pv) { + tcfree(line); + continue; + } + *pv = '\0'; + if (sc) tcstrutfnorm(line, TCUNSPACE | TCUNLOWER | TCUNNOACC | TCUNWIDTH); + if (!tcfdbput3(fdb, line, pv + 1)) { + printerr(fdb); + err = true; + } + tcfree(line); + if (cnt > 0 && cnt % 100 == 0) { + putchar('.'); + fflush(stdout); + if (cnt % 5000 == 0) printf(" (%08d)\n", cnt); + } + cnt++; + } + printf(" (%08d)\n", cnt); + if (!tcfdbclose(fdb)) { + if (!err) printerr(fdb); + err = true; + } tcfdbdel(fdb); - if(ifp != stdin) fclose(ifp); - return 1; - } - bool err = false; - char *line; - int cnt = 0; - while(!err && (line = mygetline(ifp)) != NULL){ - char *pv = strchr(line, '\t'); - if(!pv){ - tcfree(line); - continue; - } - *pv = '\0'; - if(sc) tcstrutfnorm(line, TCUNSPACE | TCUNLOWER | TCUNNOACC | TCUNWIDTH); - if(!tcfdbput3(fdb, line, pv + 1)){ - printerr(fdb); - err = true; - } - tcfree(line); - if(cnt > 0 && cnt % 100 == 0){ - putchar('.'); - fflush(stdout); - if(cnt % 5000 == 0) printf(" (%08d)\n", cnt); - } - cnt++; - } - printf(" (%08d)\n", cnt); - if(!tcfdbclose(fdb)){ - if(!err) printerr(fdb); - err = true; - } - tcfdbdel(fdb); - if(ifp != stdin) fclose(ifp); - return err ? 1 : 0; + if (ifp != stdin) fclose(ifp); + return err ? 1 : 0; } - /* perform version command */ -static int procversion(void){ - printf("Tokyo Cabinet version %s (%d:%s) for %s\n", - tcversion, _TC_LIBVER, _TC_FORMATVER, TCSYSNAME); - printf("Copyright (C) 2006-2012 FAL Labs\n"); - return 0; +static int procversion(void) { + printf("Tokyo Cabinet version %s (%d:%s) for %s\n", + tcversion, _TC_LIBVER, _TC_FORMATVER, TCSYSNAME); + printf("Copyright (C) 2006-2012 FAL Labs\n"); + return 0; } diff --git a/tcejdb/tcfmttest.c b/tcejdb/tcfmttest.c index 9ea2509..efdfa63 100644 --- a/tcejdb/tcfmttest.c +++ b/tcejdb/tcfmttest.c @@ -21,49 +21,49 @@ #define RECBUFSIZ 48 // buffer for records #define EXHEADSIZ 256 // expected header size -typedef struct { // type of structure for write thread - TCFDB *fdb; - int rnum; - bool rnd; - int id; +typedef struct { // type of structure for write thread + TCFDB *fdb; + int rnum; + bool rnd; + int id; } TARGWRITE; -typedef struct { // type of structure for read thread - TCFDB *fdb; - int rnum; - bool wb; - bool rnd; - int id; +typedef struct { // type of structure for read thread + TCFDB *fdb; + int rnum; + bool wb; + bool rnd; + int id; } TARGREAD; -typedef struct { // type of structure for remove thread - TCFDB *fdb; - int rnum; - bool rnd; - int id; +typedef struct { // type of structure for remove thread + TCFDB *fdb; + int rnum; + bool rnd; + int id; } TARGREMOVE; -typedef struct { // type of structure for wicked thread - TCFDB *fdb; - int rnum; - bool nc; - int id; - TCMAP *map; +typedef struct { // type of structure for wicked thread + TCFDB *fdb; + int rnum; + bool nc; + int id; + TCMAP *map; } TARGWICKED; -typedef struct { // type of structure for typical thread - TCFDB *fdb; - int rnum; - bool nc; - int rratio; - int id; +typedef struct { // type of structure for typical thread + TCFDB *fdb; + int rnum; + bool nc; + int rratio; + int id; } TARGTYPICAL; /* global variables */ -const char *g_progname; // program name -unsigned int g_randseed; // random seed -HANDLE g_dbgfd; // debugging output +const char *g_progname; // program name +unsigned int g_randseed; // random seed +HANDLE g_dbgfd; // debugging output /* function prototypes */ @@ -83,1144 +83,1119 @@ static int runremove(int argc, char **argv); static int runwicked(int argc, char **argv); static int runtypical(int argc, char **argv); static int procwrite(const char *path, int tnum, int rnum, int width, int64_t limsiz, - int omode, bool rnd); + int omode, bool rnd); static int procread(const char *path, int tnum, int omode, bool wb, bool rnd); static int procremove(const char *path, int tnum, int omode, bool rnd); static int procwicked(const char *path, int tnum, int rnum, int omode, bool nc); static int proctypical(const char *path, int tnum, int rnum, int width, int64_t limsiz, - int omode, bool nc, int rratio); + int omode, bool nc, int rratio); static void *threadwrite(void *targ); static void *threadread(void *targ); static void *threadremove(void *targ); static void *threadwicked(void *targ); static void *threadtypical(void *targ); - /* main routine */ -int main(int argc, char **argv){ - g_progname = argv[0]; - const char *ebuf = getenv("TCRNDSEED"); - g_randseed = ebuf ? tcatoix(ebuf) : tctime() * 1000; - srand(g_randseed); - ebuf = getenv("TCDBGFD"); - if (ebuf) { - int debugfd = tcatoix(ebuf); +int main(int argc, char **argv) { + g_progname = argv[0]; + const char *ebuf = getenv("TCRNDSEED"); + g_randseed = ebuf ? tcatoix(ebuf) : tctime() * 1000; + srand(g_randseed); + ebuf = getenv("TCDBGFD"); + if (ebuf) { + int debugfd = tcatoix(ebuf); #ifdef _WIN32 - g_dbgfd = (HANDLE) _get_osfhandle(debugfd); + g_dbgfd = (HANDLE) _get_osfhandle(debugfd); #else - g_dbgfd = debugfd; + g_dbgfd = debugfd; #endif - } - if(argc < 2) usage(); - int rv = 0; - if(!strcmp(argv[1], "write")){ - rv = runwrite(argc, argv); - } else if(!strcmp(argv[1], "read")){ - rv = runread(argc, argv); - } else if(!strcmp(argv[1], "remove")){ - rv = runremove(argc, argv); - } else if(!strcmp(argv[1], "wicked")){ - rv = runwicked(argc, argv); - } else if(!strcmp(argv[1], "typical")){ - rv = runtypical(argc, argv); - } else { - usage(); - } - if(rv != 0){ - printf("FAILED: TCRNDSEED=%u PID=%d", g_randseed, (int)getpid()); - for(int i = 0; i < argc; i++){ - printf(" %s", argv[i]); } - printf("\n\n"); - } - return rv; + if (argc < 2) usage(); + int rv = 0; + if (!strcmp(argv[1], "write")) { + rv = runwrite(argc, argv); + } else if (!strcmp(argv[1], "read")) { + rv = runread(argc, argv); + } else if (!strcmp(argv[1], "remove")) { + rv = runremove(argc, argv); + } else if (!strcmp(argv[1], "wicked")) { + rv = runwicked(argc, argv); + } else if (!strcmp(argv[1], "typical")) { + rv = runtypical(argc, argv); + } else { + usage(); + } + if (rv != 0) { + printf("FAILED: TCRNDSEED=%u PID=%d", g_randseed, (int) getpid()); + for (int i = 0; i < argc; i++) { + printf(" %s", argv[i]); + } + printf("\n\n"); + } + return rv; } - /* print the usage and exit */ -static void usage(void){ - fprintf(stderr, "%s: test cases of the fixed-length database API of Tokyo Cabinet\n", - g_progname); - fprintf(stderr, "\n"); - fprintf(stderr, "usage:\n"); - fprintf(stderr, " %s write [-nl|-nb] [-rnd] path tnum rnum [width [limsiz]]\n", g_progname); - fprintf(stderr, " %s read [-nl|-nb] [-wb] [-rnd] path tnum\n", g_progname); - fprintf(stderr, " %s remove [-nl|-nb] [-rnd] path tnum\n", g_progname); - fprintf(stderr, " %s wicked [-nl|-nb] [-nc] path tnum rnum\n", g_progname); - fprintf(stderr, " %s typical [-nl|-nb] [-nc] [-rr num] path tnum rnum [width [limsiz]]\n", - g_progname); - fprintf(stderr, "\n"); - exit(1); +static void usage(void) { + fprintf(stderr, "%s: test cases of the fixed-length database API of Tokyo Cabinet\n", + g_progname); + fprintf(stderr, "\n"); + fprintf(stderr, "usage:\n"); + fprintf(stderr, " %s write [-nl|-nb] [-rnd] path tnum rnum [width [limsiz]]\n", g_progname); + fprintf(stderr, " %s read [-nl|-nb] [-wb] [-rnd] path tnum\n", g_progname); + fprintf(stderr, " %s remove [-nl|-nb] [-rnd] path tnum\n", g_progname); + fprintf(stderr, " %s wicked [-nl|-nb] [-nc] path tnum rnum\n", g_progname); + fprintf(stderr, " %s typical [-nl|-nb] [-nc] [-rr num] path tnum rnum [width [limsiz]]\n", + g_progname); + fprintf(stderr, "\n"); + exit(1); } - /* print formatted information string and flush the buffer */ -static void iprintf(const char *format, ...){ - va_list ap; - va_start(ap, format); - vprintf(format, ap); - fflush(stdout); - va_end(ap); +static void iprintf(const char *format, ...) { + va_list ap; + va_start(ap, format); + vprintf(format, ap); + fflush(stdout); + va_end(ap); } - /* print a character and flush the buffer */ -static void iputchar(int c){ - putchar(c); - fflush(stdout); +static void iputchar(int c) { + putchar(c); + fflush(stdout); } - /* print error message of fixed-length database */ -static void eprint(TCFDB *fdb, int line, const char *func){ - const char *path = tcfdbpath(fdb); - int ecode = tcfdbecode(fdb); - fprintf(stderr, "%s: %s: %d: %s: error: %d: %s\n", - g_progname, path ? path : "-", line, func, ecode, tcfdberrmsg(ecode)); +static void eprint(TCFDB *fdb, int line, const char *func) { + const char *path = tcfdbpath(fdb); + int ecode = tcfdbecode(fdb); + fprintf(stderr, "%s: %s: %d: %s: error: %d: %s\n", + g_progname, path ? path : "-", line, func, ecode, tcfdberrmsg(ecode)); } - /* print members of fixed-length database */ -static void mprint(TCFDB *fdb){ - iprintf("minimum ID number: %" PRIuMAX "\n", (unsigned long long)tcfdbmin(fdb)); - iprintf("maximum ID number: %" PRIuMAX "\n", (unsigned long long)tcfdbmax(fdb)); - iprintf("width of the value: %u\n", (unsigned int)tcfdbwidth(fdb)); - iprintf("limit file size: %" PRIuMAX "\n", (unsigned long long)tcfdblimsiz(fdb)); - iprintf("limit ID number: %" PRIuMAX "\n", (unsigned long long)tcfdblimid(fdb)); +static void mprint(TCFDB *fdb) { + iprintf("minimum ID number: %" PRIuMAX "\n", (uint64_t) tcfdbmin(fdb)); + iprintf("maximum ID number: %" PRIuMAX "\n", (uint64_t) tcfdbmax(fdb)); + iprintf("width of the value: %u\n", (unsigned int) tcfdbwidth(fdb)); + iprintf("limit file size: %" PRIuMAX "\n", (uint64_t) tcfdblimsiz(fdb)); + iprintf("limit ID number: %" PRIuMAX "\n", (uint64_t) tcfdblimid(fdb)); #ifndef NDEBUG - if(fdb->cnt_writerec < 0) return; - iprintf("cnt_writerec: %" PRIdMAX "\n", (long long)fdb->cnt_writerec); - iprintf("cnt_readrec: %" PRIdMAX "\n", (long long)fdb->cnt_readrec); - iprintf("cnt_truncfile: %" PRIdMAX "\n", (long long)fdb->cnt_truncfile); + if (fdb->cnt_writerec < 0) return; + iprintf("cnt_writerec: %" PRIdMAX "\n", (int64_t) fdb->cnt_writerec); + iprintf("cnt_readrec: %" PRIdMAX "\n", (int64_t) fdb->cnt_readrec); + iprintf("cnt_truncfile: %" PRIdMAX "\n", (int64_t) fdb->cnt_truncfile); #endif } - /* print system information */ -static void sysprint(void){ - TCMAP *info = tcsysinfo(); - if(info){ - tcmapiterinit(info); - const char *kbuf; - while((kbuf = tcmapiternext2(info)) != NULL){ - iprintf("sys_%s: %s\n", kbuf, tcmapiterval2(kbuf)); +static void sysprint(void) { + TCMAP *info = tcsysinfo(); + if (info) { + tcmapiterinit(info); + const char *kbuf; + while ((kbuf = tcmapiternext2(info)) != NULL) { + iprintf("sys_%s: %s\n", kbuf, tcmapiterval2(kbuf)); + } + tcmapdel(info); } - tcmapdel(info); - } } - /* get a random number */ -static int myrand(int range){ - if(range < 2) return 0; - int high = (unsigned int)rand() >> 4; - int low = range * (rand() / (RAND_MAX + 1.0)); - low &= (unsigned int)INT_MAX >> 4; - return (high + low) % range; +static int myrand(int range) { + if (range < 2) return 0; + int high = (unsigned int) rand() >> 4; + int low = range * (rand() / (RAND_MAX + 1.0)); + low &= (unsigned int) INT_MAX >> 4; + return (high + low) % range; } - /* get a random number based on normal distribution */ -static int myrandnd(int range){ - int num = (int)tcdrandnd(range >> 1, range / 10); - return (num < 0 || num >= range) ? 0 : num; +static int myrandnd(int range) { + int num = (int) tcdrandnd(range >> 1, range / 10); + return (num < 0 || num >= range) ? 0 : num; } - /* iterator function */ -static bool iterfunc(const void *kbuf, int ksiz, const void *vbuf, int vsiz, void *op){ - unsigned int sum = 0; - while(--ksiz >= 0){ - sum += ((char *)kbuf)[ksiz]; - } - while(--vsiz >= 0){ - sum += ((char *)vbuf)[vsiz]; - } - return myrand(100 + (sum & 0xff)) > 0; +static bool iterfunc(const void *kbuf, int ksiz, const void *vbuf, int vsiz, void *op) { + unsigned int sum = 0; + while (--ksiz >= 0) { + sum += ((char *) kbuf)[ksiz]; + } + while (--vsiz >= 0) { + sum += ((char *) vbuf)[vsiz]; + } + return myrand(100 + (sum & 0xff)) > 0; } - /* parse arguments of write command */ -static int runwrite(int argc, char **argv){ - char *path = NULL; - char *tstr = NULL; - char *rstr = NULL; - char *wstr = NULL; - char *lstr = NULL; - int omode = 0; - bool rnd = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-nl")){ - omode |= FDBONOLCK; - } else if(!strcmp(argv[i], "-nb")){ - omode |= FDBOLCKNB; - } else if(!strcmp(argv[i], "-rnd")){ - rnd = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; - } else if(!tstr){ - tstr = argv[i]; - } else if(!rstr){ - rstr = argv[i]; - } else if(!wstr){ - wstr = argv[i]; - } else if(!lstr){ - lstr = argv[i]; - } else { - usage(); +static int runwrite(int argc, char **argv) { + char *path = NULL; + char *tstr = NULL; + char *rstr = NULL; + char *wstr = NULL; + char *lstr = NULL; + int omode = 0; + bool rnd = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-nl")) { + omode |= FDBONOLCK; + } else if (!strcmp(argv[i], "-nb")) { + omode |= FDBOLCKNB; + } else if (!strcmp(argv[i], "-rnd")) { + rnd = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else if (!tstr) { + tstr = argv[i]; + } else if (!rstr) { + rstr = argv[i]; + } else if (!wstr) { + wstr = argv[i]; + } else if (!lstr) { + lstr = argv[i]; + } else { + usage(); + } } - } - if(!path || !tstr || !rstr) usage(); - int tnum = tcatoix(tstr); - int rnum = tcatoix(rstr); - if(tnum < 1 || rnum < 1) usage(); - int width = wstr ? tcatoix(wstr) : -1; - int64_t limsiz = lstr ? tcatoix(lstr) : -1; - int rv = procwrite(path, tnum, rnum, width, limsiz, omode, rnd); - return rv; + if (!path || !tstr || !rstr) usage(); + int tnum = tcatoix(tstr); + int rnum = tcatoix(rstr); + if (tnum < 1 || rnum < 1) usage(); + int width = wstr ? tcatoix(wstr) : -1; + int64_t limsiz = lstr ? tcatoix(lstr) : -1; + int rv = procwrite(path, tnum, rnum, width, limsiz, omode, rnd); + return rv; } - /* parse arguments of read command */ -static int runread(int argc, char **argv){ - char *path = NULL; - char *tstr = NULL; - int omode = 0; - bool wb = false; - bool rnd = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-nl")){ - omode |= FDBONOLCK; - } else if(!strcmp(argv[i], "-nb")){ - omode |= FDBOLCKNB; - } else if(!strcmp(argv[i], "-wb")){ - wb = true; - } else if(!strcmp(argv[i], "-rnd")){ - rnd = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; - } else if(!tstr){ - tstr = argv[i]; - } else { - usage(); +static int runread(int argc, char **argv) { + char *path = NULL; + char *tstr = NULL; + int omode = 0; + bool wb = false; + bool rnd = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-nl")) { + omode |= FDBONOLCK; + } else if (!strcmp(argv[i], "-nb")) { + omode |= FDBOLCKNB; + } else if (!strcmp(argv[i], "-wb")) { + wb = true; + } else if (!strcmp(argv[i], "-rnd")) { + rnd = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else if (!tstr) { + tstr = argv[i]; + } else { + usage(); + } } - } - if(!path || !tstr) usage(); - int tnum = tcatoix(tstr); - if(tnum < 1) usage(); - int rv = procread(path, tnum, omode, wb, rnd); - return rv; + if (!path || !tstr) usage(); + int tnum = tcatoix(tstr); + if (tnum < 1) usage(); + int rv = procread(path, tnum, omode, wb, rnd); + return rv; } - /* parse arguments of remove command */ -static int runremove(int argc, char **argv){ - char *path = NULL; - char *tstr = NULL; - int omode = 0; - bool rnd = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-nl")){ - omode |= FDBONOLCK; - } else if(!strcmp(argv[i], "-nb")){ - omode |= FDBOLCKNB; - } else if(!strcmp(argv[i], "-rnd")){ - rnd = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; - } else if(!tstr){ - tstr = argv[i]; - } else { - usage(); +static int runremove(int argc, char **argv) { + char *path = NULL; + char *tstr = NULL; + int omode = 0; + bool rnd = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-nl")) { + omode |= FDBONOLCK; + } else if (!strcmp(argv[i], "-nb")) { + omode |= FDBOLCKNB; + } else if (!strcmp(argv[i], "-rnd")) { + rnd = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else if (!tstr) { + tstr = argv[i]; + } else { + usage(); + } } - } - if(!path || !tstr) usage(); - int tnum = tcatoix(tstr); - if(tnum < 1) usage(); - int rv = procremove(path, tnum, omode, rnd); - return rv; + if (!path || !tstr) usage(); + int tnum = tcatoix(tstr); + if (tnum < 1) usage(); + int rv = procremove(path, tnum, omode, rnd); + return rv; } - /* parse arguments of wicked command */ -static int runwicked(int argc, char **argv){ - char *path = NULL; - char *tstr = NULL; - char *rstr = NULL; - int omode = 0; - bool nc = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-nl")){ - omode |= FDBONOLCK; - } else if(!strcmp(argv[i], "-nb")){ - omode |= FDBOLCKNB; - } else if(!strcmp(argv[i], "-nc")){ - nc = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; - } else if(!tstr){ - tstr = argv[i]; - } else if(!rstr){ - rstr = argv[i]; - } else { - usage(); +static int runwicked(int argc, char **argv) { + char *path = NULL; + char *tstr = NULL; + char *rstr = NULL; + int omode = 0; + bool nc = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-nl")) { + omode |= FDBONOLCK; + } else if (!strcmp(argv[i], "-nb")) { + omode |= FDBOLCKNB; + } else if (!strcmp(argv[i], "-nc")) { + nc = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else if (!tstr) { + tstr = argv[i]; + } else if (!rstr) { + rstr = argv[i]; + } else { + usage(); + } } - } - if(!path || !tstr || !rstr) usage(); - int tnum = tcatoix(tstr); - int rnum = tcatoix(rstr); - if(tnum < 1 || rnum < 1) usage(); - int rv = procwicked(path, tnum, rnum, omode, nc); - return rv; + if (!path || !tstr || !rstr) usage(); + int tnum = tcatoix(tstr); + int rnum = tcatoix(rstr); + if (tnum < 1 || rnum < 1) usage(); + int rv = procwicked(path, tnum, rnum, omode, nc); + return rv; } - /* parse arguments of typical command */ -static int runtypical(int argc, char **argv){ - char *path = NULL; - char *tstr = NULL; - char *rstr = NULL; - char *wstr = NULL; - char *lstr = NULL; - int omode = 0; - int rratio = -1; - bool nc = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-nl")){ - omode |= FDBONOLCK; - } else if(!strcmp(argv[i], "-nb")){ - omode |= FDBOLCKNB; - } else if(!strcmp(argv[i], "-nc")){ - nc = true; - } else if(!strcmp(argv[i], "-rr")){ - if(++i >= argc) usage(); - rratio = tcatoix(argv[i]); - } else { - usage(); - } - } else if(!path){ - path = argv[i]; - } else if(!tstr){ - tstr = argv[i]; - } else if(!rstr){ - rstr = argv[i]; - } else if(!wstr){ - wstr = argv[i]; - } else if(!lstr){ - lstr = argv[i]; - } else { - usage(); +static int runtypical(int argc, char **argv) { + char *path = NULL; + char *tstr = NULL; + char *rstr = NULL; + char *wstr = NULL; + char *lstr = NULL; + int omode = 0; + int rratio = -1; + bool nc = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-nl")) { + omode |= FDBONOLCK; + } else if (!strcmp(argv[i], "-nb")) { + omode |= FDBOLCKNB; + } else if (!strcmp(argv[i], "-nc")) { + nc = true; + } else if (!strcmp(argv[i], "-rr")) { + if (++i >= argc) usage(); + rratio = tcatoix(argv[i]); + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else if (!tstr) { + tstr = argv[i]; + } else if (!rstr) { + rstr = argv[i]; + } else if (!wstr) { + wstr = argv[i]; + } else if (!lstr) { + lstr = argv[i]; + } else { + usage(); + } } - } - if(!path || !tstr || !rstr) usage(); - int tnum = tcatoix(tstr); - int rnum = tcatoix(rstr); - if(tnum < 1 || rnum < 1) usage(); - int width = wstr ? tcatoix(wstr) : -1; - int64_t limsiz = lstr ? tcatoix(lstr) : -1; - int rv = proctypical(path, tnum, rnum, width, limsiz, omode, nc, rratio); - return rv; + if (!path || !tstr || !rstr) usage(); + int tnum = tcatoix(tstr); + int rnum = tcatoix(rstr); + if (tnum < 1 || rnum < 1) usage(); + int width = wstr ? tcatoix(wstr) : -1; + int64_t limsiz = lstr ? tcatoix(lstr) : -1; + int rv = proctypical(path, tnum, rnum, width, limsiz, omode, nc, rratio); + return rv; } - /* perform write command */ static int procwrite(const char *path, int tnum, int rnum, int width, int64_t limsiz, - int omode, bool rnd){ - iprintf("\n seed=%u path=%s tnum=%d rnum=%d width=%d limsiz=%" PRIdMAX " omode=%d rnd=%d\n\n", - g_randseed, path, tnum, rnum, width, (long long)limsiz, omode, rnd); - bool err = false; - double stime = tctime(); - TCFDB *fdb = tcfdbnew(); - if(!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); - if(!tcfdbsetmutex(fdb)){ - eprint(fdb, __LINE__, "tcfdbsetmutex"); - err = true; - } - if(!tcfdbtune(fdb, width, limsiz)){ - eprint(fdb, __LINE__, "tcfdbtune"); - err = true; - } - if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)){ - eprint(fdb, __LINE__, "tcfdbopen"); - err = true; - } - TARGWRITE targs[tnum]; - pthread_t threads[tnum]; - if(tnum == 1){ - targs[0].fdb = fdb; - targs[0].rnum = rnum; - targs[0].rnd = rnd; - targs[0].id = 0; - if(threadwrite(targs) != NULL) err = true; - } else { - for(int i = 0; i < tnum; i++){ - targs[i].fdb = fdb; - targs[i].rnum = rnum; - targs[i].rnd = rnd; - targs[i].id = i; - if(pthread_create(threads + i, NULL, threadwrite, targs + i) != 0){ - eprint(fdb, __LINE__, "pthread_create"); - targs[i].id = -1; + int omode, bool rnd) { + iprintf("\n seed=%u path=%s tnum=%d rnum=%d width=%d limsiz=%" PRIdMAX " omode=%d rnd=%d\n\n", + g_randseed, path, tnum, rnum, width, (int64_t) limsiz, omode, rnd); + bool err = false; + double stime = tctime(); + TCFDB *fdb = tcfdbnew(); + if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); + if (!tcfdbsetmutex(fdb)) { + eprint(fdb, __LINE__, "tcfdbsetmutex"); err = true; - } } - for(int i = 0; i < tnum; i++){ - if(targs[i].id == -1) continue; - void *rv; - if(pthread_join(threads[i], &rv) != 0){ - eprint(fdb, __LINE__, "pthread_join"); + if (!tcfdbtune(fdb, width, limsiz)) { + eprint(fdb, __LINE__, "tcfdbtune"); err = true; - } else if(rv){ + } + if (!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)) { + eprint(fdb, __LINE__, "tcfdbopen"); + err = true; + } + TARGWRITE targs[tnum]; + pthread_t threads[tnum]; + if (tnum == 1) { + targs[0].fdb = fdb; + targs[0].rnum = rnum; + targs[0].rnd = rnd; + targs[0].id = 0; + if (threadwrite(targs) != NULL) err = true; + } else { + for (int i = 0; i < tnum; i++) { + targs[i].fdb = fdb; + targs[i].rnum = rnum; + targs[i].rnd = rnd; + targs[i].id = i; + if (pthread_create(threads + i, NULL, threadwrite, targs + i) != 0) { + eprint(fdb, __LINE__, "pthread_create"); + targs[i].id = -1; + err = true; + } + } + for (int i = 0; i < tnum; i++) { + if (targs[i].id == -1) continue; + void *rv; + if (pthread_join(threads[i], &rv) != 0) { + eprint(fdb, __LINE__, "pthread_join"); + err = true; + } else if (rv) { + err = true; + } + } + } + iprintf("record number: %" PRIuMAX "\n", (uint64_t) tcfdbrnum(fdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t) tcfdbfsiz(fdb)); + mprint(fdb); + sysprint(); + if (!tcfdbclose(fdb)) { + eprint(fdb, __LINE__, "tcfdbclose"); err = true; - } } - } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcfdbrnum(fdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcfdbfsiz(fdb)); - mprint(fdb); - sysprint(); - if(!tcfdbclose(fdb)){ - eprint(fdb, __LINE__, "tcfdbclose"); - err = true; - } - tcfdbdel(fdb); - iprintf("time: %.3f\n", tctime() - stime); - iprintf("%s\n\n", err ? "error" : "ok"); - return err ? 1 : 0; + tcfdbdel(fdb); + iprintf("time: %.3f\n", tctime() - stime); + iprintf("%s\n\n", err ? "error" : "ok"); + return err ? 1 : 0; } - /* perform read command */ -static int procread(const char *path, int tnum, int omode, bool wb, bool rnd){ - iprintf("\n seed=%u path=%s tnum=%d omode=%d wb=%d rnd=%d\n\n", - g_randseed, path, tnum, omode, wb, rnd); - bool err = false; - double stime = tctime(); - TCFDB *fdb = tcfdbnew(); - if(!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); - if(!tcfdbsetmutex(fdb)){ - eprint(fdb, __LINE__, "tcfdbsetmutex"); - err = true; - } - if(!tcfdbopen(fdb, path, FDBOREADER | omode)){ - eprint(fdb, __LINE__, "tcfdbopen"); - err = true; - } - int rnum = tcfdbrnum(fdb) / tnum; - TARGREAD targs[tnum]; - pthread_t threads[tnum]; - if(tnum == 1){ - targs[0].fdb = fdb; - targs[0].rnum = rnum; - targs[0].wb = wb; - targs[0].rnd = rnd; - targs[0].id = 0; - if(threadread(targs) != NULL) err = true; - } else { - for(int i = 0; i < tnum; i++){ - targs[i].fdb = fdb; - targs[i].rnum = rnum; - targs[i].wb = wb; - targs[i].rnd = rnd; - targs[i].id = i; - if(pthread_create(threads + i, NULL, threadread, targs + i) != 0){ - eprint(fdb, __LINE__, "pthread_create"); - targs[i].id = -1; +static int procread(const char *path, int tnum, int omode, bool wb, bool rnd) { + iprintf("\n seed=%u path=%s tnum=%d omode=%d wb=%d rnd=%d\n\n", + g_randseed, path, tnum, omode, wb, rnd); + bool err = false; + double stime = tctime(); + TCFDB *fdb = tcfdbnew(); + if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); + if (!tcfdbsetmutex(fdb)) { + eprint(fdb, __LINE__, "tcfdbsetmutex"); err = true; - } } - for(int i = 0; i < tnum; i++){ - if(targs[i].id == -1) continue; - void *rv; - if(pthread_join(threads[i], &rv) != 0){ - eprint(fdb, __LINE__, "pthread_join"); + if (!tcfdbopen(fdb, path, FDBOREADER | omode)) { + eprint(fdb, __LINE__, "tcfdbopen"); err = true; - } else if(rv){ + } + int rnum = tcfdbrnum(fdb) / tnum; + TARGREAD targs[tnum]; + pthread_t threads[tnum]; + if (tnum == 1) { + targs[0].fdb = fdb; + targs[0].rnum = rnum; + targs[0].wb = wb; + targs[0].rnd = rnd; + targs[0].id = 0; + if (threadread(targs) != NULL) err = true; + } else { + for (int i = 0; i < tnum; i++) { + targs[i].fdb = fdb; + targs[i].rnum = rnum; + targs[i].wb = wb; + targs[i].rnd = rnd; + targs[i].id = i; + if (pthread_create(threads + i, NULL, threadread, targs + i) != 0) { + eprint(fdb, __LINE__, "pthread_create"); + targs[i].id = -1; + err = true; + } + } + for (int i = 0; i < tnum; i++) { + if (targs[i].id == -1) continue; + void *rv; + if (pthread_join(threads[i], &rv) != 0) { + eprint(fdb, __LINE__, "pthread_join"); + err = true; + } else if (rv) { + err = true; + } + } + } + iprintf("record number: %" PRIuMAX "\n", (uint64_t) tcfdbrnum(fdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t) tcfdbfsiz(fdb)); + mprint(fdb); + sysprint(); + if (!tcfdbclose(fdb)) { + eprint(fdb, __LINE__, "tcfdbclose"); err = true; - } } - } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcfdbrnum(fdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcfdbfsiz(fdb)); - mprint(fdb); - sysprint(); - if(!tcfdbclose(fdb)){ - eprint(fdb, __LINE__, "tcfdbclose"); - err = true; - } - tcfdbdel(fdb); - iprintf("time: %.3f\n", tctime() - stime); - iprintf("%s\n\n", err ? "error" : "ok"); - return err ? 1 : 0; + tcfdbdel(fdb); + iprintf("time: %.3f\n", tctime() - stime); + iprintf("%s\n\n", err ? "error" : "ok"); + return err ? 1 : 0; } - /* perform remove command */ -static int procremove(const char *path, int tnum, int omode, bool rnd){ - iprintf("\n seed=%u path=%s tnum=%d omode=%d rnd=%d\n\n", - g_randseed, path, tnum, omode, rnd); - bool err = false; - double stime = tctime(); - TCFDB *fdb = tcfdbnew(); - if(!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); - if(!tcfdbsetmutex(fdb)){ - eprint(fdb, __LINE__, "tcfdbsetmutex"); - err = true; - } - if(!tcfdbopen(fdb, path, FDBOWRITER | omode)){ - eprint(fdb, __LINE__, "tcfdbopen"); - err = true; - } - int rnum = tcfdbrnum(fdb) / tnum; - TARGREMOVE targs[tnum]; - pthread_t threads[tnum]; - if(tnum == 1){ - targs[0].fdb = fdb; - targs[0].rnum = rnum; - targs[0].rnd = rnd; - targs[0].id = 0; - if(threadremove(targs) != NULL) err = true; - } else { - for(int i = 0; i < tnum; i++){ - targs[i].fdb = fdb; - targs[i].rnum = rnum; - targs[i].rnd = rnd; - targs[i].id = i; - if(pthread_create(threads + i, NULL, threadremove, targs + i) != 0){ - eprint(fdb, __LINE__, "pthread_create"); - targs[i].id = -1; +static int procremove(const char *path, int tnum, int omode, bool rnd) { + iprintf("\n seed=%u path=%s tnum=%d omode=%d rnd=%d\n\n", + g_randseed, path, tnum, omode, rnd); + bool err = false; + double stime = tctime(); + TCFDB *fdb = tcfdbnew(); + if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); + if (!tcfdbsetmutex(fdb)) { + eprint(fdb, __LINE__, "tcfdbsetmutex"); err = true; - } } - for(int i = 0; i < tnum; i++){ - if(targs[i].id == -1) continue; - void *rv; - if(pthread_join(threads[i], &rv) != 0){ - eprint(fdb, __LINE__, "pthread_join"); + if (!tcfdbopen(fdb, path, FDBOWRITER | omode)) { + eprint(fdb, __LINE__, "tcfdbopen"); err = true; - } else if(rv){ + } + int rnum = tcfdbrnum(fdb) / tnum; + TARGREMOVE targs[tnum]; + pthread_t threads[tnum]; + if (tnum == 1) { + targs[0].fdb = fdb; + targs[0].rnum = rnum; + targs[0].rnd = rnd; + targs[0].id = 0; + if (threadremove(targs) != NULL) err = true; + } else { + for (int i = 0; i < tnum; i++) { + targs[i].fdb = fdb; + targs[i].rnum = rnum; + targs[i].rnd = rnd; + targs[i].id = i; + if (pthread_create(threads + i, NULL, threadremove, targs + i) != 0) { + eprint(fdb, __LINE__, "pthread_create"); + targs[i].id = -1; + err = true; + } + } + for (int i = 0; i < tnum; i++) { + if (targs[i].id == -1) continue; + void *rv; + if (pthread_join(threads[i], &rv) != 0) { + eprint(fdb, __LINE__, "pthread_join"); + err = true; + } else if (rv) { + err = true; + } + } + } + iprintf("record number: %" PRIuMAX "\n", (uint64_t) tcfdbrnum(fdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t) tcfdbfsiz(fdb)); + mprint(fdb); + sysprint(); + if (!tcfdbclose(fdb)) { + eprint(fdb, __LINE__, "tcfdbclose"); err = true; - } } - } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcfdbrnum(fdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcfdbfsiz(fdb)); - mprint(fdb); - sysprint(); - if(!tcfdbclose(fdb)){ - eprint(fdb, __LINE__, "tcfdbclose"); - err = true; - } - tcfdbdel(fdb); - iprintf("time: %.3f\n", tctime() - stime); - iprintf("%s\n\n", err ? "error" : "ok"); - return err ? 1 : 0; + tcfdbdel(fdb); + iprintf("time: %.3f\n", tctime() - stime); + iprintf("%s\n\n", err ? "error" : "ok"); + return err ? 1 : 0; } - /* perform wicked command */ -static int procwicked(const char *path, int tnum, int rnum, int omode, bool nc){ - iprintf("\n seed=%u path=%s tnum=%d rnum=%d omode=%d nc=%d\n\n", - g_randseed, path, tnum, rnum, omode, nc); - bool err = false; - double stime = tctime(); - TCFDB *fdb = tcfdbnew(); - if(!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); - if(!tcfdbsetmutex(fdb)){ - eprint(fdb, __LINE__, "tcfdbsetmutex"); - err = true; - } - if(!tcfdbtune(fdb, RECBUFSIZ * 2, EXHEADSIZ + (RECBUFSIZ * 2 + sizeof(int)) * rnum * tnum)){ - eprint(fdb, __LINE__, "tcfdbtune"); - err = true; - } - if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)){ - eprint(fdb, __LINE__, "tcfdbopen"); - err = true; - } - if(!tcfdbiterinit(fdb)){ - eprint(fdb, __LINE__, "tcfdbiterinit"); - err = true; - } - TARGWICKED targs[tnum]; - pthread_t threads[tnum]; - TCMAP *map = tcmapnew(); - if(tnum == 1){ - targs[0].fdb = fdb; - targs[0].rnum = rnum; - targs[0].nc = nc; - targs[0].id = 0; - targs[0].map = map; - if(threadwicked(targs) != NULL) err = true; - } else { - for(int i = 0; i < tnum; i++){ - targs[i].fdb = fdb; - targs[i].rnum = rnum; - targs[i].nc = nc; - targs[i].id = i; - targs[i].map = map; - if(pthread_create(threads + i, NULL, threadwicked, targs + i) != 0){ - eprint(fdb, __LINE__, "pthread_create"); - targs[i].id = -1; +static int procwicked(const char *path, int tnum, int rnum, int omode, bool nc) { + iprintf("\n seed=%u path=%s tnum=%d rnum=%d omode=%d nc=%d\n\n", + g_randseed, path, tnum, rnum, omode, nc); + bool err = false; + double stime = tctime(); + TCFDB *fdb = tcfdbnew(); + if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); + if (!tcfdbsetmutex(fdb)) { + eprint(fdb, __LINE__, "tcfdbsetmutex"); err = true; - } } - for(int i = 0; i < tnum; i++){ - if(targs[i].id == -1) continue; - void *rv; - if(pthread_join(threads[i], &rv) != 0){ - eprint(fdb, __LINE__, "pthread_join"); + if (!tcfdbtune(fdb, RECBUFSIZ * 2, EXHEADSIZ + (RECBUFSIZ * 2 + sizeof (int)) * rnum * tnum)) { + eprint(fdb, __LINE__, "tcfdbtune"); err = true; - } else if(rv){ + } + if (!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)) { + eprint(fdb, __LINE__, "tcfdbopen"); err = true; - } } - } - if(!nc){ - if(!tcfdbsync(fdb)){ - eprint(fdb, __LINE__, "tcfdbsync"); - err = true; + if (!tcfdbiterinit(fdb)) { + eprint(fdb, __LINE__, "tcfdbiterinit"); + err = true; } - if(tcfdbrnum(fdb) != tcmaprnum(map)){ - eprint(fdb, __LINE__, "(validation)"); - err = true; + TARGWICKED targs[tnum]; + pthread_t threads[tnum]; + TCMAP *map = tcmapnew(); + if (tnum == 1) { + targs[0].fdb = fdb; + targs[0].rnum = rnum; + targs[0].nc = nc; + targs[0].id = 0; + targs[0].map = map; + if (threadwicked(targs) != NULL) err = true; + } else { + for (int i = 0; i < tnum; i++) { + targs[i].fdb = fdb; + targs[i].rnum = rnum; + targs[i].nc = nc; + targs[i].id = i; + targs[i].map = map; + if (pthread_create(threads + i, NULL, threadwicked, targs + i) != 0) { + eprint(fdb, __LINE__, "pthread_create"); + targs[i].id = -1; + err = true; + } + } + for (int i = 0; i < tnum; i++) { + if (targs[i].id == -1) continue; + void *rv; + if (pthread_join(threads[i], &rv) != 0) { + eprint(fdb, __LINE__, "pthread_join"); + err = true; + } else if (rv) { + err = true; + } + } } - int end = rnum * tnum; - for(int i = 1; i <= end && !err; i++){ - char kbuf[RECBUFSIZ]; - int ksiz = sprintf(kbuf, "%d", i); - int vsiz; - const char *vbuf = tcmapget(map, kbuf, ksiz, &vsiz); - int rsiz; - char *rbuf = tcfdbget2(fdb, kbuf, ksiz, &rsiz); - if(vbuf){ - iputchar('.'); - if(vsiz > tcfdbwidth(fdb)) vsiz = tcfdbwidth(fdb); - if(!rbuf){ - eprint(fdb, __LINE__, "tcfdbget"); - err = true; - } else if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){ - eprint(fdb, __LINE__, "(validation)"); - err = true; + if (!nc) { + if (!tcfdbsync(fdb)) { + eprint(fdb, __LINE__, "tcfdbsync"); + err = true; } - } else { - iputchar('*'); - if(rbuf || tcfdbecode(fdb) != TCENOREC){ - eprint(fdb, __LINE__, "(validation)"); - err = true; + if (tcfdbrnum(fdb) != tcmaprnum(map)) { + eprint(fdb, __LINE__, "(validation)"); + err = true; + } + int end = rnum * tnum; + for (int i = 1; i <= end && !err; i++) { + char kbuf[RECBUFSIZ]; + int ksiz = sprintf(kbuf, "%d", i); + int vsiz; + const char *vbuf = tcmapget(map, kbuf, ksiz, &vsiz); + int rsiz; + char *rbuf = tcfdbget2(fdb, kbuf, ksiz, &rsiz); + if (vbuf) { + iputchar('.'); + if (vsiz > tcfdbwidth(fdb)) vsiz = tcfdbwidth(fdb); + if (!rbuf) { + eprint(fdb, __LINE__, "tcfdbget"); + err = true; + } else if (rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)) { + eprint(fdb, __LINE__, "(validation)"); + err = true; + } + } else { + iputchar('*'); + if (rbuf || tcfdbecode(fdb) != TCENOREC) { + eprint(fdb, __LINE__, "(validation)"); + err = true; + } + } + tcfree(rbuf); + if (i % 50 == 0) iprintf(" (%08d)\n", i); } - } - tcfree(rbuf); - if(i % 50 == 0) iprintf(" (%08d)\n", i); + if (rnum % 50 > 0) iprintf(" (%08d)\n", rnum); } - if(rnum % 50 > 0) iprintf(" (%08d)\n", rnum); - } - tcmapdel(map); - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcfdbrnum(fdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcfdbfsiz(fdb)); - mprint(fdb); - sysprint(); - if(!tcfdbclose(fdb)){ - eprint(fdb, __LINE__, "tcfdbclose"); - err = true; - } - tcfdbdel(fdb); - iprintf("time: %.3f\n", tctime() - stime); - iprintf("%s\n\n", err ? "error" : "ok"); - return err ? 1 : 0; + tcmapdel(map); + iprintf("record number: %" PRIuMAX "\n", (uint64_t) tcfdbrnum(fdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t) tcfdbfsiz(fdb)); + mprint(fdb); + sysprint(); + if (!tcfdbclose(fdb)) { + eprint(fdb, __LINE__, "tcfdbclose"); + err = true; + } + tcfdbdel(fdb); + iprintf("time: %.3f\n", tctime() - stime); + iprintf("%s\n\n", err ? "error" : "ok"); + return err ? 1 : 0; } - /* perform typical command */ static int proctypical(const char *path, int tnum, int rnum, int width, int64_t limsiz, - int omode, bool nc, int rratio){ - iprintf("\n seed=%u path=%s tnum=%d rnum=%d width=%d limsiz=%" PRIdMAX " omode=%d nc=%d rratio=%d\n\n", - g_randseed, path, tnum, rnum, width, (long long)limsiz, omode, nc, rratio); - bool err = false; - double stime = tctime(); - TCFDB *fdb = tcfdbnew(); - if(!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); - if(!tcfdbsetmutex(fdb)){ - eprint(fdb, __LINE__, "tcfdbsetmutex"); - err = true; - } - if(!tcfdbtune(fdb, width, limsiz)){ - eprint(fdb, __LINE__, "tcfdbtune"); - err = true; - } - if(!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)){ - eprint(fdb, __LINE__, "tcfdbopen"); - err = true; - } - TARGTYPICAL targs[tnum]; - pthread_t threads[tnum]; - if(tnum == 1){ - targs[0].fdb = fdb; - targs[0].rnum = rnum; - targs[0].nc = nc; - targs[0].rratio = rratio; - targs[0].id = 0; - if(threadtypical(targs) != NULL) err = true; - } else { - for(int i = 0; i < tnum; i++){ - targs[i].fdb = fdb; - targs[i].rnum = rnum; - targs[i].nc = nc; - targs[i].rratio= rratio; - targs[i].id = i; - if(pthread_create(threads + i, NULL, threadtypical, targs + i) != 0){ - eprint(fdb, __LINE__, "pthread_create"); - targs[i].id = -1; + int omode, bool nc, int rratio) { + iprintf("\n seed=%u path=%s tnum=%d rnum=%d width=%d limsiz=%" PRIdMAX " omode=%d nc=%d rratio=%d\n\n", + g_randseed, path, tnum, rnum, width, (int64_t) limsiz, omode, nc, rratio); + bool err = false; + double stime = tctime(); + TCFDB *fdb = tcfdbnew(); + if (!INVALIDHANDLE(g_dbgfd)) tcfdbsetdbgfd(fdb, g_dbgfd); + if (!tcfdbsetmutex(fdb)) { + eprint(fdb, __LINE__, "tcfdbsetmutex"); + err = true; + } + if (!tcfdbtune(fdb, width, limsiz)) { + eprint(fdb, __LINE__, "tcfdbtune"); err = true; - } } - for(int i = 0; i < tnum; i++){ - if(targs[i].id == -1) continue; - void *rv; - if(pthread_join(threads[i], &rv) != 0){ - eprint(fdb, __LINE__, "pthread_join"); + if (!tcfdbopen(fdb, path, FDBOWRITER | FDBOCREAT | FDBOTRUNC | omode)) { + eprint(fdb, __LINE__, "tcfdbopen"); err = true; - } else if(rv){ + } + TARGTYPICAL targs[tnum]; + pthread_t threads[tnum]; + if (tnum == 1) { + targs[0].fdb = fdb; + targs[0].rnum = rnum; + targs[0].nc = nc; + targs[0].rratio = rratio; + targs[0].id = 0; + if (threadtypical(targs) != NULL) err = true; + } else { + for (int i = 0; i < tnum; i++) { + targs[i].fdb = fdb; + targs[i].rnum = rnum; + targs[i].nc = nc; + targs[i].rratio = rratio; + targs[i].id = i; + if (pthread_create(threads + i, NULL, threadtypical, targs + i) != 0) { + eprint(fdb, __LINE__, "pthread_create"); + targs[i].id = -1; + err = true; + } + } + for (int i = 0; i < tnum; i++) { + if (targs[i].id == -1) continue; + void *rv; + if (pthread_join(threads[i], &rv) != 0) { + eprint(fdb, __LINE__, "pthread_join"); + err = true; + } else if (rv) { + err = true; + } + } + } + iprintf("record number: %" PRIuMAX "\n", (uint64_t) tcfdbrnum(fdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t) tcfdbfsiz(fdb)); + mprint(fdb); + sysprint(); + if (!tcfdbclose(fdb)) { + eprint(fdb, __LINE__, "tcfdbclose"); err = true; - } } - } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcfdbrnum(fdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcfdbfsiz(fdb)); - mprint(fdb); - sysprint(); - if(!tcfdbclose(fdb)){ - eprint(fdb, __LINE__, "tcfdbclose"); - err = true; - } - tcfdbdel(fdb); - iprintf("time: %.3f\n", tctime() - stime); - iprintf("%s\n\n", err ? "error" : "ok"); - return err ? 1 : 0; + tcfdbdel(fdb); + iprintf("time: %.3f\n", tctime() - stime); + iprintf("%s\n\n", err ? "error" : "ok"); + return err ? 1 : 0; } - /* thread the write function */ -static void *threadwrite(void *targ){ - TCFDB *fdb = ((TARGWRITE *)targ)->fdb; - int rnum = ((TARGWRITE *)targ)->rnum; - bool rnd = ((TARGWRITE *)targ)->rnd; - int id = ((TARGWRITE *)targ)->id; - bool err = false; - int base = id * rnum; - for(int i = 1; i <= rnum; i++){ - char buf[RECBUFSIZ]; - int len = sprintf(buf, "%08d", base + (rnd ? myrand(i) + 1 : i)); - if(!tcfdbput2(fdb, buf, len, buf, len)){ - eprint(fdb, __LINE__, "tcfdbput2"); - err = true; - break; - } - if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){ - iputchar('.'); - if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); +static void *threadwrite(void *targ) { + TCFDB *fdb = ((TARGWRITE *) targ)->fdb; + int rnum = ((TARGWRITE *) targ)->rnum; + bool rnd = ((TARGWRITE *) targ)->rnd; + int id = ((TARGWRITE *) targ)->id; + bool err = false; + int base = id * rnum; + for (int i = 1; i <= rnum; i++) { + char buf[RECBUFSIZ]; + int len = sprintf(buf, "%08d", base + (rnd ? myrand(i) + 1 : i)); + if (!tcfdbput2(fdb, buf, len, buf, len)) { + eprint(fdb, __LINE__, "tcfdbput2"); + err = true; + break; + } + if (id == 0 && rnum > 250 && i % (rnum / 250) == 0) { + iputchar('.'); + if (i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); + } } - } - return err ? "error" : NULL; + return err ? "error" : NULL; } - /* thread the read function */ -static void *threadread(void *targ){ - TCFDB *fdb = ((TARGREAD *)targ)->fdb; - int rnum = ((TARGREAD *)targ)->rnum; - bool wb = ((TARGREAD *)targ)->wb; - bool rnd = ((TARGREAD *)targ)->rnd; - int id = ((TARGREAD *)targ)->id; - bool err = false; - int base = id * rnum; - for(int i = 1; i <= rnum && !err; i++){ - uint64_t kid = base + (rnd ? myrandnd(i) + 1 : i); - int vsiz; - if(wb){ - char vbuf[RECBUFSIZ]; - int vsiz = tcfdbget4(fdb, kid, vbuf, RECBUFSIZ); - if(vsiz < 0 && (!rnd || tcfdbecode(fdb) != TCENOREC)){ - eprint(fdb, __LINE__, "tcfdbget4"); - err = true; - } - } else { - char *vbuf = tcfdbget(fdb, kid, &vsiz); - if(!vbuf && (!rnd || tcfdbecode(fdb) != TCENOREC)){ - eprint(fdb, __LINE__, "tcfdbget"); - err = true; - } - tcfree(vbuf); - } - if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){ - iputchar('.'); - if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); +static void *threadread(void *targ) { + TCFDB *fdb = ((TARGREAD *) targ)->fdb; + int rnum = ((TARGREAD *) targ)->rnum; + bool wb = ((TARGREAD *) targ)->wb; + bool rnd = ((TARGREAD *) targ)->rnd; + int id = ((TARGREAD *) targ)->id; + bool err = false; + int base = id * rnum; + for (int i = 1; i <= rnum && !err; i++) { + uint64_t kid = base + (rnd ? myrandnd(i) + 1 : i); + int vsiz; + if (wb) { + char vbuf[RECBUFSIZ]; + int vsiz = tcfdbget4(fdb, kid, vbuf, RECBUFSIZ); + if (vsiz < 0 && (!rnd || tcfdbecode(fdb) != TCENOREC)) { + eprint(fdb, __LINE__, "tcfdbget4"); + err = true; + } + } else { + char *vbuf = tcfdbget(fdb, kid, &vsiz); + if (!vbuf && (!rnd || tcfdbecode(fdb) != TCENOREC)) { + eprint(fdb, __LINE__, "tcfdbget"); + err = true; + } + tcfree(vbuf); + } + if (id == 0 && rnum > 250 && i % (rnum / 250) == 0) { + iputchar('.'); + if (i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); + } } - } - return err ? "error" : NULL; + return err ? "error" : NULL; } - /* thread the remove function */ -static void *threadremove(void *targ){ - TCFDB *fdb = ((TARGREMOVE *)targ)->fdb; - int rnum = ((TARGREMOVE *)targ)->rnum; - bool rnd = ((TARGREMOVE *)targ)->rnd; - int id = ((TARGREMOVE *)targ)->id; - bool err = false; - int base = id * rnum; - for(int i = 1; i <= rnum; i++){ - char kbuf[RECBUFSIZ]; - int ksiz = sprintf(kbuf, "%08d", base + (rnd ? myrand(i + 1) + 1 : i)); - if(!tcfdbout2(fdb, kbuf, ksiz) && (!rnd || tcfdbecode(fdb) != TCENOREC)){ - eprint(fdb, __LINE__, "tcfdbout2"); - err = true; - break; - } - if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){ - iputchar('.'); - if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); - } - } - return err ? "error" : NULL; -} - - -/* thread the wicked function */ -static void *threadwicked(void *targ){ - TCFDB *fdb = ((TARGWICKED *)targ)->fdb; - int rnum = ((TARGWICKED *)targ)->rnum; - bool nc = ((TARGWICKED *)targ)->nc; - int id = ((TARGWICKED *)targ)->id; - TCMAP *map = ((TARGWICKED *)targ)->map; - bool err = false; - for(int i = 1; i <= rnum && !err; i++){ - uint64_t kid = myrand(rnum * (id + 1)) + 1; - char kbuf[RECBUFSIZ]; - int ksiz = sprintf(kbuf, "%" PRIuMAX "", (unsigned long long)kid); - char vbuf[RECBUFSIZ]; - int vsiz = myrand(RECBUFSIZ); - memset(vbuf, '*', vsiz); - vbuf[vsiz] = '\0'; - char *rbuf; - if(!nc) tcglobalmutexlock(); - switch(myrand(16)){ - case 0: - if(id == 0) iputchar('0'); - if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){ - eprint(fdb, __LINE__, "tcfdbput2"); - err = true; - } - if(!nc) tcmapput(map, kbuf, ksiz, vbuf, vsiz); - break; - case 1: - if(id == 0) iputchar('1'); - if(!tcfdbput3(fdb, kbuf, vbuf)){ - eprint(fdb, __LINE__, "tcfdbput3"); - err = true; - } - if(!nc) tcmapput2(map, kbuf, vbuf); - break; - case 2: - if(id == 0) iputchar('2'); - if(!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP){ - eprint(fdb, __LINE__, "tcfdbputkeep2"); - err = true; - } - if(!nc) tcmapputkeep(map, kbuf, ksiz, vbuf, vsiz); - break; - case 3: - if(id == 0) iputchar('3'); - if(!tcfdbputkeep3(fdb, kbuf, vbuf) && tcfdbecode(fdb) != TCEKEEP){ - eprint(fdb, __LINE__, "tcfdbputkeep3"); - err = true; - } - if(!nc) tcmapputkeep2(map, kbuf, vbuf); - break; - case 4: - if(id == 0) iputchar('4'); - if(!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz)){ - eprint(fdb, __LINE__, "tcfdbputcat2"); - err = true; - } - if(!nc) tcmapputcat(map, kbuf, ksiz, vbuf, vsiz); - break; - case 5: - if(id == 0) iputchar('5'); - if(!tcfdbputcat3(fdb, kbuf, vbuf)){ - eprint(fdb, __LINE__, "tcfdbputcat3"); - err = true; - } - if(!nc) tcmapputcat2(map, kbuf, vbuf); - break; - case 6: - if(id == 0) iputchar('6'); - if(myrand(2) == 0){ - if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){ +static void *threadremove(void *targ) { + TCFDB *fdb = ((TARGREMOVE *) targ)->fdb; + int rnum = ((TARGREMOVE *) targ)->rnum; + bool rnd = ((TARGREMOVE *) targ)->rnd; + int id = ((TARGREMOVE *) targ)->id; + bool err = false; + int base = id * rnum; + for (int i = 1; i <= rnum; i++) { + char kbuf[RECBUFSIZ]; + int ksiz = sprintf(kbuf, "%08d", base + (rnd ? myrand(i + 1) + 1 : i)); + if (!tcfdbout2(fdb, kbuf, ksiz) && (!rnd || tcfdbecode(fdb) != TCENOREC)) { eprint(fdb, __LINE__, "tcfdbout2"); err = true; - } - if(!nc) tcmapout(map, kbuf, ksiz); + break; } - break; - case 7: - if(id == 0) iputchar('7'); - if(myrand(2) == 0){ - if(!tcfdbout3(fdb, kbuf) && tcfdbecode(fdb) != TCENOREC){ - eprint(fdb, __LINE__, "tcfdbout3"); - err = true; - } - if(!nc) tcmapout2(map, kbuf); - } - break; - case 8: - if(id == 0) iputchar('8'); - if(!(rbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz))){ - if(tcfdbecode(fdb) != TCENOREC){ - eprint(fdb, __LINE__, "tcfdbget2"); - err = true; - } - rbuf = tcsprintf("[%d]", myrand(i + 1)); - vsiz = strlen(rbuf); + if (id == 0 && rnum > 250 && i % (rnum / 250) == 0) { + iputchar('.'); + if (i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } - vsiz += myrand(vsiz); - if(myrand(3) == 0) vsiz += PATH_MAX; - rbuf = tcrealloc(rbuf, vsiz + 1); - for(int j = 0; j < vsiz; j++){ - rbuf[j] = myrand(0x100); - } - if(!tcfdbput2(fdb, kbuf, ksiz, rbuf, vsiz)){ - eprint(fdb, __LINE__, "tcfdbput2"); - err = true; - } - if(!nc) tcmapput(map, kbuf, ksiz, rbuf, vsiz); - tcfree(rbuf); - break; - case 9: - if(id == 0) iputchar('9'); - if(!(rbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz)) && tcfdbecode(fdb) != TCENOREC){ - eprint(fdb, __LINE__, "tcfdbget2"); - err = true; - } - tcfree(rbuf); - break; - case 10: - if(id == 0) iputchar('A'); - if(!(rbuf = tcfdbget3(fdb, kbuf)) && tcfdbecode(fdb) != TCENOREC){ - eprint(fdb, __LINE__, "tcfdbge3"); - err = true; - } - tcfree(rbuf); - break; - case 11: - if(id == 0) iputchar('B'); - if(myrand(1) == 0) vsiz = 1; - if((vsiz = tcfdbget4(fdb, kid, vbuf, vsiz)) < 0 && tcfdbecode(fdb) != TCENOREC){ - eprint(fdb, __LINE__, "tcfdbget4"); - err = true; - } - break; - case 14: - if(id == 0) iputchar('E'); - if(myrand(rnum / 50) == 0){ - if(!tcfdbiterinit(fdb)){ - eprint(fdb, __LINE__, "tcfdbiterinit"); - err = true; - } + } + return err ? "error" : NULL; +} + +/* thread the wicked function */ +static void *threadwicked(void *targ) { + TCFDB *fdb = ((TARGWICKED *) targ)->fdb; + int rnum = ((TARGWICKED *) targ)->rnum; + bool nc = ((TARGWICKED *) targ)->nc; + int id = ((TARGWICKED *) targ)->id; + TCMAP *map = ((TARGWICKED *) targ)->map; + bool err = false; + for (int i = 1; i <= rnum && !err; i++) { + uint64_t kid = myrand(rnum * (id + 1)) + 1; + char kbuf[RECBUFSIZ]; + int ksiz = sprintf(kbuf, "%" PRIuMAX "", (uint64_t) kid); + char vbuf[RECBUFSIZ]; + int vsiz = myrand(RECBUFSIZ); + memset(vbuf, '*', vsiz); + vbuf[vsiz] = '\0'; + char *rbuf; + if (!nc) tcglobalmutexlock(); + switch (myrand(16)) { + case 0: + if (id == 0) iputchar('0'); + if (!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)) { + eprint(fdb, __LINE__, "tcfdbput2"); + err = true; + } + if (!nc) tcmapput(map, kbuf, ksiz, vbuf, vsiz); + break; + case 1: + if (id == 0) iputchar('1'); + if (!tcfdbput3(fdb, kbuf, vbuf)) { + eprint(fdb, __LINE__, "tcfdbput3"); + err = true; + } + if (!nc) tcmapput2(map, kbuf, vbuf); + break; + case 2: + if (id == 0) iputchar('2'); + if (!tcfdbputkeep2(fdb, kbuf, ksiz, vbuf, vsiz) && tcfdbecode(fdb) != TCEKEEP) { + eprint(fdb, __LINE__, "tcfdbputkeep2"); + err = true; + } + if (!nc) tcmapputkeep(map, kbuf, ksiz, vbuf, vsiz); + break; + case 3: + if (id == 0) iputchar('3'); + if (!tcfdbputkeep3(fdb, kbuf, vbuf) && tcfdbecode(fdb) != TCEKEEP) { + eprint(fdb, __LINE__, "tcfdbputkeep3"); + err = true; + } + if (!nc) tcmapputkeep2(map, kbuf, vbuf); + break; + case 4: + if (id == 0) iputchar('4'); + if (!tcfdbputcat2(fdb, kbuf, ksiz, vbuf, vsiz)) { + eprint(fdb, __LINE__, "tcfdbputcat2"); + err = true; + } + if (!nc) tcmapputcat(map, kbuf, ksiz, vbuf, vsiz); + break; + case 5: + if (id == 0) iputchar('5'); + if (!tcfdbputcat3(fdb, kbuf, vbuf)) { + eprint(fdb, __LINE__, "tcfdbputcat3"); + err = true; + } + if (!nc) tcmapputcat2(map, kbuf, vbuf); + break; + case 6: + if (id == 0) iputchar('6'); + if (myrand(2) == 0) { + if (!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC) { + eprint(fdb, __LINE__, "tcfdbout2"); + err = true; + } + if (!nc) tcmapout(map, kbuf, ksiz); + } + break; + case 7: + if (id == 0) iputchar('7'); + if (myrand(2) == 0) { + if (!tcfdbout3(fdb, kbuf) && tcfdbecode(fdb) != TCENOREC) { + eprint(fdb, __LINE__, "tcfdbout3"); + err = true; + } + if (!nc) tcmapout2(map, kbuf); + } + break; + case 8: + if (id == 0) iputchar('8'); + if (!(rbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz))) { + if (tcfdbecode(fdb) != TCENOREC) { + eprint(fdb, __LINE__, "tcfdbget2"); + err = true; + } + rbuf = tcsprintf("[%d]", myrand(i + 1)); + vsiz = strlen(rbuf); + } + vsiz += myrand(vsiz); + if (myrand(3) == 0) vsiz += PATH_MAX; + rbuf = tcrealloc(rbuf, vsiz + 1); + for (int j = 0; j < vsiz; j++) { + rbuf[j] = myrand(0x100); + } + if (!tcfdbput2(fdb, kbuf, ksiz, rbuf, vsiz)) { + eprint(fdb, __LINE__, "tcfdbput2"); + err = true; + } + if (!nc) tcmapput(map, kbuf, ksiz, rbuf, vsiz); + tcfree(rbuf); + break; + case 9: + if (id == 0) iputchar('9'); + if (!(rbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz)) && tcfdbecode(fdb) != TCENOREC) { + eprint(fdb, __LINE__, "tcfdbget2"); + err = true; + } + tcfree(rbuf); + break; + case 10: + if (id == 0) iputchar('A'); + if (!(rbuf = tcfdbget3(fdb, kbuf)) && tcfdbecode(fdb) != TCENOREC) { + eprint(fdb, __LINE__, "tcfdbge3"); + err = true; + } + tcfree(rbuf); + break; + case 11: + if (id == 0) iputchar('B'); + if (myrand(1) == 0) vsiz = 1; + if ((vsiz = tcfdbget4(fdb, kid, vbuf, vsiz)) < 0 && tcfdbecode(fdb) != TCENOREC) { + eprint(fdb, __LINE__, "tcfdbget4"); + err = true; + } + break; + case 14: + if (id == 0) iputchar('E'); + if (myrand(rnum / 50) == 0) { + if (!tcfdbiterinit(fdb)) { + eprint(fdb, __LINE__, "tcfdbiterinit"); + err = true; + } + } + for (int j = myrand(rnum) / 1000 + 1; j >= 0; j--) { + if (tcfdbiternext(fdb) < 1) { + int ecode = tcfdbecode(fdb); + if (ecode != TCEINVALID && ecode != TCENOREC) { + eprint(fdb, __LINE__, "tcfdbiternext"); + err = true; + } + } + } + break; + default: + if (id == 0) iputchar('@'); + if (tcfdbtranbegin(fdb)) { + if (myrand(2) == 0) { + if (!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)) { + eprint(fdb, __LINE__, "tcfdbput"); + err = true; + } + if (!nc) tcmapput(map, kbuf, ksiz, vbuf, vsiz); + } else { + if (!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC) { + eprint(fdb, __LINE__, "tcfdbout"); + err = true; + } + if (!nc) tcmapout(map, kbuf, ksiz); + } + if (nc && myrand(2) == 0) { + if (!tcfdbtranabort(fdb)) { + eprint(fdb, __LINE__, "tcfdbtranabort"); + err = true; + } + } else { + if (!tcfdbtrancommit(fdb)) { + eprint(fdb, __LINE__, "tcfdbtrancommit"); + err = true; + } + } + } else { + eprint(fdb, __LINE__, "tcfdbtranbegin"); + err = true; + } + if (myrand(1000) == 0) { + if (!tcfdbforeach(fdb, iterfunc, NULL)) { + eprint(fdb, __LINE__, "tcfdbforeach"); + err = true; + } + } + if (myrand(10000) == 0) srand((unsigned int) (tctime() * 1000) % UINT_MAX); + break; } - for(int j = myrand(rnum) / 1000 + 1; j >= 0; j--){ - if(tcfdbiternext(fdb) < 1){ - int ecode = tcfdbecode(fdb); - if(ecode != TCEINVALID && ecode != TCENOREC){ - eprint(fdb, __LINE__, "tcfdbiternext"); - err = true; + if (!nc) tcglobalmutexunlock(); + if (id == 0) { + if (i % 50 == 0) iprintf(" (%08d)\n", i); + if (id == 0 && i == rnum / 4) { + if (!tcfdboptimize(fdb, RECBUFSIZ, -1) && tcfdbecode(fdb) != TCEINVALID) { + eprint(fdb, __LINE__, "tcfdboptimize"); + err = true; + } + if (!tcfdbiterinit(fdb)) { + eprint(fdb, __LINE__, "tcfdbiterinit"); + err = true; + } } - } } - break; - default: - if(id == 0) iputchar('@'); - if(tcfdbtranbegin(fdb)){ - if(myrand(2) == 0){ - if(!tcfdbput2(fdb, kbuf, ksiz, vbuf, vsiz)){ - eprint(fdb, __LINE__, "tcfdbput"); - err = true; + } + return err ? "error" : NULL; +} + +/* thread the typical function */ +static void *threadtypical(void *targ) { + TCFDB *fdb = ((TARGTYPICAL *) targ)->fdb; + int rnum = ((TARGTYPICAL *) targ)->rnum; + bool nc = ((TARGTYPICAL *) targ)->nc; + int rratio = ((TARGTYPICAL *) targ)->rratio; + int id = ((TARGTYPICAL *) targ)->id; + bool err = false; + TCMAP *map = (!nc && id == 0) ? tcmapnew2(rnum + 1) : NULL; + int base = id * rnum; + int mrange = tclmax(50 + rratio, 100); + int width = tcfdbwidth(fdb); + for (int i = 1; !err && i <= rnum; i++) { + char buf[RECBUFSIZ]; + int len = sprintf(buf, "%08d", base + myrandnd(i) + 1); + int rnd = myrand(mrange); + if (rnd < 10) { + if (!tcfdbput2(fdb, buf, len, buf, len)) { + eprint(fdb, __LINE__, "tcfdbput2"); + err = true; } - if(!nc) tcmapput(map, kbuf, ksiz, vbuf, vsiz); - } else { - if(!tcfdbout2(fdb, kbuf, ksiz) && tcfdbecode(fdb) != TCENOREC){ - eprint(fdb, __LINE__, "tcfdbout"); - err = true; + if (map) tcmapput(map, buf, len, buf, len); + } else if (rnd < 15) { + if (!tcfdbputkeep2(fdb, buf, len, buf, len) && tcfdbecode(fdb) != TCEKEEP) { + eprint(fdb, __LINE__, "tcfdbputkeep2"); + err = true; } - if(!nc) tcmapout(map, kbuf, ksiz); - } - if(nc && myrand(2) == 0){ - if(!tcfdbtranabort(fdb)){ - eprint(fdb, __LINE__, "tcfdbtranabort"); - err = true; + if (map) tcmapputkeep(map, buf, len, buf, len); + } else if (rnd < 20) { + if (!tcfdbputcat2(fdb, buf, len, buf, len)) { + eprint(fdb, __LINE__, "tcfdbputcat2"); + err = true; } - } else { - if(!tcfdbtrancommit(fdb)){ - eprint(fdb, __LINE__, "tcfdbtrancommit"); - err = true; + if (map) tcmapputcat(map, buf, len, buf, len); + } else if (rnd < 25) { + if (!tcfdbout2(fdb, buf, len) && tcfdbecode(fdb) && tcfdbecode(fdb) != TCENOREC) { + eprint(fdb, __LINE__, "tcfdbout"); + err = true; + } + if (map) tcmapout(map, buf, len); + } else if (rnd < 26) { + if (myrand(10) == 0 && !tcfdbiterinit(fdb) && tcfdbecode(fdb) != TCENOREC) { + eprint(fdb, __LINE__, "tcfdbiterinit"); + err = true; + } + for (int j = 0; !err && j < 10; j++) { + if (tcfdbiternext(fdb) < 1 && + tcfdbecode(fdb) != TCEINVALID && tcfdbecode(fdb) != TCENOREC) { + eprint(fdb, __LINE__, "tcfdbiternext"); + err = true; + } } - } } else { - eprint(fdb, __LINE__, "tcfdbtranbegin"); - err = true; - } - if(myrand(1000) == 0){ - if(!tcfdbforeach(fdb, iterfunc, NULL)){ - eprint(fdb, __LINE__, "tcfdbforeach"); - err = true; - } - } - if(myrand(10000) == 0) srand((unsigned int)(tctime() * 1000) % UINT_MAX); - break; - } - if(!nc) tcglobalmutexunlock(); - if(id == 0){ - if(i % 50 == 0) iprintf(" (%08d)\n", i); - if(id == 0 && i == rnum / 4){ - if(!tcfdboptimize(fdb, RECBUFSIZ, -1) && tcfdbecode(fdb) != TCEINVALID){ - eprint(fdb, __LINE__, "tcfdboptimize"); - err = true; - } - if(!tcfdbiterinit(fdb)){ - eprint(fdb, __LINE__, "tcfdbiterinit"); - err = true; - } - } - } - } - return err ? "error" : NULL; -} - - -/* thread the typical function */ -static void *threadtypical(void *targ){ - TCFDB *fdb = ((TARGTYPICAL *)targ)->fdb; - int rnum = ((TARGTYPICAL *)targ)->rnum; - bool nc = ((TARGTYPICAL *)targ)->nc; - int rratio = ((TARGTYPICAL *)targ)->rratio; - int id = ((TARGTYPICAL *)targ)->id; - bool err = false; - TCMAP *map = (!nc && id == 0) ? tcmapnew2(rnum + 1) : NULL; - int base = id * rnum; - int mrange = tclmax(50 + rratio, 100); - int width = tcfdbwidth(fdb); - for(int i = 1; !err && i <= rnum; i++){ - char buf[RECBUFSIZ]; - int len = sprintf(buf, "%08d", base + myrandnd(i) + 1); - int rnd = myrand(mrange); - if(rnd < 10){ - if(!tcfdbput2(fdb, buf, len, buf, len)){ - eprint(fdb, __LINE__, "tcfdbput2"); - err = true; - } - if(map) tcmapput(map, buf, len, buf, len); - } else if(rnd < 15){ - if(!tcfdbputkeep2(fdb, buf, len, buf, len) && tcfdbecode(fdb) != TCEKEEP){ - eprint(fdb, __LINE__, "tcfdbputkeep2"); - err = true; - } - if(map) tcmapputkeep(map, buf, len, buf, len); - } else if(rnd < 20){ - if(!tcfdbputcat2(fdb, buf, len, buf, len)){ - eprint(fdb, __LINE__, "tcfdbputcat2"); - err = true; - } - if(map) tcmapputcat(map, buf, len, buf, len); - } else if(rnd < 25){ - if(!tcfdbout2(fdb, buf, len) && tcfdbecode(fdb) && tcfdbecode(fdb) != TCENOREC){ - eprint(fdb, __LINE__, "tcfdbout"); - err = true; - } - if(map) tcmapout(map, buf, len); - } else if(rnd < 26){ - if(myrand(10) == 0 && !tcfdbiterinit(fdb) && tcfdbecode(fdb) != TCENOREC){ - eprint(fdb, __LINE__, "tcfdbiterinit"); - err = true; - } - for(int j = 0; !err && j < 10; j++){ - if(tcfdbiternext(fdb) < 1 && - tcfdbecode(fdb) != TCEINVALID && tcfdbecode(fdb) != TCENOREC){ - eprint(fdb, __LINE__, "tcfdbiternext"); - err = true; - } - } - } else { - int vsiz; - char *vbuf = tcfdbget2(fdb, buf, len, &vsiz); - if(vbuf){ - if(map){ - int msiz = 0; - const char *mbuf = tcmapget(map, buf, len, &msiz); - if(msiz > width) msiz = width; - if(!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){ - eprint(fdb, __LINE__, "(validation)"); - err = true; - } - } - tcfree(vbuf); - } else { - if(tcfdbecode(fdb) != TCENOREC){ - eprint(fdb, __LINE__, "tcfdbget"); - err = true; + int vsiz; + char *vbuf = tcfdbget2(fdb, buf, len, &vsiz); + if (vbuf) { + if (map) { + int msiz = 0; + const char *mbuf = tcmapget(map, buf, len, &msiz); + if (msiz > width) msiz = width; + if (!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)) { + eprint(fdb, __LINE__, "(validation)"); + err = true; + } + } + tcfree(vbuf); + } else { + if (tcfdbecode(fdb) != TCENOREC) { + eprint(fdb, __LINE__, "tcfdbget"); + err = true; + } + if (map && tcmapget(map, buf, len, &vsiz)) { + eprint(fdb, __LINE__, "(validation)"); + err = true; + } + } } - if(map && tcmapget(map, buf, len, &vsiz)){ - eprint(fdb, __LINE__, "(validation)"); - err = true; + if (id == 0 && rnum > 250 && i % (rnum / 250) == 0) { + iputchar('.'); + if (i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } - } - } - if(id == 0 && rnum > 250 && i % (rnum / 250) == 0){ - iputchar('.'); - if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } - } - if(map){ - tcmapiterinit(map); - int ksiz; - const char *kbuf; - while(!err && (kbuf = tcmapiternext(map, &ksiz)) != NULL){ - int vsiz; - char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz); - if(vbuf){ - int msiz = 0; - const char *mbuf = tcmapget(map, kbuf, ksiz, &msiz); - if(msiz > width) msiz = width; - if(!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)){ - eprint(fdb, __LINE__, "(validation)"); - err = true; + if (map) { + tcmapiterinit(map); + int ksiz; + const char *kbuf; + while (!err && (kbuf = tcmapiternext(map, &ksiz)) != NULL) { + int vsiz; + char *vbuf = tcfdbget2(fdb, kbuf, ksiz, &vsiz); + if (vbuf) { + int msiz = 0; + const char *mbuf = tcmapget(map, kbuf, ksiz, &msiz); + if (msiz > width) msiz = width; + if (!mbuf || msiz != vsiz || memcmp(mbuf, vbuf, vsiz)) { + eprint(fdb, __LINE__, "(validation)"); + err = true; + } + tcfree(vbuf); + } else { + eprint(fdb, __LINE__, "(validation)"); + err = true; + } } - tcfree(vbuf); - } else { - eprint(fdb, __LINE__, "(validation)"); - err = true; - } + tcmapdel(map); } - tcmapdel(map); - } - return err ? "error" : NULL; + return err ? "error" : NULL; } diff --git a/tcejdb/tcftest.c b/tcejdb/tcftest.c index 9fd0f5e..df5f944 100644 --- a/tcejdb/tcftest.c +++ b/tcejdb/tcftest.c @@ -180,16 +180,16 @@ static void eprint(TCFDB *fdb, int line, const char *func){ /* print members of fixed-length database */ static void mprint(TCFDB *fdb){ - iprintf("minimum ID number: %" PRIuMAX "\n", (unsigned long long)tcfdbmin(fdb)); - iprintf("maximum ID number: %" PRIuMAX "\n", (unsigned long long)tcfdbmax(fdb)); + iprintf("minimum ID number: %" PRIuMAX "\n", (uint64_t)tcfdbmin(fdb)); + iprintf("maximum ID number: %" PRIuMAX "\n", (uint64_t)tcfdbmax(fdb)); iprintf("width of the value: %u\n", (unsigned int)tcfdbwidth(fdb)); - iprintf("limit file size: %" PRIuMAX "\n", (unsigned long long)tcfdblimsiz(fdb)); - iprintf("limit ID number: %" PRIuMAX "\n", (unsigned long long)tcfdblimid(fdb)); + iprintf("limit file size: %" PRIuMAX "\n", (uint64_t)tcfdblimsiz(fdb)); + iprintf("limit ID number: %" PRIuMAX "\n", (uint64_t)tcfdblimid(fdb)); #ifndef NDEBUG if(fdb->cnt_writerec < 0) return; - iprintf("cnt_writerec: %" PRIdMAX "\n", (long long)fdb->cnt_writerec); - iprintf("cnt_readrec: %" PRIdMAX "\n", (long long)fdb->cnt_readrec); - iprintf("cnt_truncfile: %" PRIdMAX "\n", (long long)fdb->cnt_truncfile); + iprintf("cnt_writerec: %" PRIdMAX "\n", (int64_t)fdb->cnt_writerec); + iprintf("cnt_readrec: %" PRIdMAX "\n", (int64_t)fdb->cnt_readrec); + iprintf("cnt_truncfile: %" PRIdMAX "\n", (int64_t)fdb->cnt_truncfile); #endif } @@ -495,7 +495,7 @@ static int runwicked(int argc, char **argv){ static int procwrite(const char *path, int rnum, int width, int64_t limsiz, bool mt, int omode, bool rnd){ iprintf("\n seed=%u path=%s rnum=%d width=%d limsiz=%" PRIdMAX " mt=%d omode=%d" - " rnd=%d\n\n", g_randseed, path, rnum, width, (long long)limsiz, mt, omode, rnd); + " rnd=%d\n\n", g_randseed, path, rnum, width, (int64_t)limsiz, mt, omode, rnd); bool err = false; double stime = tctime(); TCFDB *fdb = tcfdbnew(); @@ -526,8 +526,8 @@ static int procwrite(const char *path, int rnum, int width, int64_t limsiz, if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcfdbrnum(fdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcfdbfsiz(fdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcfdbrnum(fdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcfdbfsiz(fdb)); mprint(fdb); sysprint(); if(!tcfdbclose(fdb)){ @@ -584,8 +584,8 @@ static int procread(const char *path, bool mt, int omode, bool wb, bool rnd){ if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcfdbrnum(fdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcfdbfsiz(fdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcfdbrnum(fdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcfdbfsiz(fdb)); mprint(fdb); sysprint(); if(!tcfdbclose(fdb)){ @@ -629,8 +629,8 @@ static int procremove(const char *path, bool mt, int omode, bool rnd){ if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcfdbrnum(fdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcfdbfsiz(fdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcfdbrnum(fdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcfdbfsiz(fdb)); mprint(fdb); sysprint(); if(!tcfdbclose(fdb)){ @@ -650,7 +650,7 @@ static int procrcat(const char *path, int rnum, int width, int64_t limsiz, iprintf("\n" " seed=%u path=%s rnum=%d width=%d limsiz=%" PRIdMAX " mt=%d omode=%d pnum=%d" " dai=%d dad=%d rl=%d ru=%d\n\n", - g_randseed, path, rnum, width, (long long)limsiz, mt, omode, pnum, dai, dad, rl, ru); + g_randseed, path, rnum, width, (int64_t)limsiz, mt, omode, pnum, dai, dad, rl, ru); if(pnum < 1) pnum = rnum; bool err = false; double stime = tctime(); @@ -762,8 +762,8 @@ static int procrcat(const char *path, int rnum, int width, int64_t limsiz, if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcfdbrnum(fdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcfdbfsiz(fdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcfdbrnum(fdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcfdbfsiz(fdb)); mprint(fdb); sysprint(); if(!tcfdbclose(fdb)){ @@ -1401,8 +1401,8 @@ static int procmisc(const char *path, int rnum, bool mt, int omode){ err = true; } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcfdbrnum(fdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcfdbfsiz(fdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcfdbrnum(fdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcfdbfsiz(fdb)); mprint(fdb); sysprint(); if(!tcfdbclose(fdb)){ @@ -1444,7 +1444,7 @@ static int procwicked(const char *path, int rnum, bool mt, int omode){ for(int i = 1; i <= rnum && !err; i++){ uint64_t id = myrand(rnum) + 1; char kbuf[RECBUFSIZ]; - int ksiz = sprintf(kbuf, "%" PRIuMAX "", (unsigned long long)id); + int ksiz = sprintf(kbuf, "%" PRIuMAX "", (uint64_t)id); char vbuf[RECBUFSIZ]; int vsiz = myrand(RECBUFSIZ); memset(vbuf, '*', vsiz); @@ -1720,8 +1720,8 @@ static int procwicked(const char *path, int rnum, bool mt, int omode){ eprint(fdb, __LINE__, "(validation)"); err = true; } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tcfdbrnum(fdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tcfdbfsiz(fdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tcfdbrnum(fdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tcfdbfsiz(fdb)); mprint(fdb); sysprint(); tcmapdel(map); diff --git a/tcejdb/tchdb.c b/tcejdb/tchdb.c index 648f9a0..4752ae6 100644 --- a/tcejdb/tchdb.c +++ b/tcejdb/tchdb.c @@ -1457,7 +1457,7 @@ bool tchdbmemsync(TCHDB *hdb, bool phys) { #elif !defined __GNU__ size_t xmsiz = (hdb->xmsiz > hdb->msiz) ? hdb->xmsiz : hdb->msiz; if (!hdb->map || msync(hdb->map, xmsiz, MS_SYNC)) { - tchmdbsetecode(hdb, TCEMMAP, __FILE__, __LINE__, __func__); + tchdbsetecode(hdb, TCEMMAP, __FILE__, __LINE__, __func__); err = true; } #endif @@ -4990,7 +4990,7 @@ static bool tchdbcopyimpl(TCHDB *hdb, const char *path) { } if (*path == '@') { char tsbuf[TCNUMBUFSIZ]; - sprintf(tsbuf, "%" PRIuMAX "", (unsigned long long) (tctime() * 1000000)); + sprintf(tsbuf, "%" PRIuMAX "", (uint64_t) (tctime() * 1000000)); const char *args[3]; args[0] = path + 1; args[1] = hdb->path; @@ -5495,20 +5495,20 @@ void tchdbprintmeta(TCHDB *hdb) { wp += sprintf(wp, " rpath=%s", hdb->rpath ? hdb->rpath : "-"); wp += sprintf(wp, " type=%02X", hdb->type); wp += sprintf(wp, " flags=%02X", hdb->flags); - wp += sprintf(wp, " bnum=%" PRIuMAX "", (unsigned long long) hdb->bnum); + wp += sprintf(wp, " bnum=%" PRIuMAX "", (uint64_t) hdb->bnum); wp += sprintf(wp, " apow=%u", hdb->apow); wp += sprintf(wp, " fpow=%u", hdb->fpow); wp += sprintf(wp, " opts=%u", hdb->opts); wp += sprintf(wp, " path=%s", hdb->path ? hdb->path : "-"); wp += sprintf(wp, " fd=%d", hdb->fd); wp += sprintf(wp, " omode=%u", hdb->omode); - wp += sprintf(wp, " rnum=%" PRIuMAX "", (unsigned long long) hdb->rnum); - wp += sprintf(wp, " fsiz=%" PRIuMAX "", (unsigned long long) hdb->fsiz); - wp += sprintf(wp, " frec=%" PRIuMAX "", (unsigned long long) hdb->frec); - wp += sprintf(wp, " dfcur=%" PRIuMAX "", (unsigned long long) hdb->dfcur); - wp += sprintf(wp, " iter=%" PRIuMAX "", (unsigned long long) hdb->iter); + wp += sprintf(wp, " rnum=%" PRIuMAX "", (uint64_t) hdb->rnum); + wp += sprintf(wp, " fsiz=%" PRIuMAX "", (uint64_t) hdb->fsiz); + wp += sprintf(wp, " frec=%" PRIuMAX "", (uint64_t) hdb->frec); + wp += sprintf(wp, " dfcur=%" PRIuMAX "", (uint64_t) hdb->dfcur); + wp += sprintf(wp, " iter=%" PRIuMAX "", (uint64_t) hdb->iter); wp += sprintf(wp, " map=%p", (void *) hdb->map); - wp += sprintf(wp, " msiz=%" PRIuMAX "", (unsigned long long) hdb->msiz); + wp += sprintf(wp, " msiz=%" PRIuMAX "", (uint64_t) hdb->msiz); wp += sprintf(wp, " ba64=%d", hdb->ba64); wp += sprintf(wp, " align=%u", hdb->align); wp += sprintf(wp, " runit=%u", hdb->runit); @@ -5519,38 +5519,38 @@ void tchdbprintmeta(TCHDB *hdb) { wp += sprintf(wp, " fbpmis=%d", hdb->fbpmis); wp += sprintf(wp, " drpool=%p", (void *) hdb->drpool); wp += sprintf(wp, " drpdef=%p", (void *) hdb->drpdef); - wp += sprintf(wp, " drpoff=%" PRIuMAX "", (unsigned long long) hdb->drpoff); + wp += sprintf(wp, " drpoff=%" PRIuMAX "", (uint64_t) hdb->drpoff); wp += sprintf(wp, " recc=%p", (void *) hdb->recc); wp += sprintf(wp, " rcnum=%u", hdb->rcnum); wp += sprintf(wp, " ecode=%d", hdb->ecode); wp += sprintf(wp, " fatal=%u", hdb->fatal); - wp += sprintf(wp, " inode=%" PRIuMAX "", (unsigned long long) (uint64_t) hdb->inode); - wp += sprintf(wp, " mtime=%" PRIuMAX "", (unsigned long long) (uint64_t) hdb->mtime); + wp += sprintf(wp, " inode=%" PRIuMAX "", (uint64_t) (uint64_t) hdb->inode); + wp += sprintf(wp, " mtime=%" PRIuMAX "", (uint64_t) (uint64_t) hdb->mtime); wp += sprintf(wp, " dfunit=%u", hdb->dfunit); wp += sprintf(wp, " dfcnt=%u", hdb->dfcnt); wp += sprintf(wp, " tran=%d", hdb->tran); wp += sprintf(wp, " walfd=%d", hdb->walfd); - wp += sprintf(wp, " walend=%" PRIuMAX "", (unsigned long long) hdb->walend); + wp += sprintf(wp, " walend=%" PRIuMAX "", (uint64_t) hdb->walend); wp += sprintf(wp, " dbgfd=%d", hdb->dbgfd); #ifndef NDEBUG - wp += sprintf(wp, " cnt_writerec=%" PRIdMAX "", (long long) hdb->cnt_writerec); - wp += sprintf(wp, " cnt_reuserec=%" PRIdMAX "", (long long) hdb->cnt_reuserec); - wp += sprintf(wp, " cnt_moverec=%" PRIdMAX "", (long long) hdb->cnt_moverec); - wp += sprintf(wp, " cnt_readrec=%" PRIdMAX "", (long long) hdb->cnt_readrec); - wp += sprintf(wp, " cnt_searchfbp=%" PRIdMAX "", (long long) hdb->cnt_searchfbp); - wp += sprintf(wp, " cnt_insertfbp=%" PRIdMAX "", (long long) hdb->cnt_insertfbp); - wp += sprintf(wp, " cnt_splicefbp=%" PRIdMAX "", (long long) hdb->cnt_splicefbp); - wp += sprintf(wp, " cnt_dividefbp=%" PRIdMAX "", (long long) hdb->cnt_dividefbp); - wp += sprintf(wp, " cnt_mergefbp=%" PRIdMAX "", (long long) hdb->cnt_mergefbp); - wp += sprintf(wp, " cnt_reducefbp=%" PRIdMAX "", (long long) hdb->cnt_reducefbp); - wp += sprintf(wp, " cnt_appenddrp=%" PRIdMAX "", (long long) hdb->cnt_appenddrp); - wp += sprintf(wp, " cnt_deferdrp=%" PRIdMAX "", (long long) hdb->cnt_deferdrp); - wp += sprintf(wp, " cnt_flushdrp=%" PRIdMAX "", (long long) hdb->cnt_flushdrp); - wp += sprintf(wp, " cnt_adjrecc=%" PRIdMAX "", (long long) hdb->cnt_adjrecc); - wp += sprintf(wp, " cnt_defrag=%" PRIdMAX "", (long long) hdb->cnt_defrag); - wp += sprintf(wp, " cnt_shiftrec=%" PRIdMAX "", (long long) hdb->cnt_shiftrec); - wp += sprintf(wp, " cnt_trunc=%" PRIdMAX "", (long long) hdb->cnt_trunc); + wp += sprintf(wp, " cnt_writerec=%" PRIdMAX "", (int64_t) hdb->cnt_writerec); + wp += sprintf(wp, " cnt_reuserec=%" PRIdMAX "", (int64_t) hdb->cnt_reuserec); + wp += sprintf(wp, " cnt_moverec=%" PRIdMAX "", (int64_t) hdb->cnt_moverec); + wp += sprintf(wp, " cnt_readrec=%" PRIdMAX "", (int64_t) hdb->cnt_readrec); + wp += sprintf(wp, " cnt_searchfbp=%" PRIdMAX "", (int64_t) hdb->cnt_searchfbp); + wp += sprintf(wp, " cnt_insertfbp=%" PRIdMAX "", (int64_t) hdb->cnt_insertfbp); + wp += sprintf(wp, " cnt_splicefbp=%" PRIdMAX "", (int64_t) hdb->cnt_splicefbp); + wp += sprintf(wp, " cnt_dividefbp=%" PRIdMAX "", (int64_t) hdb->cnt_dividefbp); + wp += sprintf(wp, " cnt_mergefbp=%" PRIdMAX "", (int64_t) hdb->cnt_mergefbp); + wp += sprintf(wp, " cnt_reducefbp=%" PRIdMAX "", (int64_t) hdb->cnt_reducefbp); + wp += sprintf(wp, " cnt_appenddrp=%" PRIdMAX "", (int64_t) hdb->cnt_appenddrp); + wp += sprintf(wp, " cnt_deferdrp=%" PRIdMAX "", (int64_t) hdb->cnt_deferdrp); + wp += sprintf(wp, " cnt_flushdrp=%" PRIdMAX "", (int64_t) hdb->cnt_flushdrp); + wp += sprintf(wp, " cnt_adjrecc=%" PRIdMAX "", (int64_t) hdb->cnt_adjrecc); + wp += sprintf(wp, " cnt_defrag=%" PRIdMAX "", (int64_t) hdb->cnt_defrag); + wp += sprintf(wp, " cnt_shiftrec=%" PRIdMAX "", (int64_t) hdb->cnt_shiftrec); + wp += sprintf(wp, " cnt_trunc=%" PRIdMAX "", (int64_t) hdb->cnt_trunc); #endif *(wp++) = '\n'; @@ -5569,18 +5569,18 @@ void tchdbprintrec(TCHDB *hdb, TCHREC *rec) { char buf[HDBIOBUFSIZ]; char *wp = buf; wp += sprintf(wp, "REC:"); - wp += sprintf(wp, " off=%" PRIuMAX "", (unsigned long long) rec->off); + wp += sprintf(wp, " off=%" PRIuMAX "", (uint64_t) rec->off); wp += sprintf(wp, " rsiz=%u", rec->rsiz); wp += sprintf(wp, " magic=%02X", rec->magic); wp += sprintf(wp, " hash=%02X", rec->hash); - wp += sprintf(wp, " left=%" PRIuMAX "", (unsigned long long) rec->left); - wp += sprintf(wp, " right=%" PRIuMAX "", (unsigned long long) rec->right); + wp += sprintf(wp, " left=%" PRIuMAX "", (uint64_t) rec->left); + wp += sprintf(wp, " right=%" PRIuMAX "", (uint64_t) rec->right); wp += sprintf(wp, " ksiz=%u", rec->ksiz); wp += sprintf(wp, " vsiz=%u", rec->vsiz); wp += sprintf(wp, " psiz=%u", rec->psiz); wp += sprintf(wp, " kbuf=%p", (void *) rec->kbuf); wp += sprintf(wp, " vbuf=%p", (void *) rec->vbuf); - wp += sprintf(wp, " boff=%" PRIuMAX "", (unsigned long long) rec->boff); + wp += sprintf(wp, " boff=%" PRIuMAX "", (uint64_t) rec->boff); wp += sprintf(wp, " bbuf=%p", (void *) rec->bbuf); *(wp++) = '\n'; tcwrite(dbgfd, buf, wp - buf); diff --git a/tcejdb/tchmgr.c b/tcejdb/tchmgr.c index e1150eb..3c8663f 100644 --- a/tcejdb/tchmgr.c +++ b/tcejdb/tchmgr.c @@ -561,12 +561,12 @@ static int procinform(const char *path, int omode){ if(flags & HDBFOPEN) printf(" open"); if(flags & HDBFFATAL) printf(" fatal"); printf("\n"); - printf("bucket number: %" PRIuMAX "\n", (unsigned long long)tchdbbnum(hdb)); + printf("bucket number: %" PRIuMAX "\n", (uint64_t)tchdbbnum(hdb)); if(hdb->cnt_writerec >= 0) - printf("used bucket number: %" PRIdMAX "\n", (long long)tchdbbnumused(hdb)); + printf("used bucket number: %" PRIdMAX "\n", (int64_t)tchdbbnumused(hdb)); printf("alignment: %u\n", tchdbalign(hdb)); printf("free block pool: %u\n", tchdbfbpmax(hdb)); - printf("inode number: %" PRIdMAX "\n", (long long)tchdbinode(hdb)); + printf("inode number: %" PRIdMAX "\n", (int64_t)tchdbinode(hdb)); char date[48]; tcdatestrwww(tchdbmtime(hdb), INT_MAX, date); printf("modified time: %s\n", date); @@ -578,8 +578,8 @@ static int procinform(const char *path, int omode){ if(opts & HDBTTCBS) printf(" tcbs"); if(opts & HDBTEXCODEC) printf(" excodec"); printf("\n"); - printf("record number: %" PRIuMAX "\n", (unsigned long long)tchdbrnum(hdb)); - printf("file size: %" PRIuMAX "\n", (unsigned long long)tchdbfsiz(hdb)); + printf("record number: %" PRIuMAX "\n", (uint64_t)tchdbrnum(hdb)); + printf("file size: %" PRIuMAX "\n", (uint64_t)tchdbfsiz(hdb)); if(!tchdbclose(hdb)){ if(!err) printerr(hdb); err = true; diff --git a/tcejdb/tchmttest.c b/tcejdb/tchmttest.c index 73a69de..366c3f5 100644 --- a/tcejdb/tchmttest.c +++ b/tcejdb/tchmttest.c @@ -202,27 +202,27 @@ static void eprint(TCHDB *hdb, int line, const char *func){ /* print members of hash database */ static void mprint(TCHDB *hdb){ - iprintf("bucket number: %" PRIdMAX "\n", (long long)tchdbbnum(hdb)); - iprintf("used bucket number: %" PRIdMAX "\n", (long long)tchdbbnumused(hdb)); + iprintf("bucket number: %" PRIdMAX "\n", (int64_t)tchdbbnum(hdb)); + iprintf("used bucket number: %" PRIdMAX "\n", (int64_t)tchdbbnumused(hdb)); #ifndef NDEBUG if(hdb->cnt_writerec < 0) return; - iprintf("cnt_writerec: %" PRIdMAX "\n", (long long)hdb->cnt_writerec); - iprintf("cnt_reuserec: %" PRIdMAX "\n", (long long)hdb->cnt_reuserec); - iprintf("cnt_moverec: %" PRIdMAX "\n", (long long)hdb->cnt_moverec); - iprintf("cnt_readrec: %" PRIdMAX "\n", (long long)hdb->cnt_readrec); - iprintf("cnt_searchfbp: %" PRIdMAX "\n", (long long)hdb->cnt_searchfbp); - iprintf("cnt_insertfbp: %" PRIdMAX "\n", (long long)hdb->cnt_insertfbp); - iprintf("cnt_splicefbp: %" PRIdMAX "\n", (long long)hdb->cnt_splicefbp); - iprintf("cnt_dividefbp: %" PRIdMAX "\n", (long long)hdb->cnt_dividefbp); - iprintf("cnt_mergefbp: %" PRIdMAX "\n", (long long)hdb->cnt_mergefbp); - iprintf("cnt_reducefbp: %" PRIdMAX "\n", (long long)hdb->cnt_reducefbp); - iprintf("cnt_appenddrp: %" PRIdMAX "\n", (long long)hdb->cnt_appenddrp); - iprintf("cnt_deferdrp: %" PRIdMAX "\n", (long long)hdb->cnt_deferdrp); - iprintf("cnt_flushdrp: %" PRIdMAX "\n", (long long)hdb->cnt_flushdrp); - iprintf("cnt_adjrecc: %" PRIdMAX "\n", (long long)hdb->cnt_adjrecc); - iprintf("cnt_defrag: %" PRIdMAX "\n", (long long)hdb->cnt_defrag); - iprintf("cnt_shiftrec: %" PRIdMAX "\n", (long long)hdb->cnt_shiftrec); - iprintf("cnt_trunc: %" PRIdMAX "\n", (long long)hdb->cnt_trunc); + iprintf("cnt_writerec: %" PRIdMAX "\n", (int64_t)hdb->cnt_writerec); + iprintf("cnt_reuserec: %" PRIdMAX "\n", (int64_t)hdb->cnt_reuserec); + iprintf("cnt_moverec: %" PRIdMAX "\n", (int64_t)hdb->cnt_moverec); + iprintf("cnt_readrec: %" PRIdMAX "\n", (int64_t)hdb->cnt_readrec); + iprintf("cnt_searchfbp: %" PRIdMAX "\n", (int64_t)hdb->cnt_searchfbp); + iprintf("cnt_insertfbp: %" PRIdMAX "\n", (int64_t)hdb->cnt_insertfbp); + iprintf("cnt_splicefbp: %" PRIdMAX "\n", (int64_t)hdb->cnt_splicefbp); + iprintf("cnt_dividefbp: %" PRIdMAX "\n", (int64_t)hdb->cnt_dividefbp); + iprintf("cnt_mergefbp: %" PRIdMAX "\n", (int64_t)hdb->cnt_mergefbp); + iprintf("cnt_reducefbp: %" PRIdMAX "\n", (int64_t)hdb->cnt_reducefbp); + iprintf("cnt_appenddrp: %" PRIdMAX "\n", (int64_t)hdb->cnt_appenddrp); + iprintf("cnt_deferdrp: %" PRIdMAX "\n", (int64_t)hdb->cnt_deferdrp); + iprintf("cnt_flushdrp: %" PRIdMAX "\n", (int64_t)hdb->cnt_flushdrp); + iprintf("cnt_adjrecc: %" PRIdMAX "\n", (int64_t)hdb->cnt_adjrecc); + iprintf("cnt_defrag: %" PRIdMAX "\n", (int64_t)hdb->cnt_defrag); + iprintf("cnt_shiftrec: %" PRIdMAX "\n", (int64_t)hdb->cnt_shiftrec); + iprintf("cnt_trunc: %" PRIdMAX "\n", (int64_t)hdb->cnt_trunc); #endif } @@ -702,8 +702,8 @@ static int procwrite(const char *path, int tnum, int rnum, int bnum, int apow, i } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tchdbrnum(hdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tchdbfsiz(hdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tchdbrnum(hdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tchdbfsiz(hdb)); mprint(hdb); sysprint(); if(!tchdbclose(hdb)){ @@ -784,8 +784,8 @@ static int procread(const char *path, int tnum, int rcnum, int xmsiz, int dfunit } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tchdbrnum(hdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tchdbfsiz(hdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tchdbrnum(hdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tchdbfsiz(hdb)); mprint(hdb); sysprint(); if(!tchdbclose(hdb)){ @@ -864,8 +864,8 @@ static int procremove(const char *path, int tnum, int rcnum, int xmsiz, int dfun } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tchdbrnum(hdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tchdbfsiz(hdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tchdbrnum(hdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tchdbfsiz(hdb)); mprint(hdb); sysprint(); if(!tchdbclose(hdb)){ @@ -992,8 +992,8 @@ static int procwicked(const char *path, int tnum, int rnum, int opts, int omode, if(rnum % 50 > 0) iprintf(" (%08d)\n", rnum); } tcmapdel(map); - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tchdbrnum(hdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tchdbfsiz(hdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tchdbrnum(hdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tchdbfsiz(hdb)); mprint(hdb); sysprint(); if(!tchdbclose(hdb)){ @@ -1080,8 +1080,8 @@ static int proctypical(const char *path, int tnum, int rnum, int bnum, int apow, } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tchdbrnum(hdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tchdbfsiz(hdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tchdbrnum(hdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tchdbfsiz(hdb)); mprint(hdb); sysprint(); if(!tchdbclose(hdb)){ @@ -1158,8 +1158,8 @@ static int procrace(const char *path, int tnum, int rnum, int bnum, int apow, in } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tchdbrnum(hdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tchdbfsiz(hdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tchdbrnum(hdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tchdbfsiz(hdb)); mprint(hdb); sysprint(); if(!tchdbclose(hdb)){ diff --git a/tcejdb/tchtest.c b/tcejdb/tchtest.c index 9a49ca7..009c9a2 100644 --- a/tcejdb/tchtest.c +++ b/tcejdb/tchtest.c @@ -153,30 +153,30 @@ static void eprint(TCHDB *hdb, int line, const char *func){ /* print members of hash database */ static void mprint(TCHDB *hdb){ if(hdb->cnt_writerec < 0) return; - iprintf("bucket number: %" PRIdMAX "\n", (long long)tchdbbnum(hdb)); - iprintf("used bucket number: %" PRIdMAX "\n", (long long)tchdbbnumused(hdb)); + iprintf("bucket number: %" PRIdMAX "\n", (int64_t)tchdbbnum(hdb)); + iprintf("used bucket number: %" PRIdMAX "\n", (int64_t)tchdbbnumused(hdb)); iprintf("msiz: %" PRIdMAX "\n", hdb->msiz); iprintf("xmsiz: %" PRIdMAX "\n", hdb->xmsiz); iprintf("fbpnum: %" PRIdMAX "\n", hdb->fbpnum); iprintf("fbpool: %p\n", hdb->fbpool); #ifndef NDEBUG - iprintf("cnt_writerec: %" PRIdMAX "\n", (long long)hdb->cnt_writerec); - iprintf("cnt_reuserec: %" PRIdMAX "\n", (long long)hdb->cnt_reuserec); - iprintf("cnt_moverec: %" PRIdMAX "\n", (long long)hdb->cnt_moverec); - iprintf("cnt_readrec: %" PRIdMAX "\n", (long long)hdb->cnt_readrec); - iprintf("cnt_searchfbp: %" PRIdMAX "\n", (long long)hdb->cnt_searchfbp); - iprintf("cnt_insertfbp: %" PRIdMAX "\n", (long long)hdb->cnt_insertfbp); - iprintf("cnt_splicefbp: %" PRIdMAX "\n", (long long)hdb->cnt_splicefbp); - iprintf("cnt_dividefbp: %" PRIdMAX "\n", (long long)hdb->cnt_dividefbp); - iprintf("cnt_mergefbp: %" PRIdMAX "\n", (long long)hdb->cnt_mergefbp); - iprintf("cnt_reducefbp: %" PRIdMAX "\n", (long long)hdb->cnt_reducefbp); - iprintf("cnt_appenddrp: %" PRIdMAX "\n", (long long)hdb->cnt_appenddrp); - iprintf("cnt_deferdrp: %" PRIdMAX "\n", (long long)hdb->cnt_deferdrp); - iprintf("cnt_flushdrp: %" PRIdMAX "\n", (long long)hdb->cnt_flushdrp); - iprintf("cnt_adjrecc: %" PRIdMAX "\n", (long long)hdb->cnt_adjrecc); - iprintf("cnt_defrag: %" PRIdMAX "\n", (long long)hdb->cnt_defrag); - iprintf("cnt_shiftrec: %" PRIdMAX "\n", (long long)hdb->cnt_shiftrec); - iprintf("cnt_trunc: %" PRIdMAX "\n", (long long)hdb->cnt_trunc); + iprintf("cnt_writerec: %" PRIdMAX "\n", (int64_t)hdb->cnt_writerec); + iprintf("cnt_reuserec: %" PRIdMAX "\n", (int64_t)hdb->cnt_reuserec); + iprintf("cnt_moverec: %" PRIdMAX "\n", (int64_t)hdb->cnt_moverec); + iprintf("cnt_readrec: %" PRIdMAX "\n", (int64_t)hdb->cnt_readrec); + iprintf("cnt_searchfbp: %" PRIdMAX "\n", (int64_t)hdb->cnt_searchfbp); + iprintf("cnt_insertfbp: %" PRIdMAX "\n", (int64_t)hdb->cnt_insertfbp); + iprintf("cnt_splicefbp: %" PRIdMAX "\n", (int64_t)hdb->cnt_splicefbp); + iprintf("cnt_dividefbp: %" PRIdMAX "\n", (int64_t)hdb->cnt_dividefbp); + iprintf("cnt_mergefbp: %" PRIdMAX "\n", (int64_t)hdb->cnt_mergefbp); + iprintf("cnt_reducefbp: %" PRIdMAX "\n", (int64_t)hdb->cnt_reducefbp); + iprintf("cnt_appenddrp: %" PRIdMAX "\n", (int64_t)hdb->cnt_appenddrp); + iprintf("cnt_deferdrp: %" PRIdMAX "\n", (int64_t)hdb->cnt_deferdrp); + iprintf("cnt_flushdrp: %" PRIdMAX "\n", (int64_t)hdb->cnt_flushdrp); + iprintf("cnt_adjrecc: %" PRIdMAX "\n", (int64_t)hdb->cnt_adjrecc); + iprintf("cnt_defrag: %" PRIdMAX "\n", (int64_t)hdb->cnt_defrag); + iprintf("cnt_shiftrec: %" PRIdMAX "\n", (int64_t)hdb->cnt_shiftrec); + iprintf("cnt_trunc: %" PRIdMAX "\n", (int64_t)hdb->cnt_trunc); #endif } @@ -645,8 +645,8 @@ static int procwrite(const char *path, int rnum, int bnum, int apow, int fpow, if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tchdbrnum(hdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tchdbfsiz(hdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tchdbrnum(hdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tchdbfsiz(hdb)); mprint(hdb); sysprint(); if(!tchdbclose(hdb)){ @@ -720,8 +720,8 @@ static int procread(const char *path, bool mt, int rcnum, int xmsiz, int dfunit, if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tchdbrnum(hdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tchdbfsiz(hdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tchdbrnum(hdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tchdbfsiz(hdb)); mprint(hdb); sysprint(); if(!tchdbclose(hdb)){ @@ -782,8 +782,8 @@ static int procremove(const char *path, bool mt, int rcnum, int xmsiz, int dfuni if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tchdbrnum(hdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tchdbfsiz(hdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tchdbrnum(hdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tchdbfsiz(hdb)); mprint(hdb); sysprint(); if(!tchdbclose(hdb)){ @@ -938,8 +938,8 @@ static int procrcat(const char *path, int rnum, int bnum, int apow, int fpow, if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tchdbrnum(hdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tchdbfsiz(hdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tchdbrnum(hdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tchdbfsiz(hdb)); mprint(hdb); sysprint(); if(!tchdbclose(hdb)){ @@ -1699,8 +1699,8 @@ static int procmisc(const char *path, int rnum, bool mt, int opts, int omode){ eprint(hdb, __LINE__, "tchdbforeach"); err = true; } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tchdbrnum(hdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tchdbfsiz(hdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tchdbrnum(hdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tchdbfsiz(hdb)); mprint(hdb); sysprint(); if(!tchdbclose(hdb)){ @@ -2124,8 +2124,8 @@ static int procwicked(const char *path, int rnum, bool mt, int opts, int omode){ eprint(hdb, __LINE__, "(validation)"); err = true; } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tchdbrnum(hdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tchdbfsiz(hdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tchdbrnum(hdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tchdbfsiz(hdb)); mprint(hdb); sysprint(); tcmapdel(map); diff --git a/tcejdb/tctdb.c b/tcejdb/tctdb.c index 1a8f4ad..8b05c8a 100644 --- a/tcejdb/tctdb.c +++ b/tcejdb/tctdb.c @@ -961,7 +961,7 @@ bool tctdbqryproc(TDBQRY *qry, TDBQRYPROC proc, void *op) { } tclistdel(res); tcxstrprintf(qry->hint, "post treatment: get=%" PRIdMAX ", put=%" PRIdMAX ", out=%" PRIdMAX "\n", - (long long) getnum, (long long) putnum, (long long) outnum); + (int64_t) getnum, (int64_t) putnum, (int64_t) outnum); TDBUNLOCKMETHOD(tdb); return !err; } @@ -1591,7 +1591,7 @@ bool tctdbqryproc2(TDBQRY *qry, TDBQRYPROC proc, void *op) { } tclistdel(res); tcxstrprintf(qry->hint, "post treatment: get=%" PRIdMAX ", put=%" PRIdMAX ", out=%" PRIdMAX "\n", - (long long) getnum, (long long) putnum, (long long) outnum); + (int64_t) getnum, (int64_t) putnum, (int64_t) outnum); return !err; } @@ -3930,7 +3930,7 @@ static TCMAP *tctdbqryidxfetch(TDBQRY *qry, TDBCOND *cond, TDBIDX *idx) { tcmapdel(nmap); nmap = tctdbidxgetbyfts(tdb, idx, cond, hint); } - tcxstrprintf(hint, "auxiliary result set size: %" PRIdMAX "\n", (long long) TCMAPRNUM(nmap)); + tcxstrprintf(hint, "auxiliary result set size: %" PRIdMAX "\n", (int64_t) TCMAPRNUM(nmap)); return nmap; } @@ -5499,7 +5499,7 @@ TCMAP *tctdbidxgetbytokens(TCTDB *tdb, const TDBIDX *idx, const TCLIST *tokens, int step; TCREADVNUMBUF64(cbuf, tid, step); char pkbuf[TCNUMBUFSIZ]; - int pksiz = sprintf(pkbuf, "%" PRIdMAX "", (long long) tid); + int pksiz = sprintf(pkbuf, "%" PRIdMAX "", (int64_t) tid); if (cnt < 1) { tcmapput(res, pkbuf, pksiz, "", 0); } else if (wring) { @@ -5539,7 +5539,7 @@ TCMAP *tctdbidxgetbytokens(TCTDB *tdb, const TDBIDX *idx, const TCLIST *tokens, int step; TCREADVNUMBUF64(cbuf, tid, step); char pkbuf[TCNUMBUFSIZ]; - int pksiz = sprintf(pkbuf, "%" PRIdMAX "", (long long) tid); + int pksiz = sprintf(pkbuf, "%" PRIdMAX "", (int64_t) tid); if (cnt < 1) { tcmapput(res, pkbuf, pksiz, "", 0); } else if (wring) { @@ -5949,7 +5949,7 @@ static void tctdbidxgetbyftsunion(TDBIDX *idx, const TCLIST *tokens, bool sign, if (off == ocr->off + rem) { onum++; char pkbuf[TCNUMBUFSIZ]; - int pksiz = sprintf(pkbuf, "%" PRIdMAX "", (long long) pkid); + int pksiz = sprintf(pkbuf, "%" PRIdMAX "", (int64_t) pkid); if (ores) { int rsiz; if (tcmapget(ores, pkbuf, pksiz, &rsiz)) { @@ -6021,7 +6021,7 @@ static void tctdbidxgetbyftsunion(TDBIDX *idx, const TCLIST *tokens, bool sign, if (uniq) tcmapputkeep(uniq, pkbuf, pksiz, "", 0); } else { char numbuf[TCNUMBUFSIZ]; - int pksiz = sprintf(numbuf, "%" PRIdMAX "", (long long) pkid); + int pksiz = sprintf(numbuf, "%" PRIdMAX "", (int64_t) pkid); if (ores) { int rsiz; if (tcmapget(ores, numbuf, pksiz, &rsiz)) { @@ -6090,7 +6090,7 @@ static void tctdbidxgetbyftsunion(TDBIDX *idx, const TCLIST *tokens, bool sign, if (uniq) tcmapputkeep(uniq, pkbuf, pksiz, "", 0); } else { char numbuf[TCNUMBUFSIZ]; - int pksiz = sprintf(numbuf, "%" PRIdMAX "", (long long) pkid); + int pksiz = sprintf(numbuf, "%" PRIdMAX "", (int64_t) pkid); if (ores) { int rsiz; if (tcmapget(ores, numbuf, pksiz, &rsiz)) { @@ -6391,7 +6391,7 @@ void tctdbprintmeta(TCTDB *tdb) { wp += sprintf(wp, " opts=%u", tdb->opts); wp += sprintf(wp, " lcnum=%d", tdb->lcnum); wp += sprintf(wp, " ncnum=%d", tdb->ncnum); - wp += sprintf(wp, " iccmax=%" PRIdMAX "", (long long) tdb->iccmax); + wp += sprintf(wp, " iccmax=%" PRIdMAX "", (int64_t) tdb->iccmax); wp += sprintf(wp, " iccsync=%f", tdb->iccsync); wp += sprintf(wp, " idxs=%p", (void *) tdb->idxs); wp += sprintf(wp, " inum=%d", tdb->inum); diff --git a/tcejdb/tctmgr.c b/tcejdb/tctmgr.c index 4269909..2d54ee0 100644 --- a/tcejdb/tctmgr.c +++ b/tcejdb/tctmgr.c @@ -682,11 +682,11 @@ static int procinform(const char *path, int omode){ if(flags & TDBFOPEN) printf(" open"); if(flags & TDBFFATAL) printf(" fatal"); printf("\n"); - printf("bucket number: %" PRIuMAX "\n", (unsigned long long)tctdbbnum(tdb)); + printf("bucket number: %" PRIuMAX "\n", (uint64_t)tctdbbnum(tdb)); #ifndef NDEBUG if(tdb->hdb->cnt_writerec >= 0) - printf("used bucket number: %" PRIdMAX "\n", (long long)tctdbbnumused(tdb)); -#endif + printf("used bucket number: %" PRIdMAX "\n", (int64_t)tctdbbnumused(tdb)); +#endif printf("alignment: %u\n", tctdbalign(tdb)); printf("free block pool: %u\n", tctdbfbpmax(tdb)); printf("index number: %d\n", tctdbinum(tdb)); @@ -697,24 +697,24 @@ static int procinform(const char *path, int omode){ switch(idxp->type){ case TDBITLEXICAL: printf(" name=%s, type=lexical, rnum=%" PRIdMAX ", fsiz=%" PRIdMAX "\n", - idxp->name, (long long)tcbdbrnum(idxp->db), (long long)tcbdbfsiz(idxp->db)); + idxp->name, (int64_t)tcbdbrnum(idxp->db), (int64_t)tcbdbfsiz(idxp->db)); break; case TDBITDECIMAL: printf(" name=%s, type=decimal, rnum=%" PRIdMAX ", fsiz=%" PRIdMAX "\n", - idxp->name, (long long)tcbdbrnum(idxp->db), (long long)tcbdbfsiz(idxp->db)); + idxp->name, (int64_t)tcbdbrnum(idxp->db), (int64_t)tcbdbfsiz(idxp->db)); break; case TDBITTOKEN: printf(" name=%s, type=token, rnum=%" PRIdMAX ", fsiz=%" PRIdMAX "\n", - idxp->name, (long long)tcbdbrnum(idxp->db), (long long)tcbdbfsiz(idxp->db)); + idxp->name, (int64_t)tcbdbrnum(idxp->db), (int64_t)tcbdbfsiz(idxp->db)); break; case TDBITQGRAM: printf(" name=%s, type=qgram, rnum=%" PRIdMAX ", fsiz=%" PRIdMAX "\n", - idxp->name, (long long)tcbdbrnum(idxp->db), (long long)tcbdbfsiz(idxp->db)); + idxp->name, (int64_t)tcbdbrnum(idxp->db), (int64_t)tcbdbfsiz(idxp->db)); break; } } - printf("unique ID seed: %" PRIdMAX "\n", (long long)tctdbuidseed(tdb)); - printf("inode number: %" PRIdMAX "\n", (long long)tctdbinode(tdb)); + printf("unique ID seed: %" PRIdMAX "\n", (int64_t)tctdbuidseed(tdb)); + printf("inode number: %" PRIdMAX "\n", (int64_t)tctdbinode(tdb)); char date[48]; tcdatestrwww(tctdbmtime(tdb), INT_MAX, date); printf("modified time: %s\n", date); @@ -726,8 +726,8 @@ static int procinform(const char *path, int omode){ if(opts & TDBTTCBS) printf(" tcbs"); if(opts & TDBTEXCODEC) printf(" excodec"); printf("\n"); - printf("record number: %" PRIuMAX "\n", (unsigned long long)tctdbrnum(tdb)); - printf("file size: %" PRIuMAX "\n", (unsigned long long)tctdbfsiz(tdb)); + printf("record number: %" PRIuMAX "\n", (uint64_t)tctdbrnum(tdb)); + printf("file size: %" PRIuMAX "\n", (uint64_t)tctdbfsiz(tdb)); if(!tctdbclose(tdb)){ if(!err) printerr(tdb); err = true; @@ -751,7 +751,7 @@ static int procput(const char *path, const char *pkbuf, int pksiz, TCMAP *cols, bool err = false; char pknumbuf[TCNUMBUFSIZ]; if(pksiz < 1){ - pksiz = sprintf(pknumbuf, "%" PRIdMAX "", (long long)tctdbgenuid(tdb)); + pksiz = sprintf(pknumbuf, "%" PRIdMAX "", (int64_t)tctdbgenuid(tdb)); pkbuf = pknumbuf; } const char *vbuf; @@ -1209,7 +1209,7 @@ static int procimporttsv(const char *path, const char *file, int omode, bool sc) if(sc) tcstrutfnorm(line, TCUNSPACE | TCUNLOWER | TCUNNOACC | TCUNWIDTH); const char *pkey; if(*line == '\0'){ - sprintf(numbuf, "%" PRIdMAX "", (long long)tctdbgenuid(tdb)); + sprintf(numbuf, "%" PRIdMAX "", (int64_t)tctdbgenuid(tdb)); pkey = numbuf; } else { pkey = line; diff --git a/tcejdb/tctmttest.c b/tcejdb/tctmttest.c index b9e8162..e5adbae 100644 --- a/tcejdb/tctmttest.c +++ b/tcejdb/tctmttest.c @@ -635,8 +635,8 @@ static int procwrite(const char *path, int tnum, int rnum, int bnum, int apow, i } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tctdbrnum(tdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tctdbfsiz(tdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tctdbrnum(tdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tctdbfsiz(tdb)); sysprint(); if(!tctdbclose(tdb)){ eprint(tdb, __LINE__, "tctdbclose"); @@ -715,8 +715,8 @@ static int procread(const char *path, int tnum, int rcnum, int lcnum, int ncnum, } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tctdbrnum(tdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tctdbfsiz(tdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tctdbrnum(tdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tctdbfsiz(tdb)); sysprint(); if(!tctdbclose(tdb)){ eprint(tdb, __LINE__, "tctdbclose"); @@ -795,8 +795,8 @@ static int procremove(const char *path, int tnum, int rcnum, int lcnum, int ncnu } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tctdbrnum(tdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tctdbfsiz(tdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tctdbrnum(tdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tctdbfsiz(tdb)); sysprint(); if(!tctdbclose(tdb)){ eprint(tdb, __LINE__, "tctdbclose"); @@ -906,8 +906,8 @@ static int procwicked(const char *path, int tnum, int rnum, int opts, int omode) } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tctdbrnum(tdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tctdbfsiz(tdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tctdbrnum(tdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tctdbfsiz(tdb)); sysprint(); if(!tctdbclose(tdb)){ eprint(tdb, __LINE__, "tctdbclose"); @@ -1016,8 +1016,8 @@ static int proctypical(const char *path, int tnum, int rnum, int bnum, int apow, } } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tctdbrnum(tdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tctdbfsiz(tdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tctdbrnum(tdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tctdbfsiz(tdb)); sysprint(); if(!tctdbclose(tdb)){ eprint(tdb, __LINE__, "tctdbclose"); diff --git a/tcejdb/tcttest.c b/tcejdb/tcttest.c index 3cb43a6..407ea3d 100644 --- a/tcejdb/tcttest.c +++ b/tcejdb/tcttest.c @@ -693,8 +693,8 @@ static int procwrite(const char *path, int rnum, int bnum, int apow, int fpow, if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tctdbrnum(tdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tctdbfsiz(tdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tctdbrnum(tdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tctdbfsiz(tdb)); sysprint(); if(!tctdbclose(tdb)){ eprint(tdb, __LINE__, "tctdbclose"); @@ -758,8 +758,8 @@ static int procread(const char *path, bool mt, int rcnum, int lcnum, int ncnum, if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tctdbrnum(tdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tctdbfsiz(tdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tctdbrnum(tdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tctdbfsiz(tdb)); sysprint(); if(!tctdbclose(tdb)){ eprint(tdb, __LINE__, "tctdbclose"); @@ -820,8 +820,8 @@ static int procremove(const char *path, bool mt, int rcnum, int lcnum, int ncnum if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tctdbrnum(tdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tctdbfsiz(tdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tctdbrnum(tdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tctdbfsiz(tdb)); sysprint(); if(!tctdbclose(tdb)){ eprint(tdb, __LINE__, "tctdbclose"); @@ -1028,8 +1028,8 @@ static int procrcat(const char *path, int rnum, int bnum, int apow, int fpow, if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tctdbrnum(tdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tctdbfsiz(tdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tctdbrnum(tdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tctdbfsiz(tdb)); sysprint(); if(!tctdbclose(tdb)){ eprint(tdb, __LINE__, "tctdbclose"); @@ -1637,8 +1637,8 @@ static int procmisc(const char *path, int rnum, bool mt, int opts, int omode){ eprint(tdb, __LINE__, "tctdbforeach"); err = true; } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tctdbrnum(tdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tctdbfsiz(tdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tctdbrnum(tdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tctdbfsiz(tdb)); sysprint(); if(!tctdbclose(tdb)){ eprint(tdb, __LINE__, "tctdbclose"); @@ -2051,8 +2051,8 @@ static int procwicked(const char *path, int rnum, bool mt, int opts, int omode){ eprint(tdb, __LINE__, "tctdbsync"); err = true; } - iprintf("record number: %" PRIuMAX "\n", (unsigned long long)tctdbrnum(tdb)); - iprintf("size: %" PRIuMAX "\n", (unsigned long long)tctdbfsiz(tdb)); + iprintf("record number: %" PRIuMAX "\n", (uint64_t)tctdbrnum(tdb)); + iprintf("size: %" PRIuMAX "\n", (uint64_t)tctdbfsiz(tdb)); sysprint(); if(!tctdbclose(tdb)){ eprint(tdb, __LINE__, "tctdbclose"); diff --git a/tcejdb/tcucodec.c b/tcejdb/tcucodec.c index 6cff393..3400e29 100644 --- a/tcejdb/tcucodec.c +++ b/tcejdb/tcucodec.c @@ -19,7 +19,7 @@ /* global variables */ -const char *g_progname; // program name +const char *g_progname; // program name /* function prototypes */ @@ -47,7 +47,7 @@ static int procurl(const char *ibuf, int isiz, bool dec, bool br, const char *ba static int procbase(const char *ibuf, int isiz, bool dec); static int procquote(const char *ibuf, int isiz, bool dec); static int procmime(const char *ibuf, int isiz, bool dec, const char *ename, bool qb, bool on, - bool hd, bool bd, int part); + bool hd, bool bd, int part); static int prochex(const char *ibuf, int isiz, bool dec); static int procpack(const char *ibuf, int isiz, bool dec, bool bwt); static int proctcbs(const char *ibuf, int isiz, bool dec); @@ -62,1298 +62,1261 @@ static int procdate(const char *str, int jl, bool wf, bool rf); static int proctmpl(const char *ibuf, int isiz, TCMAP *vars); static int procconf(int mode); - /* main routine */ -int main(int argc, char **argv){ - g_progname = argv[0]; - if(argc < 2) usage(); - int rv = 0; - if(!strcmp(argv[1], "url")){ - rv = runurl(argc, argv); - } else if(!strcmp(argv[1], "base")){ - rv = runbase(argc, argv); - } else if(!strcmp(argv[1], "quote")){ - rv = runquote(argc, argv); - } else if(!strcmp(argv[1], "mime")){ - rv = runmime(argc, argv); - } else if(!strcmp(argv[1], "hex")){ - rv = runhex(argc, argv); - } else if(!strcmp(argv[1], "pack")){ - rv = runpack(argc, argv); - } else if(!strcmp(argv[1], "tcbs")){ - rv = runtcbs(argc, argv); - } else if(!strcmp(argv[1], "zlib")){ - rv = runzlib(argc, argv); - } else if(!strcmp(argv[1], "bzip")){ - rv = runbzip(argc, argv); - } else if(!strcmp(argv[1], "xml")){ - rv = runxml(argc, argv); - } else if(!strcmp(argv[1], "cstr")){ - rv = runcstr(argc, argv); - } else if(!strcmp(argv[1], "ucs")){ - rv = runucs(argc, argv); - } else if(!strcmp(argv[1], "hash")){ - rv = runhash(argc, argv); - } else if(!strcmp(argv[1], "cipher")){ - rv = runcipher(argc, argv); - } else if(!strcmp(argv[1], "date")){ - rv = rundate(argc, argv); - } else if(!strcmp(argv[1], "tmpl")){ - rv = runtmpl(argc, argv); - } else if(!strcmp(argv[1], "conf")){ - rv = runconf(argc, argv); - } else { - usage(); - } - return rv; +int main(int argc, char **argv) { + g_progname = argv[0]; + if (argc < 2) usage(); + int rv = 0; + if (!strcmp(argv[1], "url")) { + rv = runurl(argc, argv); + } else if (!strcmp(argv[1], "base")) { + rv = runbase(argc, argv); + } else if (!strcmp(argv[1], "quote")) { + rv = runquote(argc, argv); + } else if (!strcmp(argv[1], "mime")) { + rv = runmime(argc, argv); + } else if (!strcmp(argv[1], "hex")) { + rv = runhex(argc, argv); + } else if (!strcmp(argv[1], "pack")) { + rv = runpack(argc, argv); + } else if (!strcmp(argv[1], "tcbs")) { + rv = runtcbs(argc, argv); + } else if (!strcmp(argv[1], "zlib")) { + rv = runzlib(argc, argv); + } else if (!strcmp(argv[1], "bzip")) { + rv = runbzip(argc, argv); + } else if (!strcmp(argv[1], "xml")) { + rv = runxml(argc, argv); + } else if (!strcmp(argv[1], "cstr")) { + rv = runcstr(argc, argv); + } else if (!strcmp(argv[1], "ucs")) { + rv = runucs(argc, argv); + } else if (!strcmp(argv[1], "hash")) { + rv = runhash(argc, argv); + } else if (!strcmp(argv[1], "cipher")) { + rv = runcipher(argc, argv); + } else if (!strcmp(argv[1], "date")) { + rv = rundate(argc, argv); + } else if (!strcmp(argv[1], "tmpl")) { + rv = runtmpl(argc, argv); + } else if (!strcmp(argv[1], "conf")) { + rv = runconf(argc, argv); + } else { + usage(); + } + return rv; } - /* print the usage and exit */ -static void usage(void){ - fprintf(stderr, "%s: popular encoders and decoders of Tokyo Cabinet\n", g_progname); - fprintf(stderr, "\n"); - fprintf(stderr, "usage:\n"); - fprintf(stderr, " %s url [-d] [-br] [-rs base] [file]\n", g_progname); - fprintf(stderr, " %s base [-d] [file]\n", g_progname); - fprintf(stderr, " %s quote [-d] [file]\n", g_progname); - fprintf(stderr, " %s mime [-d] [-en name] [-q] [-on] [-hd] [-bd] [-part num] [file]\n", - g_progname); - fprintf(stderr, " %s hex [-d] [file]\n", g_progname); - fprintf(stderr, " %s pack [-d] [-bwt] [file]\n", g_progname); - fprintf(stderr, " %s tcbs [-d] [file]\n", g_progname); - fprintf(stderr, " %s zlib [-d] [-gz] [file]\n", g_progname); - fprintf(stderr, " %s bzip [-d] [file]\n", g_progname); - fprintf(stderr, " %s xml [-d] [-br] [file]\n", g_progname); - fprintf(stderr, " %s cstr [-d] [-js] [file]\n", g_progname); - fprintf(stderr, " %s ucs [-d] [-un] [file]\n", g_progname); - fprintf(stderr, " %s hash [-crc] [-ch num] [file]\n", g_progname); - fprintf(stderr, " %s cipher [-key str] [file]\n", g_progname); - fprintf(stderr, " %s date [-ds str] [-jl num] [-wf] [-rf]\n", g_progname); - fprintf(stderr, " %s tmpl [-var name val] [file]\n", g_progname); - fprintf(stderr, " %s conf [-v|-i|-l|-p]\n", g_progname); - fprintf(stderr, "\n"); - exit(1); +static void usage(void) { + fprintf(stderr, "%s: popular encoders and decoders of Tokyo Cabinet\n", g_progname); + fprintf(stderr, "\n"); + fprintf(stderr, "usage:\n"); + fprintf(stderr, " %s url [-d] [-br] [-rs base] [file]\n", g_progname); + fprintf(stderr, " %s base [-d] [file]\n", g_progname); + fprintf(stderr, " %s quote [-d] [file]\n", g_progname); + fprintf(stderr, " %s mime [-d] [-en name] [-q] [-on] [-hd] [-bd] [-part num] [file]\n", + g_progname); + fprintf(stderr, " %s hex [-d] [file]\n", g_progname); + fprintf(stderr, " %s pack [-d] [-bwt] [file]\n", g_progname); + fprintf(stderr, " %s tcbs [-d] [file]\n", g_progname); + fprintf(stderr, " %s zlib [-d] [-gz] [file]\n", g_progname); + fprintf(stderr, " %s bzip [-d] [file]\n", g_progname); + fprintf(stderr, " %s xml [-d] [-br] [file]\n", g_progname); + fprintf(stderr, " %s cstr [-d] [-js] [file]\n", g_progname); + fprintf(stderr, " %s ucs [-d] [-un] [file]\n", g_progname); + fprintf(stderr, " %s hash [-crc] [-ch num] [file]\n", g_progname); + fprintf(stderr, " %s cipher [-key str] [file]\n", g_progname); + fprintf(stderr, " %s date [-ds str] [-jl num] [-wf] [-rf]\n", g_progname); + fprintf(stderr, " %s tmpl [-var name val] [file]\n", g_progname); + fprintf(stderr, " %s conf [-v|-i|-l|-p]\n", g_progname); + fprintf(stderr, "\n"); + exit(1); } - /* print formatted error string */ -static void eprintf(const char *format, ...){ - va_list ap; - va_start(ap, format); - fprintf(stderr, "%s: ", g_progname); - vfprintf(stderr, format, ap); - fprintf(stderr, "\n"); - va_end(ap); +static void eprintf(const char *format, ...) { + va_list ap; + va_start(ap, format); + fprintf(stderr, "%s: ", g_progname); + vfprintf(stderr, format, ap); + fprintf(stderr, "\n"); + va_end(ap); } - /* parse arguments of url command */ -static int runurl(int argc, char **argv){ - char *path = NULL; - bool dec = false; - bool br = false; - char *base = NULL; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-d")){ - dec = true; - } else if(!strcmp(argv[i], "-br")){ - br = true; - } else if(!strcmp(argv[i], "-rs")){ - if(++i >= argc) usage(); - base = argv[i]; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runurl(int argc, char **argv) { + char *path = NULL; + bool dec = false; + bool br = false; + char *base = NULL; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-d")) { + dec = true; + } else if (!strcmp(argv[i], "-br")) { + br = true; + } else if (!strcmp(argv[i], "-rs")) { + if (++i >= argc) usage(); + base = argv[i]; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - int rv = procurl(ibuf, isiz, dec, br, base); - if(path && path[0] == '@' && !br) printf("\n"); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + int rv = procurl(ibuf, isiz, dec, br, base); + if (path && path[0] == '@' && !br) printf("\n"); + tcfree(ibuf); + return rv; } - /* parse arguments of base command */ -static int runbase(int argc, char **argv){ - char *path = NULL; - bool dec = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-d")){ - dec = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runbase(int argc, char **argv) { + char *path = NULL; + bool dec = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-d")) { + dec = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - int rv = procbase(ibuf, isiz, dec); - if(path && path[0] == '@') printf("\n"); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + int rv = procbase(ibuf, isiz, dec); + if (path && path[0] == '@') printf("\n"); + tcfree(ibuf); + return rv; } - /* parse arguments of quote command */ -static int runquote(int argc, char **argv){ - char *path = NULL; - bool dec = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-d")){ - dec = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runquote(int argc, char **argv) { + char *path = NULL; + bool dec = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-d")) { + dec = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - int rv = procquote(ibuf, isiz, dec); - if(path && path[0] == '@') printf("\n"); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + int rv = procquote(ibuf, isiz, dec); + if (path && path[0] == '@') printf("\n"); + tcfree(ibuf); + return rv; } - /* parse arguments of mime command */ -static int runmime(int argc, char **argv){ - char *path = NULL; - bool dec = false; - char *ename = NULL; - bool qb = false; - bool on = false; - bool hd = false; - bool bd = false; - int part = -1; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-d")){ - dec = true; - } else if(!strcmp(argv[i], "-en")){ - if(++i >= argc) usage(); - ename = argv[i]; - } else if(!strcmp(argv[i], "-q")){ - qb = true; - } else if(!strcmp(argv[i], "-on")){ - on = true; - } else if(!strcmp(argv[i], "-hd")){ - hd = true; - } else if(!strcmp(argv[i], "-bd")){ - bd = true; - } else if(!strcmp(argv[i], "-part")){ - if(++i >= argc) usage(); - part = tcatoix(argv[i]); - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runmime(int argc, char **argv) { + char *path = NULL; + bool dec = false; + char *ename = NULL; + bool qb = false; + bool on = false; + bool hd = false; + bool bd = false; + int part = -1; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-d")) { + dec = true; + } else if (!strcmp(argv[i], "-en")) { + if (++i >= argc) usage(); + ename = argv[i]; + } else if (!strcmp(argv[i], "-q")) { + qb = true; + } else if (!strcmp(argv[i], "-on")) { + on = true; + } else if (!strcmp(argv[i], "-hd")) { + hd = true; + } else if (!strcmp(argv[i], "-bd")) { + bd = true; + } else if (!strcmp(argv[i], "-part")) { + if (++i >= argc) usage(); + part = tcatoix(argv[i]); + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - if(!ename) ename = "UTF-8"; - int rv = procmime(ibuf, isiz, dec, ename, qb, on, hd, bd, part); - if(path && path[0] == '@') printf("\n"); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + if (!ename) ename = "UTF-8"; + int rv = procmime(ibuf, isiz, dec, ename, qb, on, hd, bd, part); + if (path && path[0] == '@') printf("\n"); + tcfree(ibuf); + return rv; } - /* parse arguments of hex command */ -static int runhex(int argc, char **argv){ - char *path = NULL; - bool dec = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-d")){ - dec = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runhex(int argc, char **argv) { + char *path = NULL; + bool dec = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-d")) { + dec = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - int rv = prochex(ibuf, isiz, dec); - if(path && path[0] == '@') printf("\n"); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + int rv = prochex(ibuf, isiz, dec); + if (path && path[0] == '@') printf("\n"); + tcfree(ibuf); + return rv; } - /* parse arguments of pack command */ -static int runpack(int argc, char **argv){ - char *path = NULL; - bool dec = false; - bool bwt = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-d")){ - dec = true; - } else if(!strcmp(argv[i], "-bwt")){ - bwt = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runpack(int argc, char **argv) { + char *path = NULL; + bool dec = false; + bool bwt = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-d")) { + dec = true; + } else if (!strcmp(argv[i], "-bwt")) { + bwt = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - int rv = procpack(ibuf, isiz, dec, bwt); - if(path && path[0] == '@') printf("\n"); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + int rv = procpack(ibuf, isiz, dec, bwt); + if (path && path[0] == '@') printf("\n"); + tcfree(ibuf); + return rv; } - /* parse arguments of tcbs command */ -static int runtcbs(int argc, char **argv){ - char *path = NULL; - bool dec = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-d")){ - dec = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runtcbs(int argc, char **argv) { + char *path = NULL; + bool dec = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-d")) { + dec = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - int rv = proctcbs(ibuf, isiz, dec); - if(path && path[0] == '@') printf("\n"); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + int rv = proctcbs(ibuf, isiz, dec); + if (path && path[0] == '@') printf("\n"); + tcfree(ibuf); + return rv; } - /* parse arguments of zlib command */ -static int runzlib(int argc, char **argv){ - char *path = NULL; - bool dec = false; - bool gz = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-d")){ - dec = true; - } else if(!strcmp(argv[i], "-gz")){ - gz = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runzlib(int argc, char **argv) { + char *path = NULL; + bool dec = false; + bool gz = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-d")) { + dec = true; + } else if (!strcmp(argv[i], "-gz")) { + gz = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - int rv = proczlib(ibuf, isiz, dec, gz); - if(path && path[0] == '@') printf("\n"); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + int rv = proczlib(ibuf, isiz, dec, gz); + if (path && path[0] == '@') printf("\n"); + tcfree(ibuf); + return rv; } - /* parse arguments of bzip command */ -static int runbzip(int argc, char **argv){ - char *path = NULL; - bool dec = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-d")){ - dec = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runbzip(int argc, char **argv) { + char *path = NULL; + bool dec = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-d")) { + dec = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - int rv = procbzip(ibuf, isiz, dec); - if(path && path[0] == '@') printf("\n"); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + int rv = procbzip(ibuf, isiz, dec); + if (path && path[0] == '@') printf("\n"); + tcfree(ibuf); + return rv; } - /* parse arguments of xml command */ -static int runxml(int argc, char **argv){ - char *path = NULL; - bool dec = false; - bool br = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-d")){ - dec = true; - } else if(!strcmp(argv[i], "-br")){ - br = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runxml(int argc, char **argv) { + char *path = NULL; + bool dec = false; + bool br = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-d")) { + dec = true; + } else if (!strcmp(argv[i], "-br")) { + br = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - int rv = procxml(ibuf, isiz, dec, br); - if(path && path[0] == '@') printf("\n"); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + int rv = procxml(ibuf, isiz, dec, br); + if (path && path[0] == '@') printf("\n"); + tcfree(ibuf); + return rv; } - /* parse arguments of cstr command */ -static int runcstr(int argc, char **argv){ - char *path = NULL; - bool dec = false; - bool js = false; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-d")){ - dec = true; - } else if(!strcmp(argv[i], "-js")){ - js = true; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runcstr(int argc, char **argv) { + char *path = NULL; + bool dec = false; + bool js = false; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-d")) { + dec = true; + } else if (!strcmp(argv[i], "-js")) { + js = true; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - int rv = proccstr(ibuf, isiz, dec, js); - if(path && path[0] == '@') printf("\n"); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + int rv = proccstr(ibuf, isiz, dec, js); + if (path && path[0] == '@') printf("\n"); + tcfree(ibuf); + return rv; } - /* parse arguments of ucs command */ -static int runucs(int argc, char **argv){ - char *path = NULL; - bool dec = false; - bool un = false; - char *kw = NULL; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-d")){ - dec = true; - } else if(!strcmp(argv[i], "-un")){ - un = true; - } else if(!strcmp(argv[i], "-kw")){ - if(++i >= argc) usage(); - kw = argv[i]; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runucs(int argc, char **argv) { + char *path = NULL; + bool dec = false; + bool un = false; + char *kw = NULL; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-d")) { + dec = true; + } else if (!strcmp(argv[i], "-un")) { + un = true; + } else if (!strcmp(argv[i], "-kw")) { + if (++i >= argc) usage(); + kw = argv[i]; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - int rv = procucs(ibuf, isiz, dec, un, kw); - if(path && path[0] == '@') printf("\n"); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + int rv = procucs(ibuf, isiz, dec, un, kw); + if (path && path[0] == '@') printf("\n"); + tcfree(ibuf); + return rv; } - /* parse arguments of hash command */ -static int runhash(int argc, char **argv){ - char *path = NULL; - bool crc = false; - int ch = 0; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-crc")){ - crc = true; - } else if(!strcmp(argv[i], "-ch")){ - if(++i >= argc) usage(); - ch = tcatoix(argv[i]); - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runhash(int argc, char **argv) { + char *path = NULL; + bool crc = false; + int ch = 0; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-crc")) { + crc = true; + } else if (!strcmp(argv[i], "-ch")) { + if (++i >= argc) usage(); + ch = tcatoix(argv[i]); + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - int rv = prochash(ibuf, isiz, crc, ch); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + int rv = prochash(ibuf, isiz, crc, ch); + tcfree(ibuf); + return rv; } - /* parse arguments of cipher command */ -static int runcipher(int argc, char **argv){ - char *path = NULL; - char *key = NULL; - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-key")){ - if(++i >= argc) usage(); - key = argv[i]; - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runcipher(int argc, char **argv) { + char *path = NULL; + char *key = NULL; + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-key")) { + if (++i >= argc) usage(); + key = argv[i]; + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - int rv = proccipher(ibuf, isiz, key); - if(path && path[0] == '@') printf("\n"); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + int rv = proccipher(ibuf, isiz, key); + if (path && path[0] == '@') printf("\n"); + tcfree(ibuf); + return rv; } - /* parse arguments of date command */ -static int rundate(int argc, char **argv){ - char *str = NULL; - int jl = INT_MAX; - bool wf = false; - bool rf = false; - for(int i = 2; i < argc; i++){ - if(argv[i][0] == '-'){ - if(!strcmp(argv[i], "-ds")){ - if(++i >= argc) usage(); - str = argv[i]; - } else if(!strcmp(argv[i], "-jl")){ - if(++i >= argc) usage(); - jl = tcatoix(argv[i]); - } else if(!strcmp(argv[i], "-wf")){ - wf = true; - } else if(!strcmp(argv[i], "-rf")){ - rf = true; - } else { - usage(); - } - } else { - usage(); +static int rundate(int argc, char **argv) { + char *str = NULL; + int jl = INT_MAX; + bool wf = false; + bool rf = false; + for (int i = 2; i < argc; i++) { + if (argv[i][0] == '-') { + if (!strcmp(argv[i], "-ds")) { + if (++i >= argc) usage(); + str = argv[i]; + } else if (!strcmp(argv[i], "-jl")) { + if (++i >= argc) usage(); + jl = tcatoix(argv[i]); + } else if (!strcmp(argv[i], "-wf")) { + wf = true; + } else if (!strcmp(argv[i], "-rf")) { + rf = true; + } else { + usage(); + } + } else { + usage(); + } } - } - int rv = procdate(str, jl, wf, rf); - return rv; + int rv = procdate(str, jl, wf, rf); + return rv; } - /* parse arguments of tmpl command */ -static int runtmpl(int argc, char **argv){ - char *path = NULL; - TCMAP *vars = tcmpoolmapnew(tcmpoolglobal()); - for(int i = 2; i < argc; i++){ - if(!path && argv[i][0] == '-'){ - if(!strcmp(argv[i], "-var")){ - if(++i >= argc) usage(); - const char *name = argv[i]; - if(++i >= argc) usage(); - const char *value = argv[i]; - tcmapput2(vars, name, value); - } else { - usage(); - } - } else if(!path){ - path = argv[i]; +static int runtmpl(int argc, char **argv) { + char *path = NULL; + TCMAP *vars = tcmpoolmapnew(tcmpoolglobal()); + for (int i = 2; i < argc; i++) { + if (!path && argv[i][0] == '-') { + if (!strcmp(argv[i], "-var")) { + if (++i >= argc) usage(); + const char *name = argv[i]; + if (++i >= argc) usage(); + const char *value = argv[i]; + tcmapput2(vars, name, value); + } else { + usage(); + } + } else if (!path) { + path = argv[i]; + } else { + usage(); + } + } + char *ibuf; + int isiz; + if (path && path[0] == '@') { + isiz = strlen(path) - 1; + ibuf = tcmemdup(path + 1, isiz); } else { - usage(); - } - } - char *ibuf; - int isiz; - if(path && path[0] == '@'){ - isiz = strlen(path) - 1; - ibuf = tcmemdup(path + 1, isiz); - } else { - ibuf = tcreadfile(path, -1, &isiz); - } - if(!ibuf){ - eprintf("%s: cannot open", path ? path : "(stdin)"); - return 1; - } - int rv = proctmpl(ibuf, isiz, vars); - if(path && path[0] == '@') printf("\n"); - tcfree(ibuf); - return rv; + ibuf = tcreadfile(path, -1, &isiz); + } + if (!ibuf) { + eprintf("%s: cannot open", path ? path : "(stdin)"); + return 1; + } + int rv = proctmpl(ibuf, isiz, vars); + if (path && path[0] == '@') printf("\n"); + tcfree(ibuf); + return rv; } - /* parse arguments of conf command */ -static int runconf(int argc, char **argv){ - int mode = 0; - for(int i = 2; i < argc; i++){ - if(argv[i][0] == '-'){ - if(!strcmp(argv[i], "-v")){ - mode = 'v'; - } else if(!strcmp(argv[i], "-i")){ - mode = 'i'; - } else if(!strcmp(argv[i], "-l")){ - mode = 'l'; - } else if(!strcmp(argv[i], "-p")){ - mode = 'p'; - } else { - usage(); - } - } else { - usage(); +static int runconf(int argc, char **argv) { + int mode = 0; + for (int i = 2; i < argc; i++) { + if (argv[i][0] == '-') { + if (!strcmp(argv[i], "-v")) { + mode = 'v'; + } else if (!strcmp(argv[i], "-i")) { + mode = 'i'; + } else if (!strcmp(argv[i], "-l")) { + mode = 'l'; + } else if (!strcmp(argv[i], "-p")) { + mode = 'p'; + } else { + usage(); + } + } else { + usage(); + } } - } - int rv = procconf(mode); - return rv; + int rv = procconf(mode); + return rv; } - /* perform url command */ -static int procurl(const char *ibuf, int isiz, bool dec, bool br, const char *base){ - if(base){ - char *obuf = tcurlresolve(base, ibuf); - printf("%s", obuf); - tcfree(obuf); - } else if(br){ - TCMAP *elems = tcurlbreak(ibuf); - const char *elem; - if((elem = tcmapget2(elems, "self")) != NULL) printf("self: %s\n", elem); - if((elem = tcmapget2(elems, "scheme")) != NULL) printf("scheme: %s\n", elem); - if((elem = tcmapget2(elems, "host")) != NULL) printf("host: %s\n", elem); - if((elem = tcmapget2(elems, "port")) != NULL) printf("port: %s\n", elem); - if((elem = tcmapget2(elems, "authority")) != NULL) printf("authority: %s\n", elem); - if((elem = tcmapget2(elems, "path")) != NULL) printf("path: %s\n", elem); - if((elem = tcmapget2(elems, "file")) != NULL) printf("file: %s\n", elem); - if((elem = tcmapget2(elems, "query")) != NULL) printf("query: %s\n", elem); - if((elem = tcmapget2(elems, "fragment")) != NULL) printf("fragment: %s\n", elem); - tcmapdel(elems); - } else if(dec){ - int osiz; - char *obuf = tcurldecode(ibuf, &osiz); - fwrite(obuf, 1, osiz, stdout); - tcfree(obuf); - } else { - char *obuf = tcurlencode(ibuf, isiz); - fwrite(obuf, 1, strlen(obuf), stdout); - tcfree(obuf); - } - return 0; +static int procurl(const char *ibuf, int isiz, bool dec, bool br, const char *base) { + if (base) { + char *obuf = tcurlresolve(base, ibuf); + printf("%s", obuf); + tcfree(obuf); + } else if (br) { + TCMAP *elems = tcurlbreak(ibuf); + const char *elem; + if ((elem = tcmapget2(elems, "self")) != NULL) printf("self: %s\n", elem); + if ((elem = tcmapget2(elems, "scheme")) != NULL) printf("scheme: %s\n", elem); + if ((elem = tcmapget2(elems, "host")) != NULL) printf("host: %s\n", elem); + if ((elem = tcmapget2(elems, "port")) != NULL) printf("port: %s\n", elem); + if ((elem = tcmapget2(elems, "authority")) != NULL) printf("authority: %s\n", elem); + if ((elem = tcmapget2(elems, "path")) != NULL) printf("path: %s\n", elem); + if ((elem = tcmapget2(elems, "file")) != NULL) printf("file: %s\n", elem); + if ((elem = tcmapget2(elems, "query")) != NULL) printf("query: %s\n", elem); + if ((elem = tcmapget2(elems, "fragment")) != NULL) printf("fragment: %s\n", elem); + tcmapdel(elems); + } else if (dec) { + int osiz; + char *obuf = tcurldecode(ibuf, &osiz); + fwrite(obuf, 1, osiz, stdout); + tcfree(obuf); + } else { + char *obuf = tcurlencode(ibuf, isiz); + fwrite(obuf, 1, strlen(obuf), stdout); + tcfree(obuf); + } + return 0; } - /* perform base command */ -static int procbase(const char *ibuf, int isiz, bool dec){ - if(dec){ - int osiz; - char *obuf = tcbasedecode(ibuf, &osiz); - fwrite(obuf, 1, osiz, stdout); - tcfree(obuf); - } else { - char *obuf = tcbaseencode(ibuf, isiz); - fwrite(obuf, 1, strlen(obuf), stdout); - tcfree(obuf); - } - return 0; +static int procbase(const char *ibuf, int isiz, bool dec) { + if (dec) { + int osiz; + char *obuf = tcbasedecode(ibuf, &osiz); + fwrite(obuf, 1, osiz, stdout); + tcfree(obuf); + } else { + char *obuf = tcbaseencode(ibuf, isiz); + fwrite(obuf, 1, strlen(obuf), stdout); + tcfree(obuf); + } + return 0; } - /* perform quote command */ -static int procquote(const char *ibuf, int isiz, bool dec){ - if(dec){ - int osiz; - char *obuf = tcquotedecode(ibuf, &osiz); - fwrite(obuf, 1, osiz, stdout); - tcfree(obuf); - } else { - char *obuf = tcquoteencode(ibuf, isiz); - fwrite(obuf, 1, strlen(obuf), stdout); - tcfree(obuf); - } - return 0; +static int procquote(const char *ibuf, int isiz, bool dec) { + if (dec) { + int osiz; + char *obuf = tcquotedecode(ibuf, &osiz); + fwrite(obuf, 1, osiz, stdout); + tcfree(obuf); + } else { + char *obuf = tcquoteencode(ibuf, isiz); + fwrite(obuf, 1, strlen(obuf), stdout); + tcfree(obuf); + } + return 0; } - /* perform mime command */ static int procmime(const char *ibuf, int isiz, bool dec, const char *ename, bool qb, bool on, - bool hd, bool bd, int part){ - if(hd || bd || part > 0){ - TCMAP *hmap = tcmapnew2(16); - int osiz; - char *obuf = tcmimebreak(ibuf, isiz, hmap, &osiz); - if(part > 0){ - const char *value; - if(!(value = tcmapget2(hmap, "TYPE")) || !tcstrifwm(value, "multipart/") || - !(value = tcmapget2(hmap, "BOUNDARY"))){ - eprintf("not multipart"); - } else { - TCLIST *parts = tcmimeparts(obuf, osiz, value); - if(part <= tclistnum(parts)){ - int bsiz; - const char *body = tclistval(parts, part - 1, &bsiz); - fwrite(body, 1, bsiz, stdout); + bool hd, bool bd, int part) { + if (hd || bd || part > 0) { + TCMAP *hmap = tcmapnew2(16); + int osiz; + char *obuf = tcmimebreak(ibuf, isiz, hmap, &osiz); + if (part > 0) { + const char *value; + if (!(value = tcmapget2(hmap, "TYPE")) || !tcstrifwm(value, "multipart/") || + !(value = tcmapget2(hmap, "BOUNDARY"))) { + eprintf("not multipart"); + } else { + TCLIST *parts = tcmimeparts(obuf, osiz, value); + if (part <= tclistnum(parts)) { + int bsiz; + const char *body = tclistval(parts, part - 1, &bsiz); + fwrite(body, 1, bsiz, stdout); + } else { + eprintf("no such part"); + } + tclistdel(parts); + } } else { - eprintf("no such part"); + if (hd) { + TCLIST *names = tcmapkeys(hmap); + tclistsort(names); + int num = tclistnum(names); + for (int i = 0; i < num; i++) { + const char *name = tclistval2(names, i); + printf("%s\t%s\n", name, tcmapget2(hmap, name)); + } + tclistdel(names); + if (bd) putchar('\n'); + } + if (bd) fwrite(obuf, 1, osiz, stdout); } - tclistdel(parts); - } - } else { - if(hd){ - TCLIST *names = tcmapkeys(hmap); - tclistsort(names); - int num = tclistnum(names); - for(int i = 0; i < num; i++){ - const char *name = tclistval2(names, i); - printf("%s\t%s\n", name, tcmapget2(hmap, name)); + tcfree(obuf); + tcmapdel(hmap); + } else if (dec) { + char ebuf[32]; + char *obuf = tcmimedecode(ibuf, ebuf); + if (on) { + fwrite(ebuf, 1, strlen(ebuf), stdout); + } else { + fwrite(obuf, 1, strlen(obuf), stdout); } - tclistdel(names); - if(bd) putchar('\n'); - } - if(bd) fwrite(obuf, 1, osiz, stdout); - } - tcfree(obuf); - tcmapdel(hmap); - } else if(dec){ - char ebuf[32]; - char *obuf = tcmimedecode(ibuf, ebuf); - if(on){ - fwrite(ebuf, 1, strlen(ebuf), stdout); + tcfree(obuf); } else { - fwrite(obuf, 1, strlen(obuf), stdout); + char *obuf = tcmimeencode(ibuf, ename, !qb); + fwrite(obuf, 1, strlen(obuf), stdout); + tcfree(obuf); } - tcfree(obuf); - } else { - char *obuf = tcmimeencode(ibuf, ename, !qb); - fwrite(obuf, 1, strlen(obuf), stdout); - tcfree(obuf); - } - return 0; + return 0; } - /* perform hex command */ -static int prochex(const char *ibuf, int isiz, bool dec){ - if(dec){ - int osiz; - char *obuf = tchexdecode(ibuf, &osiz); - fwrite(obuf, 1, osiz, stdout); - tcfree(obuf); - } else { - char *obuf = tchexencode(ibuf, isiz); - fwrite(obuf, 1, strlen(obuf), stdout); - tcfree(obuf); - } - return 0; +static int prochex(const char *ibuf, int isiz, bool dec) { + if (dec) { + int osiz; + char *obuf = tchexdecode(ibuf, &osiz); + fwrite(obuf, 1, osiz, stdout); + tcfree(obuf); + } else { + char *obuf = tchexencode(ibuf, isiz); + fwrite(obuf, 1, strlen(obuf), stdout); + tcfree(obuf); + } + return 0; } - /* perform pack command */ -static int procpack(const char *ibuf, int isiz, bool dec, bool bwt){ - if(dec){ - int osiz; - char *obuf = tcpackdecode(ibuf, isiz, &osiz); - if(bwt && osiz > 0){ - int idx, step; - TCREADVNUMBUF(obuf, idx, step); - char *tbuf = tcbwtdecode(obuf + step, osiz - step, idx); - fwrite(tbuf, 1, osiz - step, stdout); - tcfree(tbuf); +static int procpack(const char *ibuf, int isiz, bool dec, bool bwt) { + if (dec) { + int osiz; + char *obuf = tcpackdecode(ibuf, isiz, &osiz); + if (bwt && osiz > 0) { + int idx, step; + TCREADVNUMBUF(obuf, idx, step); + char *tbuf = tcbwtdecode(obuf + step, osiz - step, idx); + fwrite(tbuf, 1, osiz - step, stdout); + tcfree(tbuf); + } else { + fwrite(obuf, 1, osiz, stdout); + } + tcfree(obuf); } else { - fwrite(obuf, 1, osiz, stdout); + char *tbuf = NULL; + if (bwt) { + int idx; + tbuf = tcbwtencode(ibuf, isiz, &idx); + char vnumbuf[sizeof (int) + 1]; + int step; + TCSETVNUMBUF(step, vnumbuf, idx); + tbuf = tcrealloc(tbuf, isiz + step + 1); + memmove(tbuf + step, tbuf, isiz); + memcpy(tbuf, vnumbuf, step); + isiz += step; + ibuf = tbuf; + } + int osiz; + char *obuf = tcpackencode(ibuf, isiz, &osiz); + fwrite(obuf, 1, osiz, stdout); + tcfree(obuf); + tcfree(tbuf); } - tcfree(obuf); - } else { - char *tbuf = NULL; - if(bwt){ - int idx; - tbuf = tcbwtencode(ibuf, isiz, &idx); - char vnumbuf[sizeof(int)+1]; - int step; - TCSETVNUMBUF(step, vnumbuf, idx); - tbuf = tcrealloc(tbuf, isiz + step + 1); - memmove(tbuf + step, tbuf, isiz); - memcpy(tbuf, vnumbuf, step); - isiz += step; - ibuf = tbuf; - } - int osiz; - char *obuf = tcpackencode(ibuf, isiz, &osiz); - fwrite(obuf, 1, osiz, stdout); - tcfree(obuf); - tcfree(tbuf); - } - return 0; + return 0; } - /* perform tcbs command */ -static int proctcbs(const char *ibuf, int isiz, bool dec){ - if(dec){ - int osiz; - char *obuf = tcbsdecode(ibuf, isiz, &osiz); - fwrite(obuf, 1, osiz, stdout); - tcfree(obuf); - } else { - int osiz; - char *obuf = tcbsencode(ibuf, isiz, &osiz); - fwrite(obuf, 1, osiz, stdout); - tcfree(obuf); - } - return 0; +static int proctcbs(const char *ibuf, int isiz, bool dec) { + if (dec) { + int osiz; + char *obuf = tcbsdecode(ibuf, isiz, &osiz); + fwrite(obuf, 1, osiz, stdout); + tcfree(obuf); + } else { + int osiz; + char *obuf = tcbsencode(ibuf, isiz, &osiz); + fwrite(obuf, 1, osiz, stdout); + tcfree(obuf); + } + return 0; } - /* perform zlib command */ -static int proczlib(const char *ibuf, int isiz, bool dec, bool gz){ - if(dec){ - int osiz; - char *obuf = gz ? tcgzipdecode(ibuf, isiz, &osiz) : tcinflate(ibuf, isiz, &osiz); - if(obuf){ - fwrite(obuf, 1, osiz, stdout); - tcfree(obuf); - } else { - eprintf("inflate failure"); - } - } else { - int osiz; - char *obuf = gz ? tcgzipencode(ibuf, isiz, &osiz) : tcdeflate(ibuf, isiz, &osiz); - if(obuf){ - fwrite(obuf, 1, osiz, stdout); - tcfree(obuf); +static int proczlib(const char *ibuf, int isiz, bool dec, bool gz) { + if (dec) { + int osiz; + char *obuf = gz ? tcgzipdecode(ibuf, isiz, &osiz) : tcinflate(ibuf, isiz, &osiz); + if (obuf) { + fwrite(obuf, 1, osiz, stdout); + tcfree(obuf); + } else { + eprintf("inflate failure"); + } } else { - eprintf("deflate failure"); + int osiz; + char *obuf = gz ? tcgzipencode(ibuf, isiz, &osiz) : tcdeflate(ibuf, isiz, &osiz); + if (obuf) { + fwrite(obuf, 1, osiz, stdout); + tcfree(obuf); + } else { + eprintf("deflate failure"); + } } - } - return 0; + return 0; } - /* perform bzip command */ -static int procbzip(const char *ibuf, int isiz, bool dec){ - if(dec){ - int osiz; - char *obuf = tcbzipdecode(ibuf, isiz, &osiz); - if(obuf){ - fwrite(obuf, 1, osiz, stdout); - tcfree(obuf); - } else { - eprintf("inflate failure"); - } - } else { - int osiz; - char *obuf = tcbzipencode(ibuf, isiz, &osiz); - if(obuf){ - fwrite(obuf, 1, osiz, stdout); - tcfree(obuf); +static int procbzip(const char *ibuf, int isiz, bool dec) { + if (dec) { + int osiz; + char *obuf = tcbzipdecode(ibuf, isiz, &osiz); + if (obuf) { + fwrite(obuf, 1, osiz, stdout); + tcfree(obuf); + } else { + eprintf("inflate failure"); + } } else { - eprintf("deflate failure"); + int osiz; + char *obuf = tcbzipencode(ibuf, isiz, &osiz); + if (obuf) { + fwrite(obuf, 1, osiz, stdout); + tcfree(obuf); + } else { + eprintf("deflate failure"); + } } - } - return 0; + return 0; } - /* perform xml command */ -static int procxml(const char *ibuf, int isiz, bool dec, bool br){ - if(br){ - TCLIST *elems = tcxmlbreak(ibuf); - for(int i = 0; i < tclistnum(elems); i++){ - int esiz; - const char *elem = tclistval(elems, i, &esiz); - char *estr = tcmemdup(elem, esiz); - tcstrsubchr(estr, "\t\n\r", " "); - tcstrtrim(estr); - if(*estr != '\0'){ - if(*elem == '<'){ - if(tcstrfwm(estr, "