~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_pack.cc

  • Committer: Lee
  • Date: 2009-01-01 03:07:33 UTC
  • mto: (758.1.3 devel)
  • mto: This revision was merged to the branch mainline in revision 759.
  • Revision ID: lbieber@lbieber-desktop-20090101030733-fb411b55f07vij8q
more header file cleanup

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 <mystrings/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
29
22
 
30
23
static char * expand_tilde(char * *path);
31
 
static size_t system_filename(char * to, const char *from);
 
24
 
 
25
        /* Pack a dirname ; Changes HOME to ~/ and current dev to ./ */
 
26
        /* from is a dirname (from dirname() ?) ending with FN_LIBCHAR */
 
27
        /* to may be == from */
 
28
 
 
29
void pack_dirname(char * to, const char *from)
 
30
{
 
31
  int getcwd_error= 0;
 
32
  size_t d_length,length,buff_length= 0;
 
33
  char * start;
 
34
  char buff[FN_REFLEN];
 
35
 
 
36
  (void) intern_filename(to,from);              /* Change to intern name */
 
37
 
 
38
#ifdef FN_DEVCHAR
 
39
  if ((start=strrchr(to,FN_DEVCHAR)) != 0)      /* Skip device part */
 
40
    start++;
 
41
  else
 
42
#endif
 
43
    start=to;
 
44
 
 
45
  if (!(getcwd(buff, FN_REFLEN)))
 
46
  {
 
47
    getcwd_error= errno;
 
48
    buff_length= strlen(buff);
 
49
    d_length= (size_t) (start-to);
 
50
    if ((start == to ||
 
51
        (buff_length == d_length && !memcmp(buff,start,d_length))) &&
 
52
        *start != FN_LIBCHAR && *start)
 
53
    {                                           /* Put current dir before */
 
54
      bchange((unsigned char*) to, d_length, (unsigned char*) buff, buff_length, strlen(to)+1);
 
55
    }
 
56
  }
 
57
 
 
58
  if ((d_length= cleanup_dirname(to,to)) != 0)
 
59
  {
 
60
    length=0;
 
61
    if (home_dir)
 
62
    {
 
63
      length= strlen(home_dir);
 
64
      if (home_dir[length-1] == FN_LIBCHAR)
 
65
        length--;                               /* Don't test last '/' */
 
66
    }
 
67
    if (length > 1 && length < d_length)
 
68
    {                                           /* test if /xx/yy -> ~/yy */
 
69
      if (memcmp(to,home_dir,length) == 0 && to[length] == FN_LIBCHAR)
 
70
      {
 
71
        to[0]=FN_HOMELIB;                       /* Filename begins with ~ */
 
72
        (void) strcpy(to+1,to+length);
 
73
      }
 
74
    }
 
75
    if (! getcwd_error)
 
76
    {                                           /* Test if cwd is ~/... */
 
77
      if (length > 1 && length < buff_length)
 
78
      {
 
79
        if (memcmp(buff,home_dir,length) == 0 && buff[length] == FN_LIBCHAR)
 
80
        {
 
81
          buff[0]=FN_HOMELIB;
 
82
          (void) strcpy(buff+1,buff+length);
 
83
        }
 
84
      }
 
85
      if (is_prefix(to,buff))
 
86
      {
 
87
        length= strlen(buff);
 
88
        if (to[length])
 
89
          (void) strcpy(to,to+length);  /* Remove everything before */
 
90
        else
 
91
        {
 
92
          to[0]= FN_CURLIB;                     /* Put ./ instead of cwd */
 
93
          to[1]= FN_LIBCHAR;
 
94
          to[2]= '\0';
 
95
        }
 
96
      }
 
97
    }
 
98
  }
 
99
  return;
 
100
} /* pack_dirname */
 
101
 
32
102
 
33
103
/*
34
104
  remove unwanted chars from dirname
50
120
    #  length of new name
51
121
*/
52
122
 
