src/config.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 15:40:14 +0100
changeset 11 a769385a59c6
parent 10 311ea5fa60dd
child 13 d3554afaa768
permissions -rw-r--r--
Many Memory Leaks in config.c * strdup is safer Still reachable memory holes in irc.c * theLine is strdup(ed) and not released. committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 *  $Id: config.c 51 2008-01-10 00:19:39Z mbroeker $
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * $URL: http://localhost/svn/c/mcbot/trunk/src/config.c $
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 *
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
 */
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdio.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <stdlib.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <string.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#include <config.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
const
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
char *CONFIG_OPTIONS[] = {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
    "NICK", "PASSWORD", "SERVER", "PORT",
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    16
    "CHANNEL", "TOPIC", NULL
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
};
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
int config (UC * uc, char *fname)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
{
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
    FILE *f;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
    char buffer[513];
10
311ea5fa60dd Code cleanups and debian changelogs
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 6
diff changeset
    23
    char **line;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
    char *token;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    char *value;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
    int i = 0;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    if ((f = fopen (fname, "r")) == NULL)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
        return -1;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    while (CONFIG_OPTIONS[i] != NULL)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
        i++;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    34
    line = calloc ((size_t) (i + 1), sizeof (char *));
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
    /*
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
     * We can easily provide default values ...
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
     */
6
7eb12be31bb5 Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
    39
    uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
    uc->port = 6667;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    42
    while (!feof (f)) {
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    43
        *buffer = '\0';
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    44
        (void)fgets (buffer, 512, f);
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    45
        token = buffer;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    47
        while (*token == '\t')  /* Eat trailing tabs */
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
            token++;
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    49
        while (*token == ' ')   /* Eat trailing whitespaces */
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
            token++;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
        token = strtok (token, ":");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
        if (token != NULL) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
            value = strtok (NULL, "\n");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
            i = 0;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
            while (CONFIG_OPTIONS[i] != NULL) {
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    58
                if (!strcmp (CONFIG_OPTIONS[i], token)) {
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    59
                    if (value)
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    60
                        line[i] = strdup (value);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
                }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
                i++;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
            }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
        }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
    if (fclose (f) != 0)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
        return -1;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
    i = 0;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
    while (CONFIG_OPTIONS[i] != NULL) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
        if (line[i] != NULL)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
            switch (i) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
            case 0:            /* NICK */
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    75
                uc->nick = strdup (line[i]);
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    76
                free (line[i]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    77
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    78
            case 1:            /* PASSWORD */
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    79
                uc->pass = strdup (line[i]);
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    80
                free (line[i]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    81
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
            case 2:            /* SERVER */
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    83
                uc->server = strdup (line[i]);
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    84
                free (line[i]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    85
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    86
            case 3:            /* PORT */
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    87
                uc->port = atoi (line[i]);
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    88
                free (line[i]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    89
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    90
            case 4:            /* CHANNEL */
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    91
                uc->channel = strdup (line[i]);
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    92
                free (line[i]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    93
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    94
            case 5:            /* TOPIC */
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    95
                uc->topic = strdup (line[i]);
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    96
                free (line[i]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    97
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    98
            }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    99
        i++;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   100
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   101
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
   102
    if (line != NULL)
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
   103
        free (line);
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
   104
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   105
    if (!(uc->nick && uc->pass && uc->server && uc->channel))
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   106
        return -2;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   107
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   108
    return 0;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   109
}