Remove use of system(). Not very portable
[awesomized/libmemcached] / clients / memrm.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11 #include "config.h"
12
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <getopt.h>
16 #include <libmemcached/memcached.h>
17 #include <string.h>
18 #include "client_options.h"
19 #include "utilities.h"
20
21 static int opt_binary= 0;
22 static int opt_verbose= 0;
23 static time_t opt_expire= 0;
24 static char *opt_servers= NULL;
25 static char *opt_hash= NULL;
26 static char *opt_username;
27 static char *opt_passwd;
28
29 #define PROGRAM_NAME "memrm"
30 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
31
32 /* Prototypes */
33 static void options_parse(int argc, char *argv[]);
34
35 int main(int argc, char *argv[])
36 {
37 memcached_st *memc;
38 memcached_return_t rc;
39 memcached_server_st *servers;
40
41 int return_code= 0;
42
43 options_parse(argc, argv);
44 initialize_sockets();
45
46 if (!opt_servers)
47 {
48 char *temp;
49
50 if ((temp= getenv("MEMCACHED_SERVERS")))
51 opt_servers= strdup(temp);
52 else
53 {
54 fprintf(stderr, "No Servers provided\n");
55 exit(1);
56 }
57 }
58
59 memc= memcached_create(NULL);
60 process_hash_option(memc, opt_hash);
61
62 servers= memcached_servers_parse(opt_servers);
63 memcached_server_push(memc, servers);
64 memcached_server_list_free(servers);
65 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
66 (uint64_t) opt_binary);
67
68 if (!initialize_sasl(memc, opt_username, opt_passwd))
69 {
70 memcached_free(memc);
71 return 1;
72 }
73
74 while (optind < argc)
75 {
76 if (opt_verbose)
77 printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire);
78 rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
79
80 if (rc != MEMCACHED_SUCCESS)
81 {
82 fprintf(stderr, "memrm: %s: memcache error %s",
83 argv[optind], memcached_strerror(memc, rc));
84 if (memc->cached_errno)
85 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
86 fprintf(stderr, "\n");
87
88 return_code= -1;
89 }
90
91 optind++;
92 }
93
94 memcached_free(memc);
95
96 if (opt_servers)
97 free(opt_servers);
98 if (opt_hash)
99 free(opt_hash);
100
101 shutdown_sasl();
102
103 return return_code;
104 }
105
106
107 static void options_parse(int argc, char *argv[])
108 {
109 memcached_programs_help_st help_options[]=
110 {
111 {0},
112 };
113
114 static struct option long_options[]=
115 {
116 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
117 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
118 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
119 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
120 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
121 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
122 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
123 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
124 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
125 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
126 {0, 0, 0, 0},
127 };
128 int option_index= 0;
129 int option_rv;
130
131 while (1)
132 {
133 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
134 if (option_rv == -1) break;
135 switch (option_rv)
136 {
137 case 0:
138 break;
139 case OPT_BINARY:
140 opt_binary = 1;
141 break;
142 case OPT_VERBOSE: /* --verbose or -v */
143 opt_verbose = OPT_VERBOSE;
144 break;
145 case OPT_DEBUG: /* --debug or -d */
146 opt_verbose = OPT_DEBUG;
147 break;
148 case OPT_VERSION: /* --version or -V */
149 version_command(PROGRAM_NAME);
150 break;
151 case OPT_HELP: /* --help or -h */
152 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
153 break;
154 case OPT_SERVERS: /* --servers or -s */
155 opt_servers= strdup(optarg);
156 break;
157 case OPT_EXPIRE: /* --expire */
158 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
159 break;
160 case OPT_HASH:
161 opt_hash= strdup(optarg);
162 break;
163 case OPT_USERNAME:
164 opt_username= optarg;
165 break;
166 case OPT_PASSWD:
167 opt_passwd= optarg;
168 break;
169 case '?':
170 /* getopt_long already printed an error message. */
171 exit(1);
172 default:
173 abort();
174 }
175 }
176 }