~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_pack.c

  • Committer: Brian Aker
  • Date: 2008-07-14 16:09:55 UTC
  • Revision ID: brian@tangent.org-20080714160955-v5nzzyjj5hhv7bz6
Removing a few "additional" ways of saying uint64_t

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
 
 
16
 
#include <config.h>
17
 
 
18
 
#include <drizzled/internal/my_sys.h>
19
 
 
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include "mysys_priv.h"
 
17
#include <m_string.h>
 
18
#include "my_static.h"
 
19
#ifdef HAVE_PWD_H
20
20
#include <pwd.h>
21
 
 
22
 
#include <drizzled/internal/m_string.h>
23
 
#include "my_static.h"
24
 
 
25
 
namespace drizzled
26
 
{
27
 
namespace internal
28
 
{
 
21
#endif
 
22
#ifdef VMS
 
23
#include <rms.h>
 
24
#include <iodef.h>
 
25
#include <descrip.h>
 
26
#endif /* VMS */
29
27
 
30
28
static char * expand_tilde(char * *path);
31
 
static size_t system_filename(char * to, const char *from);
 
29
 
 
30
        /* Pack a dirname ; Changes HOME to ~/ and current dev to ./ */
 
31
        /* from is a dirname (from dirname() ?) ending with FN_LIBCHAR */
 
32
        /* to may be == from */
 
33
 
 
34
void pack_dirname(char * to, const char *from)
 
35
{
 
36
  int cwd_err;
 
37
  size_t d_length,length,buff_length= 0;
 
38
  char * start;
 
39
  char buff[FN_REFLEN];
 
40
  DBUG_ENTER("pack_dirname");
 
41
 
 
42
  (void) intern_filename(to,from);              /* Change to intern name */
 
43
 
 
44
#ifdef FN_DEVCHAR
 
45
  if ((start=strrchr(to,FN_DEVCHAR)) != 0)      /* Skip device part */
 
46
    start++;
 
47
  else
 
48
#endif
 
49
    start=to;
 
50
 
 
51
  if (!(cwd_err= my_getwd(buff,FN_REFLEN,MYF(0))))
 
52
  {
 
53
    buff_length= strlen(buff);
 
54
    d_length= (size_t) (start-to);
 
55
    if ((start == to ||
 
56
         (buff_length == d_length && !bcmp(buff,start,d_length))) &&
 
57
        *start != FN_LIBCHAR && *start)
 
58
    {                                           /* Put current dir before */
 
59
      bchange((uchar*) to, d_length, (uchar*) buff, buff_length, strlen(to)+1);
 
60
    }
 
61
  }
 
62
 
 
63
  if ((d_length= cleanup_dirname(to,to)) != 0)
 
64
  {
 
65
    length=0;
 
66
    if (home_dir)
 
67
    {
 
68
      length= strlen(home_dir);
 
69
      if (home_dir[length-1] == FN_LIBCHAR)
 
70
        length--;                               /* Don't test last '/' */
 
71
    }
 
72
    if (length > 1 && length < d_length)
 
73
    {                                           /* test if /xx/yy -> ~/yy */
 
74
      if (bcmp(to,home_dir,length) == 0 && to[length] == FN_LIBCHAR)
 
75
      {
 
76
        to[0]=FN_HOMELIB;                       /* Filename begins with ~ */
 
77
        (void) strmov_overlapp(to+1,to+length);
 
78
      }
 
79
    }
 
80
    if (! cwd_err)
 
81
    {                                           /* Test if cwd is ~/... */
 
82
      if (length > 1 && length < buff_length)
 
83
      {
 
84
        if (bcmp(buff,home_dir,length) == 0 && buff[length] == FN_LIBCHAR)
 
85
        {
 
86
          buff[0]=FN_HOMELIB;
 
87
          (void) strmov_overlapp(buff+1,buff+length);
 
88
        }
 
89
      }
 
90
      if (is_prefix(to,buff))
 
91
      {
 
92
        length= strlen(buff);
 
93
        if (to[length])
 
94
          (void) strmov_overlapp(to,to+length); /* Remove everything before */
 
95
        else
 
96
        {
 
97
          to[0]= FN_CURLIB;                     /* Put ./ instead of cwd */
 
98
          to[1]= FN_LIBCHAR;
 
99
          to[2]= '\0';
 
100
        }
 
101
      }
 
102
    }
 
103
  }
 
104
  DBUG_PRINT("exit",("to: '%s'",to));
 
105
  DBUG_VOID_RETURN;
 
106
} /* pack_dirname */
 
107
 
32
108
 
33
109
/*
34
110
  remove unwanted chars from dirname
47
123
  Unpacks current dir if if "./.." used
48
124
 
49
125
  RETURN
50
 
    #  length of new name
 
126
    #  length of new name   
51
127
*/
52
128
 
53
 
static size_t cleanup_dirname(char *to, const char *from)
 
129
size_t cleanup_dirname(register char *to, const char *from)
54
130
{
55
 
  size_t length;
56
 
  char * pos;
57
 
  const char * from_ptr;
58
 
  char * start;
 
131
  register size_t length;
 
132
  register char * pos;
 
133
  register char * from_ptr;
 
134
  register char * start;
59
135
  char parent[5],                               /* for "FN_PARENTDIR" */
60
136
       buff[FN_REFLEN+1],*end_parentdir;
 
137
#ifdef BACKSLASH_MBTAIL
 
138
  CHARSET_INFO *fs= fs_character_set();
 
139
#endif
 
140
  DBUG_ENTER("cleanup_dirname");
 
141
  DBUG_PRINT("enter",("from: '%s'",from));
61
142
 
62
143
  start=buff;
63
 
  from_ptr= from;
 
144
  from_ptr=(char *) from;
64
145
#ifdef FN_DEVCHAR
65
146
  if ((pos=strrchr(from_ptr,FN_DEVCHAR)) != 0)
66
147
  {                                             /* Skip device part */
67
148
    length=(size_t) (pos-from_ptr)+1;
68
 
    start= strncpy(buff,from_ptr,length);
69
 
    start+= strlen(from_ptr);
70
 
    from_ptr+=length;
 
149
    start=strnmov(buff,from_ptr,length); from_ptr+=length;
71
150
  }
72
151
#endif
73
152
 
74
153
  parent[0]=FN_LIBCHAR;
75
 
  length= (size_t)((strcpy(parent+1,FN_PARENTDIR)+strlen(FN_PARENTDIR))-parent);
 
154
  length=(size_t) (strmov(parent+1,FN_PARENTDIR)-parent);
76
155
  for (pos=start ; (*pos= *from_ptr++) != 0 ; pos++)
77
156
  {
78
157
#ifdef BACKSLASH_MBTAIL
79
 
    uint32_t l;
 
158
    uint l;
80
159
    if (use_mb(fs) && (l= my_ismbchar(fs, from_ptr - 1, from_ptr + 2)))
81
160
    {
82
161
      for (l-- ; l ; *++pos= *from_ptr++, l--);
88
167
      *pos = FN_LIBCHAR;
89
168
    if (*pos == FN_LIBCHAR)
90
169
    {
91
 
      if ((size_t) (pos-start) > length &&
92
 
          memcmp(pos-length,parent,length) == 0)
 
170
      if ((size_t) (pos-start) > length && bcmp(pos-length,parent,length) == 0)
93
171
      {                                         /* If .../../; skip prev */
94
172
        pos-=length;
95
173
        if (pos != start)
102
180
              pos+=length+1;                    /* Don't unpack ~/.. */
103
181
              continue;
104
182
            }
105
 
            pos= strcpy(buff,home_dir)+strlen(home_dir)-1;      /* Unpacks ~/.. */
 
183
            pos=strmov(buff,home_dir)-1;        /* Unpacks ~/.. */
106
184
            if (*pos == FN_LIBCHAR)
107
185
              pos--;                            /* home ended with '/' */
108
186
          }
109
187
          if (*pos == FN_CURLIB && (pos == start || pos[-1] == FN_LIBCHAR))
110
188
          {
111
 
            if (getcwd(curr_dir,FN_REFLEN))
 
189
            if (my_getwd(curr_dir,FN_REFLEN,MYF(0)))
112
190
            {
113
191
              pos+=length+1;                    /* Don't unpack ./.. */
114
192
              continue;
115
193
            }
116
 
            pos= strcpy(buff,curr_dir)+strlen(curr_dir)-1;      /* Unpacks ./.. */
 
194
            pos=strmov(buff,curr_dir)-1;        /* Unpacks ./.. */
117
195
            if (*pos == FN_LIBCHAR)
118
196
              pos--;                            /* home ended with '/' */
119
197
          }
120
198
          end_parentdir=pos;
121
199
          while (pos >= start && *pos != FN_LIBCHAR)    /* remove prev dir */
122
200
            pos--;
123
 
          if (pos[1] == FN_HOMELIB || memcmp(pos,parent,length) == 0)
 
201
          if (pos[1] == FN_HOMELIB || bcmp(pos,parent,length) == 0)
124
202
          {                                     /* Don't remove ~user/ */
125
 
            pos= strcpy(end_parentdir+1,parent)+strlen(parent);
 
203
            pos=strmov(end_parentdir+1,parent);
126
204
            *pos=FN_LIBCHAR;
127
205
            continue;
128
206
          }
129
207
        }
130
208
      }
131
209
      else if ((size_t) (pos-start) == length-1 &&
132
 
               !memcmp(start,parent+1,length-1))
 
210
               !bcmp(start,parent+1,length-1))
133
211
        start=pos;                              /* Starts with "../" */
134
212
      else if (pos-start > 0 && pos[-1] == FN_LIBCHAR)
135
213
      {
148
226
      }
149
227
    }
150
228
  }
