|
|
| version 1.3, 2025/03/09 19:14:25 | version 1.8, 2025/04/10 15:31:25 |
|---|---|
| Line 25 | Line 25 |
| * along with FreeM. If not, see <https://www.gnu.org/licenses/>. | * along with FreeM. If not, see <https://www.gnu.org/licenses/>. |
| * | * |
| * $Log$ | * $Log$ |
| * Revision 1.8 2025/04/10 15:31:25 snw | |
| * Attempt to fix DosCopy calls for OS/2 | |
| * | |
| * Revision 1.7 2025/04/10 15:27:39 snw | |
| * Detect Devuan distribution and fix OS/2 problem with iniconf.c | |
| * | |
| * 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 | * Revision 1.3 2025/03/09 19:14:25 snw |
| * First phase of REUSE compliance and header reformat | * First phase of REUSE compliance and header reformat |
| * | * |
| Line 44 | Line 59 |
| #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" |
| #if defined(__OS2__) | |
| # include <os2.h> | |
| #endif | |
| extern char config_file[4096]; | extern char config_file[4096]; |
| #if !defined(PATH_MAX) && defined(_SCO_DS) | #if !defined(PATH_MAX) && defined(_SCO_DS) |
| Line 64 extern char config_file[4096]; | Line 84 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 */ | /* ini_keyvalue *ini_head; */ |
| int read_profile_string(char *file, char *section, char *key, char *value); | |
| int get_conf(char *section, char *key, char *value) | int get_conf(char *section, char *key, char *value) |
| { | { |
| Line 193 int read_profile_string(char *file, char | Line 215 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 277 int file_exists(char *filename) | Line 301 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; | |
| } | |
| ini_keyvalue *ini_insert(ini_section *s, char *section, char *key, char *value) | |
| { | |
| ini_section *t; | |
| for (t = s; t != NULL; t = t->next) { | |
| if (strcmp (t->name, section) == 0) { | |
| /* this section already exists. update. */ | |
| return ini_kv_insert (s, key, value); | |
| } | |
| } | |
| /* section does not exist. insert. */ | |
| t = (ini_section *) malloc (sizeof (ini_section)); | |
| NULLPTRCHK(t,"ini_insert"); | |
| t->name = (char *) malloc ((strlen (section) + 1) * sizeof (char)); | |
| NULLPTRCHK(t->name,"ini_insert"); | |
| strcpy (t->name, section); | |
| t->next = s; | |
| s = t; | |
| return ini_kv_insert (s, key, value); | |
| } | |
| ini_keyvalue *ini_kv_insert(ini_section *s, char *key, char *value) | |
| { | { |
| ini_keyvalue *t; | strncpy (buf, &(input[1]), buflen); |
| buf[strlen (buf) - 2] = '\0'; | |
| 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; | |
| } | |
| } | |
| /* this is an insert */ | |
| t = (ini_keyvalue *) malloc (sizeof (ini_keyvalue)); | |
| NULLPTRCHK(t,"ini_kv_insert"); | |
| t->key = (char *) malloc ((strlen (key) + 1) * sizeof (char)); | |
| NULLPTRCHK(t->key,"ini_kv_insert"); | |
| t->value = (char *) malloc ((strlen (value) + 1) * sizeof (char)); | |
| NULLPTRCHK(t->value,"ini_kv_insert"); | |
| strcpy (t->key, key); | |
| strcpy (t->value, value); | |
| t->next = s->head; | |
| s->head = t; | |
| return t; | |
| } | } |
| void ini_section_delete(ini_section *head, char *name) | int modify_profile_string(char *file, char *section, char *key, char *value) |
| { | { |
| ini_section *t = head; | FILE *input_fp; |
| ini_section *p = NULL; | FILE *output_fp; |
| char output_filename[4096]; | |
| if ((t != (ini_section *) NULL) && (strcmp (t->name, name) == 0)) { | char input_line[255]; |
| head = t->next; | char current_section[255]; |
| char *savptr; | |
| char *current_key; | |
| 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); | |
| free (t->name); | /* update the current section */ |
| free (t); | parse_section_header (input_line, current_section, 255); |
| return; | } |
| } | else { |
| /* is this a key/value pair? */ | |
| while ((t != NULL) && (strcmp (t->name, name) != 0)) { | if (strchr (input_line, '=') != NULL) { |
| p = t; | /* are we in the requested section? */ |
| t = t->next; | if (strcmp (section, current_section) == 0) { |
| /* yes; we'll parse it */ | |
| strcpy (tmpsi, input_line); | |
| savptr = tmpsi; | |
| current_key = strtok_r (tmpsi, "=", &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); | |
| } | |
| } | |
| } | } |
| 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, 1); | |
| #endif | |
| return changed; | |
| } | } |
| void ini_key_delete(ini_section *head, char *key) | |
| { | |
| } |