Revision 8180,
1.9 KB
checked in by epeisach, 29 years ago
(diff) |
Include sys/types.h before sys/stat.h
|
Line | |
---|
1 | /* |
---|
2 | file: lertstop.c |
---|
3 | basic use: lertstop type |
---|
4 | remove a single letter category from db |
---|
5 | |
---|
6 | ndbm apparently fails in traversing the data if you delete or |
---|
7 | change keys while traversing. |
---|
8 | options--restart traversing or build a linked list of changes... |
---|
9 | |
---|
10 | q&d program...restart traverse! |
---|
11 | |
---|
12 | */ |
---|
13 | |
---|
14 | #include <stdio.h> |
---|
15 | #include <sys/types.h> |
---|
16 | #include <sys/stat.h> |
---|
17 | #include <fcntl.h> |
---|
18 | #include <ndbm.h> |
---|
19 | #include <sys/file.h> |
---|
20 | |
---|
21 | #include "lert.h" |
---|
22 | |
---|
23 | main(argc, argv) |
---|
24 | int argc; |
---|
25 | char ** argv; |
---|
26 | { |
---|
27 | char buffer[512]; |
---|
28 | DBM *db; |
---|
29 | datum key; |
---|
30 | datum new; |
---|
31 | datum data; |
---|
32 | register char *cp; |
---|
33 | register char *nd_p; |
---|
34 | int nd_c; |
---|
35 | int count = 0; |
---|
36 | int changed; |
---|
37 | int new_char; |
---|
38 | char * name_p; |
---|
39 | int name_c; |
---|
40 | char categ[128]; |
---|
41 | |
---|
42 | if (argc != 2) { |
---|
43 | fprintf(stderr, "usage: %s type\n", argv[0]); |
---|
44 | fprintf(stderr, " type is a single character category\n"); |
---|
45 | fprintf(stderr, " to be removed from the lertdata file\n"); |
---|
46 | exit(1); |
---|
47 | } |
---|
48 | |
---|
49 | db = dbm_open(LERTS_DATA, O_RDWR, 0600); |
---|
50 | if (db == NULL) { |
---|
51 | fprintf(stderr, "Unable to open database file %s.\n", LERTS_DATA); |
---|
52 | exit (1); |
---|
53 | } |
---|
54 | key = dbm_firstkey(db); |
---|
55 | while (key.dptr != NULL) { |
---|
56 | data = dbm_fetch(db, key); |
---|
57 | if (!dbm_error(db)) { |
---|
58 | cp = categ; |
---|
59 | changed = FALSE; |
---|
60 | for(name_c = data.dsize, name_p = data.dptr; name_c > 0; name_c--) { |
---|
61 | if (*name_p != argv[1][0]) { |
---|
62 | *cp = *name_p; |
---|
63 | cp++; |
---|
64 | } else { |
---|
65 | changed = TRUE; |
---|
66 | } |
---|
67 | name_p++; |
---|
68 | } |
---|
69 | |
---|
70 | if (changed) { |
---|
71 | if (data.dsize == 1) { |
---|
72 | if (dbm_delete(db, key) < 0) { |
---|
73 | fprintf(stderr, "dbm_delete() failed\n"); |
---|
74 | (void) dbm_clearerr(db); |
---|
75 | } |
---|
76 | } else { |
---|
77 | new.dsize = data.dsize - 1; |
---|
78 | new.dptr = categ; |
---|
79 | if (dbm_store(db, key, new, DBM_REPLACE) < 0) { |
---|
80 | fprintf(stderr, "dbm_store() failed\n"); |
---|
81 | (void) dbm_clearerr(db); |
---|
82 | } |
---|
83 | } |
---|
84 | key = dbm_firstkey(db); |
---|
85 | } else { |
---|
86 | key = dbm_nextkey(db); |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | return (0); |
---|
91 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.