src/config.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 03 Jan 2009 21:41:51 +0100
changeset 33 56571d34d754
parent 18 4435146391ae
child 36 6889aacd038e
permissions -rw-r--r--
safe buffers, a memory leak and cleanups sizeof(buffer) is always the right choice for a static buffer... The error handler must appear before the printf Useful make targets like deb and debclean Trivial changes like removing trailing white spaces 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"

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;
    int i = 0;

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

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

    line = calloc ((size_t) (i + 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");
            i = 0;
            while (CONFIG_OPTIONS[i] != NULL) {
                if (!strcmp (CONFIG_OPTIONS[i], token)) {
                    if (value)
                        line[i] = compat_strdup (value);
                }
                i++;
            }
        }
    }

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

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

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

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

    return 0;
}