151
 
  (void) strcpy(to,buff);
152
 
  return((size_t) (pos-buff));
 
229
  (void) strmov(to,buff);
 
230
  DBUG_PRINT("exit",("to: '%s'",to));
 
231
  DBUG_RETURN((size_t) (pos-buff));
153
232
} /* cleanup_dirname */
154
233
 
155
234
 
156
235
/*
157
236
  On system where you don't have symbolic links, the following
158
 
  code will allow you to create a file:
 
237
  code will allow you to create a file: 
159
238
  directory-name.sym that should contain the real path
160
239
  to the directory.  This will be used if the directory name
161
240
  doesn't exists
164
243
 
165
244
bool my_use_symdir=0;   /* Set this if you want to use symdirs */
166
245
 
 
246
#ifdef USE_SYMDIR
 
247
void symdirget(char *dir)
 
248
{
 
249
  char buff[FN_REFLEN];
 
250
  char *pos=strend(dir);
 
251
  if (dir[0] && pos[-1] != FN_DEVCHAR && my_access(dir, F_OK))
 
252
  {
 
253
    File file;
 
254
    size_t length;
 
255
    char temp= *(--pos);            /* May be "/" or "\" */
 
256
    strmov(pos,".sym");
 
257
    file= my_open(dir, O_RDONLY, MYF(0));
 
258
    *pos++=temp; *pos=0;          /* Restore old filename */
 
259
    if (file >= 0)
 
260
    {
 
261
      if ((length= my_read(file, buff, sizeof(buff), MYF(0))) > 0)
 
262
      {
 
263
        for (pos= buff + length ;
 
264
             pos > buff && (iscntrl(pos[-1]) || isspace(pos[-1])) ;
 
265
             pos --);
 
266
 
 
267
        /* Ensure that the symlink ends with the directory symbol */
 
268
        if (pos == buff || pos[-1] != FN_LIBCHAR)
 
269
          *pos++=FN_LIBCHAR;
 
270
 
 
271
        strmake(dir,buff, (size_t) (pos-buff));
 
272
      }
 
273
      my_close(file, MYF(0));
 
274
    }
 
275
  }
 
276
}
 
