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