eapol/eapol_framework/eapol_common/am/common/fileconfig_utils.cpp
changeset 0 c8830336c852
child 2 1c7bc153c08e
equal deleted inserted replaced
-1:000000000000 0:c8830336c852
       
     1 /*
       
     2 * Copyright (c) 2001-2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  EAP and WLAN authentication protocols.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // This is enumeration of EAPOL source code.
       
    20 #if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    21 	#undef EAP_FILE_NUMBER_ENUM
       
    22 	#define EAP_FILE_NUMBER_ENUM 16 
       
    23 	#undef EAP_FILE_NUMBER_DATE 
       
    24 	#define EAP_FILE_NUMBER_DATE 1127594498 
       
    25 #endif //#if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    26 
       
    27 
       
    28 #include "eap_am_memory.h"
       
    29 #include "fileconfig_utils.h"
       
    30 
       
    31 #include <stdlib.h>
       
    32 
       
    33 static char* get_subsect (FILE* file)
       
    34 {
       
    35 
       
    36 	char* result = NULL;
       
    37 	char* line = new char[MAX_LINE_LENGTH];
       
    38 
       
    39 	if(!line) {
       
    40 		return NULL;
       
    41 	}
       
    42 	
       
    43 	for(;;)
       
    44 	{
       
    45 		result = fgets(line, MAX_LINE_LENGTH, file);
       
    46 		
       
    47 		// error or end of file
       
    48 		if(!result)
       
    49 		{
       
    50 			delete[] line;
       
    51 			break;
       
    52 		}
       
    53 		
       
    54 		// too short line, ignore
       
    55 		if(strlen(result) < 2)
       
    56 		{
       
    57 			continue;
       
    58 		}
       
    59 		
       
    60 		// In Linux and Symbian we have to remove
       
    61 		// the MS-DOS style carriage return
       
    62 		// (if the conf file happens to include those CRs).
       
    63 		if(result[strlen(result) - 2] == 0x0d)
       
    64 		{
       
    65 			result[strlen(result) - 2] = 0x0a;
       
    66 			result[strlen(result) - 1] = 0x00;
       
    67 		}
       
    68 		
       
    69 		// ignore the lines starting with newline, space, tab or '#'
       
    70 		if ((*result == '\n') ||
       
    71 			(*result == ' ') ||
       
    72 			(*result == '\t') ||
       
    73 			(*result == '#'))
       
    74 		{
       
    75 			continue;
       
    76 		}
       
    77 		else
       
    78 		{
       
    79 			break;
       
    80 		}
       
    81 	}
       
    82 
       
    83 	return result;
       
    84 }
       
    85 
       
    86 static int is_sect (char* line, char const* sectname)
       
    87 {
       
    88 	size_t length;
       
    89 	
       
    90 	if (strlen (line) < (length = strlen (sectname))){
       
    91 		return 0;
       
    92 	}
       
    93 	
       
    94 	return strncmp (sectname, line, length) == 0;
       
    95 }
       
    96 
       
    97 void cnf_subsects (FILE* file, const SubSection* subsects, void* data)
       
    98 {
       
    99 	const SubSection* iter;
       
   100 	char* line;
       
   101 	int found;
       
   102 	
       
   103 	for (line = get_subsect (file); line; line = get_subsect (file))
       
   104 	{
       
   105 		for (found = 0, iter = subsects; iter->name; iter++)
       
   106 		{
       
   107 			if (is_sect (line, iter->name))
       
   108 			{
       
   109 				line [strlen (line) - 1] = '\0';
       
   110 				(*iter->handler) (line, data);
       
   111 				found = 1;
       
   112 				break;
       
   113 			}
       
   114 		}
       
   115 		if (!found)
       
   116 		{
       
   117 			printf("ERROR: unrecognized subsection: %s\n", line);
       
   118 		}
       
   119 		delete[] line;
       
   120 	}
       
   121 }
       
   122 
       
   123 
       
   124 static char* find_rvalue (char* param)
       
   125 {
       
   126 	char* rvalue;
       
   127 	
       
   128 	for (rvalue = param; *rvalue; rvalue++)
       
   129 	{
       
   130 		if (*rvalue == '=')
       
   131 		{
       
   132 			break;
       
   133 		}
       
   134 	}
       
   135 	
       
   136 	if (!*rvalue)
       
   137 	{
       
   138 		return NULL;
       
   139 	}
       
   140 
       
   141 	// Check are there defined environment variable to override the file confoguration.
       
   142 	{
       
   143 		// backup spaces.
       
   144 		char *param_end = rvalue;
       
   145 		for (param_end--; isspace(*param_end); param_end--){
       
   146 			/* empty */
       
   147 		}
       
   148 		param_end++;
       
   149 		char saved_char = *param_end;
       
   150 		*param_end = '\0';
       
   151 		char * env_value = getenv(param);
       
   152 		if (env_value != 0)
       
   153 		{
       
   154 			printf("%s=%s; from environment variable\n", param, env_value);
       
   155 		}
       
   156 		*param_end = saved_char;
       
   157 
       
   158 		if (env_value != 0)
       
   159 		{
       
   160 			// There is overriding environment variable.
       
   161 			return env_value;
       
   162 		}
       
   163 	}
       
   164 
       
   165 	
       
   166 	for (rvalue++; isspace(*rvalue); rvalue++){
       
   167 		/* empty */
       
   168 	}
       
   169 	
       
   170 	printf("%s\n", param);
       
   171 	return rvalue;
       
   172 }
       
   173 
       
   174 void cnf_get_string (char* target, char* param, size_t maxlen)
       
   175 {
       
   176 	size_t len;
       
   177 	
       
   178 	// Returned value could be pointer to environment value.
       
   179 	// Do not modify returned value.
       
   180 	param = find_rvalue (param);
       
   181 	
       
   182 	if (param == 0
       
   183 		|| target == 0)
       
   184 	{
       
   185 		return;
       
   186 	}
       
   187 	
       
   188 	len = min (maxlen - 1, strlen (param));
       
   189 	memcpy (target, param, len);
       
   190 	target[len] = '\0';
       
   191 }
       
   192 
       
   193 i32_t cnf_get_int32 (char* param)
       
   194 {
       
   195 	param = find_rvalue (param);
       
   196 	
       
   197 	if (!param)
       
   198 	{
       
   199 		return 0;
       
   200 	}
       
   201 	
       
   202 	return atol (param);
       
   203 }