Subversion Repositories mdb

Rev

Rev 5 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 andreas 1
/*
2
 * Copyright (C) 2015 by Andreas Theofilu <andreas@theosys.at>
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;
71
 
72
	if (!confFile[0] || (fd = open(confFile, O_RDONLY)) == -1)
73
	{
74
		if (confFile[0])
75
			syslog(LOG_WARNING,"Error opening the config file %s! Using built in defaults. (%s)", confFile, strerror(errno));
76
		else
77
			syslog(LOG_WARNING,"Error opening the config file! Using built in defaults.");
78
 
79
		return;
80
	}
81
 
82
	while (readLine(fd, &line[0], sizeof(line)) != NULL)
83
	{
84
		int len;
85
 
86
		trim (&line[0]);
87
 
88
		if (line[0] == '#' || !strlen(line))
89
			continue;
90
 
91
		if ((p = strchr (line, '=')) == NULL)
92
			continue;
93
 
94
		*p = 0;
95
		p++;
96
		len = strlen(line);
97
 
4 andreas 98
		if (len > (int)sizeof(hv0))
2 andreas 99
			len = sizeof(hv0) - 1;
100
 
101
		strncpy (hv0, line, len);
102
		hv0[len] = 0;
103
		trim (hv0);
104
		len = strlen(p);
105
 
4 andreas 106
		if (len > (int)sizeof(hv1))
2 andreas 107
			len = sizeof(hv0) - 1;
108
 
109
		strncpy (hv1, p, len);
110
		hv1[len] = 0;
111
		trim (hv1);
112
 
113
		if (!strcasecmp(hv0, "user"))
114
		{
115
			syslog(LOG_INFO,"Found \"user\": %s", hv1);
116
			strncpy (configs.User, hv1, sizeof(configs.User));
117
		}
4 andreas 118
		else if (!strcasecmp(hv0, "group"))
2 andreas 119
		{
120
			syslog(LOG_INFO,"Found \"group\": %s", hv1);
121
			strncpy (configs.Grp, hv1, sizeof(configs.Grp));
122
		}
4 andreas 123
		else if (!strcasecmp(hv0, "port"))
2 andreas 124
		{
125
			syslog(LOG_INFO,"Found \"port\": %s", hv1);
126
			configs.port = atoi (hv1);
127
		}
4 andreas 128
		else if (!strcasecmp(hv0, "Pidfile"))
129
		{
130
			syslog(LOG_INFO,"Found \"Pidfile\": %s", hv1);
131
			strncpy (configs.Pidfile, hv1, sizeof(configs.Pidfile));
132
		}
133
		else if (!strcasecmp(hv0, "Musicfile"))
134
		{
135
			syslog(LOG_INFO,"Found \"Musicfile\": %s", hv1);
136
			strncpy (configs.Pathfile, hv1, sizeof(configs.Pathfile));
137
		}
5 andreas 138
		else if (!strcasecmp(hv0, "Database"))
139
		{
140
			syslog(LOG_INFO,"Found \"Database\": %s", hv1);
141
			strncpy (configs.home, hv1, sizeof(configs.home));
142
		}
28 andreas 143
		else if (!strcasecmp(hv0, "OutPlayer"))
5 andreas 144
		{
28 andreas 145
			syslog(LOG_INFO,"Found \"OutPlayer\": %s", hv1);
5 andreas 146
			strncpy (configs.player, hv1, sizeof(configs.player));
147
		}
4 andreas 148
	}
2 andreas 149
 
4 andreas 150
	close (fd);
2 andreas 151
}