config.c
author viric@llimona
Tue, 30 May 2006 12:02:13 +0200
changeset 55 c72dbd390cf2
parent 44 ddab92173045
child 57 de776a4548da
permissions -rw-r--r--
Now the tcp_server sends the log file. The tcp server is now shutted down by its own function call.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
     1
#include <string.h>
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
     2
#include <stdio.h>
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
     3
#include <sys/types.h>
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
     4
#include <sys/stat.h>
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
     5
#include <fcntl.h>
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
     6
#include <unistd.h>
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
     7
#include <assert.h>
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
     8
#include <errno.h>
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
     9
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    10
#include "syslog.h"
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    11
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    12
#define CONFIG_FILE "syslog.conf"
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    13
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    14
/* Data structures */
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    15
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    16
static struct
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    17
{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    18
	int n; /* Number of config elements */
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    19
	struct config_element el[ETYPE_END];
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    20
} main_config;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    22
/* Aquesta variable ha de reflexar l'ordre de la enum etype_config de syslog.h*/
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    23
const static struct config_element default_config[ETYPE_END+1] = {
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    24
	{ FROM_UNIX, "unixlog" },
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    25
	{ FROM_NPIPE, "disabled" },
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    26
	{ FROM_UDP, "disabled" },
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    27
	{ TO_UDP_HOST, "localhost" },
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    28
	{ TO_UDP_PORT, "1235" },
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    29
	{ TCP_MANAGER, "1234" },
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    30
	{ LOG_FILE, "file.log" },
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    31
	{ ETYPE_END, "" }};
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    32
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    33
/* Aquesta variable ha de reflexar l'ordre de la enum etype_config de syslog.h*/
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    34
const static struct {
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    35
	enum etype_config type;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    36
	char string[MAX_STRING];
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    37
} etype_string[ETYPE_END+1] = {
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    38
	{ FROM_UNIX, "from_unix" },
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    39
	{ FROM_NPIPE, "from_npipe" },
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    40
	{ FROM_UDP, "from_udp" },
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    41
	{ TO_UDP_HOST, "to_udp_host" },
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    42
	{ TO_UDP_PORT, "to_udp_port" },
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    43
	{ TCP_MANAGER, "tcp_manager" },
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    44
	{ LOG_FILE, "log_file" },
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    45
	{ ETYPE_END, "" }};
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    46
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    47
/* Prototypes */
23
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
    48
static int add_config_element(const char * restrict p, const char * restrict v,
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
    49
		const int max);
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    50
static void read_config(char * restrict fname);
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    51
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    52
/* Functions' code */
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    53
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    54
void init_config()
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    55
{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    56
	main_config.n = 0;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    57
	read_config(CONFIG_FILE);
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    58
}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    59
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    60
static void read_config(char * restrict fname)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    61
{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    62
	int file;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    63
	char rchar;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    64
	int res;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    65
	char parameter[MAX_STRING];
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    66
	int nparameter;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    67
	char value[MAX_STRING];
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    68
	int nvalue;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    69
	enum {
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    70
		READ_PARAMETER,
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    71
		READ_VALUE
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    72
	} state;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    73
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    74
	file = open(fname, O_RDONLY);
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    75
	if (file == -1)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    76
	{
44
ddab92173045 Corrected some debug info.
viric@llimona
parents: 24
diff changeset
    77
		fprintf(stderr,"The config file cannot be opened.\n");
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    78
		return;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    79
	}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    80
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    81
	nparameter=0;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    82
	state=READ_PARAMETER;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    83
	while ((res = read(file, &rchar, 1)) == 1)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    84
	{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    85
		switch (rchar)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    86
		{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    87
		case '=':
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    88
			assert(nparameter <= MAX_STRING);
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    89
			parameter[nparameter]='\0';
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    90
			nvalue=0;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    91
			state=READ_VALUE;
24
29ee3a15905b Fixed bugs of 'break;'.
viric@mandarina
parents: 23
diff changeset
    92
			break;
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    93
		case '\n':
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    94
			if (state == READ_VALUE)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    95
			{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    96
				value[nvalue]='\0';
24
29ee3a15905b Fixed bugs of 'break;'.
viric@mandarina
parents: 23
diff changeset
    97
				assert( add_config_element(parameter,
29ee3a15905b Fixed bugs of 'break;'.
viric@mandarina
parents: 23
diff changeset
    98
							value, MAX_STRING)>=0);
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
    99
			}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   100
			nparameter=0;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   101
			nvalue=0;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   102
			state=READ_PARAMETER;
24
29ee3a15905b Fixed bugs of 'break;'.
viric@mandarina
parents: 23
diff changeset
   103
			break;
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   104
		default:
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   105
			switch(state)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   106
			{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   107
			case READ_PARAMETER:
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   108
				assert(nparameter <= MAX_STRING);
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   109
				parameter[nparameter++]=rchar;
24
29ee3a15905b Fixed bugs of 'break;'.
viric@mandarina
parents: 23
diff changeset
   110
				break;
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   111
			case READ_VALUE:
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   112
				assert(nvalue <= MAX_STRING);
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   113
				value[nvalue++]=rchar;
24
29ee3a15905b Fixed bugs of 'break;'.
viric@mandarina
parents: 23
diff changeset
   114
				break;
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   115
			}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   116
		}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   117
	}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   118
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   119
	if (res == -1)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   120
	{
44
ddab92173045 Corrected some debug info.
viric@llimona
parents: 24
diff changeset
   121
		fprintf(stderr, "Error reading config file: %s\n",
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   122
				strerror(errno));
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   123
	}
23
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
   124
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
   125
	close(file);
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   126
}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   127
24
29ee3a15905b Fixed bugs of 'break;'.
viric@mandarina
parents: 23
diff changeset
   128
