See changes in changelog, but...
[awesomized/libmemcached] / src / utilities.c
1 #include <ctype.h>
2 #include <strings.h>
3 #include "utilities.h"
4
5 memcached_server_st *parse_opt_servers(char *server_strings)
6 {
7 char *string;
8 unsigned int port;
9 char *begin_ptr;
10 char *end_ptr;
11 memcached_server_st *servers= NULL;
12 memcached_return rc;
13
14 assert(server_strings);
15
16 end_ptr= server_strings + strlen(server_strings);
17
18 for (begin_ptr= server_strings, string= index(server_strings, ',');
19 begin_ptr != end_ptr;
20 string= index(begin_ptr, ','))
21 {
22 char buffer[HUGE_STRING_LEN];
23 char *ptr;
24 port= 0;
25
26 memset(buffer, 0, HUGE_STRING_LEN);
27 if (string)
28 {
29 memcpy(buffer, begin_ptr, string - begin_ptr);
30 begin_ptr= string+1;
31 }
32 else
33 {
34 size_t length= strlen(begin_ptr);
35 memcpy(buffer, begin_ptr, length);
36 begin_ptr= end_ptr;
37 }
38
39 ptr= index(buffer, ':');
40
41 if (ptr)
42 {
43 ptr[0]= 0;
44
45 ptr++;
46
47 port= strtol(ptr, (char **)NULL, 10);
48 }
49
50 servers= memcached_server_list_append(servers, buffer, port, &rc);
51
52 if (isspace(*begin_ptr))
53 begin_ptr++;
54 }
55
56 return servers;
57 }
58
59 long int timedif(struct timeval a, struct timeval b)
60 {
61 register int us, s;
62
63 us = a.tv_usec - b.tv_usec;
64 us /= 1000;
65 s = a.tv_sec - b.tv_sec;
66 s *= 1000;
67 return s + us;
68 }
69
70 void version_command(char *command_name)
71 {
72 printf("%s v%u.%u\n", command_name, 1, 0);
73 exit(0);
74 }
75
76 void help_command(char *command_name, char *description,
77 const struct option *long_options,
78 memcached_programs_help_st *options)
79 {
80 unsigned int x;
81
82 printf("%s v%u.%u\n\n", command_name, 1, 0);
83 printf("\t%s\n\n", description);
84 printf("Current options. A '=' means the option takes a value.\n\n");
85
86 for (x= 0; long_options[x].name; x++)
87 printf("\t --%s%c\n", long_options[x].name,
88 long_options[x].has_arg ? '=' : ' ');
89
90 printf("\n");
91 exit(0);
92 }