summaryrefslogtreecommitdiffabout
path: root/src/gdbm_dump.c
blob: 82fb5afda7250d2d8c8a6c42f04089ccc93d2c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* This file is part of GDBM, the GNU data base manager.
   Copyright (C) 2011, 2013, 2017-2018 Free Software Foundation, Inc.

   GDBM is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GDBM is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GDBM. If not, see <http://www.gnu.org/licenses/>.   */

# include "autoconf.h"
# include "gdbm.h"
# include "gdbmapp.h"
# include "gdbmdefs.h"

char *parseopt_program_doc = "dump a GDBM database to a file";
char *parseopt_program_args = "DB_FILE [FILE]";
struct gdbm_option optab[] = {
  { 'H', "format", "binary|ascii|0|1", N_("select dump format") },
  { 0 }
};

int format = GDBM_DUMP_FMT_ASCII;

int
main (int argc, char **argv)
{
  GDBM_FILE dbf;
  int rc, opt;
  char *dbname, *filename;
  FILE *fp;

#ifdef HAVE_SETLOCALE
  setlocale (LC_ALL, "");
#endif
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  set_progname (argv[0]);

  for (opt = parseopt_first (argc, argv, optab);
       opt != EOF;
       opt = parseopt_next ())
    {
    switch (opt)
      {
      case 'H':
	if (strcmp (optarg, "binary") == 0)
	  format = GDBM_DUMP_FMT_BINARY;
	else if (strcmp (optarg, "ascii") == 0)
	  format = GDBM_DUMP_FMT_ASCII;
	else
	  {
	    format = atoi (optarg);
	    switch (format)
	      {
	      case GDBM_DUMP_FMT_BINARY:
	      case GDBM_DUMP_FMT_ASCII:
		break;
	      default:
		error (_("unknown dump format"));
		exit (EXIT_USAGE);
	      }
	  }
	break;
	
      default:
	error (_("unknown option"));
	exit (EXIT_USAGE);
      }
    }

  argc -= optind;
  argv += optind;

  if (argc == 0)
    {
      parseopt_print_help ();
      exit (EXIT_OK);
    }

  if (argc > 2)
    {
      error (_("too many arguments; try `%s -h' for more info"), progname);
      exit (EXIT_USAGE);
    }
  
  dbname = argv[0];
  if (argc == 2)
    filename = argv[1];
  else
    filename = NULL;

  if (!filename || strcmp (filename, "-") == 0)
    {
      filename = "<stdout>";
      fp = stdout;
    }
  else
    {
      fp = fopen (filename, "w");
      if (!fp)
	{
	  sys_perror (errno, _("cannot open %s"), filename);
	  exit (EXIT_FATAL);
	}
    }

  dbf = gdbm_open (dbname, 0, GDBM_READER, 0600, NULL);
  if (!dbf)
    {
      gdbm_perror (_("gdbm_open failed"));
      exit (EXIT_FATAL);
    }

  rc = gdbm_dump_to_file (dbf, fp, format);
  if (rc)
    {
      gdbm_perror (_("dump error"), filename);
    }
  
  gdbm_close (dbf);

  exit (rc == GDBM_NO_ERROR ? EXIT_OK : EXIT_FATAL);
}
  

Return to:

Send suggestions and report system problems to the System administrator.