Diff for /freem/src/iniconf.c between versions 1.1.1.1 and 1.6

version 1.1.1.1, 2025/01/19 02:04:04 version 1.6, 2025/04/10 01:24:38
Line 1 Line 1
 /*  /*
  *                            *   *   $Id$
  *                           * *  
  *                          *   *  
  *                     ***************  
  *                      * *       * *  
  *                       *  MUMPS  *  
  *                      * *       * *  
  *                     ***************  
  *                          *   *  
  *                           * *  
  *                            *  
  *  
  *   iniconf.c  
  *    Function implementations for reading   *    Function implementations for reading
  *    FreeM configuration files   *    FreeM configuration files
  *   *
  *     *  
  *   Author: Serena Willis <jpw@coherent-logic.com>   *   Author: Serena Willis <snw@coherent-logic.com>
  *    Copyright (C) 1998 MUG Deutschland   *    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.   *   This file is part of FreeM.
Line 36 Line 24
  *   You should have received a copy of the GNU Affero Public License   *   You should have received a copy of the GNU Affero Public License
  *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.   *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
  *   *
    *   $Log$
    *   Revision 1.6  2025/04/10 01:24:38  snw
    *   Remove C++ style comments
    *
    *   Revision 1.5  2025/04/09 19:52:02  snw
    *   Eliminate as many warnings as possible while building with -Wall
    *
    *   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  #define FALSE 0
Line 49 Line 53
 #include <pwd.h>  #include <pwd.h>
 #include <string.h>  #include <string.h>
 #include <limits.h>  #include <limits.h>
   #include <fs.h>
 #include "mpsdef.h"  #include "mpsdef.h"
   
 extern char config_file[4096];  extern char config_file[4096];
Line 69  extern char config_file[4096]; Line 74  extern char config_file[4096];
 # include <sys/syslimits.h>  # include <sys/syslimits.h>
 #endif  #endif
   
 ini_keyvalue *ini_head;  #if !defined(PATH_MAX)
   # define PATH_MAX 4096
   #endif
   
 /* prototypes for internal-use-only functions */  
 int read_profile_string(char *file, char *section, char *key, char *value);  /* ini_keyvalue *ini_head; */
   
 int get_conf(char *section, char *key, char *value)  int get_conf(char *section, char *key, char *value)
 {  {
Line 198  int read_profile_string(char *file, char Line 205  int read_profile_string(char *file, char
     char *cursec;      char *cursec;
     char *line;      char *line;
     int lnum = 0;      int lnum = 0;
   
       curkey = (char *) NULL;
           
     fullsec = (char *) malloc(CONF_BUFSIZE);      fullsec = (char *) malloc(CONF_BUFSIZE);
     NULLPTRCHK(fullsec,"read_profile_string");      NULLPTRCHK(fullsec,"read_profile_string");
Line 282  int file_exists(char *filename) Line 291  int file_exists(char *filename)
     }       } 
 }  }
   
 void write_profile_string(char *file, char *section, char *key, char *value)  void parse_section_header(char *input, char *buf, size_t buflen)
 {  {
     ini_keyvalue *ini_head;      strncpy (buf, &(input[1]), buflen);
           buf[strlen (buf) - 2] = '\0';
 }  }
   
 ini_keyvalue *ini_insert(ini_section *s, char *section, char *key, char *value)  int modify_profile_string(char *file, char *section, char *key, char *value)
 {  {
     ini_section *t;      FILE *input_fp;
           FILE *output_fp;
     for (t = s; t != NULL; t = t->next) {      char output_filename[4096];
       char input_line[255];    
         if (strcmp (t->name, section) == 0) {      char current_section[255];
       char *savptr;
             /* this section already exists. update. */      char *current_key;
             return ini_kv_insert (s, key, value);      char tmpsi[255];
                   char tmps[255];
         }      int changed = FALSE;
       
     }      snprintf (output_filename, 4095, "%s.tmp", file);
   
     /* section does not exist. insert. */      if ((input_fp = fopen (file, "r")) == NULL) {
     t = (ini_section *) malloc (sizeof (ini_section));          return FALSE;
     NULLPTRCHK(t,"ini_insert");      }
       
     t->name = (char *) malloc ((strlen (section) + 1) * sizeof (char));      if ((output_fp = fopen (output_filename, "w+")) == NULL) {
     NULLPTRCHK(t->name,"ini_insert");          fclose (input_fp);
               return FALSE;
     strcpy (t->name, section);      }
   
     t->next = s;      while (fgets (input_line, 255, input_fp)) {
     s = t;          if (input_line[0] == '[') {            
               /* this is a section; write it unchanged */
     return ini_kv_insert (s, key, value);              fputs (input_line, output_fp);
   
 }  
   
 ini_keyvalue *ini_kv_insert(ini_section *s, char *key, char *value)  
 {  
     ini_keyvalue *t;  
   
     for (t = s->head; t != NULL; t = t->next) {  
   
         if (strcmp (t->key, key) == 0) {  
   
             /* this is an update */  
             free (t->value);  
             t->value = (char *) malloc ((strlen (value) + 1) * sizeof (char));  
             NULLPTRCHK(t->value,"ini_kv_insert");  
               
             strcpy (t->value, value);  
   
             return t;  
   
         }              /* update the current section */
               parse_section_header (input_line, current_section, 255);
     }          }
           else {
     /* this is an insert */              /* is this a key/value pair? */
     t = (ini_keyvalue *) malloc (sizeof (ini_keyvalue));              if (strchr (input_line, '=') != NULL) {
     NULLPTRCHK(t,"ini_kv_insert");                  /* are we in the requested section? */
                       if (strcmp (section, current_section) == 0) {
     t->key = (char *) malloc ((strlen (key) + 1) * sizeof (char));                      /* yes; we'll parse it */
     NULLPTRCHK(t->key,"ini_kv_insert");                      strcpy (tmpsi, input_line);
                           savptr = tmpsi;
     t->value = (char *) malloc ((strlen (value) + 1) * sizeof (char));                      current_key = strtok_r (tmpsi, "=", &savptr);
     NULLPTRCHK(t->value,"ini_kv_insert");                      /* is this key the one we're changing? */
                           if (strcmp (key, current_key) == 0) {
     strcpy (t->key, key);                          /* yes; modify it and write out*/
     strcpy (t->value, value);                          snprintf (tmps, 255, "%s=%s\n", current_key, value);
   
     t->next = s->head;                          fputs (tmps, output_fp);
     s->head = t;                          changed = TRUE;                        
                       }
     return t;                      else {
                               /* not the requested key; pass it through unmodified */
 }                          fputs (input_line, output_fp);
                       }                    
 void ini_section_delete(ini_section *head, char *name)                  }
 {                  else {
     ini_section *t = head;                      /* not the requested section; write it out unchanged */
     ini_section *p = NULL;                      fputs (input_line, output_fp);
                   }
     if ((t != (ini_section *) NULL) && (strcmp (t->name, name) == 0)) {              }
         head = t->next;              else {
                   /* not a key/value pair; write it out unchanged */
         free (t->name);                  fputs (input_line, output_fp);
         free (t);              }
         return;          }                    
     }  
   
     while ((t != NULL) && (strcmp (t->name, name) != 0)) {  
         p = t;  
         t = t->next;  
     }      }
      
     if (t == NULL) return;      /* close both files */
           fclose (output_fp);
     free (t->name);      fclose (input_fp);
     free (t);  
       /* delete the original file */
     return;           unlink (file);
   
       /* rename the temporary file */
   #if !defined(__OS2__)
       cp (file, output_filename);
   #else
       DosCopy (output_filename, file);
   #endif    
                          
       return changed;
 }  }
   
 void ini_key_delete(ini_section *head, char *key)  
 {  
   
 }  

Removed from v.1.1.1.1  
changed lines
  Added in v.1.6


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>