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