Beispiel #1
0
/**
   This handler reads the data in the passed file name.
   Then appends the words in the table (cutting all spaces) to the existing cmd line options.
   It adds to the cmd_data_t struct.
   Actions:
   - open file
   - read characters and cut spaces away
   - add values one by one to the existing cmd data
*/
static int get_options_from_file_handler(cmd_data_t *cmd, int num, void *context)
{
	int rc = 0;
	FILE *fp = NULL;
	char *buf = NULL;
	const size_t buflen = DYNDNS_SERVER_NAME_LEN;
	ddns_t *ctx = (ddns_t *)context;
	cfg_parser_t parser;

	if (!ctx || !cmd)
		return RC_INVALID_POINTER;

	do {
		buf = malloc(buflen);
		if (!buf) {
			rc = RC_OUT_OF_MEMORY;
			break;
		}

		fp = fopen(cmd->argv[num], "r");
		if (!fp) {
			logit(LOG_ERR, "Cannot open config file %s: %s", cmd->argv[num], strerror(errno));
			rc = RC_FILE_IO_OPEN_ERROR;
			break;
		}

		/* Save for later... */
		if (ctx->cfgfile)
			free(ctx->cfgfile);
		ctx->cfgfile = strdup(cmd->argv[num]);

		if ((rc = parser_init(&parser, fp)) != 0)
			break;

		while (!feof(fp)) {
			rc = parser_read_option(&parser, buf, buflen);
			if (rc != 0)
				break;

			if (!strlen(buf))
				break;

			rc = cmd_add_val(cmd, buf);
			if (rc != 0)
				break;
		}
	}
	while (0);

	if (fp)
		fclose(fp);

	if (buf)
		free(buf);

	return rc;
}
Beispiel #2
0
/**
   This handler reads the data in the passed file name.
   Then appends the words in the table (cutting all spaces) to the existing cmd line options.
   It adds to the CMD_DATA struct.
   Actions:
   - open file
   - read characters and cut spaces away
   - add values one by one to the existing p_cmd data
*/
static RC_TYPE get_options_from_file_handler(CMD_DATA *p_cmd, int current_nr, void *p_context)
{
	RC_TYPE rc = RC_OK;
	DYN_DNS_CLIENT *p_self = (DYN_DNS_CLIENT *) p_context;
	FILE *p_file = NULL;
	char *p_tmp_buffer = NULL;
	const int buffer_size = DYNDNS_SERVER_NAME_LENGTH;
	OPTION_FILE_PARSER parser;

	if (!p_self || !p_cmd)
	{
		return RC_INVALID_POINTER;
	}

	do
	{
 		p_tmp_buffer = malloc(buffer_size);
   		if (!p_tmp_buffer)
	 	{
	 		rc = RC_OUT_OF_MEMORY;
	 		break;
	  	}
	  	p_file = fopen(p_cmd->argv[current_nr], "r");
	  	if (!p_file)
	  	{
			logit(LOG_ERR, MODULE_TAG "Cannot open config file %s: %s", p_cmd->argv[current_nr], strerror(errno));
	  		rc = RC_FILE_IO_OPEN_ERROR;
	  		break;
	  	}

		/* Save for later... */
		if (p_self->cfgfile)
			free(p_self->cfgfile);
		p_self->cfgfile = strdup(p_cmd->argv[current_nr]);

		if ((rc = parser_init(&parser, p_file)) != RC_OK)
		{
			break;
		}

		while (!feof(p_file))
		{
			rc = parser_read_option(&parser,p_tmp_buffer, buffer_size);
			if (rc != RC_OK)
			{
				break;
			}

			if (!strlen(p_tmp_buffer))
			{
				break;
			}

			rc = cmd_add_val(p_cmd, p_tmp_buffer);
			if (rc != RC_OK)
			{
				break;
			}
   		}
	}
	while (0);

	if (p_file)
	{
		fclose(p_file);
	}
	if (p_tmp_buffer)
	{
		free(p_tmp_buffer);
	}

	return rc;
}