13
13
along with this program; if not, write to the Free Software
14
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "my_global.h"
17
16
#include "mysys_priv.h"
17
#include <mystrings/m_string.h>
19
18
#include <my_dir.h>
21
20
#define BUFF_SIZE 1024
64
63
struct stat file_stat;
65
64
char linebuff[BUFF_SIZE], *src_ptr, *dst_ptr, *file_buffer;
66
65
size_t opt_len= 0, optval_len= 0, sect_len;
67
uint nr_newlines= 0, buffer_size;
66
uint32_t nr_newlines= 0, buffer_size;
68
67
bool in_section= false, opt_applied= 0;
69
uint reserve_extended;
68
uint32_t reserve_extended;
71
70
int reserve_occupied= 0;
72
DBUG_ENTER("modify_defaults_file");
74
if (!(cnf_file= my_fopen(file_location, O_RDWR | O_BINARY, MYF(0))))
72
if (!(cnf_file= my_fopen(file_location, O_RDWR, MYF(0))))
77
75
if (fstat(fileno(cnf_file), &file_stat))
108
106
for (dst_ptr= file_buffer; fgets(linebuff, BUFF_SIZE, cnf_file); )
110
108
/* Skip over whitespaces */
111
for (src_ptr= linebuff; my_isspace(&my_charset_latin1, *src_ptr);
109
for (src_ptr= linebuff; my_isspace(&my_charset_utf8_general_ci, *src_ptr);
121
119
/* correct the option (if requested) */
122
120
if (option && in_section && !strncmp(src_ptr, option, opt_len) &&
123
121
(*(src_ptr + opt_len) == '=' ||
124
my_isspace(&my_charset_latin1, *(src_ptr + opt_len)) ||
122
my_isspace(&my_charset_utf8_general_ci, *(src_ptr + opt_len)) ||
125
123
*(src_ptr + opt_len) == '\0'))
127
125
char *old_src_ptr= src_ptr;
128
src_ptr= strend(src_ptr+ opt_len); /* Find the end of the line */
126
src_ptr= strchr(src_ptr+ opt_len, '\0'); /* Find the end of the line */
130
128
/* could be negative */
131
129
reserve_occupied+= (int) new_opt_len - (int) (src_ptr - old_src_ptr);
158
156
for (; nr_newlines; nr_newlines--)
159
dst_ptr= strmov(dst_ptr, NEWLINE);
157
dst_ptr= my_stpcpy(dst_ptr, NEWLINE);
161
159
/* Skip the section if MY_REMOVE_SECTION was given */
162
160
if (!in_section || remove_option != MY_REMOVE_SECTION)
163
dst_ptr= strmov(dst_ptr, linebuff);
161
dst_ptr= my_stpcpy(dst_ptr, linebuff);
165
163
/* Look for a section */
166
164
if (*src_ptr == '[')
200
198
/* New option still remains to apply at the end */
201
199
if (!remove_option && *(dst_ptr - 1) != '\n')
202
dst_ptr= strmov(dst_ptr, NEWLINE);
200
dst_ptr= my_stpcpy(dst_ptr, NEWLINE);
203
201
dst_ptr= add_option(dst_ptr, option_value, option, remove_option);
206
204
for (; nr_newlines; nr_newlines--)
207
dst_ptr= strmov(dst_ptr, NEWLINE);
205
dst_ptr= my_stpcpy(dst_ptr, NEWLINE);
211
209
/* Don't write the file if there are no changes to be made */
212
210
if (ftruncate(fileno(cnf_file), (my_off_t) (dst_ptr - file_buffer)) ||
213
211
my_fseek(cnf_file, 0, MY_SEEK_SET, MYF(0)) ||
214
my_fwrite(cnf_file, (uchar*) file_buffer, (size_t) (dst_ptr - file_buffer),
212
my_fwrite(cnf_file, (unsigned char*) file_buffer, (size_t) (dst_ptr - file_buffer),
218
216
if (my_fclose(cnf_file, MYF(MY_WME)))
221
my_free(file_buffer, MYF(0));
225
my_free(file_buffer, MYF(0));
227
225
my_fclose(cnf_file, MYF(0));
228
DBUG_RETURN(1); /* out of resources */
226
return(1); /* out of resources */
235
233
if (!remove_option)
237
dst= strmov(dst, option);
235
dst= my_stpcpy(dst, option);
238
236
if (*option_value)
241
dst= strmov(dst, option_value);
239
dst= my_stpcpy(dst, option_value);
243
241
/* add a newline */
244
dst= strmov(dst, NEWLINE);
242
dst= my_stpcpy(dst, NEWLINE);