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 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
61 opt_expires
, opt_flags
);
62 else if (opt_replace
== 1)
63 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
65 opt_expires
, opt_flags
);
66 else if (opt_replace
== 2)
67 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
69 opt_expires
, opt_flags
);
73 munmap(mptr
, sbuf
.st_size
);
78 memcached_deinit(memc
);
83 void options_parse(int argc
, char *argv
[])
88 static struct option long_options
[] =
90 {"version", no_argument
, NULL
, OPT_VERSION
},
91 {"help", no_argument
, NULL
, OPT_HELP
},
92 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
93 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
94 {"servers", required_argument
, NULL
, OPT_SERVERS
},
95 {"flag", required_argument
, NULL
, OPT_FLAG
},
96 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
97 {"set", no_argument
, &opt_replace
, OPT_SET
},
98 {"add", no_argument
, &opt_replace
, OPT_ADD
},
99 {"replace", no_argument
, &opt_replace
, OPT_REPLACE
},
105 option_rv
= getopt_long(argc
, argv
, "", long_options
, &option_index
);
107 if (option_rv
== -1) break;
111 if (long_options
[option_index
].name
)
113 case OPT_VERSION
: /* --version */
114 printf("memcache tools, memcp, v1.0\n");
116 case OPT_HELP
: /* --help */
117 printf("useful help messages go here\n");
119 case OPT_SERVERS
: /* --servers */
122 case OPT_FLAG
: /* --flag */
123 opt_flags
= (uint16_t)strtol(optarg
, (char **)NULL
, 10);
125 case OPT_EXPIRE
: /* --expire */
126 opt_expires
= (time_t)strtol(optarg
, (char **)NULL
, 10);
129 /* getopt_long already printed an error message. */