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