277
#endif /* USE_SYMDIR */
 
278
 
167
279
 
168
280
/*
169
281
  Fixes a directroy name so that can be used by open()
188
300
{
189
301
  size_t length, h_length;
190
302
  char buff[FN_REFLEN+1+4],*suffix,*tilde_expansion;
 
303
  DBUG_ENTER("unpack_dirname");
191
304
 
192
305
  (void) intern_filename(buff,from);        /* Change to intern name */
193
306
  length= strlen(buff);                     /* Fix that '/' is last */
213
326
        if (tilde_expansion[h_length-1] == FN_LIBCHAR)
214
327
          h_length--;
215
328
        if (buff+h_length < suffix)
216
 
          memmove(buff+h_length, suffix, length);
 
329
          bmove(buff+h_length,suffix,length);
217
330
        else
218
 
          bmove_upp((unsigned char*) buff+h_length+length, (unsigned char*) suffix+length, length);
219
 
        memmove(buff, tilde_expansion, h_length);
 
331
          bmove_upp((uchar*) buff+h_length+length, (uchar*) suffix+length, length);
 
332
        bmove(buff,tilde_expansion,h_length);
220
333
      }
221
334
    }
222
335
  }
223
 
  return(system_filename(to,buff));     /* Fix for open */
 
336
#ifdef USE_SYMDIR
 
