src/config.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Tue, 16 Feb 2010 23:38:02 +0100
changeset 45 535478a0f482
parent 36 6889aacd038e
child 47 a689b6a8e6ed
permissions -rw-r--r--
Prevent further segmentation faults in parse.c Some critical parts in parse.c depend on parseable data in message->user and message->email. These values will never be NULL and will never again result in a segmentation fault. committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 *  $Id: config.c 51 2008-01-10 00:19:39Z mbroeker $
 * $URL: http://localhost/svn/c/mcbot/trunk/src/config.c $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <compat.h>
#include <config.h>
#include "common.h"

enum config_map {
    NICK, PASSWORD, SERVER, PORT, CHANNEL, TOPIC,
};

const
char *CONFIG_OPTIONS[] = {
    "NICK", "PASSWORD", "SERVER", "PORT",
    "CHANNEL", "TOPIC", NULL
};

int config (UC * uc, char *fname)
{
    FILE *f;
    char buffer[DEFAULT_BUF_SIZE];
    char **line;
    char *token;
    char *value;
    enum config_map map = 0;

    if ((f = fopen (fname, "r")) == NULL)
        return -1;

    while (CONFIG_OPTIONS[map] != NULL)
        map++;

    line = calloc ((size_t) (map + 1), sizeof (char *));

    /*
     * We can easily provide default values ...
     */
    uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL;
    uc->port = 6667;

    while (!feof (f)) {
        (void)fgets (buffer, sizeof (buffer), f);
        token = buffer;

        while (*token == '\t')  /* Eat trailing tabs */
            token++;
        while (*token == ' ')   /* Eat trailing whitespaces */
            token++;

        token = strtok (token, ":");

        if (token != NULL) {
            value = strtok (NULL, "\n");
            map = 0;
            while (CONFIG_OPTIONS[map] != NULL) {
                if (!strcmp (CONFIG_OPTIONS[map], token)) {
                    if (value)
                        line[map] = compat_strdup (value);
                }
                map++;
            }
        }
    }

    if (fclose (f) != 0)
        return -1;

    map = 0;
    while (CONFIG_OPTIONS[map] != NULL) {
        if (line[map] != NULL)
            switch (map) {
            case NICK:
                uc->nick = compat_strdup (line[map]);
                free (line[map]);
                break;
            case PASSWORD:
                uc->pass = compat_strdup (line[map]);
                free (line[map]);
                break;
            case SERVER:
                uc->server = compat_strdup (line[map]);
                free (line[map]);
                break;
            case PORT:
                uc->port = atoi (line[map]);
                free (line[map]);
                break;
            case CHANNEL:
                uc->channel = compat_strdup (line[map]);
                free (line[map]);
                break;
            case TOPIC:
                uc->topic = compat_strdup (line[map]);
                free (line[map]);
                break;
            }
        map++;
    }

    if (line != NULL)
        free (line);

    if (!(uc->nick && uc->server && uc->channel))
        return -2;

    return 0;
}