size_t string_length;\r
uint16_t flags;\r
memcached_return rc;\r
- unsigned int x;\r
\r
- for (x= 1; x < argc; x++)\r
+ memc= memcached_init(NULL);\r
+\r
+ options_parse(argc, argv);\r
+\r
+ parse_opt_servers(memc, opt_servers);\r
+\r
- string= memcached_get(memc, argv[x], strlen(argv[x]),\r
++ while (optind <= argc) \r
+ {\r
- if (rc == MEMCACHED_SUCCESS) {\r
- if (opt_displayflag) {\r
++ string= memcached_get(memc, argv[argc], strlen(argv[argc]),\r
+ &string_length, &flags, &rc);\r
- } else {\r
- if (string) \r
- {\r
- printf("%.*s\n", string_length, string);\r
- free(string);\r
- }\r
++ if (rc == MEMCACHED_SUCCESS) \r
++ {\r
++ if (opt_displayflag) \r
+ printf("%d\n", flags);\r
++ else \r
++ {\r
++ printf("%.*s\n", string_length, string);\r
++ free(string);\r
+ }\r
+ }\r
++ optind++;\r
+ }\r
+\r
+ memcached_deinit(memc);\r
+\r
+ return 0;\r
+};\r
+\r
+\r
+void options_parse(int argc, char *argv[])\r
+{\r
+ int option_index = 0;\r
+ int option_rv;\r
+\r
static struct option long_options[] =\r
{\r
{"version", no_argument, NULL, OPT_VERSION},\r
exit(0);\r
break;\r
case OPT_SERVERS: /* --servers */\r
- opt_servers= strdup(optarg);\r
- opt_servers = strdup(optarg);\r
++ opt_servers= optarg;\r
break;\r
case '?':\r
/* getopt_long already printed an error message. */\r
#include <fcntl.h>\r
\r
#include <memcached.h>\r
-\r
-struct memcached_st *parse_opt_servers (struct memcached_st *m,\r
- char *opt_servers)\r
-{\r
- char *s, *hostname;\r
- unsigned int portnum;\r
- while (s = strsep(&opt_servers, ",")) {\r
- hostname = strsep(&s, ":");\r
- portnum = atoi(s);\r
- memcached_server_add(m, hostname, portnum);\r
- }\r
- return m;\r
-}\r
+ #include "client_options.h"\r
+ \r
++/* Prototypes */\r
++void options_parse(int argc, char *argv[]);\r
++\r
+ static int opt_verbose;\r
+ static char *opt_servers;\r
+ static int opt_replace;\r
++uint16_t opt_flags= 0;\r
++time_t opt_expires= 0;\r
\r
int main(int argc, char *argv[])\r
{\r
memcached_st *memc;\r
char *string;\r
-- unsigned int x;\r
size_t string_length;\r
- uint16_t flags;\r
- uint16_t flags = 0;\r
- time_t expires = 0;\r
memcached_return rc;\r
\r
- if (argc < 3)\r
- return 0;\r
- static struct option long_options[] =\r
- {\r
- {"version", no_argument, NULL, OPT_VERSION},\r
- {"help", no_argument, NULL, OPT_HELP},\r
- {"verbose", no_argument, &opt_verbose, 1},\r
- {"debug", no_argument, &opt_verbose, 2},\r
- {"servers", required_argument, NULL, OPT_SERVERS},\r
- {"flag", required_argument, NULL, OPT_FLAG},\r
- {"expire", required_argument, NULL, OPT_EXPIRE},\r
- {"set", no_argument, &opt_replace, 0},\r
- {"add", no_argument, &opt_replace, 1},\r
- {"replace", no_argument, &opt_replace, 2},\r
- {0, 0, 0, 0},\r
- };\r
- int option_index = 0;\r
- int option_rv;\r
- while (1) \r
- {\r
- option_rv = getopt_long(argc, argv, "", long_options, &option_index);\r
- if (option_rv == -1) break;\r
- switch (option_rv) {\r
- case 0:\r
- if (long_options[option_index].name)\r
- break;\r
- case OPT_VERSION: /* --version */\r
- printf("memcache tools, memcp, v1.0\n");\r
- exit(0);\r
- break;\r
- case OPT_HELP: /* --help */\r
- printf("useful help messages go here\n");\r
- exit(0);\r
- break;\r
- case OPT_SERVERS: /* --servers */\r
- opt_servers = strdup(optarg);\r
- break;\r
- case OPT_FLAG: /* --flag */\r
- flags = (uint16_t) atoi(optarg);\r
- break;\r
- case OPT_EXPIRE: /* --expire */\r
- expires = (time_t)atoi(optarg);\r
- break;\r
- case '?':\r
- /* getopt_long already printed an error message. */\r
- exit(1);\r
- default:\r
- abort();\r
- }\r
- }\r
++ options_parse(argc, argv);\r
\r
memc= memcached_init(NULL);\r
- memc= parse_opt_servers(memc, opt_servers);\r
++ parse_opt_servers(memc, opt_servers);\r
\r
- /* Get the server name */\r
- {\r
- char *ptr;\r
- char *hostname;\r
- size_t hostname_length;\r
- unsigned int port;\r
- \r
- ptr= index(argv[argc-1], ':');\r
- \r
- if (ptr)\r
- {\r
- hostname_length= ptr - argv[argc-1];\r
- hostname= (char *)malloc(hostname_length+1);\r
- memset(hostname, 0, hostname_length+1);\r
- memcpy(hostname, argv[argc-1], hostname_length);\r
- \r
- ptr++;\r
- \r
- port= strtol(ptr, (char **)NULL, 10);\r
- \r
- memcached_server_add(memc, hostname, port);\r
- free(hostname);\r
- }\r
- else\r
- {\r
- memcached_server_add(memc, argv[argc -1], 0);\r
- }\r
- }\r
- \r
- for (x= 1; x < argc-1; x++)\r
- while (optind <= argc) {\r
++ while (optind <= argc) \r
+ {\r
char *mptr;\r
struct stat sbuf;\r
int fd;\r
(void)fstat(fd, &sbuf);\r
mptr= mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);\r
\r
- ptr= rindex(argv[x], '/');\r
+ ptr= rindex(argv[optind], '/');\r
if (ptr)\r
-- {\r
ptr++;\r
-- }\r
else\r
-- {\r
- ptr= argv[x];\r
- }\r
- \r
- rc= memcached_set(memc, ptr, strlen(ptr),\r
- mptr, sbuf.st_size,\r
- (time_t)0, (uint16_t)0);\r
+ ptr= argv[optind];\r
- }\r
+ \r
- if (opt_replace == 0) {\r
++ if (opt_replace == 0)\r
+ rc= memcached_set(memc, ptr, strlen(ptr),\r
+ mptr, sbuf.st_size,\r
- expires, flags);\r
- } else if (opt_replace == 1) {\r
++ opt_expires, opt_flags);\r
++ else if (opt_replace == 1)\r
+ rc= memcached_add(memc, ptr, strlen(ptr),\r
+ mptr, sbuf.st_size,\r
- expires, flags);\r
- } else if (opt_replace == 2) {\r
++ opt_expires, opt_flags);\r
++ else if (opt_replace == 2)\r
+ rc= memcached_replace(memc, ptr, strlen(ptr),\r
+ mptr, sbuf.st_size,\r
- expires, flags);\r
- } else {\r
++ opt_expires, opt_flags);\r
++ else\r
+ abort();\r
- }\r
\r
munmap(mptr, sbuf.st_size);\r
close(fd);\r
\r
return 0;\r
};\r
++\r
++void options_parse(int argc, char *argv[])\r
++{\r
++ int option_index= 0;\r
++ int option_rv;\r
++\r
++ static struct option long_options[] =\r
++ {\r
++ {"version", no_argument, NULL, OPT_VERSION},\r
++ {"help", no_argument, NULL, OPT_HELP},\r
++ {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},\r
++ {"debug", no_argument, &opt_verbose, OPT_DEBUG},\r
++ {"servers", required_argument, NULL, OPT_SERVERS},\r
++ {"flag", required_argument, NULL, OPT_FLAG},\r
++ {"expire", required_argument, NULL, OPT_EXPIRE},\r
++ {"set", no_argument, &opt_replace, OPT_SET},\r
++ {"add", no_argument, &opt_replace, OPT_ADD},\r
++ {"replace", no_argument, &opt_replace, OPT_REPLACE},\r
++ {0, 0, 0, 0},\r
++ };\r
++\r
++ while (1) \r
++ {\r
++ option_rv= getopt_long(argc, argv, "", long_options, &option_index);\r
++\r
++ if (option_rv == -1) break;\r
++\r
++ switch (option_rv) {\r
++ case 0:\r
++ if (long_options[option_index].name)\r
++ break;\r
++ case OPT_VERSION: /* --version */\r
++ printf("memcache tools, memcp, v1.0\n");\r
++ exit(0);\r
++ case OPT_HELP: /* --help */\r
++ printf("useful help messages go here\n");\r
++ exit(0);\r
++ case OPT_SERVERS: /* --servers */\r
++ opt_servers= optarg;\r
++ break;\r
++ case OPT_FLAG: /* --flag */\r
++ opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 10);\r
++ break;\r
++ case OPT_EXPIRE: /* --expire */\r
++ opt_expires= (time_t)strtol(optarg, (char **)NULL, 10);\r
++ break;\r
++ case '?':\r
++ /* getopt_long already printed an error message. */\r
++ exit(1);\r
++ default:\r
++ abort();\r
++ }\r
++ }\r
++}\r
--- /dev/null
-struct memcached_st *parse_opt_servers (struct memcached_st *m,\r
- char *opt_servers)\r
-{\r
- char *s, *hostname;\r
- unsigned int portnum;\r
- while (s = strsep(&opt_servers, ",")) {\r
- hostname = strsep(&s, ":");\r
- portnum = atoi(s);\r
- memcached_server_add(m, hostname, portnum);\r
- }\r
- return m;\r
-}\r
-\r
+ #include <stdio.h>\r
+ #include <unistd.h>\r
+ #include <getopt.h>\r
+ #include <memcached.h>\r
+ #include "client_options.h"\r
+ \r
+ static int opt_verbose;\r
+ static char *opt_servers;\r
+ \r
- opt_servers = strdup(optarg);\r
+ int main(int argc, char *argv[])\r
+ {\r
+ memcached_st *memc;\r
+ char *string;\r
+ size_t string_length;\r
+ time_t expires = 0;\r
+ memcached_return rc;\r
+ \r
+ static struct option long_options[] =\r
+ {\r
+ {"version", no_argument, NULL, OPT_VERSION},\r
+ {"help", no_argument, NULL, OPT_HELP},\r
+ {"verbose", no_argument, &opt_verbose, 1},\r
+ {"debug", no_argument, &opt_verbose, 2},\r
+ {"servers", required_argument, NULL, OPT_SERVERS},\r
+ {"expire", required_argument, NULL, OPT_EXPIRE},\r
+ {0, 0, 0, 0},\r
+ };\r
+ int option_index = 0;\r
+ int option_rv;\r
+ \r
+ while (1) \r
+ {\r
+ option_rv = getopt_long(argc, argv, "", long_options, &option_index);\r
+ if (option_rv == -1) break;\r
+ switch (option_rv) {\r
+ case 0:\r
+ break;\r
+ case OPT_VERSION: /* --version */\r
+ printf("memcache tools, memrm, v1.0\n");\r
+ exit(0);\r
+ break;\r
+ case OPT_HELP: /* --help */\r
+ printf("useful help messages go here\n");\r
+ exit(0);\r
+ break;\r
+ case OPT_SERVERS: /* --servers */\r
- expires = (time_t)atoi(optarg);\r
++ opt_servers = optarg;\r
+ break;\r
+ case OPT_EXPIRE: /* --expire */\r
- memc= parse_opt_servers(memc, opt_servers);\r
++ expires = (time_t)strtol(optarg, (char **)NULL, 10);\r
+ break;\r
+ case '?':\r
+ /* getopt_long already printed an error message. */\r
+ exit(1);\r
+ default:\r
+ abort();\r
+ }\r
+ }\r
+ \r
+ memc= memcached_init(NULL);\r
- while (optind <= argc) {\r
-\r
++ parse_opt_servers(memc, opt_servers);\r
+ \r
-\r
++ while (optind <= argc) \r
++ {\r
+ rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), expires);\r
+ optind++;\r
+ }\r
+ \r
+ memcached_deinit(memc);\r
+ \r
+ return 0;\r
+ };\r
--- /dev/null
- opt_servers = strdup(optarg);
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <getopt.h>
+
+#include <memcached.h>
+
+#include "client_options.h"
+#include "utilities.h"
+
+/* Prototypes */
+void options_parse(int argc, char *argv[]);
+
+static int opt_verbose;
+static int opt_displayflag;
+static char *opt_servers;
+
+int main(int argc, char *argv[])
+{
+ memcached_st *memc;
+
+ memc= memcached_init(NULL);
+ options_parse(argc, argv);
+
+ parse_opt_servers(memc, opt_servers);
+
+ memcached_deinit(memc);
+
+ return 0;
+}
+
+void options_parse(int argc, char *argv[])
+{
+ static struct option long_options[] =
+ {
+ {"version", no_argument, NULL, OPT_VERSION},
+ {"help", no_argument, NULL, OPT_HELP},
+ {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
+ {"debug", no_argument, &opt_verbose, OPT_DEBUG},
+ {"servers", required_argument, NULL, OPT_SERVERS},
+ {"flag", no_argument, &opt_displayflag, OPT_FLAG},
+ {0, 0, 0, 0},
+ };
+
+ int option_index = 0;
+ int option_rv;
+
+ while (1)
+ {
+ option_rv = getopt_long(argc, argv, "", long_options, &option_index);
+ if (option_rv == -1) break;
+ switch (option_rv) {
+ case 0:
+ break;
+ case OPT_VERSION: /* --version */
+ printf("memcache tools, memcat, v1.0\n");
+ exit(0);
+ break;
+ case OPT_HELP: /* --help */
+ printf("useful help messages go here\n");
+ exit(0);
+ break;
+ case OPT_SERVERS: /* --servers */
++ opt_servers = optarg;
+ break;
+ case '?':
+ /* getopt_long already printed an error message. */
+ exit(1);
+ default:
+ abort();
+ }
+ }
+}