/** Retrieves the hamsterdb library version. */ static void get_version(uint32_t *major, uint32_t *minor, uint32_t *revision) { ham_get_version(major, minor, revision); }
int main(int argc, char **argv) { unsigned opt; char *param, *filename=0, *endptr=0; unsigned short dbname=0xffff; int full=0; ham_u16_t names[1024]; ham_size_t i, names_count=1024; ham_status_t st; ham_env_t *env; ham_db_t *db; ham_u32_t maj, min, rev; const char *licensee, *product; ham_get_license(&licensee, &product); ham_get_version(&maj, &min, &rev); getopts_init(argc, argv, "ham_info"); while ((opt=getopts(&opts[0], ¶m))) { switch (opt) { case ARG_DBNAME: if (!param) { printf("Parameter `dbname' is missing.\n"); return (-1); } dbname=(short)strtoul(param, &endptr, 0); if (endptr && *endptr) { printf("Invalid parameter `dbname'; numerical value " "expected.\n"); return (-1); } break; case ARG_FULL: full=1; break; case GETOPTS_PARAMETER: if (filename) { printf("Multiple files specified. Please specify " "only one filename.\n"); return (-1); } filename=param; break; case ARG_HELP: printf("hamsterdb %d.%d.%d - Copyright (C) 2005-2007 " "Christoph Rupp ([email protected]).\n\n", maj, min, rev); if (licensee[0]=='\0') printf( "This program is free software; you can redistribute " "it and/or modify it\nunder the terms of the GNU " "General Public License as published by the Free\n" "Software Foundation; either version 2 of the License,\n" "or (at your option) any later version.\n\n" "See file COPYING.GPL2 and COPYING.GPL3 for License " "information.\n\n"); else printf("Commercial version; licensed for %s (%s)\n\n", licensee, product); printf("usage: ham_info [-db DBNAME] [-f] file\n"); printf("usage: ham_info -h\n"); printf(" -h: this help screen (alias: --help)\n"); printf(" -db DBNAME: only print info about " "this database (alias: --dbname=<arg>)\n"); printf(" -f: print full information " "(alias: --full)\n"); return (0); default: printf("Invalid or unknown parameter `%s'. " "Enter `ham_info --help' for usage.", param); return (-1); } } if (!filename) { printf("Filename is missing. Enter `ham_info --help' for usage.\n"); return (-1); } /* * open the environment */ st=ham_env_new(&env); if (st!=HAM_SUCCESS) error("ham_env_new", st); st=ham_env_open(env, filename, HAM_READ_ONLY); if (st==HAM_FILE_NOT_FOUND) { printf("File `%s' not found or unable to open it\n", filename); return (-1); } else if (st!=HAM_SUCCESS) error("ham_env_open", st); /* * print information about the environment */ print_environment(env); /* * get a list of all databases */ st=ham_env_get_database_names(env, names, &names_count); if (st!=HAM_SUCCESS) error("ham_env_get_database_names", st); /* * did the user specify a database name? if yes, show only this database */ if (dbname!=0xffff) { st=ham_new(&db); if (st) error("ham_new", st); st=ham_env_open_db(env, db, dbname, 0, 0); if (st==HAM_DATABASE_NOT_FOUND) { printf("Database %u (0x%x) not found\n", dbname, dbname); return (-1); } else if (st) error("ham_env_open_db", st); print_database(db, dbname, full); st=ham_close(db, 0); if (st) error("ham_close", st); ham_delete(db); } else { /* * otherwise: for each database: print information about the database */ for (i=0; i<names_count; i++) { st=ham_new(&db); if (st) error("ham_new", st); st=ham_env_open_db(env, db, names[i], 0, 0); if (st) error("ham_env_open_db", st); print_database(db, names[i], full); st=ham_close(db, 0); if (st) error("ham_close", st); ham_delete(db); } } /* * clean up */ st=ham_env_close(env, 0); if (st!=HAM_SUCCESS) error("ham_env_close", st); ham_env_delete(env); return (0); }