#include #include #include #include #include #include /* Create a gdbm data base from stdin with binary format: key_size (4 bytes), key, content_size (4 bytes), content P. Humblet July 2003. Public domain, use at own risk. */ main (int argc, char * argv[]) { GDBM_FILE dbf; datum key, content; int block_size, count = 0; int res; if (argc < 2 || argc > 3) { fprintf (stderr,"Usage loadgdbm filename [block_size]\n"); exit (1); } if (argc == 2) block_size = 512; else block_size = atoi (argv[2]); if (!access (argv[1], F_OK)) { fprintf (stderr,"%s already exists\n", argv[1]); exit (1); } dbf = gdbm_open (argv[1], block_size, GDBM_NEWDB, 0777, NULL); if (dbf == 0) { fprintf(stderr, "Error opening %s: %s\n", argv[1], errno?strerror (errno):gdbm_strerror (gdbm_errno)); exit(1); } while (1) { if (fread (&key.dsize, sizeof(key.dsize), 1, stdin) != 1) { break; } if (!(key.dptr = (char *) malloc (key.dsize))) { perror ("Malloc key"); goto err; } if (fread (key.dptr, key.dsize, 1, stdin) != 1) { perror ("Key"); goto err; } if (fread (&content.dsize, sizeof(content.dsize), 1, stdin) != 1) { perror("Content size"); goto err; } if (!(content.dptr = (char *) malloc (content.dsize))) { perror ("Malloc content"); goto err; } if ((res=fread (content.dptr, content.dsize, 1, stdin)) != 1) { perror("Content"); fprintf(stderr, "%d %d\n", res, content.dsize); goto err; } if (gdbm_store ( dbf, key, content, GDBM_INSERT )) { fprintf(stderr, "gdbm_store: %s\n", errno?strerror (errno):gdbm_strerror (gdbm_errno)); goto err; } free (key.dptr); free (content.dptr); count++; } gdbm_close (dbf); fprintf (stderr,"%d records loaded\n", count); exit (0); err: gdbm_close (dbf); fprintf (stderr,"Error at record %d\n", count); exit (1); }