Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:make all program sources more homogeneous
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:b9cf7437a241ef212b86ebbd8eaacaa3193f6ab9
User & Date: brandon 2016-06-12 16:02:58
Context
2016-06-12
16:23
add zdbi program check-in: 98ef403a3f user: brandon tags: trunk
16:02
make all program sources more homogeneous check-in: b9cf7437a2 user: brandon tags: trunk
15:11
pull in more gnulib libs check-in: ee981fdc85 user: brandon tags: trunk
Changes

Changes to src/zdbc.c.

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
...
123
124
125
126
127
128
129
130


131
132
133
134
135
136
137
...
140
141
142
143
144
145
146

147
148
149
150
151
152
153
154
155
156
157


158
159
160
161
162
163
164
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
static void *database;
static char doc[] =
  "zdbc -- a tool for creating a DBM database";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {
  {"mmap-size",   (int) 'm', "NUM", 0,
   "The size (in bytes) of the memory-mapped region to use (default=1024)", 0},
  {"num-buckets", (int) 'b', "NUM", 0,
   "The number of buckets to use (default=100)", 0},
  {"verbose", (int) 'v', 0, 0,
   "Print extra information.", 0},
  {"sync", (int) 's', 0, 0,
   "Automatically synchronize all database operations to the disk", 0},
  {"no-lock", (int) 'l', 0, 0,
   "Do not perform any locking of the database file", 0},
#ifdef HAVE_GDBM
  {"no-mmap", (int) 'n', 0, 0,
   "Do not memory-map the database file", 0},
#endif
  {0, 0, 0, 0, 0, 0}
};

