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"
16
17
#include "mysys_priv.h"
17
#include <mystrings/m_string.h>
18
19
#include <my_dir.h>
20
21
#define BUFF_SIZE 1024
63
64
struct stat file_stat;
64
65
char linebuff[BUFF_SIZE], *src_ptr, *dst_ptr, *file_buffer;
65
66
size_t opt_len= 0, optval_len= 0, sect_len;
66
uint32_t nr_newlines= 0, buffer_size;
67
bool in_section= false, opt_applied= 0;
68
uint32_t reserve_extended;
67
uint nr_newlines= 0, buffer_size;
68
my_bool in_section= FALSE, opt_applied= 0;
69
uint reserve_extended;
70
71
int reserve_occupied= 0;
72
DBUG_ENTER("modify_defaults_file");
72
if (!(cnf_file= my_fopen(file_location, O_RDWR, MYF(0))))
74
if (!(cnf_file= my_fopen(file_location, O_RDWR | O_BINARY, MYF(0))))
75
77
if (fstat(fileno(cnf_file), &file_stat))
106
108
for (dst_ptr= file_buffer; fgets(linebuff, BUFF_SIZE, cnf_file); )
108
110
/* Skip over whitespaces */
109
for (src_ptr= linebuff; my_isspace(&my_charset_utf8_general_ci, *src_ptr);
111
for (src_ptr= linebuff; my_isspace(&my_charset_latin1, *src_ptr);
119
121
/* correct the option (if requested) */
120
122
if (option && in_section && !strncmp(src_ptr, option, opt_len) &&
121
123
(*(src_ptr + opt_len) == '=' ||
122
my_isspace(&my_charset_utf8_general_ci, *(src_ptr + opt_len)) ||
124
my_isspace(&my_charset_latin1, *(src_ptr + opt_len)) ||
123
125
*(src_ptr + opt_len) == '\0'))
125
127
char *old_src_ptr= src_ptr;
126
src_ptr= strchr(src_ptr+ opt_len, '\0'); /* Find the end of the line */
128
src_ptr= strend(src_ptr+ opt_len); /* Find the end of the line */
128
130
/* could be negative */
129
131
reserve_occupied+= (int) new_opt_len - (int) (src_ptr - old_src_ptr);
156
158
for (; nr_newlines; nr_newlines--)
157
dst_ptr= my_stpcpy(dst_ptr, NEWLINE);
159
dst_ptr= strmov(dst_ptr, NEWLINE);
159
161
/* Skip the section if MY_REMOVE_SECTION was given */
160
162
if (!in_section || remove_option != MY_REMOVE_SECTION)
161
dst_ptr= my_stpcpy(dst_ptr, linebuff);
163
dst_ptr= strmov(dst_ptr, linebuff);
163
165
/* Look for a section */
164
166
if (*src_ptr == '[')
169
171
src_ptr+= sect_len;
170
172
/* Skip over whitespaces. They are allowed after section name */
171
for (; my_isspace(&my_charset_utf8_general_ci, *src_ptr); src_ptr++)
173
for (; my_isspace(&my_charset_latin1, *src_ptr); src_ptr++)
174
176
if (*src_ptr != ']')
177
179
continue; /* Missing closing parenthesis. Assume this was no group */
180
182
if (remove_option == MY_REMOVE_SECTION)
181
183
dst_ptr= dst_ptr - strlen(linebuff);
186
in_section= false; /* mark that this section is of no interest to us */
188
in_section= FALSE; /* mark that this section is of no interest to us */
198
200
/* New option still remains to apply at the end */
199
201
if (!remove_option && *(dst_ptr - 1) != '\n')
200
dst_ptr= my_stpcpy(dst_ptr, NEWLINE);
202
dst_ptr= strmov(dst_ptr, NEWLINE);
201
203
dst_ptr= add_option(dst_ptr, option_value, option, remove_option);
204
206
for (; nr_newlines; nr_newlines--)
205
dst_ptr= my_stpcpy(dst_ptr, NEWLINE);
207
dst_ptr= strmov(dst_ptr, NEWLINE);
209
211
/* Don't write the file if there are no changes to be made */
210
212
if (ftruncate(fileno(cnf_file), (my_off_t) (dst_ptr - file_buffer)) ||
211
213
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),
214
my_fwrite(cnf_file, (uchar*) file_buffer, (size_t) (dst_ptr - file_buffer),
216
218
if (my_fclose(cnf_file, MYF(MY_WME)))
221
my_free(file_buffer, MYF(0));
225
my_free(file_buffer, MYF(0));
225
227
my_fclose(cnf_file, MYF(0));
226
return(1); /* out of resources */
228
DBUG_RETURN(1); /* out of resources */
233
235
if (!remove_option)
235
dst= my_stpcpy(dst, option);
237
dst= strmov(dst, option);
236
238
if (*option_value)
239
dst= my_stpcpy(dst, option_value);
241
dst= strmov(dst, option_value);
241
243
/* add a newline */
242
dst= my_stpcpy(dst, NEWLINE);
244
dst= strmov(dst, NEWLINE);