53
 
static size_t cleanup_dirname(char *to, const char *from)
 
123
size_t cleanup_dirname(register char *to, const char *from)
54
124
{
55
 
  size_t length;
56
 
  char * pos;
57
 
  const char * from_ptr;
58
 
  char * start;
 
125
  register size_t length;
 
126
  register char * pos;
 
127
  register const char * from_ptr;
 
128
  register char * start;
59
129
  char parent[5],                               /* for "FN_PARENTDIR" */
60
130
       buff[FN_REFLEN+1],*end_parentdir;
 
131
#ifdef BACKSLASH_MBTAIL
 
132
  CHARSET_INFO *fs= fs_character_set();
 
133
#endif
61
134
 
62
135
  start=buff;
63
136
  from_ptr= from;
164
237
 
165
238
bool my_use_symdir=0;   /* Set this if you want to use symdirs */
166
239
 
 
240
#ifdef USE_SYMDIR
 
241
void symdirget(char *dir)
 
242
{
 
243
  char buff[FN_REFLEN];
 
244
  char *pos= strchr(dir, '\0');
 
245
  if (dir[0] && pos[-1] != FN_DEVCHAR && my_access(dir, F_OK))
 
246
  {
 
247
    File file;
 
248
    size_t length;
 
249
    char temp= *(--pos);            /* May be "/" or "\" */
 
250
    strcpy(pos,".sym");
 
251
    file= my_open(dir, O_RDONLY, MYF(0));
 
252
    *pos++=temp; *pos=0;          /* Restore old filename */
 
253
    if (file >= 0)
 
254
    {
 
255
      if ((length= my_read(file, buff, sizeof(buff), MYF(0))) > 0)
 
256
      {
 
257
        for (pos= buff + length ;
 
258
             pos > buff && (iscntrl(pos[-1]) || isspace(pos[-1])) ;
 
259
             pos --);
 
260
 
 
261
        /* Ensure that the symlink ends with the directory symbol */
 
262
        if (pos == buff || pos[-1] != FN_LIBCHAR)
 
263
          *pos++=FN_LIBCHAR;
 
264
 
 
265
        strncpy(dir,buff, FN_REFLEN-1);
 
266
      }
 
267
      my_close(file, MYF(0));
 
268
    }
 
269
  }
 
270
}
 
271
#endif /* USE_SYMDIR */
 
272
 
167
273
 
168
274
/*
169
275
  Fixes a directroy name so that can be used by open()
220
326
      }
221
327
    }
222
328
  }
 
329
#ifdef USE_SYMDIR
 
330
  if (my_use_symdir)
 
331
    symdirget(buff);
 
332
#endif
223
333
  return(system_filename(to,buff));     /* Fix for open */
224
334
} /* unpack_dirname */
225
335
 
231
341
{
232
342
  if (path[0][0] == FN_LIBCHAR)
233
343
    return home_dir;                    /* ~/ expanded to home */
234
 
  char *str,save;
235
 
  struct passwd *user_entry;
 
344
#ifdef HAVE_GETPWNAM
 
345
  {
 
346
    char *str,save;
 
347
    struct passwd *user_entry;
236
348
 
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)
244
 
  {
245
 
    *path=str;
246
 
    return user_entry->pw_dir;
 
349
    if (!(str=strchr(*path,FN_LIBCHAR)))
 
350
      str= strchr(*path, '\0');
 
351
    save= *str; *str= '\0';
 
352
    user_entry=getpwnam(*path);
 
353
    *str=save;
 
354
    endpwent();
 
355
    if (user_entry)
 
356
    {
 
357
      *path=str;
 
358
      return user_entry->pw_dir;
 
359
    }
247
360
  }
 
361
#endif
248
362
  return NULL;
249
363
}
250
364
 
288
402
        /* Used before system command's like open(), create() .. */
289
403
        /* Returns used length of to; total length should be FN_REFLEN */
