6ae8e3b4905154b3de3115e23408a44e442c064a
10 #include <memcached.h>
11 #include "client_options.h"
14 void options_parse(int argc
, char *argv
[]);
16 static int opt_verbose
;
17 static char *opt_servers
;
18 static int opt_replace
;
19 uint16_t opt_flags
= 0;
20 time_t opt_expires
= 0;
22 int main(int argc
, char *argv
[])
29 options_parse(argc
, argv
);
31 memc
= memcached_init(NULL
);
32 parse_opt_servers(memc
, opt_servers
);
34 while (optind
<= argc
)
41 fd
= open(argv
[optind
], O_RDONLY
);
45 fprintf(stderr
, "Failed opening %s\n", argv
[optind
]);
49 (void)fstat(fd
, &sbuf
);
50 mptr
= mmap(NULL
, sbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
52 ptr
= rindex(argv
[optind
], '/');
59 static char *opstr
[] = { "set", "add", "replace" };
60 printf("op: %s\nsource file: %s\nlength: %d\n"
61 "key: %s\nflags: %d\n expires: %ld\n",
62 opstr
[opt_replace
], argv
[optind
], sbuf
.st_size
,
63 ptr
, opt_flags
, opt_expires
);
67 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
69 opt_expires
, opt_flags
);
70 else if (opt_replace
== 1)
71 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
73 opt_expires
, opt_flags
);
74 else if (opt_replace
== 2)
75 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
77 opt_expires
, opt_flags
);
81 munmap(mptr
, sbuf
.st_size
);
86 memcached_deinit(memc
);
91 void options_parse(int argc
, char *argv
[])
96 static struct option long_options
[] =
98 {"version", no_argument
, NULL
, OPT_VERSION
},
99 {"help", no_argument
, NULL
, OPT_HELP
},
100 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
101 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
102 {"servers", required_argument
, NULL
, OPT_SERVERS
},
103 {"flag", required_argument
, NULL
, OPT_FLAG
},
104 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
105 {"set", no_argument
, &opt_replace
, OPT_SET
},
106 {"add", no_argument
, &opt_replace
, OPT_ADD
},
107 {"replace", no_argument
, &opt_replace
, OPT_REPLACE
},
113 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
115 if (option_rv
== -1) break;
120 case OPT_VERSION
: /* --version or -V */
121 printf("memcache tools, memcp, v1.0\n");
123 case OPT_HELP
: /* --help or -h */
124 printf("useful help messages go here\n");
126 case OPT_SERVERS
: /* --servers or -s */
129 case OPT_FLAG
: /* --flag */
130 opt_flags
= (uint16_t)strtol(optarg
, (char **)NULL
, 10);
132 case OPT_EXPIRE
: /* --expire */
133 opt_expires
= (time_t)strtol(optarg
, (char **)NULL
, 10);
136 /* getopt_long already printed an error message. */