File: parse.c

package info (click to toggle)
apcd 0.6a.nr-4slink1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 376 kB
  • ctags: 143
  • sloc: ansic: 1,166; sh: 129; makefile: 60
file content (154 lines) | stat: -rw-r--r-- 4,000 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * parse.c - Parsing of the configuration file
 *
 * Copyright (c) 1995 Pavel Korensky
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 * 
 * IN NO EVENT SHALL PAVEL KORENSKY BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF PAVEL
 * KORENSKY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * PAVEL KORENSKY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND PAVEL KORENSKY HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */

/*	
 * Version:
 *	
 * $Id: parse.c,v 1.2 1995/11/07 12:40:46 root Exp root $
 *	
 *	
 * History:
 *	
 * $Log: parse.c,v $
 * Revision 1.2  1995/11/07  12:40:46  root
 * Version 0.5 Beta uploaded to the sunsite
 *
 * Revision 1.1  1995/11/01  15:24:07  root
 * Initial revision
 *
 *	
 */	


#include "apcd.h"
#include "version.h"

#ifndef lint
static char *version="$Id: parse.c,v 1.2 1995/11/07 12:40:46 root Exp root $";
#endif

int parse_config()
{
	FILE	*cf;
	char	*buffer;
	int     status=0;
		
	if((cf = fopen(CONFIGFILENAME,"r")) == NULL) {
		fprintf(stderr,"Cannot open configuration file %s\n",
			CONFIGFILENAME);
		perror(DAEMONID);
			return(1);
	}
	buffer=calloc(255,sizeof(char));
	while(fgets(buffer,100,cf)) {
		if(buffer[0]=='#') continue;
		if(strncmp("PORT",buffer,4) == 0) {
			sscanf(buffer,"%*s %s",use_port);
#ifdef DEBUGGING
			printf("PORT %s\n",use_port);
#endif
			status=2;		/* I must be master */
		}
		if(strncmp("TIMEOUT",buffer,7) == 0) {
			sscanf(buffer,"%*s %d",&power_timer);
#ifdef DEBUGGING
			printf("TIMEOUT %d\n",power_timer);
#endif
			status=2;             /* I must be master */
		}
		if(strncmp("NOTEST",buffer,6) == 0) {
#ifdef DEBUGGING
			printf("No UPS test at start.");
#endif
			notest = 1;
			status=2;             /* I must be master */
		}
		if(strncmp("LOGTIMEOUT",buffer,10) == 0) {
			sscanf(buffer,"%*s %d",&log_timer);
#ifdef DEBUGGING
			printf("LOGTIMEOUT %d\n",log_timer);
#endif
			status=2;             /* I must be master */
		}
		if(strncmp("MINBATTLOAD",buffer,11) == 0) {
			sscanf(buffer,"%*s %g",&min_batt_load);
#ifdef DEBUGGING
			printf("MINBATTLOAD %g\n",min_batt_load);
#endif
			status=2;              /* I must be master */
		}
		if(strncmp("LOGFILENAME",buffer,11) == 0) {
			sscanf(buffer,"%*s %s",logfilename);
#ifdef DEBUGGING
			printf("LOGFILENAME %s\n",logfilename);
#endif
			status=2;             /* I must be master */
		}
		if((strncmp("SLAVE",buffer,5) == 0)
		   && (num_slaves <= MAX_SLAVES)) {
			sscanf(buffer,"%*s %s",slaves[num_slaves++]);
#ifdef DEBUGGING
			printf("SLAVE %s   %d of %d\n",slaves[num_slaves-1],
			       num_slaves,MAX_SLAVES);
#endif
		}
		if(strncmp("MASTER",buffer,6) == 0) {
			if(status  != 0) {
				printf("I can't be both MASTER and SLAVE\n");
				return(1);
			}
			sscanf(buffer,"%*s %s",master_name);
#ifdef DEBUGGING
			printf("MASTER %s\n",master_name);
#endif
			status=1; 
		}
		
	}
	fclose(cf);

#ifdef DEBUGGING
	if(status==1) printf("I am slave\n");
	if(status==2) printf("I am master\n");
	if(status==0) printf("I am unknown, slave variable is 0 !!!\n");
#endif
	if (status==1) {
		slave=TRUE; master=FALSE;
	} else if (status==2) {
		slave=FALSE; master=TRUE;
	} else {
		fprintf(stderr,"%s: Error reading config file:\n",DAEMONID);
		fprintf(stderr,"%s: Can't decide whether I'm master or slave\n",
			DAEMONID);
		return(1);
	}
	return 0;
}
	
/*
Local Variables:
c-basic-offset: 8
tab-width: 8
End:
*/