Bunch of fixes related to portability.
[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 <fcntl.h>
8 #include <errno.h>
9 #include <strings.h>
10
11 #include <memcached.h>
12 #include "client_options.h"
13 #include "utilities.h"
14
15 /* Prototypes */
16 void options_parse(int argc, char *argv[]);
17
18 static int opt_verbose= 0;
19 static char *opt_servers= NULL;
20 static int opt_method= OPT_SET;
21 static uint16_t opt_flags= 0;
22 static time_t opt_expires= 0;
23
24 int main(int argc, char *argv[])
25 {
26 memcached_st *memc;
27 memcached_return rc;
28
29 options_parse(argc, argv);
30
31 memc= memcached_init(NULL);
32
33 if (opt_servers)
34 parse_opt_servers(memc, opt_servers);
35 else
36 parse_opt_servers(memc, argv[--argc]);
37
38 while (optind < argc)
39 {
40 struct stat sbuf;
41 int fd;
42 char *ptr;
43 ssize_t read_length;
44 char *file_buffer_ptr;
45
46 fd= open(argv[optind], O_RDONLY);
47 if (fd < 0)
48 {
49 fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
50 optind++;
51 continue;
52 }
53
54 (void)fstat(fd, &sbuf);
55
56 ptr= rindex(argv[optind], '/');
57 if (ptr)
58 ptr++;
59 else
60 ptr= argv[optind];
61
62 if (opt_verbose)
63 {
64 static char *opstr[] = { "set", "add", "replace" };
65 printf("op: %s\nsource file: %s\nlength: %zu\n"
66 "key: %s\nflags: %x\nexpires: %llu\n",
67 opstr[opt_method - OPT_SET], argv[optind], (size_t)sbuf.st_size,
68 ptr, opt_flags, (unsigned long long)opt_expires);
69 }
70
71 if ((file_buffer_ptr= (char *)malloc(sizeof(char) * sbuf.st_size)) == NULL)
72 {
73 fprintf(stderr, "malloc: %s\n", strerror(errno));
74 exit(1);
75 }
76
77 if ((read_length= read(fd, file_buffer_ptr, sbuf.st_size)) == -1)
78 {
79 fprintf(stderr, "read: %s\n", strerror(errno));
80 exit(1);
81 }
82 assert(read_length == sbuf.st_size);
83
84 if (opt_method == OPT_ADD)
85 rc= memcached_add(memc, ptr, strlen(ptr),
86 file_buffer_ptr, sbuf.st_size,
87 opt_expires, opt_flags);
88 else if (opt_method == OPT_REPLACE)
89 rc= memcached_replace(memc, ptr, strlen(ptr),
90 file_buffer_ptr, sbuf.st_size,
91 opt_expires, opt_flags);
92 else
93 rc= memcached_set(memc, ptr, strlen(ptr),
94 file_buffer_ptr, sbuf.st_size,
95 opt_expires, opt_flags);
96
97 if (rc != MEMCACHED_SUCCESS)
98 fprintf(stderr, "memcp: %s: memcache error %s\n",
99 ptr, memcached_strerror(memc, rc));
100
101 free(file_buffer_ptr);
102 close(fd);
103 optind++;
104 }
105
106 memcached_deinit(memc);
107
108 free(opt_servers);
109
110 return 0;
111 }
112
113 void options_parse(int argc, char *argv[])
114 {
115 int option_index= 0;
116 int option_rv;
117
118 static struct option long_options[]=
119 {
120 {"version", no_argument, NULL, OPT_VERSION},
121 {"help", no_argument, NULL, OPT_HELP},
122 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
123 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
124 {"servers", required_argument, NULL, OPT_SERVERS},
125 {"flag", required_argument, NULL, OPT_FLAG},
126 {"expire", required_argument, NULL, OPT_EXPIRE},
127 {"set", no_argument, NULL, OPT_SET},
128 {"add", no_argument, NULL, OPT_ADD},
129 {"replace", no_argument, NULL, OPT_REPLACE},
130 {0, 0, 0, 0},
131 };
132
133 while (1)
134 {
135 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
136
137 if (option_rv == -1) break;
138
139 switch (option_rv)
140 {
141 case 0:
142 break;
143 case OPT_VERBOSE: /* --verbose or -v */
144 opt_verbose = OPT_VERBOSE;
145 break;
146 case OPT_DEBUG: /* --debug or -d */
147 opt_verbose = OPT_DEBUG;
148 break;
149 case OPT_VERSION: /* --version or -V */
150 printf("memcache tools, memcp, v1.0\n");
151 exit(0);
152 case OPT_HELP: /* --help or -h */
153 printf("useful help messages go here\n");
154 exit(0);
155 case OPT_SERVERS: /* --servers or -s */
156 opt_servers= strdup(optarg);
157 break;
158 case OPT_FLAG: /* --flag */
159 opt_flags= (uint16_t)strtol(optarg, (char **)NULL, 16);
160 break;
161 case OPT_EXPIRE: /* --expire */
162 opt_expires= (time_t)strtoll(optarg, (char **)NULL, 10);
163 break;
164 case OPT_SET:
165 opt_method= OPT_SET;
166 break;
167 case OPT_REPLACE:
168 opt_method= OPT_REPLACE;
169 break;
170 case OPT_ADD:
171 opt_method= OPT_ADD;
172 break;
173 case '?':
174 /* getopt_long already printed an error message. */
175 exit(1);
176 default:
177 abort();
178 }
179 }
180 }