1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/*
** Copyright 1998 - 1999 Double Precision, Inc.
** See COPYING for distribution information.
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "dbobj.h"
static int addgdbm(char *p, struct dbobj *o)
{
char *key, *val;
if (!*p || *p == '#')
return (0);
key=p;
if ( (val=strchr(p, '\t')) == 0) val="";
else *val++=0;
if (*key)
{
if (!*val) val="1";
if (dbobj_store(o, key, strlen(key), val, strlen(val), "I"))
{
fprintf(stderr, "Cannot store record for %s - duplicate or out of disk space.\n", key);
return (-1);
}
}
return (0);
}
static int buildgdbm(FILE *i, struct dbobj *o)
{
char *buf=0;
size_t bufsize, buflen;
bufsize=0;
buflen=0;
for (;;)
{
int c;
buflen=0;
for (;;)
{
if (buflen >= bufsize)
{
bufsize += 256;
buf= buf ? realloc(buf, bufsize):
malloc(bufsize);
if (!buf)
{
perror("malloc");
return (-1);
}
}
c=getc(i);
if (c == '\n' || c == EOF) break;
buf[buflen++]=c;
}
buf[buflen]=0;
if (c == EOF) return (-1);
if (strcmp(buf, ".") == 0) return (0);
if (addgdbm(buf, o)) return (-1);
}
}
int main(int argc, char **argv)
{
FILE *i;
struct dbobj obj;
if (argc < 4)
{
fprintf(stderr, "Usage: %s textfile tmpfile gdbmfile\n",
argv[0]);
exit(1);
}
if (strcmp(argv[1], "-") == 0)
i= stdin;
else
{
if ((i=fopen(argv[1], "r")) == 0)
{
perror(argv[1]);
exit(1);
}
}
dbobj_init(&obj);
if (dbobj_open(&obj, argv[2], "N"))
{
fprintf(stderr, "Cannot create %s\n", argv[2]);
exit (1);
}
if (buildgdbm(i, &obj))
{
dbobj_close(&obj);
unlink(argv[2]);
exit (1);
}
dbobj_close(&obj);
if (rename(argv[2], argv[3]))
{
perror("rename");
unlink(argv[2]);
exit(1);
}
exit(0);
return (0);
}
|