src/config.c
changeset 51 5d9aed3948a6
parent 47 a689b6a8e6ed
child 58 500a5ea7fcb8
equal deleted inserted replaced
50:412ac658eb97 51:5d9aed3948a6
    35         return -1;
    35         return -1;
    36 
    36 
    37     while (CONFIG_OPTIONS[map] != NULL)
    37     while (CONFIG_OPTIONS[map] != NULL)
    38         map++;
    38         map++;
    39 
    39 
    40     line = calloc ((size_t) (map + 1), sizeof (char *));
    40     if ((line = calloc ((size_t) (map + 1), sizeof (char *))) == NULL) {
       
    41         fclose (f);
       
    42         return -1;
       
    43     }
    41 
    44 
    42     /*
    45     /*
    43      * We can easily provide default values ...
    46      * We can easily provide default values ...
    44      */
    47      */
    45     uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL;
    48     uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL;
    48     while (!feof (f)) {
    51     while (!feof (f)) {
    49         if (fgets (buffer, sizeof (buffer), f) == NULL)
    52         if (fgets (buffer, sizeof (buffer), f) == NULL)
    50             break;
    53             break;
    51         token = buffer;
    54         token = buffer;
    52 
    55 
    53         while (*token == '\t')  /* Eat trailing tabs */
    56         while (*token == '\t' || *token == ' ') /* Eat trailing tabs and whitespaces */
    54             token++;
       
    55         while (*token == ' ')   /* Eat trailing whitespaces */
       
    56             token++;
    57             token++;
    57 
    58 
    58         token = strtok (token, ":");
    59         token = strtok (token, ":");
    59 
    60 
    60         if (token != NULL) {
    61         if (token != NULL) {
    61             value = strtok (NULL, "\n");
    62             value = strtok (NULL, "\n");
    62             map = 0;
    63             map = 0;
    63             while (CONFIG_OPTIONS[map] != NULL) {
    64             while (CONFIG_OPTIONS[map] != NULL) {
    64                 if (!strcmp (CONFIG_OPTIONS[map], token)) {
    65                 if (!strcmp (CONFIG_OPTIONS[map], token)) {
    65                     if (value)
    66                     if (value) {
       
    67                         if (line[map] != '\0')
       
    68                             free (line[map]);
    66                         line[map] = compat_strdup (value);
    69                         line[map] = compat_strdup (value);
       
    70                     }
    67                 }
    71                 }
    68                 map++;
    72                 map++;
    69             }
    73             }
    70         }
    74         }
    71     }
    75     }
    72 
    76 
    73     if (fclose (f) != 0)
       
    74         return -1;
       
    75 
       
    76     map = 0;
    77     map = 0;
    77     while (CONFIG_OPTIONS[map] != NULL) {
    78     while (CONFIG_OPTIONS[map] != NULL) {
    78         if (line[map] != NULL)
    79         if (line[map] != '\0')
    79             switch (map) {
    80             switch (map) {
    80             case NICK:
    81             case NICK:
    81                 uc->nick = compat_strdup (line[map]);
    82                 uc->nick = compat_strdup (line[map]);
    82                 free (line[map]);
    83                 free (line[map]);
    83                 break;
    84                 break;
   103                 break;
   104                 break;
   104             }
   105             }
   105         map++;
   106         map++;
   106     }
   107     }
   107 
   108 
   108     if (line != NULL)
   109     free (line);
   109         free (line);
   110 
       
   111     if (fclose (f) != 0)
       
   112         return -1;
   110 
   113 
   111     if (!(uc->nick && uc->server && uc->channel))
   114     if (!(uc->nick && uc->server && uc->channel))
   112         return -2;
   115         return -2;
   113 
   116 
   114     return 0;
   117     return 0;