import from HEAD:
[freeradius.git] / src / modules / rlm_dbm / rlm_dbm_cat.c
1 /*
2  * rlm_dbm_cat.c :    List rlm_dbm DBM file
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2001 Koulik Andrei, Sandy Service
21  */
22
23 #include "autoconf.h"
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #ifdef HAVE_NDBM_H
30 #include <ndbm.h>
31 #endif
32
33 #ifdef HAVE_GDBM_NDBM_H
34 #include <gdbm/ndbm.h>
35 #endif
36
37 #ifdef HAVE_GDBMNDBM_H
38 #include <gdbm-ndbm.h>
39 #endif
40
41 #include <unistd.h>
42 #include <ctype.h>
43
44 #define LOTSTUP 20
45 #define WRAPLEN 40
46
47
48 int wraplen = WRAPLEN, needwrap = 0, lotstup = LOTSTUP;
49 char const * progname;
50
51 static void dump_record(datum key,datum data)
52 {
53         int i,j;
54         char *p;
55         for(i = 0, p = key.dptr; i < key.dsize; i++, p++)
56           putchar(*p);
57         if ( i < lotstup ) while( i++ <= lotstup) putchar(' ');
58                 else putchar(' ');
59
60         for(j = 0, p = data.dptr ; j < data.dsize && *p ; i++, p++ ) {
61                 putchar(*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(' ');
65                         i = 0;
66                 }
67         }
68
69         putchar('\n');
70 }
71
72 static void usage(void)
73 {
74         fprintf(stderr, "Usage: %s: [-f file] [-w] [-i number] [-l number] [-v]\n\n",progname);
75
76         exit(1);
77 }
78
79 int main(int n, char **argv) {
80
81         const char      *fname = NULL;
82         DBM     *pdb;
83         datum   k,d;
84         int     ch;
85         int     i;
86
87         progname = argv[0];
88
89
90
91         while ((ch = getopt(n, argv, "i:l:wf:v")) != -1)
92                 switch (ch) {
93                         case 'i':       if (!isdigit((int) *optarg)) usage();
94                                         lotstup = atoi(optarg);
95                                         break;
96                         case 'l':       if (!isdigit((int) *optarg)) usage();
97                                         wraplen = atoi(optarg);
98                                         break;
99                         case 'w':       needwrap = 1;
100                                         break;
101                         case 'f':       fname = optarg;
102                                         break;
103                         case 'v':       printf("%s: $Id$\n",progname);
104                                         exit(0);
105                                         break;
106                         default : usage(); exit(1); break;
107
108                 }
109         n -= (optind - 1);
110         argv += (optind -1);
111
112         if ( fname == NULL) fname = "sandy_db";
113
114         if ( ( pdb = dbm_open(fname, O_RDONLY, 0777) ) == NULL ) {
115                 perror("Couldn't open database");
116                 exit(1);
117         }
118         if ( n > 1 ) {
119                 for ( i = 1 ; i < n ; i++ ) {
120                         printf(" Check: %s\n",argv[i]);
121                         k.dptr  = 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);
126                 }
127         } else {
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");
131                                 exit(1);
132                         } else dump_record(k, d);
133         }
134         dbm_close(pdb);
135         fflush(stdout);
136
137         return 0;
138
139 }