From 8ed85f5e5e82146dcd869c5f260252e92bad2662 Mon Sep 17 00:00:00 2001 From: Mark Atwood Date: Wed, 19 Sep 2007 05:29:25 -0700 Subject: [PATCH] add memrm tool --- src/Makefile.am | 5 ++- src/memrm.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/memrm.c diff --git a/src/Makefile.am b/src/Makefile.am index fc47cc49..38146fb3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,10 +1,13 @@ INCLUDES = -I$(top_builddir)/include LDADDS = ../lib/libmemcached.la -bin_PROGRAMS = memcat memcp +bin_PROGRAMS = memcat memcp memrm memcat_SOURCES = memcat.c memcat_LDADD = $(LDADDS) memcp_SOURCES = memcp.c memcp_LDADD = $(LDADDS) + +memrm_SOURCES = memrm.c +memrm_LDADD = $(LDADDS) diff --git a/src/memrm.c b/src/memrm.c new file mode 100644 index 00000000..519d9398 --- /dev/null +++ b/src/memrm.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include "client_options.h" + +static int opt_verbose; +static char *opt_servers; + +struct memcached_st *parse_opt_servers (struct memcached_st *m, + char *opt_servers) +{ + char *s, *hostname; + unsigned int portnum; + while (s = strsep(&opt_servers, ",")) { + hostname = strsep(&s, ":"); + portnum = atoi(s); + memcached_server_add(m, hostname, portnum); + } + return m; +} + +int main(int argc, char *argv[]) +{ + memcached_st *memc; + char *string; + size_t string_length; + time_t expires = 0; + memcached_return rc; + + static struct option long_options[] = + { + {"version", no_argument, NULL, OPT_VERSION}, + {"help", no_argument, NULL, OPT_HELP}, + {"verbose", no_argument, &opt_verbose, 1}, + {"debug", no_argument, &opt_verbose, 2}, + {"servers", required_argument, NULL, OPT_SERVERS}, + {"expire", required_argument, NULL, OPT_EXPIRE}, + {0, 0, 0, 0}, + }; + int option_index = 0; + int option_rv; + + while (1) + { + option_rv = getopt_long(argc, argv, "", long_options, &option_index); + if (option_rv == -1) break; + switch (option_rv) { + case 0: + break; + case OPT_VERSION: /* --version */ + printf("memcache tools, memrm, v1.0\n"); + exit(0); + break; + case OPT_HELP: /* --help */ + printf("useful help messages go here\n"); + exit(0); + break; + case OPT_SERVERS: /* --servers */ + opt_servers = strdup(optarg); + break; + case OPT_EXPIRE: /* --expire */ + expires = (time_t)atoi(optarg); + break; + case '?': + /* getopt_long already printed an error message. */ + exit(1); + default: + abort(); + } + } + + memc= memcached_init(NULL); + memc= parse_opt_servers(memc, opt_servers); + + while (optind <= argc) { + + rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), expires); + + optind++; + } + + memcached_deinit(memc); + + return 0; +}; -- 2.30.2