3a76049a1429fbeb0dfcace4b13baa684157a9a7
11 #include <memcached.h>
12 #include "client_options.h"
15 void options_parse(int argc
, char *argv
[]);
17 static int opt_verbose
;
18 static char *opt_servers
;
19 static int opt_replace
;
20 uint16_t opt_flags
= 0;
21 time_t opt_expires
= 0;
23 int main(int argc
, char *argv
[])
30 options_parse(argc
, argv
);
32 memc
= memcached_init(NULL
);
33 parse_opt_servers(memc
, opt_servers
);
35 while (optind
<= argc
)
42 fd
= open(argv
[optind
], O_RDONLY
);
45 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
50 (void)fstat(fd
, &sbuf
);
51 mptr
= mmap(NULL
, sbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
52 if (mptr
== MAP_FAILED
)
54 fprintf(stderr
, "memcp: %s: %s\n", argv
[optind
], strerror(errno
));
60 ptr
= rindex(argv
[optind
], '/');
68 static char *opstr
[]= { "set", "add", "replace" };
69 printf("op: %s\nsource file: %s\nlength: %zu\n"
70 "key: %s\nflags: %x\n expires: %ld\n",
71 opstr
[opt_replace
], argv
[optind
], sbuf
.st_size
,
72 ptr
, opt_flags
, opt_expires
);
76 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
78 opt_expires
, opt_flags
);
79 else if (opt_replace
== 1)
80 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
82 opt_expires
, opt_flags
);
83 else if (opt_replace
== 2)
84 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
86 opt_expires
, opt_flags
);
90 if (rc
!= MEMCACHED_SUCCESS
)
92 fprintf(stderr
, "memcp: %s: memcache error %s\n",
93 ptr
, memcached_strerror(memc
, rc
));
96 munmap(mptr
, sbuf
.st_size
);
101 memcached_deinit(memc
);
106 void options_parse(int argc
, char *argv
[])
111 static struct option long_options
[]=
113 {"version", no_argument
, NULL
, OPT_VERSION
},
114 {"help", no_argument
, NULL
, OPT_HELP
},
115 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
116 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
117 {"servers", required_argument
, NULL
, OPT_SERVERS
},
118 {"flag", required_argument
, NULL
, OPT_FLAG
},
119 {"expire", required_argument
, NULL
, OPT_EXPIRE
},
120 {"set", no_argument
, &opt_replace
, OPT_SET
},
121 {"add", no_argument
, &opt_replace
, OPT_ADD
},
122 {"replace", no_argument
, &opt_replace
, OPT_REPLACE
},
128 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
130 if (option_rv
== -1) break;
136 case OPT_VERSION
: /* --version or -V */
137 printf("memcache tools, memcp, v1.0\n");
139 case OPT_HELP
: /* --help or -h */
140 printf("useful help messages go here\n");
142 case OPT_SERVERS
: /* --servers or -s */
145 case OPT_FLAG
: /* --flag */
146 opt_flags
= (uint16_t)strtol(optarg
, (char **)NULL
, 16);
148 case OPT_EXPIRE
: /* --expire */
149 opt_expires
= (time_t)strtol(optarg
, (char **)NULL
, 10);
152 /* getopt_long already printed an error message. */