290
404
 
291
 
static size_t system_filename(char * to, const char *from)
 
405
size_t system_filename(char * to, const char *from)
292
406
{
 
407
#ifndef FN_C_BEFORE_DIR
293
408
  return strlen(strncpy(to,from,FN_REFLEN-1));
 
409
#else   /* VMS */
 
410
 
 
411
        /* change 'dev:lib/xxx' to 'dev:[lib]xxx' */
 
412
        /* change 'dev:xxx' to 'dev:xxx' */
 
413
        /* change './xxx' to 'xxx' */
 
414
        /* change './lib/' or lib/ to '[.lib]' */
 
415
        /* change '/x/y/z to '[x.y]x' */
 
416
        /* change 'dev:/x' to 'dev:[000000]x' */
 
417
 
 
418
  int libchar_found;
 
419
  size_t length;
 
420
  char * to_pos,from_pos,pos;
 
421
  char buff[FN_REFLEN];
 
422
 
 
423
  libchar_found=0;
 
424
  (void) strcpy(buff,from);                      /* If to == from */
 
425
  from_pos= buff;
 
426
  if ((pos=strrchr(from_pos,FN_DEVCHAR)))       /* Skip device part */
 
427
  {
 
428
    pos++;
 
429
    to_pos= strncpy(to,from_pos,(size_t) (pos-from_pos));
 
430
    to_pos+= strlen(to);
 
431
    from_pos=pos;
 
432
  }
 
433
  else
 
434
    to_pos=to;
 
435
 
 
436
  if (from_pos[0] == FN_CURLIB && from_pos[1] == FN_LIBCHAR)
 
437
    from_pos+=2;                                /* Skip './' */
 
438
  if (strchr(from_pos,FN_LIBCHAR))
 
439
  {
 
440
    *(to_pos++) = FN_C_BEFORE_DIR;
 
441
    if (strstr(from_pos,FN_ROOTDIR) == from_pos)
 
442
    {
 
443
      from_pos+=strlen(FN_ROOTDIR);             /* Actually +1 but... */
 
444
      if (! strchr(from_pos,FN_LIBCHAR))
 
445
      {                                         /* No dir, use [000000] */
 
446
        to_pos= strcpy(to_pos,FN_C_ROOT_DIR)+strlen(FN_C_ROOT_DIR);
 
447
        libchar_found++;
 
448
      }
 
449
    }
 
450
    else
 
451
      *(to_pos++)=FN_C_DIR_SEP;                 /* '.' gives current dir */
 
452
 
 
453
    while ((pos=strchr(from_pos,FN_LIBCHAR)))
 
454
    {
 
455
      if (libchar_found++)
 
456
        *(to_pos++)=FN_C_DIR_SEP;               /* Add '.' between dirs */
 
457
      if (strstr(from_pos,FN_PARENTDIR) == from_pos &&
 
458
          from_pos+strlen(FN_PARENTDIR) == pos) {
 
459
        to_pos= strcpy(to_pos,FN_C_PARENT_DIR); /* Found '../' */
 
460
        to_pos+= strlen(FN_C_PARENT_DIR);
 
461
      }
 
462
      else
 
463
      {
 
464
        to_pos= strncpy(to_pos,from_pos,(size_t) (pos-from_pos));
 
465
        to_pos+= strlen(to_pos);
 
466
      }
 
467
      from_pos=pos+1;
 
468
    }
 
469
    *(to_pos++)=FN_C_AFTER_DIR;
 
470
  }
 
471
 
 
472
  strcpy(to_pos, from_pos);
 
473
  length= strlen(to);
 
474
  return(length);
 
475
#endif
294
476
} /* system_filename */
295
477
 
296
478
 
309
491
  (void) strcpy(to + to_length,from+length);
310
492
  return (to);
311
493
} /* intern_filename */
312
 
 
313
 
} /* namespace internal */
314
 
} /* namespace drizzled */