add single letter command line options
[awesomized/libmemcached] / src / memcp.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <getopt.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <sys/mman.h>
8 #include <fcntl.h>
9
10 #include <memcached.h>
11 #include "client_options.h"
12
13 /* Prototypes */
14 void options_parse(int argc, char *argv[]);
15
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;
21
22 int main(int argc, char *argv[])
23 {
24 memcached_st *memc;
25 char *string;
26 size_t string_length;
27 memcached_return rc;
28
29 options_parse(argc, argv);
30
31 memc= memcached_init(NULL);
32 parse_opt_servers(memc, opt_servers);
33
34 while (optind <= argc)
35 {
36 char *mptr;
37 struct stat sbuf;
38 int fd;
39 char *ptr;
40
41 fd= open(argv[optind], O_RDONLY);
42
43 if (fd == -1)
44 {
45 fprintf(stderr, "Failed opening %s\n", argv[optind]);
46 continue;
47 }
48
49 (void)fstat(fd, &sbuf);
50 mptr= mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
51
52 ptr= rindex(argv[optind], '/');
53 if (ptr)
54 ptr++;
55 else
56 ptr= argv[optind];
57
58 if (opt_replace == 0)
59 rc= memcached_set(memc, ptr, strlen(ptr),
60 mptr, sbuf.st_size,
61 opt_expires, opt_flags);
62 else if (opt_replace == 1)
63 rc= memcached_add(memc, ptr, strlen(ptr),
64 mptr, sbuf.st_size,
65 opt_expires, opt_flags);
66 else if (opt_replace == 2)
67 rc= memcached_replace(memc, ptr, strlen(ptr),
68 mptr, sbuf.st_size,
69 opt_expires, opt_flags);
70 else
71 abort();
72
73 munmap(mptr, sbuf.st_size);
74 close(fd);
75 optind++;
76 }
77
78 memcached_deinit(memc);
79
80 return 0;
81 };
82
83 void options_parse(int argc, char *argv[])
84 {
85 int option_index= 0;
86 int option_rv;
87
88 static struct option long_options[] =
89 {
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},
100 {0, 0, 0, 0},
101 };
102
103 while (1)
104 {
105 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
106
107 if (option_rv == -1) break;
108
109 switch (option_rv) {
110 case 0:
111 break;
112 case OPT_VERSION: /* --version or -V */
113 printf("memcache tools, memcp, v1.0\n");
114 exit(0);
115 case OPT_HELP: /* --help or -h */
116 printf("useful help messages go here\n");
117 exit(0);
118 case OPT_SERVERS: /* --servers or -s */
119 opt_servers= optarg;
120 break;
121 case OPT_FLAG: /* --flag */
122 opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 10);
123 break;
124 case OPT_EXPIRE: /* --expire */
125 opt_expires= (time_t)strtol(optarg, (char **)NULL, 10);
126 break;
127 case '?':
128 /* getopt_long already printed an error message. */
129 exit(1);
130 default:
131 abort();
132 }
133 }
134 }