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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2001 Koulik Andrei, Sandy Service
23 #include <freeradius-devel/autoconf.h>
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(' ');
73 static void __attribute__((noreturn)) usage(void)
75 static void usage(void)
78 fprintf(stderr, "Usage: %s: [-f file] [-w] [-i number] [-l number] [-v]\n\n",progname);
83 int main(int n, char **argv) {
85 const char *fname = NULL;
95 while ((ch = getopt(n, argv, "i:l:wf:v")) != -1)
97 case 'i': if (!isdigit((int) *optarg)) usage();
98 lotstup = atoi(optarg);
100 case 'l': if (!isdigit((int) *optarg)) usage();
101 wraplen = atoi(optarg);
103 case 'w': needwrap = 1;
105 case 'f': fname = optarg;
107 case 'v': printf("%s: $Id$\n",progname);
110 default : usage(); exit(1); break;
116 if ( fname == NULL) fname = "sandy_db";
118 if ( ( pdb = dbm_open(fname, O_RDONLY, 0777) ) == NULL ) {
119 perror("Couldn't open database");
123 for ( i = 1 ; i < n ; i++ ) {
124 printf(" Check: %s\n",argv[i]);
126 k.dsize = strlen(argv[i]) + 1;
127 if ( (d = dbm_fetch(pdb,k)).dptr == NULL ) {
128 printf("Not found\n");
129 } else dump_record(k, d);
132 for ( k = dbm_firstkey(pdb) ; k.dptr != NULL ; k = dbm_nextkey(pdb) )
133 if ( (d = dbm_fetch(pdb,k)).dptr == NULL ) {
134 perror("Couldn't fetch user record");
136 } else dump_record(k, d);