|
|
1.1 root 1: #include <stdio.h>
2: #include <gdbm.h>
3: #include <sys/file.h>
4: #include <ctype.h>
5:
6: main (argc, argv)
7: int argc;
8: char **argv;
9: {
10: int filearg, verbose;
11: FILE *fp;
12: GDBM_FILE db;
13: datum key, content;
14: char buf[100000], kbuf[256];
15: char *bp, *rc;
16: int buflen, len;
17: char type[80], version[256];
18: char gfname[1024];
19: char *TidyString();
20:
21: if ( argc < 2 || argc > 3 ) {
22: fprintf(stderr, "usage: %s [-v] edbfile\n", argv[0]);
23: exit(1);
24: }
25:
26: filearg = 1;
27: verbose = 0;
28: if ( argc == 3 )
29: if ( strcmp(argv[1], "-v") == 0 ) {
30: verbose++;
31: filearg++;
32: } else if ( strcmp(argv[1], "-vv") == 0 ) {
33: verbose += 2;
34: filearg++;
35: } else
36: fprintf(stderr, "invalid flag ignored: %s\n", argv[1]);
37:
38: if ( (fp = fopen(argv[filearg], "r")) == NULL ) {
39: perror(argv[filearg]);
40: exit(1);
41: }
42:
43: strcpy(gfname, argv[filearg]);
44: strcat(gfname, ".gdbm");
45: if ( (db = gdbm_open(gfname, 0, GDBM_NEWDB, 00664, 0)) == NULL ) {
46: fprintf(stderr, "could not open %s\n", gfname);
47: exit(1);
48: }
49:
50: if ( fgets(type, sizeof(type), fp) == NULL ) {
51: fprintf(stderr, "File is empty!\n");
52: exit(1);
53: }
54: if ( fgets(version, sizeof(version), fp) == NULL ) {
55: fprintf(stderr, "No version specified!\n");
56: exit(1);
57: }
58: sprintf(buf, "%s%s", type, version);
59: key.dptr = "HEADER";
60: key.dsize = sizeof("HEADER");
61: content.dptr = buf;
62: content.dsize = strlen(buf) + 1;
63: if ( verbose > 0 ) printf("HEADER: (%s)\n", content.dptr);
64: if ( gdbm_store(db, key, content, GDBM_INSERT) != 0 ) {
65: fprintf(stderr, "could not dbm_store header");
66: exit(1);
67: }
68:
69: while ( feof(fp) == 0 ) {
70: buflen = sizeof(buf);
71: bp = buf;
72: while ( rc = fgets(bp, buflen, fp) )
73: if ( *buf != '#' && *buf != '\n' ) break;
74: if ( rc == NULL ) break;
75:
76: strcpy(kbuf, buf);
77: kbuf[strlen(kbuf)-1] = '\0';
78: key.dptr = TidyString(kbuf);
79: key.dsize = strlen(kbuf) + 1;
80:
81: if ( verbose > 0 ) printf("key (%s)\n", key.dptr);
82:
83: sprintf(buf, "%s\n", kbuf);
84: len = strlen(bp);
85: bp += len;
86: buflen -= len;
87:
88: while ( rc = fgets(bp, buflen, fp) ) {
89: if ( *bp == '\n' ) break;
90:
91: len = strlen(bp);
92: bp += len;
93: buflen -= len;
94: }
95: *bp = '\n';
96:
97: content.dptr = buf;
98: content.dsize = strlen(buf) + 1;
99: if ( verbose > 1 ) printf("content (%s)\n", content.dptr);
100: if ( gdbm_store(db, key, content, GDBM_INSERT) != 0 ) {
101: fprintf(stderr, "error: dbm_store\n");
102: exit(1);
103: }
104: free(content.dptr);
105: free(key.dptr);
106: }
107:
108: (void) gdbm_close(db);
109: }
110:
111:
112: char * TidyString (a)
113: register char * a;
114: {
115: register char * b;
116: char * c;
117: register int i = 0;
118:
119: if (!*a)
120: return (a);
121:
122: /* remove white space from front of string */
123: while (isspace (*a))
124: a++;
125:
126: /* now continue removing multiple and trailing spaces */
127: c = a, b = a;
128: while (*a) {
129: if (isspace (*a)) {
130: *b = ' '; /* make sure not a tab etc */
131: while (isspace (*++a))
132: i = 1;
133:
134: if (*a)
135: b++;
136: else
137: break;
138: }
139: if (i == 1)
140: *b = *a;
141:
142: a++, b++;
143: }
144:
145: *b = 0;
146:
147: if (*--b == '\n')
148: *b-- = 0;
149:
150: if (*b == ' ')
151: *b = 0;
152:
153: return (c);
154: }
155:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.