/* Retorna el tipus si ha pogut afegir l'element, -1 si hi ha hagut error */
29ee3a15905b Fixed bugs of 'break;'.
viric@mandarina
parents: 23
diff changeset
   129
static int add_config_element(const char * restrict p, const char * restrict v,
29ee3a15905b Fixed bugs of 'break;'.
viric@mandarina
parents: 23
diff changeset
   130
		const int max)
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   131
{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   132
	enum etype_config i, type;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   133
	int j, pos;
23
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
   134
	int my_max;
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
   135
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
   136
	my_max = max;
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
   137
	if (my_max > MAX_STRING)
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
   138
		my_max = MAX_STRING;
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   139
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   140
	/* busquem de quin paràmetre es tracta */
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   141
	for(i=0; i<ETYPE_END; i++)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   142
	{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   143
		/* a etype_string corresponen índex amb tipus */
23
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
   144
		
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
   145
		if (strncmp(p,etype_string[i].string, my_max) == 0)
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   146
		{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   147
			type=i;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   148
			break;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   149
		}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   150
	}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   151
	if (i==ETYPE_END)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   152
	{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   153
		/* Si el paràmetre no és conegut... */
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   154
		return -1;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   155
	}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   156
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   157
	/* Busquem si el paràmetre ja està a la configuració main_config */
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   158
	for (j=0; j<main_config.n; j++)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   159
	{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   160
		if(main_config.el[j].type == type)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   161
		{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   162
			pos = j;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   163
			break;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   164
		}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   165
	}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   166
	if(j == main_config.n)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   167
	{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   168
		pos = j;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   169
		main_config.n++;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   170
		/* At maximum, there should be ETYPE_END elements) */
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   171
		assert(main_config.n <= ETYPE_END);
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   172
	}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   173
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   174
	main_config.el[pos].type = type;
23
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
   175
	strncpy(main_config.el[pos].value, v, MAX_STRING);
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   176
24
29ee3a15905b Fixed bugs of 'break;'.
viric@mandarina
parents: 23
diff changeset
   177
	return type;
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   178
}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   179
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   180
/* Returns 2 on configured type, 1 on default value, 0 on disabled,
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   181
 * -1 on error */
23
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
   182
int get_config(const enum etype_config type, char * restrict v, const int max)
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   183
{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   184
	int j;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   185
	enum {
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   186
		DISABLED=0,
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   187
		DEFAULT=1,
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   188
		CONFIGURED=2,
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   189
		ERROR=-1
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   190
	} ret;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   191
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   192
	if (type < 0 || type >= ETYPE_END)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   193
		return ERROR;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   194
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   195
	for (j=0; j<main_config.n; j++)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   196
	{
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   197
		if(main_config.el[j].type == type)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   198
		{
23
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
   199
			strncpy(v, main_config.el[j].value, max);
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   200
			ret = CONFIGURED;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   201
			break;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   202
		}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   203
	}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   204
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   205
	if(j == main_config.n)
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   206
	{
23
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
   207
		strncpy(v, default_config[type].value, max);
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   208
		ret = DEFAULT;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   209
	}
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   210
23
3e2a39def0db Added use of strncmp, strncpy instead of the non-limited ones.
viric@mandarina
parents: 21
diff changeset
   211
	if(strncmp(v, "disabled", max) == 0)
21
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   212
		return DISABLED;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   213
	else
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   214
		return ret;
24714adbc325 Implemented the configuration file for starting the services.
viric@mandarina
parents:
diff changeset
   215
}