Turned on more warnings, and fixed the errors. Moved common.h, which includes
[awesomized/libmemcached] / clients / memrm.c
1 #include "libmemcached/common.h"
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <getopt.h>
5 #include <libmemcached/memcached.h>
6 #include <string.h>
7 #include "client_options.h"
8 #include "utilities.h"
9
10 static int opt_binary= 0;
11 static int opt_verbose= 0;
12 static time_t opt_expire= 0;
13 static char *opt_servers= NULL;
14 static char *opt_hash= NULL;
15
16 #define PROGRAM_NAME "memrm"
17 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
18
19 /* Prototypes */
20 void options_parse(int argc, char *argv[]);
21
22 int main(int argc, char *argv[])
23 {
24 memcached_st *memc;
25 memcached_return rc;
26 memcached_server_st *servers;
27
28 options_parse(argc, argv);
29
30 if (!opt_servers)
31 {
32 char *temp;
33
34 if ((temp= getenv("MEMCACHED_SERVERS")))
35 opt_servers= strdup(temp);
36 else
37 {
38 fprintf(stderr, "No Servers provided\n");
39 exit(1);
40 }
41 }
42
43 memc= memcached_create(NULL);
44 process_hash_option(memc, opt_hash);
45
46 servers= memcached_servers_parse(opt_servers);
47 memcached_server_push(memc, servers);
48 memcached_server_list_free(servers);
49 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary);
50
51 while (optind < argc)
52 {
53 if (opt_verbose)
54 printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire);
55 rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
56
57 if (rc != MEMCACHED_SUCCESS)
58 {
59 fprintf(stderr, "memrm: %s: memcache error %s",
60 argv[optind], memcached_strerror(memc, rc));
61 if (memc->cached_errno)
62 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
63 fprintf(stderr, "\n");
64 }
65
66 optind++;
67 }
68
69 memcached_free(memc);
70
71 if (opt_servers)
72 free(opt_servers);
73 if (opt_hash)
74 free(opt_hash);
75
76 return 0;
77 }
78
79
80 void options_parse(int argc, char *argv[])
81 {
82 memcached_programs_help_st help_options[]=
83 {
84 {0},
85 };
86
87 static struct option long_options[]=
88 {
89 {"version", no_argument, NULL, OPT_VERSION},
90 {"help", no_argument, NULL, OPT_HELP},
91 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
92 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
93 {"servers", required_argument, NULL, OPT_SERVERS},
94 {"expire", required_argument, NULL, OPT_EXPIRE},
95 {"hash", required_argument, NULL, OPT_HASH},
96 {"binary", no_argument, NULL, OPT_BINARY},
97 {0, 0, 0, 0},
98 };
99 int option_index= 0;
100 int option_rv;
101
102 while (1)
103 {
104 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
105 if (option_rv == -1) break;
106 switch (option_rv)
107 {
108 case 0:
109 break;
110 case OPT_BINARY:
111 opt_binary = 1;
112 break;
113 case OPT_VERBOSE: /* --verbose or -v */
114 opt_verbose = OPT_VERBOSE;
115 break;
116 case OPT_DEBUG: /* --debug or -d */
117 opt_verbose = OPT_DEBUG;
118 break;
119 case OPT_VERSION: /* --version or -V */
120 version_command(PROGRAM_NAME);
121 break;
122 case OPT_HELP: /* --help or -h */
123 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
124 break;
125 case OPT_SERVERS: /* --servers or -s */
126 opt_servers= strdup(optarg);
127 break;
128 case OPT_EXPIRE: /* --expire */
129 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
130 break;
131 case OPT_HASH:
132 opt_hash= strdup(optarg);
133 break;
134 case '?':
135 /* getopt_long already printed an error message. */
136 exit(1);
137 default:
138 abort();
139 }
140 }
141 }