Subversion Repositories mdb

Rev

Rev 4 | Go to most recent revision | Details | 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");
68
	strcpy(configs.home, home);			// In this directory the database is
69
	strcat(configs.home, "/.mdb");		// stored.
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
 
98
		if (len > sizeof(hv0))
99
			len = sizeof(hv0) - 1;
100
 
101
		strncpy (hv0, line, len);
102
		hv0[len] = 0;
103
		trim (hv0);
104
		len = strlen(p);
105
 
106
		if (len > sizeof(hv1))
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
		}
118
 
119
		if (!strcasecmp(hv0, "group"))
120
		{
121
			syslog(LOG_INFO,"Found \"group\": %s", hv1);
122
			strncpy (configs.Grp, hv1, sizeof(configs.Grp));
123
		}
124
 
125
		if (!strcasecmp(hv0, "port"))
126
		{
127
			syslog(LOG_INFO,"Found \"port\": %s", hv1);
128
			configs.port = atoi (hv1);
129
 
130
			if (!strcasecmp(hv0, "Pidfile"))
131
			{
132
				syslog(LOG_INFO,"Found \"Pidfile\": %s", hv1);
133
				strncpy (configs.Pidfile, hv1, sizeof(configs.Pidfile));
134
			}
135
 
136
			if (!strcasecmp(hv0, "Musicfile"))
137
			{
138
				syslog(LOG_INFO,"Found \"Musicfile\": %s", hv1);
139
				strncpy (configs.Pathfile, hv1, sizeof(configs.Pathfile));
140
			}
141
		}
142
 
143
		close (fd);
144
	}
145
}