2 * rlm_dbm_cat.c : List rlm_dbm DBM file
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2001 Koulik Andrei, Sandy Service
33 #ifdef HAVE_GDBM_NDBM_H
34 #include <gdbm/ndbm.h>
37 #ifdef HAVE_GDBMNDBM_H
38 #include <gdbm-ndbm.h>
48 int wraplen = WRAPLEN, needwrap = 0, lotstup = LOTSTUP;
49 char const * progname;
51 static void dump_record(datum key,datum data)
55 for(i = 0, p = key.dptr; i < key.dsize; i++, p++)
57 if ( i < lotstup ) while( i++ <= lotstup) putchar(' ');
60 for(j = 0, p = data.dptr ; j < data.dsize && *p ; i++, p++ ) {
62 if ( needwrap && *p == ',' && i > wraplen ) putchar('\n');
63 if ( *p == '\n' || ( needwrap && *p == ',' && i > wraplen ) ) {
64 for(i = 0; i < lotstup; i++) putchar(' ');
72 static void usage(void)
74 fprintf(stderr, "Usage: %s: [-f file] [-w] [-i number] [-l number] [-v]\n\n",progname);
79 int main(int n, char **argv) {
81 const char *fname = NULL;
91 while ((ch = getopt(n, argv, "i:l:wf:v")) != -1)
93 case 'i': if (!isdigit((int) *optarg)) usage();
94 lotstup = atoi(optarg);
96 case 'l': if (!isdigit((int) *optarg)) usage();
97 wraplen = atoi(optarg);
99 case 'w': needwrap = 1;
101 case 'f': fname = optarg;
103 case 'v': printf("%s: $Id$\n",progname);
106 default : usage(); exit(1); break;
112 if ( fname == NULL) fname = "sandy_db";
114 if ( ( pdb = dbm_open(fname, O_RDONLY, 0777) ) == NULL ) {
115 perror("Couldn't open database");
119 for ( i = 1 ; i < n ; i++ ) {
120 printf(" Check: %s\n",argv[i]);
122 k.dsize = strlen(argv[i]) + 1;
123 if ( (d = dbm_fetch(pdb,k)).dptr == NULL ) {
124 printf("Not found\n");
125 } else dump_record(k, d);
128 for ( k = dbm_firstkey(pdb) ; k.dptr != NULL ; k = dbm_nextkey(pdb) )
129 if ( (d = dbm_fetch(pdb,k)).dptr == NULL ) {
130 perror("Couldn't fetch user record");
132 } else dump_record(k, d);