63
63
struct stat file_stat;
64
64
char linebuff[BUFF_SIZE], *src_ptr, *dst_ptr, *file_buffer;
65
65
size_t opt_len= 0, optval_len= 0, sect_len;
66
uint32_t nr_newlines= 0, buffer_size;
66
uint nr_newlines= 0, buffer_size;
67
67
bool in_section= false, opt_applied= 0;
68
uint32_t reserve_extended;
68
uint reserve_extended;
70
70
int reserve_occupied= 0;
72
if (!(cnf_file= my_fopen(file_location, O_RDWR, MYF(0))))
72
if (!(cnf_file= my_fopen(file_location, O_RDWR | O_BINARY, MYF(0))))
75
75
if (fstat(fileno(cnf_file), &file_stat))
106
106
for (dst_ptr= file_buffer; fgets(linebuff, BUFF_SIZE, cnf_file); )
108
108
/* Skip over whitespaces */
109
for (src_ptr= linebuff; my_isspace(&my_charset_utf8_general_ci, *src_ptr);
109
for (src_ptr= linebuff; my_isspace(&my_charset_latin1, *src_ptr);
119
119
/* correct the option (if requested) */
120
120
if (option && in_section && !strncmp(src_ptr, option, opt_len) &&
121
121
(*(src_ptr + opt_len) == '=' ||
122
my_isspace(&my_charset_utf8_general_ci, *(src_ptr + opt_len)) ||
122
my_isspace(&my_charset_latin1, *(src_ptr + opt_len)) ||
123
123
*(src_ptr + opt_len) == '\0'))
125
125
char *old_src_ptr= src_ptr;
156
156
for (; nr_newlines; nr_newlines--)
157
dst_ptr= my_stpcpy(dst_ptr, NEWLINE);
157
dst_ptr= stpcpy(dst_ptr, NEWLINE);
159
159
/* Skip the section if MY_REMOVE_SECTION was given */
160
160
if (!in_section || remove_option != MY_REMOVE_SECTION)
161
dst_ptr= my_stpcpy(dst_ptr, linebuff);
161
dst_ptr= stpcpy(dst_ptr, linebuff);
163
163
/* Look for a section */
164
164
if (*src_ptr == '[')
198
198
/* New option still remains to apply at the end */
199
199
if (!remove_option && *(dst_ptr - 1) != '\n')
200
dst_ptr= my_stpcpy(dst_ptr, NEWLINE);
200
dst_ptr= stpcpy(dst_ptr, NEWLINE);
201
201
dst_ptr= add_option(dst_ptr, option_value, option, remove_option);
204
204
for (; nr_newlines; nr_newlines--)
205
dst_ptr= my_stpcpy(dst_ptr, NEWLINE);
205
dst_ptr= stpcpy(dst_ptr, NEWLINE);
209
209
/* Don't write the file if there are no changes to be made */
210
210
if (ftruncate(fileno(cnf_file), (my_off_t) (dst_ptr - file_buffer)) ||
211
211
my_fseek(cnf_file, 0, MY_SEEK_SET, MYF(0)) ||
212
my_fwrite(cnf_file, (unsigned char*) file_buffer, (size_t) (dst_ptr - file_buffer),
212
my_fwrite(cnf_file, (uchar*) file_buffer, (size_t) (dst_ptr - file_buffer),
216
216
if (my_fclose(cnf_file, MYF(MY_WME)))
219
my_free(file_buffer, MYF(0));
223
my_free(file_buffer, MYF(0));
225
225
my_fclose(cnf_file, MYF(0));
226
226
return(1); /* out of resources */
233
233
if (!remove_option)
235
dst= my_stpcpy(dst, option);
235
dst= stpcpy(dst, option);
236
236
if (*option_value)
239
dst= my_stpcpy(dst, option_value);
239
dst= stpcpy(dst, option_value);
241
241
/* add a newline */
242
dst= my_stpcpy(dst, NEWLINE);
242
dst= stpcpy(dst, NEWLINE);