Skip to content
This repository
Browse code

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 2 changed files with 54 additions and 0 deletions. Show diff stats Hide diff stats

  1. +48 0 src/configfile.c
  2. +6 0 src/configfile.h
48 src/configfile.c
@@ -1094,6 +1094,54 @@ int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1094 1094 return (tmp);
1095 1095 } /* }}} int cf_util_get_port_number */
1096 1096
  1097 +int cf_util_get_service (const oconfig_item_t *ci, char **ret_string) /* {{{ */
  1098 +{
  1099 + int port;
  1100 + char *service;
  1101 + int status;
  1102 +
  1103 + if (ci->values_num != 1)
  1104 + {
  1105 + ERROR ("cf_util_get_service: The %s option requires exactly "
  1106 + "one argument.", ci->key);
  1107 + return (-1);
  1108 + }
  1109 +
  1110 + if (ci->values[0].type == OCONFIG_TYPE_STRING)
  1111 + return (cf_util_get_string (ci, ret_string));
  1112 + if (ci->values[0].type != OCONFIG_TYPE_NUMBER)
  1113 + {
  1114 + ERROR ("cf_util_get_service: The %s option requires "
  1115 + "exactly one string or numeric argument.",
  1116 + ci->key);
  1117 + }
  1118 +
  1119 + port = 0;
  1120 + status = cf_util_get_int (ci, &port);
  1121 + if (status != 0)
  1122 + return (status);
  1123 + else if ((port < 1) || (port > 65535))
  1124 + {
  1125 + ERROR ("cf_util_get_service: The port number given "
  1126 + "for the %s option is out of "
  1127 + "range (%i).", ci->key, port);
  1128 + return (-1);
  1129 + }
  1130 +
  1131 + service = malloc (6);
  1132 + if (service == NULL)
  1133 + {
  1134 + ERROR ("cf_util_get_service: Out of memory.");
  1135 + return (-1);
  1136 + }
  1137 + ssnprintf (service, 6, "%i", port);
  1138 +
  1139 + sfree (*ret_string);
  1140 + *ret_string = service;
  1141 +
  1142 + return (0);
  1143 +} /* }}} int cf_util_get_service */
  1144 +
1097 1145 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */
1098 1146 {
1099 1147 if ((ci == NULL) || (ret_value == NULL))
6 src/configfile.h
@@ -116,6 +116,12 @@ int cf_util_get_flag (const oconfig_item_t *ci,
116 116 * failure. */
117 117 int cf_util_get_port_number (const oconfig_item_t *ci);
118 118
  119 +/* Assures that the config option is either a service name (a string) or a port
  120 + * number (an integer in the appropriate range) and returns a newly allocated
  121 + * string. If ret_string points to a non-NULL pointer, it is freed before
  122 + * assigning a new value. */
  123 +int cf_util_get_service (const oconfig_item_t *ci, char **ret_string);
  124 +
119 125 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value);
120 126
121 127 #endif /* defined(CONFIGFILE_H) */

0 comments on commit d0efcb2

Please sign in to comment.
Something went wrong with that request. Please try again.