Subversion Repositories mdb

Rev

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

/*
 * Copyright (C) 2015, 2016 by Andreas Theofilu <andreas@theosys.at>
 *
 * All rights reserved. No warranty, explicit or implicit, provided.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Andreas Theofilu and his suppliers, if any.
 * The intellectual and technical concepts contained
 * herein are proprietary to Andreas Theofilu and its suppliers and
 * may be covered by European and Foreign Patents, patents in process,
 * and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Andreas Theofilu.
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <syslog.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "config.h"
#include "helplib.h"

CONFIGURE configs;
/*
 * The following functions read a config file and put the
 * contents into a structure.
 */
void readConf(void)
{
        int fd;
        char confFile[512], line[512];
        char *home, *p;
        char hv0[64], hv1[128];
        
        home = getenv("HOME");
        fd = -1;
        
        if (!access("/etc/mdb.conf",R_OK))
                strcpy(confFile,"/etc/mdb.conf");
        else if (!access("/etc/mdb/mdb.conf",R_OK))
                strcpy(confFile,"/etc/mdb/mdb.conf");
        else if (!access("/usr/etc/mdb.conf",R_OK))
                strcpy(confFile,"/usr/etc/mdb.conf");
        else if (home)
        {
                strcpy(confFile,home);
                strcat(confFile,"/.mdb.conf");
                
                if (access(confFile,R_OK))
                {
                        syslog(LOG_WARNING,"Even config file %s was not found!", confFile);
                        confFile[0] = 0;
                }
        }
        else
                confFile[0] = 0;

        memset(&configs, 0, sizeof(configs));
        strcpy(configs.User,"nobody");
        strcpy(configs.Grp,"nobody");
        strcpy(configs.Pidfile, "/var/run/mdb.pid");
        strcpy(configs.home, "/var/lib/mdb");
        strcpy(configs.player, "/usr/lib/mpg321");
        configs.port = 11003;
        configs.debug = 0;
        
        if (!confFile[0] || (fd = open(confFile, O_RDONLY)) == -1)
        {
                if (confFile[0])
                        syslog(LOG_WARNING,"Error opening the config file %s! Using built in defaults. (%s)", confFile, strerror(errno));
                else
                        syslog(LOG_WARNING,"Error opening the config file! Using built in defaults.");

                return;
        }

        while (readLine(fd, &line[0], sizeof(line)) != NULL)
        {
                int len;

                trim (&line[0]);

                if (line[0] == '#' || !strlen(line))
                        continue;

                if ((p = strchr (line, '=')) == NULL)
                        continue;

                *p = 0;
                p++;
                len = strlen(line);

                if (len > (int)sizeof(hv0))
                        len = sizeof(hv0) - 1;

                strncpy (hv0, line, len);
                hv0[len] = 0;
                trim (hv0);
                len = strlen(p);

                if (len > (int)sizeof(hv1))
                        len = sizeof(hv0) - 1;

                strncpy (hv1, p, len);
                hv1[len] = 0;
                trim (hv1);

                if (!strcasecmp(hv0, "user"))
                {
                        syslog(LOG_INFO,"Found \"user\": %s", hv1);
                        strncpy (configs.User, hv1, sizeof(configs.User));
                }
                else if (!strcasecmp(hv0, "group"))
                {
                        syslog(LOG_INFO,"Found \"group\": %s", hv1);
                        strncpy (configs.Grp, hv1, sizeof(configs.Grp));
                }
                else if (!strcasecmp(hv0, "port"))
                {
                        syslog(LOG_INFO,"Found \"port\": %s", hv1);
                        configs.port = atoi (hv1);
                }
                else if (!strcasecmp(hv0, "Pidfile"))
                {
                        syslog(LOG_INFO,"Found \"Pidfile\": %s", hv1);
                        strncpy (configs.Pidfile, hv1, sizeof(configs.Pidfile));
                }
                else if (!strcasecmp(hv0, "Musicfile"))
                {
                        syslog(LOG_INFO,"Found \"Musicfile\": %s", hv1);
                        strncpy (configs.Pathfile, hv1, sizeof(configs.Pathfile));
                }
                else if (!strcasecmp(hv0, "Database"))
                {
                        syslog(LOG_INFO,"Found \"Database\": %s", hv1);
                        strncpy (configs.home, hv1, sizeof(configs.home));
                }
                else if (!strcasecmp(hv0, "OutPlayer"))
                {
                        syslog(LOG_INFO,"Found \"OutPlayer\": %s", hv1);
                        strncpy (configs.player, hv1, sizeof(configs.player));
                }
                else if (!strcasecmp(hv0, "Debug"))
                {
                        syslog(LOG_INFO,"Found \"Debug\": %s", hv1);

                        if (!strcasecmp(hv1, "1") || !strcasecmp(hv1, "true") || !strcasecmp(hv1, "yes"))
                        {
                                configs.debug = 1;
                                syslog(LOG_DEBUG, "Debugging is activated");
                        }
                }
        }

        if (!configs.debug)
                syslog(LOG_DEBUG, "Debugging is deactivated");

        close (fd);
}