--- a/src/config.c
+++ b/src/config.c
@@ -13,7 +13,7 @@
const
char *CONFIG_OPTIONS[] = {
"NICK", "PASSWORD", "SERVER", "PORT",
- "CHANNEL", "TOPIC",
+ "CHANNEL", "TOPIC", NULL
};
int config (UC * uc, char *fname)
@@ -31,7 +31,7 @@
while (CONFIG_OPTIONS[i] != NULL)
i++;
- line = calloc (i, sizeof (char *));
+ line = calloc ((size_t) (i + 1), sizeof (char *));
/*
* We can easily provide default values ...
@@ -39,22 +39,14 @@
uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL;
uc->port = 6667;
- *buffer = 0;
- fgets (buffer, 512, f);
-
- token = buffer;
+ while (!feof (f)) {
+ *buffer = '\0';
+ (void)fgets (buffer, 512, f);
+ token = buffer;
- while (!feof (f)) {
- /*
- * eat trailing tabs
- */
- while (*token == '\t')
+ while (*token == '\t') /* Eat trailing tabs */
token++;
-
- /*
- * eat trailing whitespaces
- */
- while (*token == ' ')
+ while (*token == ' ') /* Eat trailing whitespaces */
token++;
token = strtok (token, ":");
@@ -63,15 +55,13 @@
value = strtok (NULL, "\n");
i = 0;
while (CONFIG_OPTIONS[i] != NULL) {
- if (!strcmp (token, CONFIG_OPTIONS[i])) {
- line[i] = malloc (strlen (value) + 1);
- strncpy (line[i], value, strlen (value));
+ if (!strcmp (CONFIG_OPTIONS[i], token)) {
+ if (value)
+ line[i] = strdup (value);
}
i++;
}
}
- *buffer = 0;
- fgets (buffer, 512, f);
}
if (fclose (f) != 0)
@@ -82,27 +72,36 @@
if (line[i] != NULL)
switch (i) {
case 0: /* NICK */
- uc->nick = line[i];
+ uc->nick = strdup (line[i]);
+ free (line[i]);
break;
case 1: /* PASSWORD */
- uc->pass = line[i];
+ uc->pass = strdup (line[i]);
+ free (line[i]);
break;
case 2: /* SERVER */
- uc->server = line[i];
+ uc->server = strdup (line[i]);
+ free (line[i]);
break;
case 3: /* PORT */
uc->port = atoi (line[i]);
+ free (line[i]);
break;
case 4: /* CHANNEL */
- uc->channel = line[i];
+ uc->channel = strdup (line[i]);
+ free (line[i]);
break;
case 5: /* TOPIC */
- uc->topic = line[i];
+ uc->topic = strdup (line[i]);
+ free (line[i]);
break;
}
i++;
}
+ if (line != NULL)
+ free (line);
+
if (!(uc->nick && uc->pass && uc->server && uc->channel))
return -2;