e07654f3cad4a7968c5c90ec3fcf97f0dfffe0c4
[m6w6/libmemcached] / src / memrm.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <getopt.h>
4 #include <memcached.h>
5 #include "client_options.h"
6 #include "utilities.h"
7
8 static int opt_verbose= 0;
9 static time_t opt_expire= 0;
10 static char *opt_servers= NULL;
11
12 /* Prototypes */
13 void options_parse(int argc, char *argv[]);
14
15 int main(int argc, char *argv[])
16 {
17 memcached_st *memc;
18 memcached_return rc;
19 memcached_server_st *servers;
20
21 options_parse(argc, argv);
22
23 if (!opt_servers)
24 return 0;
25
26 memc= memcached_init(NULL);
27
28 servers= parse_opt_servers(opt_servers);
29 memcached_server_push(memc, servers);
30 memcached_server_list_free(servers);
31
32 while (optind < argc)
33 {
34 if (opt_verbose)
35 printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire);
36 rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
37
38 if (rc != MEMCACHED_SUCCESS)
39 {
40 fprintf(stderr, "memrm: %s: memcache error %s\n",
41 argv[optind], memcached_strerror(memc, rc));
42 }
43
44 optind++;
45 }
46
47 memcached_deinit(memc);
48
49 free(opt_servers);
50
51 return 0;
52 }
53
54
55 void options_parse(int argc, char *argv[])
56 {
57 static struct option long_options[]=
58 {
59 {"version", no_argument, NULL, OPT_VERSION},
60 {"help", no_argument, NULL, OPT_HELP},
61 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
62 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
63 {"servers", required_argument, NULL, OPT_SERVERS},
64 {"expire", required_argument, NULL, OPT_EXPIRE},
65 {0, 0, 0, 0},
66 };
67 int option_index= 0;
68 int option_rv;
69
70 while (1)
71 {
72 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
73 if (option_rv == -1) break;
74 switch (option_rv)
75 {
76 case 0:
77 break;
78 case OPT_VERBOSE: /* --verbose or -v */
79 opt_verbose = OPT_VERBOSE;
80 break;
81 case OPT_DEBUG: /* --debug or -d */
82 opt_verbose = OPT_DEBUG;
83 break;
84 case OPT_VERSION: /* --version or -V */
85 printf("memcache tools, memrm, v1.0\n");
86 exit(0);
87 break;
88 case OPT_HELP: /* --help or -h */
89 printf("useful help messages go here\n");
90 exit(0);
91 break;
92 case OPT_SERVERS: /* --servers or -s */
93 opt_servers= strdup(optarg);
94 break;
95 case OPT_EXPIRE: /* --expire */
96 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
97 break;
98 case '?':
99 /* getopt_long already printed an error message. */
100 exit(1);
101 default:
102 abort();
103 }
104 }
105 }