1 /* Create simple DB database from textual input.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library 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 GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
34 #include "nss_db/dummy-db.h"
36 /* Get libc version number. */
37 #include "../version.h"
39 #define PACKAGE _libc_intl_domainname
41 /* If non-zero convert key to lower case. */
42 static int to_lowercase;
44 /* If non-zero print content of input file, one entry per line. */
47 /* If non-zero do not print informational messages. */
50 /* Name of output file. */
51 static const char *output_name;
53 /* Various definitions for the libdb handling. */
59 static int (*db_open) (const char *, int,
60 uint32_t, int, void *, void *, void **);
62 /* Constants which vary from version to version are actually variables
66 static int db_nooverwrite;
67 static int db_truncate;
70 /* Name and version of program. */
71 static void print_version (FILE *stream, struct argp_state *state);
72 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
74 /* Definitions of arguments for argp functions. */
75 static const struct argp_option options[] =
77 { "fold-case", 'f', NULL, 0, N_("Convert key to lower case") },
78 { "output", 'o', N_("NAME"), 0, N_("Write output to file NAME") },
79 { "quiet", 'q', NULL, 0,
80 N_("Do not print messages while building database") },
81 { "undo", 'u', NULL, 0,
82 N_("Print content of database file, one entry a line") },
83 { NULL, 0, NULL, 0, NULL }
86 /* Short description of program. */
87 static const char doc[] = N_("Create simple DB database from textual input.");
89 /* Strings for arguments in help texts. */
90 static const char args_doc[] = N_("\
91 INPUT-FILE OUTPUT-FILE\n-o OUTPUT-FILE INPUT-FILE\n-u INPUT-FILE");
93 /* Prototype for option handler. */
94 static error_t parse_opt (int key, char *arg, struct argp_state *state);
96 /* Function to print some extra text in the help message. */
97 static char *more_help (int key, const char *text, void *input);
99 /* Data structure to communicate with argp functions. */
100 static struct argp argp =
102 options, parse_opt, args_doc, doc, NULL, more_help
106 /* Prototypes for local functions. */
107 static int process_input (FILE *input, const char *inname, NSS_DB *output,
108 int to_lowercase, int be_quiet);
109 static int print_database (NSS_DB *db);
110 static NSS_DB *dbopen (const char *fname, int oper, int mode);
114 main (int argc, char *argv[])
116 const char *input_name;
123 /* Set locale via LC_ALL. */
124 setlocale (LC_ALL, "");
126 /* Set the text message domain. */
127 textdomain (_libc_intl_domainname);
129 /* Initialize local variables. */
132 /* Parse and process arguments. */
133 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
135 /* Determine file names. */
136 if (do_undo || output_name != NULL)
138 if (remaining + 1 != argc)
141 error (0, 0, gettext ("wrong number of arguments"));
142 argp_help (&argp, stdout, ARGP_HELP_SEE,
143 program_invocation_short_name);
146 input_name = argv[remaining];
150 if (remaining + 2 != argc)
151 goto wrong_arguments;
153 input_name = argv[remaining++];
154 output_name = argv[remaining];
157 /* Special handling if we are asked to print the database. */
160 db_file = dbopen (input_name, DB_RDONLY, 0666);
162 error (EXIT_FAILURE, 0, gettext ("cannot open database file `%s': %s"),
164 (errno == EINVAL ? gettext ("incorrectly formatted file")
165 : strerror (errno)));
167 status = print_database (db_file);
169 db_file->close (db_file->db, 0);
174 /* Open input file. */
175 if (strcmp (input_name, "-") == 0 || strcmp (input_name, "/dev/stdin") == 0)
181 input_file = fopen (input_name, "r");
182 if (input_file == NULL)
183 error (EXIT_FAILURE, errno, gettext ("cannot open input file `%s'"),
186 /* Get the access rights from the source file. The output file should
188 if (fstat (fileno (input_file), &st) >= 0)
189 mode = st.st_mode & ACCESSPERMS;
192 /* Open output file. This must not be standard output so we don't
193 handle "-" and "/dev/stdout" special. */
194 db_file = dbopen (output_name, DB_CREATE | db_truncate, mode);
196 error (EXIT_FAILURE, errno, gettext ("cannot open output file `%s'"),
199 /* Start the real work. */
200 status = process_input (input_file, input_name, db_file, to_lowercase,
204 if (input_file != stdin)
206 db_file->close (db_file->db, 0);
212 /* Handle program arguments. */
214 parse_opt (int key, char *arg, struct argp_state *state)
231 return ARGP_ERR_UNKNOWN;
238 more_help (int key, const char *text, void *input)
242 case ARGP_KEY_HELP_EXTRA:
243 /* We print some extra information. */
244 return strdup (gettext ("\
245 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
249 return (char *) text;
252 /* Print the version information. */
254 print_version (FILE *stream, struct argp_state *state)
256 fprintf (stream, "makedb (GNU %s) %s\n", PACKAGE, VERSION);
257 fprintf (stream, gettext ("\
258 Copyright (C) %s Free Software Foundation, Inc.\n\
259 This is free software; see the source for copying conditions. There is NO\n\
260 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
262 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
267 process_input (input, inname, output, to_lowercase, be_quiet)
281 status = EXIT_SUCCESS;
284 while (!feof (input))
291 n = getline (&line, &linelen, input);
293 /* This means end of file or some bug. */
296 /* Short read. Probably interrupted system call. */
301 if (line[n - 1] == '\n')
302 /* Remove trailing newline. */
306 while (isspace (*cp))
310 /* First non-space character in line '#': it's a comment. */
314 while (*cp != '\0' && !isspace (*cp))
322 /* It's an empty line. */
325 key.size = cp - (char *) key.data;
328 while (isspace (*cp))
332 val.size = (&line[n] - cp) + 1;
335 /* Store the value. */
336 status = output->put (output->db, NULL, &key, &val, db_nooverwrite);
339 if (status == DB_KEYEXIST)
342 error_at_line (0, 0, inname, linenr,
343 gettext ("duplicate key"));
344 /* This is no real error. Just give a warning. */
349 error (0, status, gettext ("while writing database file"));
351 status = EXIT_FAILURE;
360 error (0, 0, gettext ("problems while reading `%s'"), inname);
361 status = EXIT_FAILURE;
377 status = db->cursor (db->db, NULL, &cursor);
380 error (0, status, gettext ("while reading database"));
386 status = cursor->c_get (cursor->cursor, &key, &val, db_first);
389 printf ("%.*s %s\n", (int) key.size, (char *) key.data,
392 status = cursor->c_get (cursor->cursor, &key, &val, db_next);
395 if (status != DB_NOTFOUND)
397 error (0, status, gettext ("while reading database"));
408 static const char *libnames[] = { "libdb.so.3" };
412 for(x = 0; x < sizeof (libnames) / sizeof (libnames[0]); ++x)
414 handle = dlopen (libnames[x], RTLD_LAZY);
418 db_open = dlsym (handle, "db_open");
421 /* Alright, we got a library. Now find out which version it is. */
422 const char *(*db_version) (int *, int *, int *);
424 db_version = dlsym (handle, "db_version");
425 if (db_version != NULL)
427 /* Call the function and get the information. */
428 int major, minor, subminor;
430 DL_CALL_FCT (db_version, (&major, &minor, &subminor));
433 /* We currently cannot handle other versions than the
435 if (minor < 6 || (minor == 6 && subminor < 4))
437 libdb_version = db24;
438 db_first = DB24_FIRST;
440 db_nooverwrite = DB24_NOOVERWRITE;
441 db_truncate = DB24_TRUNCATE;
445 libdb_version = db27;
446 db_first = DB27_FIRST;
448 db_nooverwrite = DB27_NOOVERWRITE;
449 db_truncate = DB27_TRUNCATE;
454 if (libdb_version != nodb)
467 db_cursor (void *db, void *txn, NSS_DBC **dbcp)
473 switch (libdb_version)
476 ret = ((struct db24 *) db)->cursor (db, txn, &ptr);
479 ret = ((struct db27 *) db)->cursor (db, txn, &ptr, 0);
487 dbc = (NSS_DBC *) malloc (sizeof (NSS_DBC));
489 error (EXIT_FAILURE, errno, gettext ("while reading database"));
492 switch (libdb_version)
496 (int (*) (void *, void *, void *, uint32_t))
497 ((struct dbc24 *) ptr)->c_get;
501 (int (*) (void *, void *, void *, uint32_t))
502 ((struct dbc27 *) ptr)->c_get;
516 dbopen (const char *fname, int oper, int mode)
522 /* First load the shared object. */
528 /* Actually open the database. */
529 err = DL_CALL_FCT (db_open, (fname, DB_BTREE, oper, mode, NULL, NULL, &odb));
536 /* Construct the object we pass up. */
537 db = (NSS_DB *) malloc (sizeof (NSS_DB));
542 /* The functions are at different positions for the different
544 switch (libdb_version)
548 (int (*) (void *, uint32_t)) ((struct db24 *) odb)->close;
550 (int (*) (void *, int *)) ((struct db24 *) odb)->fd;
552 (int (*) (void *, void *, void *, void *, uint32_t))
553 ((struct db24 *) odb)->get;
555 (int (*) (void *, void *, void *, void *, uint32_t))
556 ((struct db24 *) odb)->put;
560 (int (*) (void *, uint32_t)) ((struct db27 *) odb)->close;
562 (int (*) (void *, int *)) ((struct db27 *) odb)->fd;
564 (int (*) (void *, void *, void *, void *, uint32_t))
565 ((struct db27 *) odb)->get;
567 (int (*) (void *, void *, void *, void *, uint32_t))
568 ((struct db27 *) odb)->put;
573 db->cursor = db_cursor;