Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

src/configfile.[ch]: Add cf_util_get_service() for getting service na…

…mes.

Change-Id: I310c0d076a82927958fba04dd9eedbc706742dce
  • Loading branch information...
commit d0efcb2eda3317f1b2f8da154a8722cdef03159a 1 parent f394c08
Florian Forster octo authored
Showing with 54 additions and 0 deletions.
  1. +48 −0 src/configfile.c
  2. +6 −0 src/configfile.h
48 src/configfile.c
View
@@ -1094,6 +1094,54 @@ int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
return (tmp);
} /* }}} int cf_util_get_port_number */
+int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
+{
+ int port;
+ char *service;
+ int status;
+
+ if (ci->values_num != 1)
+ {
+ ERROR ("cf_util_get_service: The %s option requires exactly "
+ "one argument.", ci->key);
+ return (-1);
+ }
+
+ if (ci->values[0].type == OCONFIG_TYPE_STRING)
+ return (cf_util_get_string (ci, ret_string));
+ if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
+ {
+ ERROR ("cf_util_get_service: The %s option requires "
+ "exactly one string or numeric argument.",
+ ci->key);
+ }
+
+ port = 0;
+ status = cf_util_get_int (ci, &port);
+ if (status != 0)
+ return (status);
+ else if ((port < 1) || (port > 65535))
+ {
+ ERROR ("cf_util_get_service: The port number given "
+ "for the %s option is out of "
+ "range (%i).", ci->key, port);
+ return (-1);
+ }
+
+ service = malloc (6);
+ if (service == NULL)
+ {
+ ERROR ("cf_util_get_service: Out of memory.");
+ return (-1);
+ }
+ ssnprintf (service, 6, "%i", port);
+
+ sfree (*ret_string);
+ *ret_string = service;
+
+ return (0);
+} /* }}} int cf_util_get_service */
+
int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
{
if ((ci == NULL) || (ret_value == NULL))
6 src/configfile.h
View
@@ -116,6 +116,12 @@ int cf_util_get_flag (const oconfig_item_t *ci,
* failure. */
int cf_util_get_port_number (const oconfig_item_t *ci);
+/* Assures that the config option is either a service name (a string) or a port
+ * number (an integer in the appropriate range) and returns a newly allocated
+ * string. If ret_string points to a non-NULL pointer, it is freed before
+ * assigning a new value. */
+int cf_util_get_service (const oconfig_item_t *ci, char **ret_string);
+
int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value);
#endif /* defined(CONFIGFILE_H) */
Please sign in to comment.
Something went wrong with that request. Please try again.