337
  if (my_use_symdir)
 
338
    symdirget(buff);
 
339
#endif
 
340
  DBUG_RETURN(system_filename(to,buff));        /* Fix for open */
224
341
} /* unpack_dirname */
225
342
 
226
343
 
231
348
{
232
349
  if (path[0][0] == FN_LIBCHAR)
233
350
    return home_dir;                    /* ~/ expanded to home */
234
 
  char *str,save;
235
 
  struct passwd *user_entry;
236
 
 
237
 
  if (!(str=strchr(*path,FN_LIBCHAR)))
238
 
    str= strchr(*path, '\0');
239
 
  save= *str; *str= '\0';
240
 
  user_entry=getpwnam(*path);
241
 
  *str=save;
242
 
  endpwent();
243
 
  if (user_entry)
 
351
#ifdef HAVE_GETPWNAM
244
352
  {
245
 
    *path=str;
246
 
    return user_entry->pw_dir;
 
353
    char *str,save;
 
354
    struct passwd *user_entry;
 
355
 
 
356
    if (!(str=strchr(*path,FN_LIBCHAR)))
 
357
      str=strend(*path);
 
358
    save= *str; *str= '\0';
 
359
    user_entry=getpwnam(*path);
 
360
    *str=save;
 
361
    endpwent();
 
362
    if (user_entry)
 
363
    {
 
364
      *path=str;
 
365
      return user_entry->pw_dir;
 
366
    }
247
367
  }
248
 
  return NULL;
 
368
#endif
 
369
  return (char *) 0;
249
370
}
250
371
 
251
372
 
270
391
{
271
392
  size_t length, n_length, buff_length;
272
393
  char buff[FN_REFLEN];
 
394
  DBUG_ENTER("unpack_filename");
273
395
 
274
396
  length=dirname_part(buff, from, &buff_length);/* copy & convert dirname */
275
397
  n_length=unpack_dirname(buff,buff);
276
398
  if (n_length+strlen(from+length) < FN_REFLEN)
277
399
  {
278
 
    (void) strcpy(buff+n_length,from+length);
 
400
    (void) strmov(buff+n_length,from+length);
279
401
    length= system_filename(to,buff);           /* Fix to usably filename */
280
402
  }
281
403
  else
282
404
    length= system_filename(to,from);           /* Fix to usably filename */
283
 
  return(length);
 
405
  DBUG_RETURN(length);
284
406
} /* unpack_filename */
285
407
 
286
408
 
288
410
        /* Used before system command's like open(), create() .. */
289
411
        /* Returns used length of to; total length should be FN_REFLEN */
290
412
 
291
 
static size_t system_filename(char * to, const char *from)
 
