Imported Upstream version 1.10
[platform/upstream/gdbm.git] / tests / dtdump.c
1 /* This file is part of GDBM test suite.
2    Copyright (C) 2011 Free Software Foundation, Inc.
3
4    GDBM is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    GDBM is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with GDBM. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "autoconf.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include "dbm.h"
22 #include "progname.h"
23
24 int
25 main (int argc, char **argv)
26 {
27   const char *progname = canonical_progname (argv[0]);
28   char *dbname;
29   datum key;
30   datum data;
31   int delim = '\t';
32   
33   while (--argc)
34     {
35       char *arg = *++argv;
36
37       if (strcmp (arg, "-h") == 0)
38         {
39           printf ("usage: %s [-delim=CHR] DBFILE\n", progname);
40           exit (0);
41         }
42       else if (strncmp (arg, "-delim=", 7) == 0)
43         delim = arg[7];
44       else if (strcmp (arg, "--") == 0)
45         {
46           --argc;
47           ++argv;
48           break;
49         }
50       else if (arg[0] == '-')
51         {
52           fprintf (stderr, "%s: unknown option %s\n", progname, arg);
53           exit (1);
54         }
55       else
56         break;
57     }
58
59   if (argc != 1)
60     {
61       fprintf (stderr, "%s: wrong arguments\n", progname);
62       exit (1);
63     }
64   dbname = *argv;
65   
66   if (dbminit (dbname))
67     {
68       fprintf (stderr, "dbminit failed\n");
69       exit (1);
70     }
71
72   for (key = firstkey (); key.dptr; key = nextkey (key))
73     {
74       int i;
75       
76       for (i = 0; i < key.dsize && key.dptr[i]; i++)
77         {
78           if (key.dptr[i] == delim || key.dptr[i] == '\\')
79             fputc ('\\', stdout);
80           fputc (key.dptr[i], stdout);
81         }
82
83       fputc (delim, stdout);
84
85       data = fetch (key);
86       i = data.dsize;
87       if (data.dptr[i-1] == 0)
88         i--;
89       
90       fwrite (data.dptr, i, 1, stdout);
91       
92       fputc ('\n', stdout);
93     }
94
95   dbmclose ();
96   exit (0);
97 }