From 824eadc676706aef2500ed96141f60eeedb4a90c Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 30 Sep 2020 23:50:47 +0200 Subject: [PATCH 1/1] Start some basic command line options. --- plocate.cpp | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/plocate.cpp b/plocate.cpp index 672ba52..c41d65c 100644 --- a/plocate.cpp +++ b/plocate.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -383,7 +384,43 @@ void do_search_file(const string &needle, const char *filename) 1e3 * duration(steady_clock::now() - start).count(), matched); } +const char *dbpath = "/var/lib/mlocate/plocate.db"; + +void usage() +{ + printf("Usage: slocate [OPTION]... PATTERN\n"); + printf(" -d, --database DBPATH use DBPATH instead of default database (which is\n"); + printf(" %s)\n", dbpath); + printf(" -h, --help print this help\n"); +} + int main(int argc, char **argv) { - do_search_file(argv[1], "/var/lib/mlocate/plocate.db"); + static const struct option long_options[] = { + { "help", no_argument, 0, 'h' }, + { "database", required_argument, 0, 'd' }, + { 0, 0, 0, 0 } + }; + + for (;;) { + int option_index = 0; + int c = getopt_long(argc, argv, "d:h:", long_options, &option_index); + if (c == -1) { + break; + } + switch (c) { + case 'd': + dbpath = strdup(optarg); + break; + case 'h': + usage(); + exit(0); + } + } + + if (optind == argc) { + fprintf(stderr, "slocate: no pattern to search for specified\n"); + exit(0); + } + do_search_file(argv[optind], dbpath); } -- 2.39.2