~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/default_modify.c

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:30 UTC
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063230-4brxsra0qsmsg84q
Added -Wunused-macros.

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"
17
16
#include "mysys_priv.h"
18
 
#include "m_string.h"
 
17
#include <mystrings/m_string.h>
19
18
#include <my_dir.h>
20
19
 
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;
70
 
  uint new_opt_len;
 
68
  uint32_t reserve_extended;
 
69
  uint32_t new_opt_len;
71
70
  int reserve_occupied= 0;
72
 
  DBUG_ENTER("modify_defaults_file");
73
71
 
74
 
  if (!(cnf_file= my_fopen(file_location, O_RDWR | O_BINARY, MYF(0))))
75
 
    DBUG_RETURN(2);
 
72
  if (!(cnf_file= my_fopen(file_location, O_RDWR, MYF(0))))
 
73
    return(2);
76
74
 
77
75
  if (fstat(fileno(cnf_file), &file_stat))
78
76
    goto malloc_err;
108
106
  for (dst_ptr= file_buffer; fgets(linebuff, BUFF_SIZE, cnf_file); )
109
107
  {
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);
112
110
         src_ptr++)
113
111
    {}
114
112
 
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'))
126
124
    {
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 */
129
127
 
130
128
      /* could be negative */
131
129
      reserve_occupied+= (int) new_opt_len - (int) (src_ptr - old_src_ptr);
156
154
      }
157
155
 
158
156
      for (; nr_newlines; nr_newlines--)
159
 
        dst_ptr= strmov(dst_ptr, NEWLINE);
 
157
        dst_ptr= my_stpcpy(dst_ptr, NEWLINE);
160
158
 
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);
164
162
    }
165
163
    /* Look for a section */
166
164
    if (*src_ptr == '[')
170
168
      {
171
169
        src_ptr+= sect_len;
172
170
        /* Skip over whitespaces. They are allowed after section name */
173
 
        for (; my_isspace(&my_charset_latin1, *src_ptr); src_ptr++)
 
171
        for (; my_isspace(&my_charset_utf8_general_ci, *src_ptr); src_ptr++)
174
172
        {}
175
173
 
176
174
        if (*src_ptr != ']')
199
197
  {
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);
204
202
    opt_applied= 1;
205
203
  }
206
204
  for (; nr_newlines; nr_newlines--)
207
 
    dst_ptr= strmov(dst_ptr, NEWLINE);
 
205
    dst_ptr= my_stpcpy(dst_ptr, NEWLINE);
208
206
 
209
207
  if (opt_applied)
210
208
  {
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),
215
213
                  MYF(MY_NABP)))
216
214
      goto err;
217
215
  }
218
216
  if (my_fclose(cnf_file, MYF(MY_WME)))
219
 
    DBUG_RETURN(1);
 
217
    return(1);
220
218
 
221
 
  my_free(file_buffer, MYF(0));
222
 
  DBUG_RETURN(0);
 
219
  free(file_buffer);
 
220
  return(0);
223
221
 
224
222
err:
225
 
  my_free(file_buffer, MYF(0));
 
223
  free(file_buffer);
226
224
malloc_err:
227
225
  my_fclose(cnf_file, MYF(0));
228
 
  DBUG_RETURN(1); /* out of resources */
 
226
  return(1); /* out of resources */
229
227
}
230
228
 
231
229
 
234
232
{
235
233
  if (!remove_option)
236
234
  {
237
 
    dst= strmov(dst, option);
 
235
    dst= my_stpcpy(dst, option);
238
236
    if (*option_value)
239
237
    {
240
238
      *dst++= '=';
241
 
      dst= strmov(dst, option_value);
 
239
      dst= my_stpcpy(dst, option_value);
242
240
    }
243
241
    /* add a newline */
244
 
    dst= strmov(dst, NEWLINE);
 
242
    dst= my_stpcpy(dst, NEWLINE);
245
243
  }
246
244
  return dst;
247
245
}