Fixed license declaration at spec file
[platform/upstream/gdbm.git] / tests / dtload.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 <unistd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include "dbm.h"
27 #include "progname.h"
28
29 #define PAGSUF ".pag"
30
31 int
32 main (int argc, char **argv)
33 {
34   const char *progname = canonical_progname (argv[0]);
35   char *dbname;
36   int line = 0;
37   char buf[1024];
38   datum key;
39   datum data;
40   int delim = '\t';
41   int data_z = 0;
42   
43   while (--argc)
44     {
45       char *arg = *++argv;
46
47       if (strcmp (arg, "-h") == 0)
48         {
49           printf ("usage: %s [-null] [-delim=CHR] DBFILE\n", progname);
50           exit (0);
51         }
52       else if (strcmp (arg, "-null") == 0)
53         data_z = 1;
54       else if (strncmp (arg, "-delim=", 7) == 0)
55         delim = arg[7];
56       else if (strcmp (arg, "--") == 0)
57         {
58           --argc;
59           ++argv;
60           break;
61         }
62       else if (arg[0] == '-')
63         {
64           fprintf (stderr, "%s: unknown option %s\n", progname, arg);
65           exit (1);
66         }
67       else
68         break;
69     }
70
71   if (argc != 1)
72     {
73       fprintf (stderr, "%s: wrong arguments\n", progname);
74       exit (1);
75     }
76
77   /* Check if .pag file exists. Create it if it doesn't, as DBM
78      cannot do it itself. */
79   
80   dbname = malloc (strlen (*argv) + sizeof (PAGSUF));
81   if (!dbname)
82     abort ();
83
84   strcat (strcpy (dbname, *argv), PAGSUF);
85
86   if (access (dbname, F_OK))
87     {
88       int fd = creat (dbname, 0644);
89       if (fd < 0)
90         {
91           fprintf (stderr, "%s: ", progname);
92           perror (dbname);
93           exit (1);
94         }
95       close (fd);
96     }
97   free (dbname);
98
99   if (dbminit (*argv))
100     {
101       fprintf (stderr, "dbminit failed\n");
102       exit (1);
103     }
104
105   while (fgets (buf, sizeof buf, stdin))
106     {
107       size_t i, j;
108       size_t len = strlen (buf);
109
110       if (buf[len - 1] != '\n')
111         {
112           fprintf (stderr, "%s: %d: line too long\n",
113                    progname, line);
114           continue;
115         }
116
117       buf[--len] = 0;
118       
119       line++;
120
121       for (i = j = 0; i < len; i++)
122         {
123           if (buf[i] == '\\')
124             i++;
125           else if (buf[i] == delim)
126             break;
127           else
128             buf[j++] = buf[i];
129         }
130
131       if (buf[i] != delim)
132         {
133           fprintf (stderr, "%s: %d: malformed line\n",
134                    progname, line);
135           continue;
136         }
137       buf[j] = 0;
138       
139       key.dptr = buf;
140       key.dsize = j + data_z;
141       data.dptr = buf + i + 1;
142       data.dsize = strlen (data.dptr) + data_z;
143       if (store (key, data) != 0)
144         {
145           fprintf (stderr, "%s: %d: item not inserted\n",
146                    progname, line);
147           exit (1);
148         }
149     }
150   dbmclose ();
151   exit (0);
152 }