1 /* gdbmimp.c - Import a GDBM database. */
3 /* This file is part of GDBM, the GNU data base manager.
4 Copyright (C) 2007, 2011, 2013 Free Software Foundation, Inc.
6 GDBM is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GDBM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
19 # include "autoconf.h"
20 # include <arpa/inet.h>
22 # include "gdbmdefs.h"
26 gdbm_import_from_file (GDBM_FILE dbf, FILE *fp, int flag)
28 int seenbang, seennewline, rsize, size, kbufsize, dbufsize, rret;
30 char *kbuffer, *dbuffer;
39 /* Read (and discard) four lines begining with ! and ending with \n. */
42 if ((c = fgetc (fp)) == -1)
49 if (seenbang > 3 && seennewline > 2)
51 /* End of last line. */
58 /* Allocate buffers. */
60 kbuffer = malloc (kbufsize);
63 gdbm_errno = GDBM_MALLOC_ERROR;
67 dbuffer = malloc (dbufsize);
71 gdbm_errno = GDBM_MALLOC_ERROR;
75 /* Insert/replace records in the database until we run out of file. */
76 while ((rret = fread (&rsize, sizeof (rsize), 1, fp)) == 1)
82 kbufsize = (size + 512);
83 kbuffer = realloc (kbuffer, kbufsize);
87 gdbm_errno = GDBM_MALLOC_ERROR;
91 if (fread (kbuffer, size, 1, fp) != 1)
98 if (fread (&rsize, sizeof (rsize), 1, fp) != 1)
101 size = ntohl (rsize);
104 dbufsize = (size + 512);
105 dbuffer = realloc (dbuffer, dbufsize);
109 gdbm_errno = GDBM_MALLOC_ERROR;
113 if (fread (dbuffer, size, 1, fp) != 1)
119 if (gdbm_store (dbf, key, data, flag) != 0)
121 /* Keep the existing errno. */
137 gdbm_errno = GDBM_FILE_READ_ERROR;
142 gdbm_import (GDBM_FILE dbf, const char *importfile, int flag)
147 fp = fopen (importfile, "r");
150 gdbm_errno = GDBM_FILE_OPEN_ERROR;
153 rc = gdbm_import_from_file (dbf, fp, flag);