Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / apps / im / src / config.c
blob55e455331f42ff41c9e2c0c5de404644c1b03c85
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "platform.h"
21 #include "config.h"
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <stdlib.h>
29 config_t *cfg;
31 void config_set (char *variable, char *value)
33 printf ("config -> %s : %s\n", variable, value);
35 if (!strcmp (variable, "nick"))
36 cfg->nick = strdup (value);
37 else if (!strcmp (variable, "password"))
38 cfg->password = strdup (value);
39 else if (!strcmp (variable, "server"))
40 cfg->server = strdup (value);
41 else if (!strcmp (variable, "port"))
42 cfg->port = atoi (value);
45 void config_parse (char *cfgfile, unsigned cfglen)
47 unsigned i = 0, y = cfglen, z = 0;
48 unsigned a = 0;
50 while (i < y) {
51 if (cfgfile[i] == '\n') {
52 char var[16];
53 char val[32];
55 a = 0;
57 while (a < 16) {
58 if (cfgfile[a+z] == ' ')
59 break;
61 a ++;
64 if (a >= 15)
65 continue;
67 memcpy (var, cfgfile+z, a);
68 var[a] = '\0';
70 a ++;
72 memcpy (val, cfgfile+z+a, i-z-a);
73 val[i-z-a] = '\0';
75 config_set (var, val);
77 i ++;
79 z = i;
82 i ++;
86 int config_load (char *file)
88 char *cfgfile = (char *) malloc (sizeof (char) * DEFAULT_CONFIG_MAXLENGTH);
90 if (!cfgfile)
91 return 0;
93 // html web page
94 int fd = open (file, O_RDONLY);
96 if (!fd) {
97 printf ("error -> file 'config' not found\n");
98 return 0;
101 int cfglen = read (fd, cfgfile, DEFAULT_CONFIG_MAXLENGTH-1);
103 if (!cfglen) {
104 printf ("error -> something was wrong !\n");
105 return 0;
108 cfgfile[DEFAULT_CONFIG_MAXLENGTH-1] = '\0';
110 cfg = (config_t *) malloc (sizeof (config_t));
112 if (!cfg)
113 return 0;
115 config_parse (cfgfile, (unsigned) cfglen);
117 free (cfgfile);
119 close (fd);
121 return 1;
124 /** INIT CONFIG function */
125 int init_config ()
127 if (!config_load (DEFAULT_CONFIG_FILE))
128 return 0;
130 if (!strcmp (cfg->nick, "#") || !strcmp (cfg->password, "#")) {
131 printf ("WARNING -> Please re-write nick and password variable in 'config' file for autologin\nNOTE -> For register new account use command: /r <nick> <password>\nNOTE -> For more information use command /h for help\n");
133 cfg->autolog = 0;
134 } else
135 cfg->autolog = 1;
137 return 1;