413
size_t system_filename(char * to, const char *from)
292
414
{
293
 
  return strlen(strncpy(to,from,FN_REFLEN-1));
 
415
#ifndef FN_C_BEFORE_DIR
 
416
  return (size_t) (strmake(to,from,FN_REFLEN-1)-to);
 
417
#else   /* VMS */
 
418
 
 
419
        /* change 'dev:lib/xxx' to 'dev:[lib]xxx' */
 
420
        /* change 'dev:xxx' to 'dev:xxx' */
 
421
        /* change './xxx' to 'xxx' */
 
422
        /* change './lib/' or lib/ to '[.lib]' */
 
423
        /* change '/x/y/z to '[x.y]x' */
 
424
        /* change 'dev:/x' to 'dev:[000000]x' */
 
425
 
 
426
  int libchar_found;
 
427
  size_t length;
 
428
  char * to_pos,from_pos,pos;
 
429
  char buff[FN_REFLEN];
 
430
  DBUG_ENTER("system_filename");
 
431
 
 
432
  libchar_found=0;
 
433
  (void) strmov(buff,from);                      /* If to == from */
 
434
  from_pos= buff;
 
435
  if ((pos=strrchr(from_pos,FN_DEVCHAR)))       /* Skip device part */
 
436
  {
 
437
    pos++;
 
438
    to_pos=strnmov(to,from_pos,(size_t) (pos-from_pos));
 
439
    from_pos=pos;
 
440
  }
 
441
  else
 
442
    to_pos=to;
 
443
 
 
444
  if (from_pos[0] == FN_CURLIB && from_pos[1] == FN_LIBCHAR)
 
445
    from_pos+=2;                                /* Skip './' */
 
446
  if (strchr(from_pos,FN_LIBCHAR))
 
447
  {
 
448
    *(to_pos++) = FN_C_BEFORE_DIR;
 
449
    if (strinstr(from_pos,FN_ROOTDIR) == 1)
 
450
    {
 
451
      from_pos+=strlen(FN_ROOTDIR);             /* Actually +1 but... */
 
452
      if (! strchr(from_pos,FN_LIBCHAR))
 
453
      {                                         /* No dir, use [000000] */
 
454
        to_pos=strmov(to_pos,FN_C_ROOT_DIR);
 
455
        libchar_found++;
 
456
      }
 
457
    }
 
458
    else
 
459
      *(to_pos++)=FN_C_DIR_SEP;                 /* '.' gives current dir */
 
460
 
 
461
    while ((pos=strchr(from_pos,FN_LIBCHAR)))
 
462
    {
 
463
      if (libchar_found++)
 
464
        *(to_pos++)=FN_C_DIR_SEP;               /* Add '.' between dirs */
 
465
      if (strinstr(from_pos,FN_PARENTDIR) == 1 &&
 
466
          from_pos+strlen(FN_PARENTDIR) == pos)
 
467
        to_pos=strmov(to_pos,FN_C_PARENT_DIR);  /* Found '../' */
 
468
      else
 
469
        to_pos=strnmov(to_pos,from_pos,(size_t) (pos-from_pos));
 
470
      from_pos=pos+1;
 
471
    }
 
472
    *(to_pos++)=FN_C_AFTER_DIR;
 
473
  }
 
474
  length= (size_t) (strmov(to_pos,from_pos)-to);
 
475
  DBUG_PRINT("exit",("name: '%s'",to));
 
476
  DBUG_RETURN(length);
 
477
#endif
294
478
} /* system_filename */
295
479
 
296
480
 
302
486
  char buff[FN_REFLEN];
303
487
  if (from == to)
304
488
  {                                             /* Dirname may destroy from */
305
 
    strcpy(buff,from);
 
489
    strmov(buff,from);
306
490
    from=buff;
307
491
  }
308
492
  length= dirname_part(to, from, &to_length);   /* Copy dirname & fix chars */
309
 
  (void) strcpy(to + to_length,from+length);
 
493
  (void) strmov(to + to_length,from+length);
310
494
  return (to);
311
495
} /* intern_filename */
312
 
 
313
 
} /* namespace internal */
314
 
} /* namespace drizzled */