source: trunk/athena/bin/lert/lertload.c @ 22415

Revision 22415, 2.1 KB checked in by ghudson, 19 years ago (diff)
Close the database when we're done.
RevLine 
[7779]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>
[17860]9#include <stdlib.h>
10#include <unistd.h>
11#include <string.h>
[7779]12#include <sys/types.h>
13#include <sys/stat.h>
14#include <fcntl.h>
15#include <sys/file.h>
[14432]16#include <ctype.h>
[7779]17
18#include "lert.h"     
19
[17860]20int main(int argc, char **argv)
[7779]21{
22  char buffer[512];
23  DBM *db;
24  datum key;
25  datum old;
26  datum data;
[14432]27  char *cp;
28  char *bp;
29  char *nd_p;
[7779]30  int nd_c;
31  int count;
32  int new_char;
33  char categ[128];
34
35  if (argc != 2) {
36     fprintf(stderr, "usage: %s type\n", argv[0]);
37     fprintf(stderr, "   type is a single character category\n");
38     fprintf(stderr, "   and a file of names is fed in as stdin\n");
39     fprintf(stderr, "   and a matching lertdata file is written\n");
40     exit(1);
41  }
42   
43  db = dbm_open(LERTS_DATA, O_RDWR|O_CREAT, 0600);
44  if (db == NULL) {
45    fprintf(stderr, "Unable to open database file %s.\n", LERTS_DATA);
46    exit (1);
47  }
48  while(fgets(buffer, 512, stdin) != NULL) {
[14432]49    bp = buffer;
50    while (isspace(*bp))
51      bp++; /* skip leading spaces */
52    cp = bp;
53    while (!isspace(*cp) && *cp != '\0')
54      cp++;
[7779]55    *cp = '\0';
[14432]56    key.dptr = bp;
57    key.dsize = strlen(bp) + 1;
[7779]58
59
60    old = dbm_fetch(db, key);
61
62    if (old.dptr == NULL) {
63      categ[0] = argv[1][0];
64      nd_p = categ;
65      data.dptr = categ;
66      nd_c = 1;
67    } else {
68      cp = old.dptr;
69      count = old.dsize;
70      data.dptr = (char *)malloc(old.dsize + 1);
71
72      nd_p = data.dptr;
73      nd_c = 0;
74      new_char = TRUE;
75
76      while (count > nd_c) {
77        *nd_p = *cp;
78/*
79   already in list?
80 */
81        if (*cp == argv[1][0]) {
82          new_char = FALSE;
83        }
84      nd_p++;
85      cp++;
86      nd_c++;
87      }
88
89      if (new_char) {
90        *nd_p = argv[1][0];
91        nd_c++;
92      }
93    }
94/*
95    data.dptr = nd_p;
96 */
97    data.dsize = nd_c;
98    if (dbm_store(db, key, data, DBM_REPLACE) < 0) {
99      fprintf(stderr, "dbm_store() failed: Entry = %d\n", key.dptr);
100      (void) dbm_clearerr(db);
101    }
102  }
[22415]103  dbm_close(db);
[7779]104  return (0);
105}
106   
Note: See TracBrowser for help on using the repository browser.