Imported Upstream version 1.11
[platform/upstream/gdbm.git] / export / export.c
1 /* export.c - Stand alone flat file exporter for older versions of GDBM. */
2
3 /* This file is part of GDBM, the GNU data base manager.
4    Copyright (C) 2007,  Free Software Foundation, Inc.
5
6    GDBM is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10
11    GDBM is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GDBM. If not, see <http://www.gnu.org/licenses/>.   */
18
19 /* Include system configuration before all else. */
20 #include "autoconf.h"
21
22 #include "systems.h"
23
24 #include <gdbm.h>
25
26 /* Pull in gdbm_export() */
27 #include "gdbmexp.c"
28
29 void
30 usage (char *s)
31 {
32   printf ("Usage: %s database outfile\n", s);
33   printf ("   or: %s [-hv]\n", s);
34   printf ("Convert GDBM database into a flat dump format.\n");
35   printf ("Linked with %s\n", gdbm_version);
36   printf ("\n");
37   printf ("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
38 }
39
40
41 void
42 version ()
43 {
44   printf ("gdbmexport (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
45   printf ("Copyright (C) 2007 Free Software Foundation, Inc.\n");
46   printf ("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n");
47   printf ("This is free software: you are free to change and redistribute it.\n");
48   printf ("There is NO WARRANTY, to the extent permitted by law.\n");
49 }
50
51
52 int
53 main(int argc, char *argv[])
54 {
55   int c;
56   GDBM_FILE dbf;
57   int flags = 0;
58
59   while ((c = getopt (argc, argv, "hlv")) != -1)
60     switch (c)
61       {
62       case 'h':
63         usage (argv[0]);
64         exit (0);
65
66       case 'l':
67         flags = GDBM_NOLOCK;
68         break;
69
70       case 'v':
71         version ();
72         exit (0);
73         
74       default:
75         usage (argv[0]);
76         exit (1);
77       }
78
79   if (argc != 3)
80     {
81       usage (argv[0]);
82       exit (1);
83     }
84
85   dbf = gdbm_open (argv[1], 0, GDBM_READER | flags, 0600, NULL);
86   if (dbf == NULL)
87     {
88       fprintf (stderr, "%s: couldn't open database, %s\n", argv[0],
89                gdbm_strerror (gdbm_errno));
90       exit (1);
91     }
92
93   if (gdbm_export (dbf, argv[2], GDBM_WRCREAT | flags, 0600) == -1)
94     {
95       fprintf (stderr, "%s: export failed, %s\n",
96                argv[0], gdbm_strerror (gdbm_errno));
97       gdbm_close (dbf);
98       exit (1);
99     }
100   gdbm_close (dbf);
101   exit (0);
102 }