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 "mysys_priv.h"
16
#include "mysys/mysys_priv.h"
17
17
#include <mystrings/m_string.h>
18
#include <mysys/my_dir.h>
20
25
#define BUFF_SIZE 1024
21
26
#define RESERVE 1024 /* Extend buffer with this extent */
63
68
struct stat file_stat;
64
69
char linebuff[BUFF_SIZE], *src_ptr, *dst_ptr, *file_buffer;
65
70
size_t opt_len= 0, optval_len= 0, sect_len;
66
uint nr_newlines= 0, buffer_size;
71
uint32_t nr_newlines= 0;
67
73
bool in_section= false, opt_applied= 0;
68
uint reserve_extended;
74
size_t reserve_extended;
70
76
int reserve_occupied= 0;
72
if (!(cnf_file= my_fopen(file_location, O_RDWR | O_BINARY, MYF(0))))
78
if (!(cnf_file= fopen(file_location, "r+")))
75
81
if (fstat(fileno(cnf_file), &file_stat))
90
96
NEWLINE_LEN + /* Space for newline */
91
97
RESERVE); /* Some additional space */
93
buffer_size= (file_stat.st_size +
94
1); /* The ending zero */
99
buffer_size= (size_t)max((uint64_t)file_stat.st_size + 1, (uint64_t)SIZE_MAX);
97
102
Reserve space to read the contents of the file and some more
98
103
for the option we want to add.
100
if (!(file_buffer= (char*) my_malloc(buffer_size + reserve_extended,
105
if (!(file_buffer= (char*) malloc(max(buffer_size + reserve_extended,
104
109
sect_len= strlen(section_name);
106
111
for (dst_ptr= file_buffer; fgets(linebuff, BUFF_SIZE, cnf_file); )
108
113
/* Skip over whitespaces */
109
for (src_ptr= linebuff; my_isspace(&my_charset_latin1, *src_ptr);
114
for (src_ptr= linebuff; my_isspace(&my_charset_utf8_general_ci, *src_ptr);
119
124
/* correct the option (if requested) */
120
125
if (option && in_section && !strncmp(src_ptr, option, opt_len) &&
121
126
(*(src_ptr + opt_len) == '=' ||
122
my_isspace(&my_charset_latin1, *(src_ptr + opt_len)) ||
127
my_isspace(&my_charset_utf8_general_ci, *(src_ptr + opt_len)) ||
123
128
*(src_ptr + opt_len) == '\0'))
125
130
char *old_src_ptr= src_ptr;
126
src_ptr= strend(src_ptr+ opt_len); /* Find the end of the line */
131
src_ptr= strchr(src_ptr+ opt_len, '\0'); /* Find the end of the line */
128
133
/* could be negative */
129
134
reserve_occupied+= (int) new_opt_len - (int) (src_ptr - old_src_ptr);
130
135
if (reserve_occupied >= (int) reserve_extended)
132
reserve_extended= (uint) reserve_occupied + RESERVE;
133
if (!(file_buffer= (char*) my_realloc(file_buffer, buffer_size +
135
MYF(MY_WME|MY_FREE_ON_ERROR))))
137
reserve_extended= (uint32_t) reserve_occupied + RESERVE;
138
if (!(file_buffer= (char*) realloc(file_buffer, buffer_size +
156
160
for (; nr_newlines; nr_newlines--)
157
dst_ptr= stpcpy(dst_ptr, NEWLINE);
161
dst_ptr= strcpy(dst_ptr, NEWLINE)+NEWLINE_LEN;
159
163
/* Skip the section if MY_REMOVE_SECTION was given */
160
164
if (!in_section || remove_option != MY_REMOVE_SECTION)
161
dst_ptr= stpcpy(dst_ptr, linebuff);
165
dst_ptr= strcpy(dst_ptr, linebuff);
166
dst_ptr+= strlen(linebuff);
163
168
/* Look for a section */
164
169
if (*src_ptr == '[')
198
203
/* New option still remains to apply at the end */
199
204
if (!remove_option && *(dst_ptr - 1) != '\n')
200
dst_ptr= stpcpy(dst_ptr, NEWLINE);
205
dst_ptr= strcpy(dst_ptr, NEWLINE)+NEWLINE_LEN;
201
206
dst_ptr= add_option(dst_ptr, option_value, option, remove_option);
204
209
for (; nr_newlines; nr_newlines--)
205
dst_ptr= stpcpy(dst_ptr, NEWLINE);
210
dst_ptr= strcpy(dst_ptr, NEWLINE)+NEWLINE_LEN;
209
214
/* Don't write the file if there are no changes to be made */
210
if (ftruncate(fileno(cnf_file), (my_off_t) (dst_ptr - file_buffer)) ||
211
my_fseek(cnf_file, 0, MY_SEEK_SET, MYF(0)) ||
212
my_fwrite(cnf_file, (uchar*) file_buffer, (size_t) (dst_ptr - file_buffer),
215
if (ftruncate(fileno(cnf_file), (size_t) (dst_ptr - file_buffer)) ||
216
fseeko(cnf_file, 0, SEEK_SET) ||
217
fwrite(file_buffer, 1, (size_t) (dst_ptr - file_buffer), cnf_file))
216
if (my_fclose(cnf_file, MYF(MY_WME)))
220
if (fclose(cnf_file))
219
my_free(file_buffer, MYF(0));
223
my_free(file_buffer, MYF(0));
225
my_fclose(cnf_file, MYF(0));
226
return(1); /* out of resources */
231
return 1; /* out of resources */
233
238
if (!remove_option)
235
dst= stpcpy(dst, option);
240
dst= strcpy(dst, option);
241
dst+= strlen(option);
236
242
if (*option_value)
239
dst= stpcpy(dst, option_value);
245
dst= strcpy(dst, option_value);
246
dst+= strlen(option_value);
241
248
/* add a newline */
242
dst= stpcpy(dst, NEWLINE);
249
dst= strcpy(dst, NEWLINE)+NEWLINE_LEN;