Annotation of freem/src/iniconf.c, revision 1.1.1.1
1.1 snw 1: /*
2: * *
3: * * *
4: * * *
5: * ***************
6: * * * * *
7: * * MUMPS *
8: * * * * *
9: * ***************
10: * * *
11: * * *
12: * *
13: *
14: * iniconf.c
15: * Function implementations for reading
16: * FreeM configuration files
17: *
18: *
19: * Author: Serena Willis <jpw@coherent-logic.com>
20: * Copyright (C) 1998 MUG Deutschland
21: * Copyright (C) 2020 Coherent Logic Development LLC
22: *
23: *
24: * This file is part of FreeM.
25: *
26: * FreeM is free software: you can redistribute it and/or modify
27: * it under the terms of the GNU Affero Public License as published by
28: * the Free Software Foundation, either version 3 of the License, or
29: * (at your option) any later version.
30: *
31: * FreeM is distributed in the hope that it will be useful,
32: * but WITHOUT ANY WARRANTY; without even the implied warranty of
33: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34: * GNU Affero Public License for more details.
35: *
36: * You should have received a copy of the GNU Affero Public License
37: * along with FreeM. If not, see <https://www.gnu.org/licenses/>.
38: *
39: **/
40:
41: #define FALSE 0
42: #define TRUE 1
43: #include <stddef.h>
44: #include "iniconf.h"
45: #include "libfill.h"
46: #include <stdio.h>
47: #include <unistd.h>
48: #include <sys/types.h>
49: #include <pwd.h>
50: #include <string.h>
51: #include <limits.h>
52: #include "mpsdef.h"
53:
54: extern char config_file[4096];
55:
56: #if !defined(PATH_MAX) && defined(_SCO_DS)
57: # define PATH_MAX 4096
58: #endif
59:
60: #if !defined(PATH_MAX) && defined(__gnu_hurd__)
61: # define PATH_MAX 1024
62: #endif
63:
64: #if !defined(PATH_MAX) && defined(_AIX)
65: # define PATH_MAX 1024
66: #endif
67:
68: #if defined(__NetBSD__) || defined(__FreeBSD__)
69: # include <sys/syslimits.h>
70: #endif
71:
72: ini_keyvalue *ini_head;
73:
74: /* prototypes for internal-use-only functions */
75: int read_profile_string(char *file, char *section, char *key, char *value);
76:
77: int get_conf(char *section, char *key, char *value)
78: {
79: char *etcfile;
80: char *dotfile;
81: char *homedir;
82:
83: int exists_in_etc = FALSE;
84: int exists_in_dotfile = FALSE;
85: int dotexists;
86: int etcexists;
87:
88: char *etc_value;
89: char *dot_value;
90:
91: etc_value = (char *) malloc(CONF_BUFSIZE);
92: NULLPTRCHK(etc_value,"get_conf");
93: dot_value = (char *) malloc(CONF_BUFSIZE);
94: NULLPTRCHK(dot_value,"get_conf");
95:
96: etcfile = config_file;
97:
98:
99: #if !defined(__AMIGA) && !defined(_SCO_DS) && !defined(_AIX)
100: uid_t uid = geteuid();
101: struct passwd *pw = getpwuid(uid);
102:
103: if (pw == NULL) {
104: free (etc_value);
105: free (dot_value);
106:
107: return (FALSE);
108: }
109:
110: homedir = (char *) calloc(strlen(pw->pw_dir) + 1, sizeof(char));
111: NULLPTRCHK(homedir,"get_conf");
112:
113: (void) strcpy(homedir, pw->pw_dir);
114:
115: dotfile = calloc(PATH_MAX, sizeof(char));
116: NULLPTRCHK(dotfile,"get_conf");
117:
118: (void) strcat(dotfile, homedir);
119: (void) strcat(dotfile, "/.freemrc");
120:
121: etcexists = file_exists(etcfile);
122: dotexists = file_exists(dotfile);
123: #else
124:
125: #if defined(__AMIGA)
126: strcpy (etcfile, "./freem.conf");
127: etcexists = TRUE;
128: dotexists = FALSE;
129: #else
130: strcpy (etcfile, SYSCONFDIR"/freem.conf");
131:
132: etcexists = TRUE;
133: dotexists = FALSE;
134: #endif
135:
136: #endif
137:
138: if (etcexists == TRUE) {
139: exists_in_etc = read_profile_string(etcfile, section, key, etc_value);
140: }
141: else {
142: exists_in_etc = FALSE;
143: }
144:
145: if (dotexists == TRUE) {
146: exists_in_dotfile = read_profile_string(dotfile, section, key, dot_value);
147: }
148: else {
149: exists_in_dotfile = FALSE;
150: }
151:
152: if (exists_in_dotfile) {
153: strcpy (value, dot_value);
154:
155: free (etc_value);
156: free (dot_value);
157: #if !defined(__AMIGA) && !defined(_SCO_DS) && !defined(_AIX)
158: free (homedir);
159: free (dotfile);
160: #endif
161:
162: return (TRUE);
163: }
164:
165: if (exists_in_etc) {
166: strcpy(value, etc_value);
167:
168: free (etc_value);
169: free (dot_value);
170: #if !defined(__AMIGA) && !defined(_SCO_DS) && !defined(_AIX)
171: free (homedir);
172: free (dotfile);
173: #endif
174:
175: return (TRUE);
176: }
177:
178: free (etc_value);
179: free (dot_value);
180: #if !defined(__AMIGA) && !defined(_SCO_DS) && !defined(_AIX)
181: free (homedir);
182: free (dotfile);
183: #endif
184:
185: return (FALSE); /* didn't exist anywhere */
186: }
187:
188: int read_profile_string(char *file, char *section, char *key, char *value)
189: {
190:
191: register int i;
192:
193: FILE *fp;
194:
195: char *curkey;
196: char *curval;
197: char *fullsec;
198: char *cursec;
199: char *line;
200: int lnum = 0;
201:
202: fullsec = (char *) malloc(CONF_BUFSIZE);
203: NULLPTRCHK(fullsec,"read_profile_string");
204:
205: cursec = (char *) malloc(CONF_BUFSIZE);
206: NULLPTRCHK(cursec,"read_profile_string");
207:
208: line = (char *) malloc(CONF_BUFSIZE);
209: NULLPTRCHK(line,"read_profile_string");
210:
211:
212:
213:
214: snprintf(fullsec, CONF_BUFSIZE, "[%s]%c", section, '\0');
215:
216: strcpy(cursec, "[]");
217:
218:
219: fp = fopen(file, "r");
220:
221: while(fgets(line, CONF_BUFSIZE, fp) != NULL) {
222: ++lnum;
223:
224: if(line[0] == '[') {
225: strcpy(cursec, line);
226:
227: for(i = 0; i < CONF_BUFSIZE; i++) {
228: if(cursec[i] == ']') {
229: cursec[i + 1] = '\0';
230: break;
231: }
232: }
233: }
234: else {
235: if ((line[0] != '[') && (strchr(line, '=') != NULL)) {
236: curkey = strtok(line, "=");
237: curval = strtok(NULL, "=");
238: curval = strtok(curval, "\n");
239:
240:
241: if((strcmp(curkey, key) == 0) && (strcmp(cursec, fullsec) == 0)) {
242: strcpy(value, curval);
243: (void) fclose(fp);
244:
245: free (fullsec);
246: free (curkey);
247: free (cursec);
248:
249: return(TRUE);
250: }
251: }
252: }
253:
254: }
255:
256: if (fp != NULL) {
257: (void) fclose(fp);
258: }
259:
260: /* if we've gotten here, the section and/or key was not found */
261: sprintf (value, "\0");
262:
263: free (fullsec);
264: free (curkey);
265: free (cursec);
266:
267: return(FALSE);
268:
269: }
270:
271: int file_exists(char *filename)
272: {
273: FILE *fp;
274:
275: if ((fp = fopen(filename, "r")) != NULL) {
276: (void) fclose(fp);
277:
278: return(TRUE);
279: }
280: else {
281: return(FALSE);
282: }
283: }
284:
285: void write_profile_string(char *file, char *section, char *key, char *value)
286: {
287: ini_keyvalue *ini_head;
288:
289: }
290:
291: ini_keyvalue *ini_insert(ini_section *s, char *section, char *key, char *value)
292: {
293: ini_section *t;
294:
295: for (t = s; t != NULL; t = t->next) {
296:
297: if (strcmp (t->name, section) == 0) {
298:
299: /* this section already exists. update. */
300: return ini_kv_insert (s, key, value);
301:
302: }
303:
304: }
305:
306: /* section does not exist. insert. */
307: t = (ini_section *) malloc (sizeof (ini_section));
308: NULLPTRCHK(t,"ini_insert");
309:
310: t->name = (char *) malloc ((strlen (section) + 1) * sizeof (char));
311: NULLPTRCHK(t->name,"ini_insert");
312:
313: strcpy (t->name, section);
314:
315: t->next = s;
316: s = t;
317:
318: return ini_kv_insert (s, key, value);
319:
320: }
321:
322: ini_keyvalue *ini_kv_insert(ini_section *s, char *key, char *value)
323: {
324: ini_keyvalue *t;
325:
326: for (t = s->head; t != NULL; t = t->next) {
327:
328: if (strcmp (t->key, key) == 0) {
329:
330: /* this is an update */
331: free (t->value);
332: t->value = (char *) malloc ((strlen (value) + 1) * sizeof (char));
333: NULLPTRCHK(t->value,"ini_kv_insert");
334:
335: strcpy (t->value, value);
336:
337: return t;
338:
339: }
340:
341: }
342:
343: /* this is an insert */
344: t = (ini_keyvalue *) malloc (sizeof (ini_keyvalue));
345: NULLPTRCHK(t,"ini_kv_insert");
346:
347: t->key = (char *) malloc ((strlen (key) + 1) * sizeof (char));
348: NULLPTRCHK(t->key,"ini_kv_insert");
349:
350: t->value = (char *) malloc ((strlen (value) + 1) * sizeof (char));
351: NULLPTRCHK(t->value,"ini_kv_insert");
352:
353: strcpy (t->key, key);
354: strcpy (t->value, value);
355:
356: t->next = s->head;
357: s->head = t;
358:
359: return t;
360:
361: }
362:
363: void ini_section_delete(ini_section *head, char *name)
364: {
365: ini_section *t = head;
366: ini_section *p = NULL;
367:
368: if ((t != (ini_section *) NULL) && (strcmp (t->name, name) == 0)) {
369: head = t->next;
370:
371: free (t->name);
372: free (t);
373: return;
374: }
375:
376: while ((t != NULL) && (strcmp (t->name, name) != 0)) {
377: p = t;
378: t = t->next;
379: }
380:
381: if (t == NULL) return;
382:
383: free (t->name);
384: free (t);
385:
386: return;
387: }
388:
389: void ini_key_delete(ini_section *head, char *key)
390: {
391:
392: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>