Subversion Repositories mdb

Rev

Rev 28 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 andreas 1
/*
34 andreas 2
 * Copyright (C) 2015, 2016 by Andreas Theofilu <andreas@theosys.at>
2 andreas 3
 *
4
 * All rights reserved. No warranty, explicit or implicit, provided.
5
 *
6
 * NOTICE:  All information contained herein is, and remains
7
 * the property of Andreas Theofilu and his suppliers, if any.
8
 * The intellectual and technical concepts contained
9
 * herein are proprietary to Andreas Theofilu and its suppliers and
10
 * may be covered by European and Foreign Patents, patents in process,
11
 * and are protected by trade secret or copyright law.
12
 * Dissemination of this information or reproduction of this material
13
 * is strictly forbidden unless prior written permission is obtained
14
 * from Andreas Theofilu.
15
 */
16
 
17
#include <stdio.h>
18
#include <string.h>
19
#include <unistd.h>
20
#include <stdlib.h>
21
#include <syslog.h>
22
#include <errno.h>
23
#include <sys/stat.h>
24
#include <sys/types.h>
25
#include <fcntl.h>
26
#include "config.h"
27
#include "helplib.h"
28
 
29
CONFIGURE configs;
30
/*
31
 * The following functions read a config file and put the
32
 * contents into a structure.
33
 */
34
void readConf(void)
35
{
36
	int fd;
37
	char confFile[512], line[512];
38
	char *home, *p;
39
	char hv0[64], hv1[128];
40
 
41
	home = getenv("HOME");
42
	fd = -1;
43
 
44
	if (!access("/etc/mdb.conf",R_OK))
45
		strcpy(confFile,"/etc/mdb.conf");
46
	else if (!access("/etc/mdb/mdb.conf",R_OK))
47
		strcpy(confFile,"/etc/mdb/mdb.conf");
48
	else if (!access("/usr/etc/mdb.conf",R_OK))
49
		strcpy(confFile,"/usr/etc/mdb.conf");
50
	else if (home)
51
	{
52
		strcpy(confFile,home);
53
		strcat(confFile,"/.mdb.conf");
54
 
55
		if (access(confFile,R_OK))
56
		{
57
			syslog(LOG_WARNING,"Even config file %s was not found!", confFile);
58
			confFile[0] = 0;
59
		}
60
	}
61
	else
62
		confFile[0] = 0;
63
 
64
	memset(&configs, 0, sizeof(configs));
65
	strcpy(configs.User,"nobody");
66
	strcpy(configs.Grp,"nobody");
67
	strcpy(configs.Pidfile, "/var/run/mdb.pid");
5 andreas 68
	strcpy(configs.home, "/var/lib/mdb");
69
	strcpy(configs.player, "/usr/lib/mpg321");
2 andreas 70
	configs.port = 11003;
34 andreas 71
	configs.debug = 0;
2 andreas 72
 
73
	if (!confFile[0] || (fd = open(confFile, O_RDONLY)) == -1)
74
	{
75
		if (confFile[0])
76
			syslog(LOG_WARNING,"Error opening the config file %s! Using built in defaults. (%s)", confFile, strerror(errno));
77
		else
78
			syslog(LOG_WARNING,"Error opening the config file! Using built in defaults.");
79
 
80
		return;
81
	}
82
 
83
	while (readLine(fd, &line[0], sizeof(line)) != NULL)
84
	{
85
		int len;
86
 
87
		trim (&line[0]);
88
 
89
		if (line[0] == '#' || !strlen(line))
90
			continue;
91
 
92
		if ((p = strchr (line, '=')) == NULL)
93
			continue;
94
 
95
		*p = 0;
96
		p++;
97
		len = strlen(line);
98
 
4 andreas 99
		if (len > (int)sizeof(hv0))
2 andreas 100
			len = sizeof(hv0) - 1;
101
 
102
		strncpy (hv0, line, len);
103
		hv0[len] = 0;
104
		trim (hv0);
105
		len = strlen(p);
106
 
4 andreas 107
		if (len > (int)sizeof(hv1))
2 andreas 108
			len = sizeof(hv0) - 1;
109
 
110
		strncpy (hv1, p, len);
111
		hv1[len] = 0;
112
		trim (hv1);
113
 
114
		if (!strcasecmp(hv0, "user"))
115
		{
116
			syslog(LOG_INFO,"Found \"user\": %s", hv1);
117
			strncpy (configs.User, hv1, sizeof(configs.User));
118
		}
4 andreas 119
		else if (!strcasecmp(hv0, "group"))
2 andreas 120
		{
121
			syslog(LOG_INFO,"Found \"group\": %s", hv1);
122
			strncpy (configs.Grp, hv1, sizeof(configs.Grp));
123
		}
4 andreas 124
		else if (!strcasecmp(hv0, "port"))
2 andreas 125
		{
126
			syslog(LOG_INFO,"Found \"port\": %s", hv1);
127
			configs.port = atoi (hv1);
128
		}
4 andreas 129
		else if (!strcasecmp(hv0, "Pidfile"))
130
		{
131
			syslog(LOG_INFO,"Found \"Pidfile\": %s", hv1);
132
			strncpy (configs.Pidfile, hv1, sizeof(configs.Pidfile));
133
		}
134
		else if (!strcasecmp(hv0, "Musicfile"))
135
		{
136
			syslog(LOG_INFO,"Found \"Musicfile\": %s", hv1);
137
			strncpy (configs.Pathfile, hv1, sizeof(configs.Pathfile));
138
		}
5 andreas 139
		else if (!strcasecmp(hv0, "Database"))
140
		{
141
			syslog(LOG_INFO,"Found \"Database\": %s", hv1);
142
			strncpy (configs.home, hv1, sizeof(configs.home));
143
		}
28 andreas 144
		else if (!strcasecmp(hv0, "OutPlayer"))
5 andreas 145
		{
28 andreas 146
			syslog(LOG_INFO,"Found \"OutPlayer\": %s", hv1);
5 andreas 147
			strncpy (configs.player, hv1, sizeof(configs.player));
148
		}
34 andreas 149
		else if (!strcasecmp(hv0, "Debug"))
150
		{
151
			syslog(LOG_INFO,"Found \"Debug\": %s", hv1);
152
 
153
			if (!strcasecmp(hv1, "1") || !strcasecmp(hv1, "true") || !strcasecmp(hv1, "yes"))
154
			{
155
				configs.debug = 1;
156
				syslog(LOG_DEBUG, "Debugging is activated");
157
			}
158
		}
4 andreas 159
	}
2 andreas 160
 
34 andreas 161
	if (!configs.debug)
162
		syslog(LOG_DEBUG, "Debugging is deactivated");
163
 
4 andreas 164
	close (fd);
2 andreas 165
}