Fixed license declaration at spec file
[platform/upstream/gdbm.git] / tests / gtdump.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 "gdbm.h"
22 #include "progname.h"
23
24 int
25 main (int argc, char **argv)
26 {
27   const char *progname = canonical_progname (argv[0]);
28   const char *dbname;
29   datum key;
30   datum data;
31   int flags = 0;
32   GDBM_FILE dbf;
33   int delim = '\t';
34   
35   while (--argc)
36     {
37       char *arg = *++argv;
38
39       if (strcmp (arg, "-h") == 0)
40         {
41           printf ("usage: %s [-nolock] [-nommap] [-delim=CHR] DBFILE\n",
42                   progname);
43           exit (0);
44         }
45       else if (strcmp (arg, "-nolock") == 0)
46         flags |= GDBM_NOLOCK;
47       else if (strcmp (arg, "-nommap") == 0)
48         flags |= GDBM_NOMMAP;
49       else if (strcmp (arg, "-sync") == 0)
50         flags |= GDBM_SYNC;
51       else if (strncmp (arg, "-delim=", 7) == 0)
52         delim = arg[7];
53       else if (strcmp (arg, "--") == 0)
54         {
55           --argc;
56           ++argv;
57           break;
58         }
59       else if (arg[0] == '-')
60         {
61           fprintf (stderr, "%s: unknown option %s\n", progname, arg);
62           exit (1);
63         }
64       else
65         break;
66     }
67
68   if (argc != 1)
69     {
70       fprintf (stderr, "%s: wrong arguments\n", progname);
71       exit (1);
72     }
73   dbname = *argv;
74   
75   dbf = gdbm_open (dbname, 0, GDBM_READER|flags, 00664, NULL);
76   if (!dbf)
77     {
78       fprintf (stderr, "gdbm_open failed: %s\n", gdbm_strerror (gdbm_errno));
79       exit (1);
80     }
81
82   key = gdbm_firstkey (dbf);
83   while (key.dptr)
84     {
85       size_t i;
86       datum nextkey = gdbm_nextkey (dbf, key);
87       
88       for (i = 0; i < key.dsize && key.dptr[i]; i++)
89         {
90           if (key.dptr[i] == delim || key.dptr[i] == '\\')
91             fputc ('\\', stdout);
92           fputc (key.dptr[i], stdout);
93         }
94
95       fputc (delim, stdout);
96
97       data = gdbm_fetch (dbf, key);
98       i = data.dsize;
99       if (data.dptr[i-1] == 0)
100         i--;
101       
102       fwrite (data.dptr, i, 1, stdout);
103       free (data.dptr);
104       
105       fputc ('\n', stdout);
106       
107       free (key.dptr);
108       key = nextkey;
109     }
110
111   gdbm_close (dbf);
112   exit (0);
113 }