Found bug in multi get where key size was not being calculated.
[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 #define PROGRAM_NAME "memrm"
13 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
14
15 /* Prototypes */
16 void options_parse(int argc, char *argv[]);
17
18 int main(int argc, char *argv[])
19 {
20 memcached_st *memc;
21 memcached_return rc;
22 memcached_server_st *servers;
23
24 options_parse(argc, argv);
25
26 if (!opt_servers)
27 {
28 char *temp;
29
30 if ((temp= getenv("MEMCACHED_SERVERS")))
31 opt_servers= strdup(temp);
32 else
33 exit(1);
34 }
35
36 memc= memcached_create(NULL);
37
38 servers= memcached_servers_parse(opt_servers);
39 memcached_server_push(memc, servers);
40 memcached_server_list_free(servers);
41
42 while (optind < argc)
43 {
44 if (opt_verbose)
45 printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire);
46 rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
47
48 if (rc != MEMCACHED_SUCCESS)
49 {
50 fprintf(stderr, "memrm: %s: memcache error %s\n",
51 argv[optind], memcached_strerror(memc, rc));
52 }
53
54 optind++;
55 }
56
57 memcached_free(memc);
58
59 free(opt_servers);
60
61 return 0;
62 }
63
64
65 void options_parse(int argc, char *argv[])
66 {
67 memcached_programs_help_st help_options[]=
68 {
69 {0},
70 };
71
72 static struct option long_options[]=
73 {
74 {"version", no_argument, NULL, OPT_VERSION},
75 {"help", no_argument, NULL, OPT_HELP},
76 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
77 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
78 {"servers", required_argument, NULL, OPT_SERVERS},
79 {"expire", required_argument, NULL, OPT_EXPIRE},
80 {0, 0, 0, 0},
81 };
82 int option_index= 0;
83 int option_rv;
84
85 while (1)
86 {
87 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
88 if (option_rv == -1) break;
89 switch (option_rv)
90 {
91 case 0:
92 break;
93 case OPT_VERBOSE: /* --verbose or -v */
94 opt_verbose = OPT_VERBOSE;
95 break;
96 case OPT_DEBUG: /* --debug or -d */
97 opt_verbose = OPT_DEBUG;
98 break;
99 case OPT_VERSION: /* --version or -V */
100 version_command(PROGRAM_NAME);
101 break;
102 case OPT_HELP: /* --help or -h */
103 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
104 break;
105 case OPT_SERVERS: /* --servers or -s */
106 opt_servers= strdup(optarg);
107 break;
108 case OPT_EXPIRE: /* --expire */
109 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
110 break;
111 case '?':
112 /* getopt_long already printed an error message. */
113 exit(1);
114 default:
115 abort();
116 }
117 }
118 }