--- freem/src/iniconf.c 2025/03/09 15:20:18 1.2 +++ freem/src/iniconf.c 2025/04/02 19:59:38 1.4 @@ -1,24 +1,12 @@ /* - * * - * * * - * * * - * *************** - * * * * * - * * MUMPS * - * * * * * - * *************** - * * * - * * * - * * - * - * iniconf.c + * $Id: iniconf.c,v 1.4 2025/04/02 19:59:38 snw Exp $ * Function implementations for reading * FreeM configuration files * * * Author: Serena Willis * Copyright (C) 1998 MUG Deutschland - * Copyright (C) 2020 Coherent Logic Development LLC + * Copyright (C) 2020, 2025 Coherent Logic Development LLC * * * This file is part of FreeM. @@ -36,6 +24,16 @@ * You should have received a copy of the GNU Affero Public License * along with FreeM. If not, see . * + * $Log: iniconf.c,v $ + * Revision 1.4 2025/04/02 19:59:38 snw + * Automatically modify env.conf from fmadm reconfigure + * + * Revision 1.3 2025/03/09 19:14:25 snw + * First phase of REUSE compliance and header reformat + * + * + * SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC + * SPDX-License-Identifier: AGPL-3.0-or-later **/ #define FALSE 0 @@ -49,6 +47,7 @@ #include #include #include +#include #include "mpsdef.h" extern char config_file[4096]; @@ -71,9 +70,6 @@ extern char config_file[4096]; ini_keyvalue *ini_head; -/* prototypes for internal-use-only functions */ -int read_profile_string(char *file, char *section, char *key, char *value); - int get_conf(char *section, char *key, char *value) { char *etcfile; @@ -284,10 +280,102 @@ int file_exists(char *filename) void write_profile_string(char *file, char *section, char *key, char *value) { - ini_keyvalue *ini_head; + ini_keyvalue *ini_head; +} + +void parse_section_header(char *input, char *buf, size_t buflen) +{ + strncpy (buf, &(input[1]), buflen); + buf[strlen (buf) - 2] = '\0'; +} + +int modify_profile_string(char *file, char *section, char *key, char *value) +{ + FILE *input_fp; + FILE *output_fp; + char output_filename[4096]; + char input_line[255]; + char output_line[255]; + char current_section[255]; + char *savptr; + char *current_key; + char *current_value; + char tmpsi[255]; + char tmps[255]; + int changed = FALSE; + snprintf (output_filename, 4095, "%s.tmp", file); + + if ((input_fp = fopen (file, "r")) == NULL) { + return FALSE; + } + + if ((output_fp = fopen (output_filename, "w+")) == NULL) { + fclose (input_fp); + return FALSE; + } + + while (fgets (input_line, 255, input_fp)) { + if (input_line[0] == '[') { + /* this is a section; write it unchanged */ + fputs (input_line, output_fp); + + /* update the current section */ + parse_section_header (input_line, current_section, 255); + } + else { + /* is this a key/value pair? */ + if (strchr (input_line, '=') != NULL) { + /* are we in the requested section? */ + if (strcmp (section, current_section) == 0) { + /* yes; we'll parse it */ + strcpy (tmpsi, input_line); + savptr = tmpsi; + current_key = strtok_r (tmpsi, "=", &savptr); + current_value = strtok_r (NULL, "=", &savptr); + /* is this key the one we're changing? */ + if (strcmp (key, current_key) == 0) { + /* yes; modify it and write out*/ + snprintf (tmps, 255, "%s=%s\n", current_key, value); + + fputs (tmps, output_fp); + changed = TRUE; + } + else { + /* not the requested key; pass it through unmodified */ + fputs (input_line, output_fp); + } + } + else { + /* not the requested section; write it out unchanged */ + fputs (input_line, output_fp); + } + } + else { + /* not a key/value pair; write it out unchanged */ + fputs (input_line, output_fp); + } + } + } + + /* close both files */ + fclose (output_fp); + fclose (input_fp); + + /* delete the original file */ + unlink (file); + + /* rename the temporary file */ +#if !defined(__OS2__) + cp (file, output_filename); +#else + DosCopy (output_filename, file); +#endif + + return changed; } + ini_keyvalue *ini_insert(ini_section *s, char *section, char *key, char *value) { ini_section *t;