~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/default_modify.c

  • Committer: brian
  • Date: 2008-07-03 12:39:14 UTC
  • Revision ID: brian@localhost.localdomain-20080703123914-lry82qf74f6cbyzs
Disabling myisam tools until incomming link patch from Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 */
15
15
 
 
16
#include "my_global.h"
16
17
#include "mysys_priv.h"
17
 
#include <mystrings/m_string.h>
 
18
#include "m_string.h"
18
19
#include <my_dir.h>
19
20
 
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;
69
 
  uint32_t new_opt_len;
 
67
  uint nr_newlines= 0, buffer_size;
 
68
  my_bool in_section= FALSE, opt_applied= 0;
 
69
  uint reserve_extended;
 
70
  uint new_opt_len;
70
71
  int reserve_occupied= 0;
 
72
  DBUG_ENTER("modify_defaults_file");
71
73
 
72
 
  if (!(cnf_file= my_fopen(file_location, O_RDWR, MYF(0))))
73
 
    return(2);
 
74
  if (!(cnf_file= my_fopen(file_location, O_RDWR | O_BINARY, MYF(0))))
 
75
    DBUG_RETURN(2);
74
76
 
75
77
  if (fstat(fileno(cnf_file), &file_stat))
76
78
    goto malloc_err;
106
108
  for (dst_ptr= file_buffer; fgets(linebuff, BUFF_SIZE, cnf_file); )
107
109
  {
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);
110
112
         src_ptr++)
111
113
    {}
112
114
 
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'))
124
126
    {
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 */
127
129
 
128
130
      /* could be negative */
129
131
      reserve_occupied+= (int) new_opt_len - (int) (src_ptr - old_src_ptr);
154
156
      }
155
157
 
156
158
      for (; nr_newlines; nr_newlines--)
157
 
        dst_ptr= my_stpcpy(dst_ptr, NEWLINE);
 
159
        dst_ptr= strmov(dst_ptr, NEWLINE);
158
160
 
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);
162
164
    }
163
165
    /* Look for a section */
164
166
    if (*src_ptr == '[')
168
170
      {
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++)
172
174
        {}
173
175
 
174
176
        if (*src_ptr != ']')
175
177
        {
176
 
          in_section= false;
 
178
          in_section= FALSE;
177
179
          continue; /* Missing closing parenthesis. Assume this was no group */
178
180
        }
179
181
 
180
182
        if (remove_option == MY_REMOVE_SECTION)
181
183
          dst_ptr= dst_ptr - strlen(linebuff);
182
184
 
183
 
        in_section= true;
 
185
        in_section= TRUE;
184
186
      }
185
187
      else
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 */
187
189
    }
188
190
  }
189
191
 
197
199
  {
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);
202
204
    opt_applied= 1;
203
205
  }
204
206
  for (; nr_newlines; nr_newlines--)
205
 
    dst_ptr= my_stpcpy(dst_ptr, NEWLINE);
 
207
    dst_ptr= strmov(dst_ptr, NEWLINE);
206
208
 
207
209
  if (opt_applied)
208
210
  {
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),
213
215
                  MYF(MY_NABP)))
214
216
      goto err;
215
217
  }
216
218
  if (my_fclose(cnf_file, MYF(MY_WME)))
217
 
    return(1);
 
219
    DBUG_RETURN(1);
218
220
 
219
 
  free(file_buffer);
220
 
  return(0);
 
221
  my_free(file_buffer, MYF(0));
 
222
  DBUG_RETURN(0);
221
223
 
222
224
err:
223
 
  free(file_buffer);
 
225
  my_free(file_buffer, MYF(0));
224
226
malloc_err:
225
227
  my_fclose(cnf_file, MYF(0));
226
 
  return(1); /* out of resources */
 
228
  DBUG_RETURN(1); /* out of resources */
227
229
}
228
230
 
229
231
 
232
234
{
233
235
  if (!remove_option)
234
236
  {
235
 
    dst= my_stpcpy(dst, option);
 
237
    dst= strmov(dst, option);
236
238
    if (*option_value)
237
239
    {
238
240
      *dst++= '=';
239
 
      dst= my_stpcpy(dst, option_value);
 
241
      dst= strmov(dst, option_value);
240
242
    }
241
243
    /* add a newline */
242
 
    dst= my_stpcpy(dst, NEWLINE);
 
244
    dst= strmov(dst, NEWLINE);
243
245
  }
244
246
  return dst;
245
247
}