add changelog
[platform/upstream/gdbm.git] / tests / gtdel.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   int flags = 0;
31   GDBM_FILE dbf;
32   int data_z = 0;
33   int rc = 0;
34   
35   while (--argc)
36     {
37       char *arg = *++argv;
38
39       if (strcmp (arg, "-h") == 0)
40         {
41           printf ("usage: %s [-null] [-nolock] [-nommap] [-sync] DBFILE KEY [KEY...]\n",
42                   progname);
43           exit (0);
44         }
45       else if (strcmp (arg, "-null") == 0)
46         data_z = 1;
47       else if (strcmp (arg, "-nolock") == 0)
48         flags |= GDBM_NOLOCK;
49       else if (strcmp (arg, "-nommap") == 0)
50         flags |= GDBM_NOMMAP;
51       else if (strcmp (arg, "-sync") == 0)
52         flags |= GDBM_SYNC;
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 < 2)
69     {
70       fprintf (stderr, "%s: wrong arguments\n", progname);
71       exit (1);
72     }
73   dbname = *argv;
74   
75   dbf = gdbm_open (dbname, 0, GDBM_WRITER|flags, 0, NULL);
76   if (!dbf)
77     {
78       fprintf (stderr, "gdbm_open failed: %s\n", gdbm_strerror (gdbm_errno));
79       exit (1);
80     }
81
82   while (--argc)
83     {
84       char *arg = *++argv;
85
86       key.dptr = arg;
87       key.dsize = strlen (arg) + !!data_z;
88
89       if (gdbm_delete(dbf, key))
90         {
91           fprintf (stderr, "%s: cannot delete %s: %s\n",
92                    progname, arg, gdbm_strerror (gdbm_errno));
93           rc = 2;
94         }
95     }
96   gdbm_close (dbf);
97   exit (rc);
98 }