summaryrefslogtreecommitdiff
path: root/application/spidlib
diff options
context:
space:
mode:
authorJérome Lefranc2008-11-26 11:45:23 +0100
committerJérome Lefranc2008-11-26 11:45:23 +0100
commitac6649001f7c5eb5249e9a41d6e5b22c38ac8206 (patch)
treed402019c9239f54952a7a0a05db3791b524f1321 /application/spidlib
parent318ede41e18f20081c6f6d77cd57a470a38b28c2 (diff)
[spidlib] add spidlib_write_config() + unit tests
Diffstat (limited to 'application/spidlib')
-rw-r--r--application/spidlib/check/check.c4
-rw-r--r--application/spidlib/check/check_config.c96
-rw-r--r--application/spidlib/check/testfiles/config_file_14
-rw-r--r--application/spidlib/check/testfiles/config_file_214
-rw-r--r--application/spidlib/check/testfiles/config_file_313
-rw-r--r--application/spidlib/inc/spidlib.h1
-rw-r--r--application/spidlib/src/config/read_config_param.c267
-rw-r--r--application/spidlib/src/config/write_config_param.c273
-rw-r--r--application/spidlib/src/system/admin_logical_id.c6
9 files changed, 191 insertions, 487 deletions
diff --git a/application/spidlib/check/check.c b/application/spidlib/check/check.c
index e583825619..96f12abc25 100644
--- a/application/spidlib/check/check.c
+++ b/application/spidlib/check/check.c
@@ -1,12 +1,12 @@
#include <stdlib.h>
#include <check.h>
+extern Suite *config_suite(void);
extern Suite *ethernet_suite(void);
extern Suite *network_suite(void);
extern Suite *plc_suite(void);
extern Suite *services_suite(void);
extern Suite *system_suite(void);
-extern Suite *varfunc_suite(void);
int main(void)
{
@@ -15,7 +15,7 @@ int main(void)
Suite *s = suite_create("spidlib");
SRunner *sr = srunner_create(s);
- srunner_add_suite(sr, varfunc_suite());
+ srunner_add_suite(sr, config_suite());
srunner_add_suite(sr, ethernet_suite());
/*srunner_add_suite(sr, network_suite());
srunner_add_suite(sr, plc_suite());
diff --git a/application/spidlib/check/check_config.c b/application/spidlib/check/check_config.c
index 6a754e9c4a..a5c145952d 100644
--- a/application/spidlib/check/check_config.c
+++ b/application/spidlib/check/check_config.c
@@ -1,68 +1,114 @@
#include <stdlib.h>
#include <stdio.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include <errno.h>
#include <check.h>
+#define CONFIG_FILE_1 "testfiles/config_file_1"
+#define CONFIG_FILE_2 "testfiles/config_file_2"
+#define CONFIG_FILE_3 "testfiles/config_file_3"
+#define CONFIG_FILE_TMP "testfiles/config_file_tmp"
+
START_TEST (test_read_param)
{
char value[64];
fail_unless(((spidlib_read_config_param(NULL, "test", value, 16) < 0)
- && (errno == EINVAL)),
+ && (errno == EINVAL)),
"filename = NULL");
- fail_unless(((spidlib_read_config_param("config_file_1", NULL, value, 16) < 0)
- && (errno == EINVAL)),
+ fail_unless(((spidlib_read_config_param(CONFIG_FILE_1, NULL, value, 16) < 0)
+ && (errno == EINVAL)),
"label = NULL");
- fail_unless(((spidlib_read_config_param("config_file_1", "test", NULL, 16) < 0)
- && (errno == EINVAL)),
+ fail_unless(((spidlib_read_config_param(CONFIG_FILE_1, "config_file_1", "test", NULL, 16) < 0)
+ && (errno == EINVAL)),
"value = NULL");
- fail_unless(((spidlib_read_config_param("config_file_1", "test", value, 0) < 0)
- && (errno == EINVAL)),
+ fail_unless(((spidlib_read_config_param(CONFIG_FILE_1 , "test", value, 0) < 0)
+ && (errno == EINVAL)),
"value_len <= 0");
fail_unless(((spidlib_read_config_param("noname_file", "test", value, 16) < 0)
- && (errno == ENOENT)),
+ && (errno == ENOENT)),
"file doesn't exist");
- fail_unless(((spidlib_read_config_param("config_file_1", "nolabel", value, 16) < 0)
- && (errno == ENODATA)),
+ fail_unless(((spidlib_read_config_param(CONFIG_FILE_1, "nolabel", value, 16) < 0)
+ && (errno == ENODATA)),
"label doesn't exist");
- fail_unless(((spidlib_read_config_param("config_file_1", "label2", value, 5) < 0)
- && (errno == ENOSPC)),
+ fail_unless(((spidlib_read_config_param(CONFIG_FILE_1, "#", value, 16) < 0)
+ && (errno == ENODATA)),
+ "comment line");
+
+ fail_unless(((spidlib_read_config_param(CONFIG_FILE_1, "label2", value, 6) < 0)
+ && (errno == ENOSPC)),
"value buffer too short");
- fail_unless(((spidlib_read_config_param("config_file_1", "label2", value, 6) >= 0)
- && !strcmp(value, "value1")),
+ fail_unless(((spidlib_read_config_param(CONFIG_FILE_1, "label2", value, 7) >= 0)
+ && !strcmp(value, "value1")),
"label2 = value1");
- fail_unless(((spidlib_read_config_param("config_file_1", "label3", value, 32) >= 0)
- && !strcmp(value, "multiple value string")),
+ fail_unless(((spidlib_read_config_param(CONFIG_FILE_1, "label3", value, 32) >= 0)
+ && !strcmp(value, "multiple value string")),
"multiple value string");
- fail_unless(((spidlib_read_config_param("config_file_1", "label4", value, 32) >= 0)
- && !strcmp(value, "no space between delimiter")),
+ fail_unless(((spidlib_read_config_param(CONFIG_FILE_1, "label4", value, 32) >= 0)
+ && !strcmp(value, "no space between delimiter")),
"no space between delimiter");
- fail_unless(((spidlib_read_config_param("config_file_1", "label5", value, 32) >= 0)
- && !strcmp(value, "multi spaced")),
+ fail_unless(((spidlib_read_config_param(CONFIG_FILE_1, "label5", value, 32) >= 0)
+ && !strcmp(value, "multi spaced")),
"multi spaced");
- fail_unless(((spidlib_read_config_param("config_file_1", "label6", value, 32) >= 0)
- && !strcmp(value, "")),
+ fail_unless(((spidlib_read_config_param(CONFIG_FILE_1, "label6", value, 32) >= 0)
+ && !strcmp(value, "")),
"empty value");
- fail_unless(((spidlib_read_config_param("config_file_1", "label7", value, 32) >= 0)
- && !strcmp(value, "after bad entry")),
+ fail_unless(((spidlib_read_config_param(CONFIG_FILE_1, "label7", value, 32) >= 0)
+ && !strcmp(value, "after bad entry")),
"label after bad entry");
}
END_TEST
-START_TEST (test_read_param)
+START_TEST (test_write_param)
{
+ char value[64];
+ struct stat file_stat;
+
+ fail_unless(((spidlib_write_config_param(NULL, "label", "value") < 0)
+ && (errno == EINVAL)),
+ "filename = NULL");
+
+ fail_unless(((spidlib_write_config_param(CONFIG_FILE_1, NULL, "value") < 0)
+ && (errno == EINVAL)),
+ "label = NULL");
+
+ fail_unless(((spidlib_write_config_param(CONFIG_FILE_1, "", "value") < 0)
+ && (errno == EINVAL)),
+ "label = NULL");
+
+ fail_unless(((spidlib_write_config_param(CONFIG_FILE_1, "label", NULL) < 0)
+ && (errno == EINVAL)),
+ "value = NULL");
+
+ fail_unless(((spidlib_write_config_param("noname_file", "label", "value") < 0)
+ && (errno == ENOENT)),
+ "file doesn't exist");
+ system("cp " CONFIG_FILE_1 " " CONFIG_FILE_TMP);
+ fail_unless(((spidlib_write_config_param(CONFIG_FILE_TMP, "label", "value") >= 0)
+ && (system("diff " CONFIG_FILE_TMP " " CONFIG_FILE_2 " > result.tmp") == 0)
+ && (stat("result.tmp", &file_stat) >= 0)
+ && (file_stat.st_size == 0)),
+ "label = value");
+
+ system("cp " CONFIG_FILE_1 " " CONFIG_FILE_TMP);
+ fail_unless(((spidlib_write_config_param(CONFIG_FILE_TMP, "label3", "new value") >= 0)
+ && (system("diff " CONFIG_FILE_TMP " " CONFIG_FILE_3 " > result.tmp") == 0)
+ && (stat("result.tmp", &file_stat) >= 0)
+ && (file_stat.st_size == 0)),
+ "label = new value");
}
END_TEST
diff --git a/application/spidlib/check/testfiles/config_file_1 b/application/spidlib/check/testfiles/config_file_1
index 92873f6e39..eb2cabecdb 100644
--- a/application/spidlib/check/testfiles/config_file_1
+++ b/application/spidlib/check/testfiles/config_file_1
@@ -1,11 +1,11 @@
# this is example config file
label1 = value1
-label2 = value1
+label2 = value1
label3 = multiple value string
# comment here
label4=no space between delimiter
-label5 = multi spaced
+label5 = multi spaced
label6 =
# oups, bad entries
= no label
diff --git a/application/spidlib/check/testfiles/config_file_2 b/application/spidlib/check/testfiles/config_file_2
new file mode 100644
index 0000000000..ab8ed2a5ea
--- /dev/null
+++ b/application/spidlib/check/testfiles/config_file_2
@@ -0,0 +1,14 @@
+# this is example config file
+label1 = value1
+label2 = value1
+label3 = multiple value string
+# comment here
+
+label4=no space between delimiter
+label5 = multi spaced
+label6 =
+# oups, bad entries
+ = no label
+# can we reach this label ?
+label7 = after bad entry
+label = value
diff --git a/application/spidlib/check/testfiles/config_file_3 b/application/spidlib/check/testfiles/config_file_3
new file mode 100644
index 0000000000..9b81898204
--- /dev/null
+++ b/application/spidlib/check/testfiles/config_file_3
@@ -0,0 +1,13 @@
+# this is example config file
+label1 = value1
+label2 = value1
+label3 = new value
+# comment here
+
+label4=no space between delimiter
+label5 = multi spaced
+label6 =
+# oups, bad entries
+ = no label
+# can we reach this label ?
+label7 = after bad entry
diff --git a/application/spidlib/inc/spidlib.h b/application/spidlib/inc/spidlib.h
index b028af3137..18a80994b1 100644
--- a/application/spidlib/inc/spidlib.h
+++ b/application/spidlib/inc/spidlib.h
@@ -22,6 +22,7 @@ typedef int spidlib_boolean_t;
/*---------------------- config file mgt --------------------------------------------*/
#define SPIDLIB_CONFIG_DELIMITER "="
+#define MAX_LINE_BUFFER_LEN 1024
extern int spidlib_read_config_param(const char *filename, const char *label, char *value,
int buffer_len);
extern int spidlib_write_config_param(const char *filename, const char *label, const char *value);
diff --git a/application/spidlib/src/config/read_config_param.c b/application/spidlib/src/config/read_config_param.c
index f755175cd6..33802612b9 100644
--- a/application/spidlib/src/config/read_config_param.c
+++ b/application/spidlib/src/config/read_config_param.c
@@ -1,13 +1,13 @@
/*-----------------------------------------------------------------------------
- File: config_handlers.c
- Description: Functions to handle read/write for configuration file.
+ File: read_config_param.c
+ Description: Functions to handle read for configuration file.
Project: SPiDLIB
- Target: SPiDCOM modem w/ SPC200e
+ Target: SPiDCOM modem w/ SPC200
Version: 0.1
Revision list: -
- Company: SPiDCOM Eastern Europe
- Author: Slobodan Stanisic
- Date: June 2008.
+ Company: SPiDCOM
+ Author: Jerome Lefranc
+ Date: Nov 2008.
-----------------------------------------------------------------------------*/
/*---------------------- Include files --------------------------------------*/
@@ -18,7 +18,6 @@
#include <errno.h>
#include "spidlib.h"
-#define MAX_LINE_BUFFER_LEN 1024
/*-----------------------------------------------------------------------------
Function: read_config_param
@@ -40,8 +39,8 @@ spidlib_read_config_param(const char *filename, const char *label, char *value,
{
char buffer[MAX_LINE_BUFFER_LEN];
FILE *fp;
- char *result, *strtok_ctx, *ptr;
-#if 0
+ char *result, *strtok_ctx, *ptr, *blank_ptr;
+
/* check input parameters */
if((filename == NULL) || (label == NULL) || (value == NULL) || (buffer_len <= 0))
{
@@ -57,236 +56,44 @@ spidlib_read_config_param(const char *filename, const char *label, char *value,
while(result = fgets(buffer, MAX_LINE_BUFFER_LEN - 1, fp))
{
+ /* get the label */
ptr = strtok_r(buffer, SPIDLIB_CONFIG_DELIMITER " \t", &strtok_ctx);
- if((ptr == NULL) || (strlen(ptr) <= 0) || strcmp(ptr, label))
- continue;
- ptr = strtok_r(NULL, "\n\r", &strtok_ctx);
- if(
-
- int param_correct = 0;
- int buffer_index = 0;
- int parameter_extracted = 0;
- int buffer_overrun_flag = -1;
- int x;
-
-/* Test max. output length */
- if ( (output_buffer_len <= 0) ||
- (output_buffer_len > MAX_OUTPUT_BUFFER_LENGTH) )
- {
- return (-(errno = EINVAL));
- }
-
-/* Try to open file for reading */
- if ( (fp = fopen(filename, "r") ) == NULL )
- return (-(errno = ENOTDIR));
-
-/* Reset output string */
- strcpy(output_value, "");
-
-/* Browse through all lines */
- while ( fgets(buffer,MAX_FILE_LINE_LENGTH,fp) )
- {
-/* Reset buffer index */
- buffer_index = 0;
-
-/* Reset flag for correctly detected parameter */
- param_correct = 0;
-
-/* Ignore spaces at the beginning of a line */
- while ( buffer[buffer_index] == ' ' )
- {
- buffer_index++;
- }
-
-/* '#' at beginning of a line means comment - ignore it */
- if ( buffer[buffer_index] == '#' )
+ //printf("label=%s\n", ptr);
+ /* check if it is the wanted label */
+ if((ptr == NULL) || (strlen(ptr) <= 0) || (*ptr == '#') || strcmp(ptr, label))
continue;
-
-/* Now check for correct parameter name */
- x = 0;
- while ( *(param+x) != '\0' )
+ /* remove space and '=' before the value */
+ ptr += strlen(ptr) + 1;
+ while(isblank(*ptr) || (*ptr == *SPIDLIB_CONFIG_DELIMITER))
+ ptr++;
+ ptr = strtok_r(ptr, "\n\r", &strtok_ctx);
+ /* check if value is empty */
+ *value = '\0';
+ if(ptr != NULL)
{
- if ( buffer[buffer_index++] != *(param+x) )
+ /* remove spaces at the end of value */
+ blank_ptr = ptr + strlen(ptr) - 1;
+ while((blank_ptr >= ptr) && isblank(*blank_ptr))
{
- param_correct = -1;
- continue;
+ *blank_ptr = '\0';
+ blank_ptr--;
}
- x++;
- }
-
-/* Check if compare succeded */
- if ( param_correct )
- continue;
-
-/* Dump spaces after parameter name */
- while ( buffer[buffer_index] == ' ' )
- {
- buffer_index++;
- }
-
-/* Check for '=' char */
- if ( buffer[buffer_index] != '=' )
- continue;
-
-/* Skip '=' sign */
- buffer_index++;
-
-/* Dump spaces after '=' sign */
- while ( buffer[buffer_index] == ' ' )
- {
- buffer_index++;
- }
-
-/* Pull rest of the line in the output buffer */
-/* x is counter of elements in output string */
- x = 0;
-/* Set parameter_extracted flag to note that parameter is found. If there are
- no errors while reading parameter value, this flag will indicate that
- requested parameter is found. */
- parameter_extracted = -1;
-
- while ( (buffer[buffer_index] != '\0') &&
- (buffer[buffer_index] != '\n') )
- {
- *(output_value++) = buffer[buffer_index++];
- x++;
-/* Check for buffer overrun, in case of error terminate string */
- if ( x > output_buffer_len )
+ //printf("value='%s'\n", ptr);
+ /* check for buffer space to copy the value */
+ if(strlen(ptr) >= buffer_len)
{
- buffer_overrun_flag = 0;
- *(--output_value) = '\0';
- continue;
+ errno = ENOSPC;
+ goto error;
}
+ strcpy(value, ptr);
}
+ fclose(fp);
+ return 0;
}
-
+ /* label not found */
+ errno = ENODATA;
+error:
fclose(fp);
-
-/* Check if buffer overrun occured */
- if ( !buffer_overrun_flag )
- {
- return (-(errno = EFAULT));
- }
-
-/* Check if parameter extracted */
- if ( !parameter_extracted )
- {
- return (-(errno = EFAULT));
- }
-#endif
- return 0;
+ return -1;
}
-/*-----------------------------------------------------------------------------
- Function: write_config_param
- Input: char *filename - full filename of a configuration file
- char *param - pointer to beginning of string containing parameter name
- char *value - pointer to buffer where parameter value is located
- Output: 0 - Function returned successfully
- -errno - There were errors
- Error handling: Look for output value. In case of error check errno variable:
- EINVAL - inappropriate buffer length requested
- ENOTDIR - missing configuration file
- EFAULT - buffer overrun or no parameter
- Note: -
------------------------------------------------------------------------------*/
-int
-spidlib_write_config_param(const char *filename, const char *label, const char *value)
-{
- char buffer[MAX_LINE_BUFFER_LEN];
- char temp_buffer[MAX_LINE_BUFFER_LEN];
- FILE *fp, *fp_memo;
- int param_correct = 0;
- int buffer_index = 0;
- int parameter_found = -1;
- int line_length;
- int c;
- int x;
-
-/* Test max. parameter length
- if ( strlen(value) > MAX_PARAMETER_VALUE_LENGTH )
- return (-(errno = EINVAL)); */
-
-/* Try to open file for reading and writing */
- if ( (fp = fopen(filename, "rw") ) == NULL )
- return (-(errno = ENOTDIR));
-
-/* Browse through all lines */
- while ( fgets(buffer,MAX_LINE_BUFFER_LEN,fp) && parameter_found )
- {
-/* Reset buffer index */
- buffer_index = 0;
-
-/* Reset flag for correctly detected parameter */
- param_correct = 0;
-
-/* Get read line length */
- line_length = strlen ( buffer );
-
-/* Ignore spaces at the beginning of a line */
- while ( buffer[buffer_index] == ' ' )
- {
- buffer_index++;
- }
-
-/* '#' at beginning of a line means comment - ignore it */
- if ( buffer[buffer_index] == '#' )
- continue;
-
-/* Now check for correct parameter name */
- x = 0;
- while ( *(label+x) != '\0' )
- {
- if ( buffer[buffer_index++] != *(label+x) )
- {
- param_correct = -1;
- continue;
- }
- x++;
- }
-
-/* Check if compare succeded */
- if ( param_correct )
- continue;
- else
-/* Leave the loop by flagging parameter_found */
- parameter_found = 0;
- }
-
-/* Make a backup of current position */
- *fp_memo = *fp;
-
- if ( !parameter_found )
- {
-/* Read rest of the file and put it in the buffer */
- x = 0;
- while ( (c = fgetc(fp)) != EOF )
- {
- temp_buffer[x++] = (char)c;
- }
-
-/* Write new value to output file */
-/* First, calculate file pointer position */
- fp_memo -= buffer_index;
-
-/* Write new value to file */
- fwrite ( value, 1, strlen(value), fp_memo );
-/* Add NULL and end of line */
- fputc ( '\0', fp_memo );
- fputc ( '\n', fp_memo );
-
-/* Add rest of the file */
-/* TODO!!! */
- }
- else
- {
-
- }
-
-
-/* Unfinished part */
-
- fclose(fp);
-
- return 0;
-}
diff --git a/application/spidlib/src/config/write_config_param.c b/application/spidlib/src/config/write_config_param.c
index 5e873b5b3a..392455325f 100644
--- a/application/spidlib/src/config/write_config_param.c
+++ b/application/spidlib/src/config/write_config_param.c
@@ -1,13 +1,13 @@
/*-----------------------------------------------------------------------------
- File: config_handlers.c
- Description: Functions to handle read/write for configuration file.
+ File: write_config_param.c
+ Description: Functions to handle write for configuration file.
Project: SPiDLIB
Target: SPiDCOM modem w/ SPC200e
Version: 0.1
Revision list: -
- Company: SPiDCOM Eastern Europe
- Author: Slobodan Stanisic
- Date: June 2008.
+ Company: SPiDCOM
+ Author: Jerome Lefranc
+ Date: Nov 2008.
-----------------------------------------------------------------------------*/
/*---------------------- Include files --------------------------------------*/
@@ -20,11 +20,10 @@
#include "spidlib.h"
/*-----------------------------------------------------------------------------
- Function: read_config_param
+ Function: write_config_param
Input: char *filename - full filename of a configuration file
char *param - pointer to beginning of string containing parameter name
- char *output_value - pointer to buffer where output is expected
- int output_buffer_len - max. length of output buffer
+ char *value - pointer to buffer where parameter value is located
Output: 0 - Function returned successfully
-errno - There were errors
Error handling: Look for output value. In case of error check errno variable:
@@ -34,235 +33,59 @@
Note: -
-----------------------------------------------------------------------------*/
int
-read_config_param(char *filename, char *param, char *output_value,
- int output_buffer_len)
+spidlib_write_config_param(const char *filename, const char *label, const char *value)
{
- char buffer[MAX_FILE_LINE_LENGTH];
- FILE *fp;
- int param_correct = 0;
- int buffer_index = 0;
- int parameter_extracted = 0;
- int buffer_overrun_flag = -1;
- int x;
-
-/* Test max. output length */
- if ( (output_buffer_len <= 0) ||
- (output_buffer_len > MAX_OUTPUT_BUFFER_LENGTH) )
+ char buffer[MAX_LINE_BUFFER_LEN];
+ char out_filename[64];
+ char *ptr, *ptr2;
+ FILE *fp_in, *fp_out;
+ int is_found = 0;
+
+ /* check input parameters */
+ if((filename == NULL) || (label == NULL) || (strlen(label) <= 0) || (value == NULL))
{
- return (-(errno = EINVAL));
+ errno = EINVAL;
+ return -1;
}
-/* Try to open file for reading */
- if ( (fp = fopen(filename, "r") ) == NULL )
- return (-(errno = ENOTDIR));
-
-/* Reset output string */
- strcpy(output_value, "");
-
-/* Browse through all lines */
- while ( fgets(buffer,MAX_FILE_LINE_LENGTH,fp) )
- {
-/* Reset buffer index */
- buffer_index = 0;
-
-/* Reset flag for correctly detected parameter */
- param_correct = 0;
-
-/* Ignore spaces at the beginning of a line */
- while ( buffer[buffer_index] == ' ' )
- {
- buffer_index++;
- }
-
-/* '#' at beginning of a line means comment - ignore it */
- if ( buffer[buffer_index] == '#' )
- continue;
-
-/* Now check for correct parameter name */
- x = 0;
- while ( *(param+x) != '\0' )
- {
- if ( buffer[buffer_index++] != *(param+x) )
- {
- param_correct = -1;
- continue;
- }
- x++;
- }
-
-/* Check if compare succeded */
- if ( param_correct )
- continue;
-
-/* Dump spaces after parameter name */
- while ( buffer[buffer_index] == ' ' )
- {
- buffer_index++;
- }
-
-/* Check for '=' char */
- if ( buffer[buffer_index] != '=' )
- continue;
-
-/* Skip '=' sign */
- buffer_index++;
-
-/* Dump spaces after '=' sign */
- while ( buffer[buffer_index] == ' ' )
- {
- buffer_index++;
- }
-
-/* Pull rest of the line in the output buffer */
-/* x is counter of elements in output string */
- x = 0;
-/* Set parameter_extracted flag to note that parameter is found. If there are
- no errors while reading parameter value, this flag will indicate that
- requested parameter is found. */
- parameter_extracted = -1;
-
- while ( (buffer[buffer_index] != '\0') &&
- (buffer[buffer_index] != '\n') )
- {
- *(output_value++) = buffer[buffer_index++];
- x++;
-/* Check for buffer overrun, in case of error terminate string */
- if ( x > output_buffer_len )
- {
- buffer_overrun_flag = 0;
- *(--output_value) = '\0';
- continue;
- }
- }
- }
-
- fclose(fp);
-
-/* Check if buffer overrun occured */
- if ( !buffer_overrun_flag )
+ /* open config file */
+ if((fp_in = fopen(filename, "r")) == NULL)
{
- return (-(errno = EFAULT));
- }
-
-/* Check if parameter extracted */
- if ( !parameter_extracted )
+ return -1;
+ }
+ /* create the modified config file */
+ sprintf(out_filename, "%sXXXXXX", filename);
+ if((fp_out = fdopen(mkstemp(out_filename), "w")) == NULL)
{
- return (-(errno = EFAULT));
- }
-
- return 0;
-}
-
-/*-----------------------------------------------------------------------------
- Function: write_config_param
- Input: char *filename - full filename of a configuration file
- char *param - pointer to beginning of string containing parameter name
- char *value - pointer to buffer where parameter value is located
- Output: 0 - Function returned successfully
- -errno - There were errors
- Error handling: Look for output value. In case of error check errno variable:
- EINVAL - inappropriate buffer length requested
- ENOTDIR - missing configuration file
- EFAULT - buffer overrun or no parameter
- Note: -
------------------------------------------------------------------------------*/
-int
-write_config_param(char *filename, char *param, char *value)
-{
- char buffer[MAX_FILE_LINE_LENGTH];
- char temp_buffer[MAX_FILE_LINE_LENGTH];
- FILE *fp, *fp_memo;
- int param_correct = 0;
- int buffer_index = 0;
- int parameter_found = -1;
- int line_length;
- int c;
- int x;
-
-/* Test max. parameter length
- if ( strlen(value) > MAX_PARAMETER_VALUE_LENGTH )
- return (-(errno = EINVAL)); */
-
-/* Try to open file for reading and writing */
- if ( (fp = fopen(filename, "rw") ) == NULL )
- return (-(errno = ENOTDIR));
-
-/* Browse through all lines */
- while ( fgets(buffer,MAX_FILE_LINE_LENGTH,fp) && parameter_found )
- {
-/* Reset buffer index */
- buffer_index = 0;
-
-/* Reset flag for correctly detected parameter */
- param_correct = 0;
-
-/* Get read line length */
- line_length = strlen ( buffer );
-
-/* Ignore spaces at the beginning of a line */
- while ( buffer[buffer_index] == ' ' )
- {
- buffer_index++;
- }
-
-/* '#' at beginning of a line means comment - ignore it */
- if ( buffer[buffer_index] == '#' )
- continue;
-
-/* Now check for correct parameter name */
- x = 0;
- while ( *(param+x) != '\0' )
- {
- if ( buffer[buffer_index++] != *(param+x) )
- {
- param_correct = -1;
- continue;
- }
- x++;
- }
-
-/* Check if compare succeded */
- if ( param_correct )
- continue;
- else
-/* Leave the loop by flagging parameter_found */
- parameter_found = 0;
+ fclose(fp_in);
+ return -1;
}
-
-/* Make a backup of current position */
- *fp_memo = *fp;
-
- if ( !parameter_found )
+ /* find the label to modify */
+ while(fgets(buffer, MAX_LINE_BUFFER_LEN - 1, fp_in))
{
-/* Read rest of the file and put it in the buffer */
- x = 0;
- while ( (c = fgetc(fp)) != EOF )
+ /* get the label */
+ ptr = ptr2 = buffer;
+ while((*ptr2 != *SPIDLIB_CONFIG_DELIMITER) && !isblank(*ptr2))
+ ptr2++;
+
+ /* check if label is found */
+ if((ptr != ptr2) && (*ptr != '#') && !memcmp(ptr, label, ptr2 - ptr))
{
- temp_buffer[x++] = (char)c;
+ /* write the new value */
+ sprintf(ptr2, " %s %s\n", SPIDLIB_CONFIG_DELIMITER, value);
+ is_found = 1;
}
-
-/* Write new value to output file */
-/* First, calculate file pointer position */
- fp_memo -= buffer_index;
-
-/* Write new value to file */
- fwrite ( value, 1, strlen(value), fp_memo );
-/* Add NULL and end of line */
- fputc ( '\0', fp_memo );
- fputc ( '\n', fp_memo );
-
-/* Add rest of the file */
-/* TODO!!! */
+ fprintf(fp_out, "%s", buffer);
}
- else
+ if(!is_found)
{
-
+ /* add new 'label = value' at the end of file */
+ //printf("%s %s %s\n", label, SPIDLIB_CONFIG_DELIMITER, value);
+ fprintf(fp_out, "%s %s %s\n", label, SPIDLIB_CONFIG_DELIMITER, value);
}
-
-
-/* Unfinished part */
-
- fclose(fp);
-
+ fclose(fp_out);
+ fclose(fp_in);
+ if(rename(out_filename, filename) < 0)
+ printf("rename errno=%d\n", errno);
return 0;
}
diff --git a/application/spidlib/src/system/admin_logical_id.c b/application/spidlib/src/system/admin_logical_id.c
index 8c8b8a5da7..def2478bf4 100644
--- a/application/spidlib/src/system/admin_logical_id.c
+++ b/application/spidlib/src/system/admin_logical_id.c
@@ -46,9 +46,9 @@ spidlib_get_admin_logical_id(char *admin_logical_id)
return 0;
#else
return spidlib_read_config_param(SPIDLIB_CONFIG_SNMP_FILE,
- SPIDLIB_ADMIN_LOGICAL_ID_NAME,
- admin_logical_id,
- SPIDLIB_ADMIN_LOGICAL_ID_STRING_MAX_LENGTH);
+ SPIDLIB_ADMIN_LOGICAL_ID_NAME,
+ admin_logical_id,
+ SPIDLIB_ADMIN_LOGICAL_ID_STRING_MAX_LENGTH);
#endif /* SPIDLIB_STUB */
}