00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <sys/types.h>
00011
00012 #include <stddef.h>
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015
00016 #include <db.h>
00017
00018 #ifdef macintosh
00019 #define DATABASE_HOME ":database"
00020 #define CONFIG_DATA_DIR ":database"
00021 #else
00022 #ifdef DB_WIN32
00023 #define DATABASE_HOME "\\tmp\\database"
00024 #define CONFIG_DATA_DIR "\\database\\files"
00025 #else
00026 #define DATABASE_HOME "/tmp/database"
00027 #define CONFIG_DATA_DIR "/database/files"
00028 #endif
00029 #endif
00030
00031 int db_setup __P((const char *, const char *, FILE *, const char *));
00032 int db_teardown __P((const char *, const char *, FILE *, const char *));
00033 int main __P((void));
00034
00035
00036
00037
00038 int
00039 main()
00040 {
00041 const char *data_dir, *home;
00042 const char *progname = "ex_env";
00043
00044
00045
00046
00047
00048 home = DATABASE_HOME;
00049 data_dir = CONFIG_DATA_DIR;
00050
00051 printf("Setup env\n");
00052 if (db_setup(home, data_dir, stderr, progname) != 0)
00053 return (EXIT_FAILURE);
00054
00055 printf("Teardown env\n");
00056 if (db_teardown(home, data_dir, stderr, progname) != 0)
00057 return (EXIT_FAILURE);
00058
00059 return (EXIT_SUCCESS);
00060 }
00061
00062 int
00063 db_setup(home, data_dir, errfp, progname)
00064 const char *home, *data_dir, *progname;
00065 FILE *errfp;
00066 {
00067 DB_ENV *dbenv;
00068 int ret;
00069
00070
00071
00072
00073
00074 if ((ret = db_env_create(&dbenv, 0)) != 0) {
00075 fprintf(errfp, "%s: %s\n", progname, db_strerror(ret));
00076 return (1);
00077 }
00078 dbenv->set_errfile(dbenv, errfp);
00079 dbenv->set_errpfx(dbenv, progname);
00080
00081
00082
00083
00084
00085 if ((ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0)) != 0) {
00086 dbenv->err(dbenv, ret, "set_cachesize");
00087 dbenv->close(dbenv, 0);
00088 return (1);
00089 }
00090
00091
00092 (void)dbenv->set_data_dir(dbenv, data_dir);
00093
00094
00095 if ((ret = dbenv->open(dbenv, home,
00096 DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN,
00097 0)) != 0) {
00098 dbenv->err(dbenv, ret, "environment open: %s", home);
00099 dbenv->close(dbenv, 0);
00100 return (1);
00101 }
00102
00103
00104
00105
00106 if ((ret = dbenv->close(dbenv, 0)) != 0) {
00107 fprintf(stderr, "DB_ENV->close: %s\n", db_strerror(ret));
00108 return (1);
00109 }
00110 return (0);
00111 }
00112
00113 int
00114 db_teardown(home, data_dir, errfp, progname)
00115 const char *home, *data_dir, *progname;
00116 FILE *errfp;
00117 {
00118 DB_ENV *dbenv;
00119 int ret;
00120
00121
00122 if ((ret = db_env_create(&dbenv, 0)) != 0) {
00123 fprintf(errfp, "%s: %s\n", progname, db_strerror(ret));
00124 return (1);
00125 }
00126 dbenv->set_errfile(dbenv, errfp);
00127 dbenv->set_errpfx(dbenv, progname);
00128
00129 (void)dbenv->set_data_dir(dbenv, data_dir);
00130 if ((ret = dbenv->remove(dbenv, home, 0)) != 0) {
00131 fprintf(stderr, "DB_ENV->remove: %s\n", db_strerror(ret));
00132 return (1);
00133 }
00134 return (0);
00135 }