whatis.c revision 1.2
1/* $NetBSD: whatis.c,v 1.2 2012/02/07 19:17:16 joerg Exp $ */ 2/*- 3 * Copyright (c) 2012 Joerg Sonnenberger <joerg@NetBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 27 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31#include <sys/cdefs.h> 32__RCSID("$NetBSD: whatis.c,v 1.2 2012/02/07 19:17:16 joerg Exp $"); 33 34#include <err.h> 35#include <stdio.h> 36#include <stdlib.h> 37#include <unistd.h> 38 39#include "apropos-utils.h" 40 41__dead static void 42usage(void) 43{ 44 fprintf(stderr, "%s ...\n", "whatis"); 45 exit(EXIT_FAILURE); 46} 47 48static int 49whatis(sqlite3 *db, const char *cmd) 50{ 51 static const char sqlstr[] = "SELECT name, section, name_desc" 52 " FROM mandb WHERE name=?" 53 " ORDER BY section, name"; 54 sqlite3_stmt *stmt = NULL; 55 int retval; 56 57 if (sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL) != SQLITE_OK) 58 errx(EXIT_FAILURE, "Unable to query database"); 59 if (sqlite3_bind_text(stmt, 1, cmd, -1, NULL) != SQLITE_OK) 60 errx(EXIT_FAILURE, "Unable to query database"); 61 retval = 1; 62 while (sqlite3_step(stmt) == SQLITE_ROW) { 63 printf("%s(%s) - %s\n", sqlite3_column_text(stmt, 0), 64 sqlite3_column_text(stmt, 1), 65 sqlite3_column_text(stmt, 2)); 66 retval = 0; 67 } 68 sqlite3_finalize(stmt); 69 if (retval) 70 fprintf(stderr, "%s: not found\n", cmd); 71 return retval; 72} 73 74int 75main(int argc, char *argv[]) 76{ 77 sqlite3 *db; 78 int ch, retval; 79 80 while ((ch = getopt(argc, argv, "")) != -1) { 81 switch (ch) { 82 default: 83 usage(); 84 } 85 } 86 argc -= optind; 87 argv += optind; 88 89 if (argc == 0) 90 usage(); 91 92 if ((db = init_db(MANDB_READONLY)) == NULL) 93 exit(EXIT_FAILURE); 94 95 retval = 0; 96 while (argc--) 97 retval |= whatis(db, *argv++); 98 99 close_db(db); 100 return retval; 101} 102