Fixed license declaration at spec file
[platform/upstream/gdbm.git] / tests / gtload.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 <errno.h>
22 #include "gdbm.h"
23 #include "progname.h"
24
25 int
26 main (int argc, char **argv)
27 {
28   const char *progname = canonical_progname (argv[0]);
29   const char *dbname;
30   int line = 0;
31   char buf[1024];
32   datum key;
33   datum data;
34   int replace = 0;
35   int flags = 0;
36   int mode = GDBM_WRCREAT;
37   int block_size = 0;
38   GDBM_FILE dbf;
39   int delim = '\t';
40   int data_z = 0;
41   size_t mapped_size_max = 0;
42   
43   while (--argc)
44     {
45       char *arg = *++argv;
46
47       if (strcmp (arg, "-h") == 0)
48         {
49           printf ("usage: %s [-replace] [-clear] [-blocksize=N] [-null] [-nolock] [-nommap] [-maxmap=N] [-sync] [-delim=CHR] DBFILE\n", progname);
50           exit (0);
51         }
52       else if (strcmp (arg, "-replace") == 0)
53         replace |= GDBM_REPLACE;
54       else if (strcmp (arg, "-clear") == 0)
55         mode = GDBM_NEWDB;
56       else if (strcmp (arg, "-null") == 0)
57         data_z = 1;
58       else if (strcmp (arg, "-nolock") == 0)
59         flags |= GDBM_NOLOCK;
60       else if (strcmp (arg, "-nommap") == 0)
61         flags |= GDBM_NOMMAP;
62       else if (strcmp (arg, "-sync") == 0)
63         flags |= GDBM_SYNC;
64       else if (strncmp (arg, "-blocksize=", 11) == 0)
65         block_size = atoi (arg + 11);
66       else if (strncmp (arg, "-maxmap=", 8) == 0)
67         {
68           char *p;
69
70           errno = 0;
71           mapped_size_max = strtoul (arg + 8, &p, 10);
72           
73           if (errno)
74             {
75               fprintf (stderr, "%s: ", progname);
76               perror ("maxmap");
77               exit (1);
78             }
79
80           if (*p)
81             {
82               fprintf (stderr, "%s: bad maxmap\n", progname);
83               exit (1);
84             }
85         }
86       else if (strncmp (arg, "-delim=", 7) == 0)
87         delim = arg[7];
88       else if (strcmp (arg, "--") == 0)
89         {
90           --argc;
91           ++argv;
92           break;
93         }
94       else if (arg[0] == '-')
95         {
96           fprintf (stderr, "%s: unknown option %s\n", progname, arg);
97           exit (1);
98         }
99       else
100         break;
101     }
102
103   if (argc != 1)
104     {
105       fprintf (stderr, "%s: wrong arguments\n", progname);
106       exit (1);
107     }
108   dbname = *argv;
109   
110   dbf = gdbm_open (dbname, block_size, mode|flags, 00664, NULL);
111   if (!dbf)
112     {
113       fprintf (stderr, "gdbm_open failed: %s\n", gdbm_strerror (gdbm_errno));
114       exit (1);
115     }
116
117   if (mapped_size_max)
118     {
119       if (gdbm_setopt (dbf, GDBM_SETMAXMAPSIZE, &mapped_size_max,
120                        sizeof (mapped_size_max)))
121         {
122           fprintf (stderr, "gdbm_setopt failed: %s\n",
123                    gdbm_strerror (gdbm_errno));
124           exit (1);
125         }
126     }  
127   
128   while (fgets (buf, sizeof buf, stdin))
129     {
130       size_t i, j;
131       size_t len = strlen (buf);
132
133       if (buf[len - 1] != '\n')
134         {
135           fprintf (stderr, "%s: %d: line too long\n",
136                    progname, line);
137           continue;
138         }
139
140       buf[--len] = 0;
141       
142       line++;
143
144       for (i = j = 0; i < len; i++)
145         {
146           if (buf[i] == '\\')
147             i++;
148           else if (buf[i] == delim)
149             break;
150           else
151             buf[j++] = buf[i];
152         }
153
154       if (buf[i] != delim)
155         {
156           fprintf (stderr, "%s: %d: malformed line\n",
157                    progname, line);
158           continue;
159         }
160       buf[j] = 0;
161       
162       key.dptr = buf;
163       key.dsize = j + data_z;
164       data.dptr = buf + i + 1;
165       data.dsize = strlen (data.dptr) + data_z;
166       if (gdbm_store (dbf, key, data, replace) != 0)
167         {
168           fprintf (stderr, "%s: %d: item not inserted\n",
169                    progname, line);
170           exit (1);
171         }
172     }
173   gdbm_close (dbf);
174   exit (0);
175 }