Fixed license declaration at spec file
[platform/upstream/gdbm.git] / tests / dtfetch.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 "dbm.h"
22 #include "progname.h"
23
24 void
25 print_key (FILE *fp, datum key, int delim)
26 {
27   size_t i;
28   
29   for (i = 0; i < key.dsize && key.dptr[i]; i++)
30     {
31       if (key.dptr[i] == delim || key.dptr[i] == '\\')
32         fputc ('\\', fp);
33       fputc (key.dptr[i], fp);
34     }
35 }
36
37 int
38 main (int argc, char **argv)
39 {
40   const char *progname = canonical_progname (argv[0]);
41   char *dbname;
42   datum key;
43   datum data;
44   int data_z = 0;
45   int delim = 0;
46   int rc = 0;
47   
48   while (--argc)
49     {
50       char *arg = *++argv;
51
52       if (strcmp (arg, "-h") == 0)
53         {
54           printf ("usage: %s [-null] [-delim=CHR] DBFILE KEY [KEY...]\n",
55                   progname);
56           exit (0);
57         }
58       else if (strcmp (arg, "-null") == 0)
59         data_z = 1;
60       else if (strncmp (arg, "-delim=", 7) == 0)
61         delim = arg[7];
62       else if (strcmp (arg, "--") == 0)
63         {
64           --argc;
65           ++argv;
66           break;
67         }
68       else if (arg[0] == '-')
69         {
70           fprintf (stderr, "%s: unknown option %s\n", progname, arg);
71           exit (1);
72         }
73       else
74         break;
75     }
76
77   if (argc < 2)
78     {
79       fprintf (stderr, "%s: wrong arguments\n", progname);
80       exit (1);
81     }
82   dbname = *argv;
83
84   if (dbminit (dbname))
85     {
86       fprintf (stderr, "dbminit failed\n");
87       exit (1);
88     }
89
90   while (--argc)
91     {
92       char *arg = *++argv;
93
94       key.dptr = arg;
95       key.dsize = strlen (arg) + !!data_z;
96
97       data = fetch (key);
98       if (data.dptr == NULL)
99         {
100           rc = 2;
101           fprintf (stderr, "%s: ", progname);
102           print_key (stderr, key, delim);
103           fprintf (stderr, ": not found\n");
104           continue;
105         }
106       if (delim)
107         {
108           print_key (stdout, key, delim);
109           fputc (delim, stdout);
110         }
111
112       fwrite (data.dptr, data.dsize - !!data_z, 1, stdout);
113       
114       fputc ('\n', stdout);
115     }
116
117   dbmclose ();
118   exit (rc);
119 }