struct arguments
{
  char *args[1];
  size_t mmap_size;
  size_t num_buckets;
................................................................................
      break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp argp = {options, parse_opt, args_doc, doc, NULL, NULL, NULL};



static void
termination_handler (int signum __attribute__ ((unused)))
{
  printf ("Interrupt caught, closing database\n");
  if (database && zdb_close (database, false))
    error (EXIT_FAILURE, errno, "Failed to close database");
................................................................................
}

int
main (int argc, char **argv)
{
  struct arguments arguments;
  int mode = ZDB_CREATOR;


  arguments.verbose = false;
  arguments.sync = false;
  arguments.no_lock = false;
#ifdef HAVE_GDBM
  arguments.no_mmap = false;
#endif
  arguments.mmap_size = 1024;
  arguments.num_buckets = 10;

  argp_parse (&argp, argc, argv, 0, 0, &arguments);



  if (signal (SIGINT, termination_handler) == SIG_IGN)
    signal (SIGINT, SIG_IGN);
  if (signal (SIGHUP, termination_handler) == SIG_IGN)
    signal (SIGHUP, SIG_IGN);
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);







|

|

|

|

|


|


|







 







|
>
>







 







>










|
>
>







35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
...
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
static void *database;
static char doc[] =
  "zdbc -- a tool for creating a DBM database";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {
  {"mmap-size",   (int)'m', "NUM", 0,
   "The size (in bytes) of the memory-mapped region to use (default=1024)", 0},
  {"num-buckets", (int)'b', "NUM", 0,
   "The number of buckets to use (default=100)", 0},
  {"verbose", (int)'v', (const char *)NULL, 0,
   "Print extra information.", 0},
  {"sync", (int)'s', (const char *)NULL, 0,
   "Automatically synchronize all database operations to the disk", 0},
  {"no-lock", (int)'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
#ifdef HAVE_GDBM
  {"no-mmap", (int)'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
#endif
  {(const char *)NULL, 0, (const char *)NULL, 0, (const char *)NULL, 0}
};

struct arguments
{
  char *args[1];
  size_t mmap_size;
  size_t num_buckets;
................................................................................
      break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp argp = {options, parse_opt, args_doc, doc,
                           (const struct argp_child *)NULL,
                           NULL, (const char *)NULL};

static void
termination_handler (int signum __attribute__ ((unused)))
{
  printf ("Interrupt caught, closing database\n");
  if (database && zdb_close (database, false))
    error (EXIT_FAILURE, errno, "Failed to close database");
................................................................................
}

int
main (int argc, char **argv)
{
  struct arguments arguments;
  int mode = ZDB_CREATOR;
  error_t argp_result;

  arguments.verbose = false;
  arguments.sync = false;
  arguments.no_lock = false;
#ifdef HAVE_GDBM
  arguments.no_mmap = false;
#endif
  arguments.mmap_size = 1024;
  arguments.num_buckets = 10;

  argp_result = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  if (argp_result)
    error (EXIT_FAILURE, argp_result, "Failed to parse arguments");

  if (signal (SIGINT, termination_handler) == SIG_IGN)
    signal (SIGINT, SIG_IGN);
  if (signal (SIGHUP, termination_handler) == SIG_IGN)
    signal (SIGHUP, SIG_IGN);
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);

Changes to src/zdbf.c.

18
19
20
21
22
23
24

25
26
27
28
29
30
31
..
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
..
85
86
87
88
89
90
91








92







93
94
95
96
97
98
99
...
116
117
118
119
120
121
122
123



124
125
126
127
128
129
130
131
132
133

134
135
136




137
138
139
140


141
142
143
144

145


146
147
148
149
150
151
152
153
154
155
156
157
...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

178
179
180
181
182
183
184
185
186
187
188
189


190
191
192
193
194
195
196
...
216
217
218
219
220
221
222
223
224
225
226
227



228
229

230
231
232


233
234






235
236
237
238
239
   along with zeptodb.  If not, see <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE

#include <config.h>
#include <string.h>

#include "error.h"
#include "argp.h"
#include <signal.h>
#include <stdbool.h>
#include "zdb.h"

const char *program_name = "zdbf";
................................................................................
  "If an input file is provided, lines are read from it as individual queries, "
  "otherwise queries are taken from stdin.";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {
  {"input", (int)'i', "FILE", 0,
   "Read queries from a file", 0},
  {"all", (int)'a', 0, 0,
   "Fetch all the records in the database", 0},
  {"delim", (int)'d', "CHAR", 0,
   "Character to print between keys and values.  Keys will not be printed if "
   "left unspecified.", 0},
  {"sync", (int)'s', 0, 0,
   "Automatically synchronize all database operations to the disk", 0},
  {"no-lock", (int)'l', 0, 0,
   "Do not perform any locking of the database file", 0},
#ifdef HAVE_GDBM
  {"no-mmap", (int)'n', 0, 0,
   "Do not memory-map the database file", 0},
#endif
  {"verbose", (int)'v', 0, 0,
   "Print extra information.", 0},
  {0}
};

struct arguments
{
  char *args[1];
  char *input_file;
  bool all;
................................................................................
      break;
    case 'd':
      if (strlen (arg) > 1)
        argp_usage (state);
      arguments->delim = arg;
      break;
    case 'i':








      arguments->input_file = arg;







      break;
    case 'l':
      arguments->no_lock = true;
      break;
#ifdef HAVE_GDBM
    case 'n':
      arguments->no_mmap = true;
................................................................................
      break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp argp = {options, parse_opt, args_doc, doc, NULL, NULL, NULL};




static void
fetchqueries (void *db, FILE * input, char *delim)
{
  /*
   * This function fetches records from the database from queries
   * in a file object.
   */

  size_t linelen = 512;

  char *lineptr = (char *) malloc (linelen);
  char *key_str;
  size_t key_size;





  /* query the database for each line of input */
  while (getline (&lineptr, &linelen, input) != EOF)
    {


      key_size = strlen (lineptr);
      if (lineptr[key_size - 1] == '\n')
        key_size -= 1;
      if (key_size <= 0)

        error (0, errno, "Empty key value");


      key_str = (char *) malloc (sizeof (char) * (key_size+1));
      if (!key_str)
        {
          free (lineptr);
          error (EXIT_FAILURE, errno, "Virtual memory exhausted");
        }
      /* get rid of the newline */
      strncpy (key_str, lineptr, sizeof (char) * (key_size));
      key_str[key_size] = '\0';
      if (zdb_fetch (db, key_str, key_size, delim))
        error (0, errno, "Key does not exist in the database: %s", key_str);
      free (key_str);
................................................................................
  free (lineptr);
}

static void
termination_handler (int signum __attribute__ ((unused)))
{
  printf ("Interrupt caught, closing database\n");
  if (zdb_close (database, false))
    error (EXIT_FAILURE, errno, "Failed to close database");
  else
    exit (EXIT_SUCCESS);
}

int
main (int argc, char **argv)
{
  FILE *input;
  struct arguments arguments;
  int mode = ZDB_READER;


  arguments.verbose = false;
  arguments.sync = false;
  arguments.no_lock = false;
#ifdef HAVE_GDBM
  arguments.no_mmap = false;
#endif
  arguments.input_file = "-";
  arguments.all = false;
  arguments.delim = NULL;

  argp_parse (&argp, argc, argv, 0, 0, &arguments);



  if (signal (SIGINT, termination_handler) == SIG_IGN)
    signal (SIGINT, SIG_IGN);
  if (signal (SIGHUP, termination_handler) == SIG_IGN)
    signal (SIGHUP, SIG_IGN);
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);
................................................................................
    {
      zdb_fetchall (database, arguments.delim);
      zdb_close (database, arguments.verbose);
      exit (EXIT_SUCCESS);
    }

  /* if an input file was provided, use it, otherwise use stdin */
  if (strcmp (arguments.input_file, "-") == 0)
    input = stdin;
  else
    input = fopen (arguments.input_file, "r");
  if (!input)



    error (EXIT_FAILURE, errno,
           "Failed to open input file %s", arguments.input_file);

  fetchqueries (database, input, arguments.delim);

  if (fclose (input) != 0)


    error (EXIT_FAILURE, errno,
           "Failed to close input file %s", arguments.input_file);






  if (zdb_close (database, arguments.verbose) != 0)
    error (EXIT_FAILURE, errno,
           "Failed to close database %s", arguments.args[0]);
  exit (EXIT_SUCCESS);
}







>







 







|




|

|


|


|

|







 







>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>







 







|
>
>
>




<
<
<
<
<

>


<
>
>
>
>




>
>



|
>
|
>
>




|







 







|











>







|



|
>
>







 







|




>
>
>
|
|
>


|
>
>
|
|
>
>
>
>
>
>
|




18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
..
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
..
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146





147
148
149
150

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
...
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
   along with zeptodb.  If not, see <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE

#include <config.h>
#include <string.h>
#include <stdio.h>
#include "error.h"
#include "argp.h"
#include <signal.h>
#include <stdbool.h>
#include "zdb.h"

const char *program_name = "zdbf";
................................................................................
  "If an input file is provided, lines are read from it as individual queries, "
  "otherwise queries are taken from stdin.";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {
  {"input", (int)'i', "FILE", 0,
   "Read queries from a file", 0},
  {"all", (int)'a', (const char *)NULL, 0,
   "Fetch all the records in the database", 0},
  {"delim", (int)'d', "CHAR", 0,
   "Character to print between keys and values.  Keys will not be printed if "
   "left unspecified.", 0},
  {"sync", (int)'s', (const char *)NULL, 0,
   "Automatically synchronize all database operations to the disk", 0},
  {"no-lock", (int)'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
#ifdef HAVE_GDBM
  {"no-mmap", (int)'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
#endif
  {"verbose", (int)'v', (const char *)NULL, 0,
   "Print extra information.", 0},
  {(const char *)NULL, 0, (const char *)NULL, 0, (const char *)NULL, 0}
};

struct arguments
{
  char *args[1];
  char *input_file;
  bool all;
................................................................................
      break;
    case 'd':
      if (strlen (arg) > 1)
        argp_usage (state);
      arguments->delim = arg;
      break;
    case 'i':
      if (arguments->input_file)
        free (arguments->input_file);
      if (!arg)
        {
          arguments->input_file = NULL;
        }
      else
        {
          arguments->input_file =
            (char *) malloc (sizeof (char) * (strlen (arg)+1));
          if (!arguments->input_file)
            error (EXIT_FAILURE, errno,
                   "Failed to allocate memory");
          strncpy (arguments->input_file, arg, strlen (arg));
          arguments->input_file[strlen (arg)] = '\0';
        }
      break;
    case 'l':
      arguments->no_lock = true;
      break;
#ifdef HAVE_GDBM
    case 'n':
      arguments->no_mmap = true;
................................................................................
      break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp argp = {options, parse_opt, args_doc, doc,
                           (const struct argp_child *)NULL,
                           NULL, (const char *)NULL};


static void
fetchqueries (void *db, FILE * input, char *delim)
{





  size_t linelen = 512;
  size_t key_size;
  char *lineptr = (char *) malloc (linelen);
  char *key_str;


  if (!lineptr)
    error (EXIT_FAILURE, errno,
           "Failed to allocate memory");

  /* query the database for each line of input */
  while (getline (&lineptr, &linelen, input) != EOF)
    {
      if (!lineptr)
          error (EXIT_FAILURE, errno, "Failed to read line");
      key_size = strlen (lineptr);
      if (lineptr[key_size - 1] == '\n')
        key_size -= 1;
      if (key_size == 0)
        {
          error (0, errno, "Empty key value");
          continue;
        }
      key_str = (char *) malloc (sizeof (char) * (key_size+1));
      if (!key_str)
        {
          free (lineptr);
          error (EXIT_FAILURE, errno, "Failed to allocate memory");
        }
      /* get rid of the newline */
      strncpy (key_str, lineptr, sizeof (char) * (key_size));
      key_str[key_size] = '\0';
      if (zdb_fetch (db, key_str, key_size, delim))
        error (0, errno, "Key does not exist in the database: %s", key_str);
      free (key_str);
................................................................................
  free (lineptr);
}

static void
termination_handler (int signum __attribute__ ((unused)))
{
  printf ("Interrupt caught, closing database\n");
  if (database && zdb_close (database, false))
    error (EXIT_FAILURE, errno, "Failed to close database");
  else
    exit (EXIT_SUCCESS);
}

int
main (int argc, char **argv)
{
  FILE *input;
  struct arguments arguments;
  int mode = ZDB_READER;
  error_t argp_result;

  arguments.verbose = false;
  arguments.sync = false;
  arguments.no_lock = false;
#ifdef HAVE_GDBM
  arguments.no_mmap = false;
#endif
  arguments.input_file = NULL;
  arguments.all = false;
  arguments.delim = NULL;

  argp_result = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  if (argp_result)
    error (EXIT_FAILURE, argp_result, "Failed to parse arguments");

  if (signal (SIGINT, termination_handler) == SIG_IGN)
    signal (SIGINT, SIG_IGN);
  if (signal (SIGHUP, termination_handler) == SIG_IGN)
    signal (SIGHUP, SIG_IGN);
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);
................................................................................
    {
      zdb_fetchall (database, arguments.delim);
      zdb_close (database, arguments.verbose);
      exit (EXIT_SUCCESS);
    }

  /* if an input file was provided, use it, otherwise use stdin */
  if (!arguments.input_file || strcmp (arguments.input_file, "-") == 0)
    input = stdin;
  else
    input = fopen (arguments.input_file, "r");
  if (!input)
    {
      if (arguments.input_file)
        free (arguments.input_file);
      error (EXIT_FAILURE, errno,
             "Failed to open input file %s", arguments.input_file);
    }
  fetchqueries (database, input, arguments.delim);

  if (fclose (input))
    {
      if (arguments.input_file)
        error (EXIT_FAILURE, errno,
               "Failed to close input file %s", arguments.input_file);
      else
        error (EXIT_FAILURE, errno,
               "Failed to close stdin");
    }
  if (arguments.input_file)
    free (arguments.input_file);
  if (zdb_close (database, arguments.verbose))
    error (EXIT_FAILURE, errno,
           "Failed to close database %s", arguments.args[0]);
  exit (EXIT_SUCCESS);
}

Changes to src/zdbr.c.

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
...
152
153
154
155
156
157
158

159


160
161
162
163
164
165
166
...
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249








250
251
252
253
254
255
256
  "many records from the database, some fragmentation can occur, in which case "
  "it is recommended to reorganize the database. If the 'reorganize' argument "
  "is passed, the database will be reorganized after any records have been "
  "removed";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {
  {"input", (int) 'i', "FILE", 0,
   "Read queries from a file", 0},
  {"reorganize", (int) 'r', (const char *)NULL, 0,
   "Reorganize the database", 0},
  {"sync", (int) 's', (const char *)NULL, 0,
   "Automatically synchronize all database operations to the disk", 0},
  {"no-lock", (int) 'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
#ifdef HAVE_GDBM
  {"no-mmap", (int) 'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
#endif
  {"verbose", (int) 'v', (const char *)NULL, 0,
   "Print extra information.", 0},
  {0, 0, 0, 0, 0, 0}
};

struct arguments
{
  char *args[1];
  char *input_file;
  bool reorg;
................................................................................
    {
      if (!lineptr)
          error (EXIT_FAILURE, errno, "Failed to read line");
      key_size = strlen (lineptr);
      if (lineptr[key_size - 1] == '\n')
        key_size -= 1;
      if (key_size == 0)

        error (0, errno, "Empty key value");


      key_str = (char *) malloc (sizeof (char) * (key_size+1));
      if (!key_str)
        {
          free (lineptr);
          error (EXIT_FAILURE, errno, "Failed to allocate memory");
        }
      /* get rid of the newline */
................................................................................
  else
    input = fopen (arguments.input_file, "r");
  if (!input)
    {
      if (arguments.input_file)
        free (arguments.input_file);
      error (EXIT_FAILURE, errno,
             "Failed to open file %s", arguments.input_file);
    }
  remrecords (database, input, arguments.verbose);

  /* if the "reorganize" argument was passed, reorganize  */
  /* the database (GDBM only) */
  if (arguments.reorg)
    zdb_reorganize (database);

  fclose (input);








  if (arguments.input_file)
    free (arguments.input_file);
  if (zdb_close (database, arguments.verbose))
    error (EXIT_FAILURE, errno,
           "Failed to close database %s", arguments.args[0]);
  exit (EXIT_SUCCESS);
}







|

|

|

|


|


|

|







 







>
|
>
>







 







|








|
>
>
>
>
>
>
>
>







40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
...
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
...
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  "many records from the database, some fragmentation can occur, in which case "
  "it is recommended to reorganize the database. If the 'reorganize' argument "
  "is passed, the database will be reorganized after any records have been "
  "removed";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {
  {"input", (int)'i', "FILE", 0,
   "Read queries from a file", 0},
  {"reorganize", (int)'r', (const char *)NULL, 0,
   "Reorganize the database", 0},
  {"sync", (int)'s', (const char *)NULL, 0,
   "Automatically synchronize all database operations to the disk", 0},
  {"no-lock", (int)'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
#ifdef HAVE_GDBM
  {"no-mmap", (int)'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
#endif
  {"verbose", (int)'v', (const char *)NULL, 0,
   "Print extra information.", 0},
  {(const char *)NULL, 0, (const char *)NULL, 0, (const char *)NULL, 0}
};

struct arguments
{
  char *args[1];
  char *input_file;
  bool reorg;
................................................................................
    {
      if (!lineptr)
          error (EXIT_FAILURE, errno, "Failed to read line");
      key_size = strlen (lineptr);
      if (lineptr[key_size - 1] == '\n')
        key_size -= 1;
      if (key_size == 0)
        {
          error (0, errno, "Empty key value");
          continue;
        }
      key_str = (char *) malloc (sizeof (char) * (key_size+1));
      if (!key_str)
        {
          free (lineptr);
          error (EXIT_FAILURE, errno, "Failed to allocate memory");
        }
      /* get rid of the newline */
................................................................................
  else
    input = fopen (arguments.input_file, "r");
  if (!input)
    {
      if (arguments.input_file)
        free (arguments.input_file);
      error (EXIT_FAILURE, errno,
             "Failed to open input file %s", arguments.input_file);
    }
  remrecords (database, input, arguments.verbose);

  /* if the "reorganize" argument was passed, reorganize  */
  /* the database (GDBM only) */
  if (arguments.reorg)
    zdb_reorganize (database);

  if (fclose (input))
    {
      if (arguments.input_file)
        error (EXIT_FAILURE, errno,
               "Failed to close input file %s", arguments.input_file);
      else
        error (EXIT_FAILURE, errno,
               "Failed to close stdin");
    }
  if (arguments.input_file)
    free (arguments.input_file);
  if (zdb_close (database, arguments.verbose))
    error (EXIT_FAILURE, errno,
           "Failed to close database %s", arguments.args[0]);
  exit (EXIT_SUCCESS);
}

Changes to src/zdbs.c.

18
19
20
21
22
23
24

25
26
27
28
29
30
31
..
36
37
38
39
40
41
42

43
44
45

46
47

48
49

50
51

52
53

54
55
56
57
58
59
60
..
75
76
77
78
79
80
81








82







83
84
85
86
87
88
89
...
106
107
108
109
110
111
112
113



114
115
116
117
118
119
120
121
122
123
124




125
126
127


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
...
148
149
150
151
152
153
154












155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

182
183
184
185
186
187
188
189
190
191
192
193


194
195
196
197
198
199
200
...
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222



223
224

225
226
227










228
229
230
231
232
233
234
   along with zeptodb.  If not, see <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE

#include <config.h>
#include <string.h>

#include "error.h"
#include "argp.h"
#include <signal.h>
#include <stdbool.h>
#include "zdb.h"

const char *program_name = "zdbs";
................................................................................
  "zdbs -- a tool for storing records in a DBM database\v"
  "If an input file is provided, lines are read from it as new records, "
  "otherwise records are added from stdin. Records are expected in the "
  "format KEY|VALUE.";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {

  {"input", 'i', "FILE", 0, "Read new records from a file"},
  {"delim", 'd', "CHAR", 0,
   "Character used to separate keys from values (default '|')"},

  {"sync", 's', 0, 0, "Automatically synchronize all database operations to the"
   " disk"},

  {"no-lock", 'l', 0, 0, "Do not perform any locking of the database file"},
#ifdef HAVE_GDBM

  {"no-mmap", 'n', 0, 0, "Do not memory-map the database file"},
#endif

  {"verbose", 'v', 0, 0, "Print extra information."},
  {0}

};

struct arguments
{
  char *args[1];
  char *input_file;
  const char *delim;
................................................................................
    {
    case 'd':
      if (strlen (arg) > 1)
        argp_usage (state);
      arguments->delim = arg;
      break;
    case 'i':








      arguments->input_file = arg;







      break;
    case 'l':
      arguments->no_lock = true;
      break;
#ifdef HAVE_GDBM
    case 'n':
      arguments->no_mmap = true;
................................................................................
      break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp argp = { options, parse_opt, args_doc, doc };




int
addrecords (void *db, FILE *input, const char *delim, bool verbose)
{
  size_t linelen = 512;
  char *lineptr = (char *) malloc (linelen);
  char *linecpy;
  char *key_str, *value_str;
  size_t line_size, key_size, value_size;
  long int count = 1;





  /* add each line of the input to the database */
  while (getline (&lineptr, &linelen, input) != EOF)
    {


      line_size = strlen (lineptr);
      if (lineptr[line_size - 1] == '\n')
        line_size -= 1;
      if (line_size <= 0)
        error (0, errno, "Empty record");
      linecpy = (char *) malloc (sizeof (char) * (line_size+1));
      if (!linecpy)
        {
          free (lineptr);
          error (EXIT_FAILURE, errno, "Virtual memory exhausted");
        }
      strncpy (linecpy, lineptr, sizeof (char) * (line_size));
      linecpy[line_size] = '\0';
      key_str = strtok (linecpy, delim);
      if (!key_str)
        {
          error (0, errno, "Invalid record format (key%svalue)", delim);
................................................................................
      if (!value_str)
        {
          error (0, errno, "Invalid record format (key%svalue)", delim);
          continue;
        }
      value_size = strlen (value_str);
      key_size = strlen (key_str);












      if (verbose)
        printf ("%ld: ", count);
      if (zdb_store (db, key_str, key_size, value_str, value_size, verbose))
        error (0, errno, "Could not store record in database: %s", key_str);
      count++;
      free (linecpy);
    }
  free (lineptr);
  return (0);
}

void
termination_handler (int signum)
{
  printf ("Interrupt caught, closing database\n");
  if (zdb_close (database, false))
    error (EXIT_FAILURE, errno, "Failed to close database");
  else
    exit (EXIT_SUCCESS);
}

int
main (int argc, char **argv)
{
  FILE *input;
  struct arguments arguments;
  int mode = ZDB_WRITER;

  int result;

  arguments.verbose = false;
  arguments.sync = false;
  arguments.no_lock = false;
#ifdef HAVE_GDBM
  arguments.no_mmap = false;
#endif
  arguments.input_file = "-";
  arguments.delim = "|";

  argp_parse (&argp, argc, argv, 0, 0, &arguments);



  if (signal (SIGINT, termination_handler) == SIG_IGN)
    signal (SIGINT, SIG_IGN);
  if (signal (SIGHUP, termination_handler) == SIG_IGN)
    signal (SIGHUP, SIG_IGN);
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);
................................................................................
    mode ^= ZDB_NOLOCK;
#ifdef HAVE_GDBM
  if (arguments.no_mmap)
    mode ^= ZDB_NOMMAP;
#endif

  /* open the database */
  database = zdb_open (arguments.args[0], mode,  arguments.verbose);
  if (!database)
    error (EXIT_FAILURE, errno,
           "Failed to open database %s", arguments.args[0]);

  /* if an input file was provided, use it, otherwise use stdin */
  if (strcmp (arguments.input_file, "-") == 0)
    input = stdin;
  else
    input = fopen (arguments.input_file, "r");
  if (!input)



    error (EXIT_FAILURE, errno,
           "Failed to open input file %s", arguments.input_file);

  result = addrecords (database, input, arguments.delim, arguments.verbose);

  fclose (input);










  if (zdb_close (database, arguments.verbose))
    error (EXIT_FAILURE, errno,
           "Failed to close database %s", arguments.args[0]);
  if (result != 0)
    exit (EXIT_FAILURE);
  exit (EXIT_SUCCESS);
}







>







 







>
|
|
|
>
|
<
>
|

>
|

>
|
<
>







 







>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>







 







|
>
>
>











>
>
>
>



>
>



|





|







 







>
>
>
>
>
>
>
>
>
>
>
>












|


|











>











|
>
>







 







|





|




>
>
>
|
|
>


|
>
>
>
>
>
>
>
>
>
>







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
..
37
38
39
40
41
42
43
44
45
46
47
48
49

50
51
52
53
54
55
56
57

58
59
60
61
62
63
64
65
..
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
...
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
   along with zeptodb.  If not, see <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE

#include <config.h>
#include <string.h>
#include <stdio.h>
#include "error.h"
#include "argp.h"
#include <signal.h>
#include <stdbool.h>
#include "zdb.h"

const char *program_name = "zdbs";
................................................................................
  "zdbs -- a tool for storing records in a DBM database\v"
  "If an input file is provided, lines are read from it as new records, "
  "otherwise records are added from stdin. Records are expected in the "
  "format KEY|VALUE.";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {
  {"input", (int)'i', "FILE", 0,
   "Read new records from a file", 0},
  {"delim", (int)'d', "CHAR", 0,
   "Character used to separate keys from values (default '|')", 0},
  {"sync", 's', (const char *)NULL, 0,
   "Automatically synchronize all database operations to the disk", 0},

  {"no-lock", (int)'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
#ifdef HAVE_GDBM
  {"no-mmap", (int)'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
#endif
  {"verbose", (int)'v', (const char *)NULL, 0,
   "Print extra information.", 0},

  {(const char *)NULL, 0, (const char *)NULL, 0, (const char *)NULL, 0}
};

struct arguments
{
  char *args[1];
  char *input_file;
  const char *delim;
................................................................................
    {
    case 'd':
      if (strlen (arg) > 1)
        argp_usage (state);
      arguments->delim = arg;
      break;
    case 'i':
      if (arguments->input_file)
        free (arguments->input_file);
      if (!arg)
        {
          arguments->input_file = NULL;
        }
      else
        {
          arguments->input_file =
            (char *) malloc (sizeof (char) * (strlen (arg)+1));
          if (!arguments->input_file)
            error (EXIT_FAILURE, errno,
                   "Failed to allocate memory");
          strncpy (arguments->input_file, arg, strlen (arg));
          arguments->input_file[strlen (arg)] = '\0';
        }
      break;
    case 'l':
      arguments->no_lock = true;
      break;
#ifdef HAVE_GDBM
    case 'n':
      arguments->no_mmap = true;
................................................................................
      break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp argp = {options, parse_opt, args_doc, doc,
                           (const struct argp_child *)NULL,
                           NULL, (const char *)NULL};


int
addrecords (void *db, FILE *input, const char *delim, bool verbose)
{
  size_t linelen = 512;
  char *lineptr = (char *) malloc (linelen);
  char *linecpy;
  char *key_str, *value_str;
  size_t line_size, key_size, value_size;
  long int count = 1;

  if (!lineptr)
    error (EXIT_FAILURE, errno,
           "Failed to allocate memory");

  /* add each line of the input to the database */
  while (getline (&lineptr, &linelen, input) != EOF)
    {
      if (!lineptr)
          error (EXIT_FAILURE, errno, "Failed to read line");
      line_size = strlen (lineptr);
      if (lineptr[line_size - 1] == '\n')
        line_size -= 1;
      if (line_size == 0)
        error (0, errno, "Empty record");
      linecpy = (char *) malloc (sizeof (char) * (line_size+1));
      if (!linecpy)
        {
          free (lineptr);
          error (EXIT_FAILURE, errno, "Failed to allocate memory");
        }
      strncpy (linecpy, lineptr, sizeof (char) * (line_size));
      linecpy[line_size] = '\0';
      key_str = strtok (linecpy, delim);
      if (!key_str)
        {
          error (0, errno, "Invalid record format (key%svalue)", delim);
................................................................................
      if (!value_str)
        {
          error (0, errno, "Invalid record format (key%svalue)", delim);
          continue;
        }
      value_size = strlen (value_str);
      key_size = strlen (key_str);
      if (key_size == 0)
        {
          error (0, errno, "Empty key value");
          free (linecpy);
          continue;
        }
      if (value_size == 0)
        {
          error (0, errno, "Empty value");
          free (linecpy);
          continue;
        }
      if (verbose)
        printf ("%ld: ", count);
      if (zdb_store (db, key_str, key_size, value_str, value_size, verbose))
        error (0, errno, "Could not store record in database: %s", key_str);
      count++;
      free (linecpy);
    }
  free (lineptr);
  return (0);
}

void
termination_handler (int signum __attribute__ ((unused)))
{
  printf ("Interrupt caught, closing database\n");
  if (database && zdb_close (database, false))
    error (EXIT_FAILURE, errno, "Failed to close database");
  else
    exit (EXIT_SUCCESS);
}

int
main (int argc, char **argv)
{
  FILE *input;
  struct arguments arguments;
  int mode = ZDB_WRITER;
  error_t argp_result;
  int result;

  arguments.verbose = false;
  arguments.sync = false;
  arguments.no_lock = false;
#ifdef HAVE_GDBM
  arguments.no_mmap = false;
#endif
  arguments.input_file = "-";
  arguments.delim = "|";

  argp_result = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  if (argp_result)
    error (EXIT_FAILURE, argp_result, "Failed to parse arguments");

  if (signal (SIGINT, termination_handler) == SIG_IGN)
    signal (SIGINT, SIG_IGN);
  if (signal (SIGHUP, termination_handler) == SIG_IGN)
    signal (SIGHUP, SIG_IGN);
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);
................................................................................
    mode ^= ZDB_NOLOCK;
#ifdef HAVE_GDBM
  if (arguments.no_mmap)
    mode ^= ZDB_NOMMAP;
#endif

  /* open the database */
  database = zdb_open (arguments.args[0], mode, arguments.verbose);
  if (!database)
    error (EXIT_FAILURE, errno,
           "Failed to open database %s", arguments.args[0]);

  /* if an input file was provided, use it, otherwise use stdin */
  if (!arguments.input_file || strcmp (arguments.input_file, "-") == 0)
    input = stdin;
  else
    input = fopen (arguments.input_file, "r");
  if (!input)
    {
      if (arguments.input_file)
        free (arguments.input_file);
      error (EXIT_FAILURE, errno,
             "Failed to open input file %s", arguments.input_file);
    }
  result = addrecords (database, input, arguments.delim, arguments.verbose);

  if (fclose (input))
    {
      if (arguments.input_file)
        error (EXIT_FAILURE, errno,
               "Failed to close input file %s", arguments.input_file);
      else
        error (EXIT_FAILURE, errno,
               "Failed to close stdin");
    }
  if (arguments.input_file)
    free (arguments.input_file);
  if (zdb_close (database, arguments.verbose))
    error (EXIT_FAILURE, errno,
           "Failed to close database %s", arguments.args[0]);
  if (result != 0)
    exit (EXIT_FAILURE);
  exit (EXIT_SUCCESS);
}