1 /* This file is part of GDBM, the GNU data base manager.
2    Copyright (C) 2011-2021 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 3, 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 "gdbm.h"
19 # include "gdbmapp.h"
20 # include "gdbmdefs.h"
21 
22 char *parseopt_program_doc = N_("dump a GDBM database to a file");
23 char *parseopt_program_args = N_("DB_FILE [FILE]");
24 struct gdbm_option optab[] = {
25   { 'H', "format", "binary|ascii|0|1", N_("select dump format") },
26   { 0 }
27 };
28 
29 int format = GDBM_DUMP_FMT_ASCII;
30 
31 int
main(int argc,char ** argv)32 main (int argc, char **argv)
33 {
34   GDBM_FILE dbf;
35   int rc, opt;
36   char *dbname, *filename;
37   FILE *fp;
38 
39 #ifdef HAVE_SETLOCALE
40   setlocale (LC_ALL, "");
41 #endif
42   bindtextdomain (PACKAGE, LOCALEDIR);
43   textdomain (PACKAGE);
44 
45   set_progname (argv[0]);
46 
47   for (opt = parseopt_first (argc, argv, optab);
48        opt != EOF;
49        opt = parseopt_next ())
50     {
51     switch (opt)
52       {
53       case 'H':
54 	if (strcmp (optarg, "binary") == 0)
55 	  format = GDBM_DUMP_FMT_BINARY;
56 	else if (strcmp (optarg, "ascii") == 0)
57 	  format = GDBM_DUMP_FMT_ASCII;
58 	else
59 	  {
60 	    format = atoi (optarg);
61 	    switch (format)
62 	      {
63 	      case GDBM_DUMP_FMT_BINARY:
64 	      case GDBM_DUMP_FMT_ASCII:
65 		break;
66 	      default:
67 		error (_("unknown dump format"));
68 		exit (EXIT_USAGE);
69 	      }
70 	  }
71 	break;
72 
73       default:
74 	error (_("unknown option"));
75 	exit (EXIT_USAGE);
76       }
77     }
78 
79   argc -= optind;
80   argv += optind;
81 
82   if (argc == 0)
83     {
84       parseopt_print_help ();
85       exit (EXIT_OK);
86     }
87 
88   if (argc > 2)
89     {
90       error (_("too many arguments; try `%s -h' for more info"), progname);
91       exit (EXIT_USAGE);
92     }
93 
94   dbname = argv[0];
95   if (argc == 2)
96     filename = argv[1];
97   else
98     filename = NULL;
99 
100   if (!filename || strcmp (filename, "-") == 0)
101     {
102       filename = "<stdout>";
103       fp = stdout;
104     }
105   else
106     {
107       fp = fopen (filename, "w");
108       if (!fp)
109 	{
110 	  sys_perror (errno, _("cannot open %s"), filename);
111 	  exit (EXIT_FATAL);
112 	}
113     }
114 
115   dbf = gdbm_open (dbname, 0, GDBM_READER, 0600, NULL);
116   if (!dbf)
117     {
118       gdbm_perror (_("gdbm_open failed"));
119       exit (EXIT_FATAL);
120     }
121 
122   rc = gdbm_dump_to_file (dbf, fp, format);
123   if (rc)
124     {
125       gdbm_perror (_("dump error"), filename);
126     }
127 
128   gdbm_close (dbf);
129 
130   exit (rc == GDBM_NO_ERROR ? EXIT_OK : EXIT_FATAL);
131 }
132 
133