+ free(value);
+ goto read_error;
+ }
+
+ value[*value_length]= 0;
+ value[(*value_length) + 1]= 0;
+
+ return value;
+ }
+ }
+ else if (*error == MEMCACHED_END)
+ *error= MEMCACHED_NOTFOUND;
+
+ return NULL;
+read_error:
+ *error= MEMCACHED_PARTIAL_READ;
+ return NULL;
+}
+
+char *memcached_get(memcached_st *ptr, char *key, size_t key_length,
+ size_t *value_length,
+ uint16_t *flags,
+ memcached_return *error)
+{
+ size_t send_length;
+ char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
+ unsigned int server_key;
+ char *value;
+ LIBMEMCACHED_MEMCACHED_GET_START();
+
+ server_key= memcached_generate_hash(ptr, key, key_length);
+
+ *value_length= 0;
+ *error= memcached_connect(ptr, server_key);
+
+ if (*error != MEMCACHED_SUCCESS)
+ goto error;
+
+ send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "get %.*s\r\n",
+ (int)key_length, key);
+
+ if ((memcached_io_write(ptr, server_key, buffer, send_length, 1)) == -1)
+ {
+ *error= MEMCACHED_WRITE_FAILURE;
+ goto error;
+ }
+
+ value= memcached_value_fetch(ptr, key, &key_length, value_length, flags,
+ error, 0, server_key);
+ if (*error == MEMCACHED_END && *value_length == 0)
+ {
+ *error= MEMCACHED_NOTFOUND;
+ goto error;
+ }
+ else if (*error == MEMCACHED_END)
+ assert(0); /* If this happens we have somehow messed up the fetch */
+ else if (*error == MEMCACHED_SUCCESS)
+ {
+ memcached_return rc;
+ /* We need to read END */
+ rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
+
+ if (rc != MEMCACHED_END)
+ {
+ *error= MEMCACHED_PROTOCOL_ERROR;
+ goto error;
+ }
+ }
+ else
+ goto error;
+
+ LIBMEMCACHED_MEMCACHED_GET_END();
+
+ return value;