mcabber/src/parsecfg.c
changeset 169 0ed6099b5a54
parent 155 8a54d46e889a
child 170 ea5e101fd29e
equal deleted inserted replaced
168:6ad156673b19 169:0ed6099b5a54
     1 #include <stdio.h>
     1 #include <stdio.h>
     2 #include <stdlib.h>
     2 #include <stdlib.h>
     3 #include <unistd.h>
     3 #include <unistd.h>
     4 #include <ctype.h>
     4 #include <ctype.h>
     5 #include <string.h>
     5 #include <string.h>
       
     6 #include <glib.h>
     6 
     7 
     7 #include "list.h"
     8 #include "list.h"
     8 
     9 
     9 #define MAX_LENGHT_INPUT 1024
    10 #define MAX_LENGHT_INPUT 1024
    10 #define cfg_entry(n) list_entry(n, cfg_entry_t, list)
    11 #define cfg_entry(n) list_entry(n, cfg_entry_t, list)
    18 static LIST_HEAD(cfg_list);
    19 static LIST_HEAD(cfg_list);
    19 
    20 
    20 
    21 
    21 void push_in_list(char *key, char *value)
    22 void push_in_list(char *key, char *value)
    22 {
    23 {
    23   cfg_entry_t *new_entry = calloc(1, sizeof(cfg_entry_t));
    24   cfg_entry_t *new_entry = g_new0(1, sizeof(cfg_entry_t));
    24 
    25 
    25   new_entry->key = (char *) calloc(1, strlen(key) + 1);
    26   new_entry->key = (char *) g_new0(1, strlen(key) + 1);
    26   new_entry->value = (char *) calloc(1, strlen(value) + 1);
    27   new_entry->value = (char *) g_new0(1, strlen(value) + 1);
    27 
    28 
    28   strcpy(new_entry->key, key);
    29   strcpy(new_entry->key, key);
    29   strcpy(new_entry->value, value);
    30   strcpy(new_entry->value, value);
    30 
    31 
    31   list_add(&new_entry->list, &cfg_list);
    32   list_add(&new_entry->list, &cfg_list);
    36   FILE *fp;
    37   FILE *fp;
    37   char *buf;
    38   char *buf;
    38   char *line;
    39   char *line;
    39   char *value;
    40   char *value;
    40 
    41 
    41   buf = malloc(255);
    42   if (!filename) {
    42 
    43     // Use default config file locations
    43   if ((fp = fopen(filename, "r")) == NULL) {
    44     char *home = getenv("HOME");
       
    45     if (!home) {
       
    46       ut_WriteLog("Can't find home dir!\n");
       
    47       exit(EXIT_FAILURE);
       
    48     }
       
    49     filename = g_new(char, strlen(home)+24);
       
    50     sprintf(filename, "%s/.mcabber/mcabberrc", home);
       
    51     if ((fp = fopen(filename, "r")) == NULL) {
       
    52       // 2nd try...
       
    53       sprintf(filename, "%s/.mcabberrc", home);
       
    54       if ((fp = fopen(filename, "r")) == NULL) {
       
    55         fprintf(stderr, "Cannot open config file!\n");
       
    56         exit(EXIT_FAILURE);
       
    57       }
       
    58     }
       
    59     g_free(filename);
       
    60   }
       
    61   else if ((fp = fopen(filename, "r")) == NULL) {
    44     perror("fopen (parsecfg.c:46)");
    62     perror("fopen (parsecfg.c:46)");
    45     exit(EXIT_FAILURE);
    63     exit(EXIT_FAILURE);
    46   }
    64   }
       
    65 
       
    66   buf = g_new(255);
    47 
    67 
    48   while (fgets(buf, 255, fp) != NULL) {
    68   while (fgets(buf, 255, fp) != NULL) {
    49     line = buf;
    69     line = buf;
    50 
    70 
    51     while (isspace((int) *line))
    71     while (isspace((int) *line))
    73       push_in_list(line, value);
    93       push_in_list(line, value);
    74       continue;
    94       continue;
    75     }
    95     }
    76     fprintf(stderr, "CFG: orphaned line \"%s\"\n", line);
    96     fprintf(stderr, "CFG: orphaned line \"%s\"\n", line);
    77   }
    97   }
    78   free(buf);
    98   g_free(buf);
    79   return 1;
    99   return 1;
    80 }
   100 }
    81 
   101 
    82 char *cfg_read(char *key)
   102 char *cfg_read(char *key)
    83 {
   103 {