Revision 13853,
1.9 KB
checked in by tb, 25 years ago
(diff) |
Check for <db.h> and <nbdm.h> in configure.in; use lert.h to select the right
db header file. Change .c files not to include <ndbm.h> anymore.
|
Line | |
---|
1 | /* |
---|
2 | file: lertload.c |
---|
3 | basic use: lertload a < file_of_names |
---|
4 | puts a set of names with a single letter category in the lert_msg db |
---|
5 | |
---|
6 | */ |
---|
7 | |
---|
8 | #include <stdio.h> |
---|
9 | #include <sys/types.h> |
---|
10 | #include <sys/stat.h> |
---|
11 | #include <fcntl.h> |
---|
12 | #include <sys/file.h> |
---|
13 | |
---|
14 | #include "lert.h" |
---|
15 | |
---|
16 | |
---|
17 | main(argc, argv) |
---|
18 | int argc; |
---|
19 | char ** argv; |
---|
20 | { |
---|
21 | char buffer[512]; |
---|
22 | DBM *db; |
---|
23 | datum key; |
---|
24 | datum old; |
---|
25 | datum data; |
---|
26 | register char *cp; |
---|
27 | register char *nd_p; |
---|
28 | int nd_c; |
---|
29 | int count; |
---|
30 | int new_char; |
---|
31 | char categ[128]; |
---|
32 | |
---|
33 | if (argc != 2) { |
---|
34 | fprintf(stderr, "usage: %s type\n", argv[0]); |
---|
35 | fprintf(stderr, " type is a single character category\n"); |
---|
36 | fprintf(stderr, " and a file of names is fed in as stdin\n"); |
---|
37 | fprintf(stderr, " and a matching lertdata file is written\n"); |
---|
38 | exit(1); |
---|
39 | } |
---|
40 | |
---|
41 | db = dbm_open(LERTS_DATA, O_RDWR|O_CREAT, 0600); |
---|
42 | if (db == NULL) { |
---|
43 | fprintf(stderr, "Unable to open database file %s.\n", LERTS_DATA); |
---|
44 | exit (1); |
---|
45 | } |
---|
46 | while(fgets(buffer, 512, stdin) != NULL) { |
---|
47 | cp = buffer; |
---|
48 | while (*cp != '\n' && *cp != '\0') cp++; |
---|
49 | *cp = '\0'; |
---|
50 | key.dptr = buffer; |
---|
51 | key.dsize = strlen(buffer) + 1; |
---|
52 | |
---|
53 | |
---|
54 | old = dbm_fetch(db, key); |
---|
55 | |
---|
56 | if (old.dptr == NULL) { |
---|
57 | categ[0] = argv[1][0]; |
---|
58 | nd_p = categ; |
---|
59 | data.dptr = categ; |
---|
60 | nd_c = 1; |
---|
61 | } else { |
---|
62 | cp = old.dptr; |
---|
63 | count = old.dsize; |
---|
64 | data.dptr = (char *)malloc(old.dsize + 1); |
---|
65 | |
---|
66 | nd_p = data.dptr; |
---|
67 | nd_c = 0; |
---|
68 | new_char = TRUE; |
---|
69 | |
---|
70 | while (count > nd_c) { |
---|
71 | *nd_p = *cp; |
---|
72 | /* |
---|
73 | already in list? |
---|
74 | */ |
---|
75 | if (*cp == argv[1][0]) { |
---|
76 | new_char = FALSE; |
---|
77 | } |
---|
78 | nd_p++; |
---|
79 | cp++; |
---|
80 | nd_c++; |
---|
81 | } |
---|
82 | |
---|
83 | if (new_char) { |
---|
84 | *nd_p = argv[1][0]; |
---|
85 | nd_c++; |
---|
86 | } |
---|
87 | } |
---|
88 | /* |
---|
89 | data.dptr = nd_p; |
---|
90 | */ |
---|
91 | data.dsize = nd_c; |
---|
92 | if (dbm_store(db, key, data, DBM_REPLACE) < 0) { |
---|
93 | fprintf(stderr, "dbm_store() failed: Entry = %d\n", key.dptr); |
---|
94 | (void) dbm_clearerr(db); |
---|
95 | } |
---|
96 | } |
---|
97 | return (0); |
---|
98 | } |
---|
99 | |
---|
Note: See
TracBrowser
for help on using the repository browser.