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