src/config.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Mon, 22 Feb 2010 19:06:15 +0100
changeset 47 a689b6a8e6ed
parent 36 6889aacd038e
child 51 5d9aed3948a6
permissions -rw-r--r--
fgets may return a NULL pointer... fgets can return NULL and if it returns NULL, the application will be in an undefined state. 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
13
d3554afaa768 mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 11
diff changeset
    11
#include <compat.h>
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#include <config.h>
33
56571d34d754 safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 18
diff changeset
    13
#include "common.h"
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    15
enum config_map {
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    16
    NICK, PASSWORD, SERVER, PORT, CHANNEL, TOPIC,
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    17
};
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    18
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
const
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
char *CONFIG_OPTIONS[] = {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
    "NICK", "PASSWORD", "SERVER", "PORT",
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    22
    "CHANNEL", "TOPIC", NULL
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
};
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
int config (UC * uc, char *fname)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
{
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    FILE *f;
33
56571d34d754 safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 18
diff changeset
    28
    char buffer[DEFAULT_BUF_SIZE];
10
311ea5fa60dd Code cleanups and debian changelogs
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 6
diff changeset
    29
    char **line;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    char *token;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    char *value;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    32
    enum config_map map = 0;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    if ((f = fopen (fname, "r")) == NULL)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
        return -1;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    37
    while (CONFIG_OPTIONS[map] != NULL)
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    38
        map++;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    40
    line = calloc ((size_t) (map + 1), sizeof (char *));
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
    /*
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
     * We can easily provide default values ...
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
     */
6
7eb12be31bb5 Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
    45
    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
    46
    uc->port = 6667;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    48
    while (!feof (f)) {
47
a689b6a8e6ed fgets may return a NULL pointer...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 36
diff changeset
    49
        if (fgets (buffer, sizeof (buffer), f) == NULL)
a689b6a8e6ed fgets may return a NULL pointer...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 36
diff changeset
    50
            break;
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    51
        token = buffer;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    53
        while (*token == '\t')  /* Eat trailing tabs */
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
            token++;
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    55
        while (*token == ' ')   /* Eat trailing whitespaces */
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
            token++;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
        token = strtok (token, ":");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
        if (token != NULL) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
            value = strtok (NULL, "\n");
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    62
            map = 0;
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    63
            while (CONFIG_OPTIONS[map] != NULL) {
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    64
                if (!strcmp (CONFIG_OPTIONS[map], token)) {
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    65
                    if (value)
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    66
                        line[map] = compat_strdup (value);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
                }
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    68
                map++;
0
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
        }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
    if (fclose (f) != 0)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
        return -1;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    76
    map = 0;
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    77
    while (CONFIG_OPTIONS[map] != NULL) {
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    78
        if (line[map] != NULL)
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    79
            switch (map) {
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    80
            case NICK:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    81
                uc->nick = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    82
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    83
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    84
            case PASSWORD:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    85
                uc->pass = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    86
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    87
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    88
            case SERVER:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    89
                uc->server = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    90
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    91
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    92
            case PORT:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    93
                uc->port = atoi (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    94
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    95
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    96
            case CHANNEL:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    97
                uc->channel = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    98
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    99
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
   100
            case TOPIC:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
   101
                uc->topic = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
   102
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   103
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   104
            }
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
   105
        map++;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   106
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   107
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
   108
    if (line != NULL)
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
   109
        free (line);
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
   110
18
4435146391ae LOGIN without Password added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 13
diff changeset
   111
    if (!(uc->nick && uc->server && uc->channel))
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   112
        return -2;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   113
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   114
    return 0;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   115
}