Fixed license declaration at spec file
[platform/upstream/gdbm.git] / tests / gtfetch.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 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   const char *dbname;
42   datum key;
43   datum data;
44   int flags = 0;
45   GDBM_FILE dbf;
46   int data_z = 0;
47   int delim = 0;
48   int rc = 0;
49   
50   while (--argc)
51     {
52       char *arg = *++argv;
53
54       if (strcmp (arg, "-h") == 0)
55         {
56           printf ("usage: %s [-nolock] [-nommap] [-null] [-delim=CHR] DBFILE KEY [KEY...]\n",
57                   progname);
58           exit (0);
59         }
60       else if (strcmp (arg, "-nolock") == 0)
61         flags |= GDBM_NOLOCK;
62       else if (strcmp (arg, "-nommap") == 0)
63         flags |= GDBM_NOMMAP;
64       else if (strcmp (arg, "-null") == 0)
65         data_z = 1;
66       else if (strncmp (arg, "-delim=", 7) == 0)
67         delim = arg[7];
68       else if (strcmp (arg, "--") == 0)
69         {
70           --argc;
71           ++argv;
72           break;
73         }
74       else if (arg[0] == '-')
75         {
76           fprintf (stderr, "%s: unknown option %s\n", progname, arg);
77           exit (1);
78         }
79       else
80         break;
81     }
82
83   if (argc < 2)
84     {
85       fprintf (stderr, "%s: wrong arguments\n", progname);
86       exit (1);
87     }
88   dbname = *argv;
89   
90   dbf = gdbm_open (dbname, 0, GDBM_READER|flags, 00664, NULL);
91   if (!dbf)
92     {
93       fprintf (stderr, "gdbm_open failed: %s\n", gdbm_strerror (gdbm_errno));
94       exit (1);
95     }
96
97   while (--argc)
98     {
99       char *arg = *++argv;
100
101       key.dptr = arg;
102       key.dsize = strlen (arg) + !!data_z;
103
104       data = gdbm_fetch (dbf, key);
105       if (data.dptr == NULL)
106         {
107           rc = 2;
108           fprintf (stderr, "%s: ", progname);
109           print_key (stderr, key, delim);
110           fprintf (stderr, ": not found\n");
111           continue;
112         }
113       if (delim)
114         {
115           print_key (stdout, key, delim);
116           fputc (delim, stdout);
117         }
118
119       fwrite (data.dptr, data.dsize - !!data_z, 1, stdout);
120       free (data.dptr);
121       
122       fputc ('\n', stdout);
123     }
124
125   gdbm_close (dbf);
126   exit (rc);
127 }