~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_pack.c

Merged vcol stuff.

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
 
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
 
 
41
  (void) intern_filename(to,from);              /* Change to intern name */
 
42
 
 
43
#ifdef FN_DEVCHAR
 
44
  if ((start=strrchr(to,FN_DEVCHAR)) != 0)      /* Skip device part */
 
45
    start++;
 
46
  else
 
47
#endif
 
48
    start=to;
 
49
 
 
50
  if (!(cwd_err= my_getwd(buff,FN_REFLEN,MYF(0))))
 
51
  {
 
52
    buff_length= strlen(buff);
 
53
    d_length= (size_t) (start-to);
 
54
    if ((start == to ||
 
55
         (buff_length == d_length && !memcmp(buff,start,d_length))) &&
 
56
        *start != FN_LIBCHAR && *start)
 
57
    {                                           /* Put current dir before */
 
58
      bchange((unsigned char*) to, d_length, (unsigned char*) buff, buff_length, strlen(to)+1);
 
59
    }
 
60
  }
 
61
 
 
62
  if ((d_length= cleanup_dirname(to,to)) != 0)
 
63
  {
 
64
    length=0;
 
65
    if (home_dir)
 
66
    {
 
67
      length= strlen(home_dir);
 
68
      if (home_dir[length-1] == FN_LIBCHAR)
 
69
        length--;                               /* Don't test last '/' */
 
70
    }
 
71
    if (length > 1 && length < d_length)
 
72
    {                                           /* test if /xx/yy -> ~/yy */
 
73
      if (memcmp(to,home_dir,length) == 0 && to[length] == FN_LIBCHAR)
 
74
      {
 
75
        to[0]=FN_HOMELIB;                       /* Filename begins with ~ */
 
76
        (void) strmov_overlapp(to+1,to+length);
 
77
      }
 
78
    }
 
79
    if (! cwd_err)
 
80
    {                                           /* Test if cwd is ~/... */
 
81
      if (length > 1 && length < buff_length)
 
82
      {
 
83
        if (memcmp(buff,home_dir,length) == 0 && buff[length] == FN_LIBCHAR)
 
84
        {
 
85
          buff[0]=FN_HOMELIB;
 
86
          (void) strmov_overlapp(buff+1,buff+length);
 
87
        }
 
88
      }
 
89
      if (is_prefix(to,buff))
 
90
      {
 
91
        length= strlen(buff);
 
92
        if (to[length])
 
93
          (void) strmov_overlapp(to,to+length); /* Remove everything before */
 
94
        else
 
95
        {
 
96
          to[0]= FN_CURLIB;                     /* Put ./ instead of cwd */
 
97
          to[1]= FN_LIBCHAR;
 
98
          to[2]= '\0';
 
99
        }
 
100
      }
 
101
    }
 
102
  }
 
103
  return;
 
104
} /* pack_dirname */
 
105
 
32
106
 
33
107
/*
34
108
  remove unwanted chars from dirname
47
121
  Unpacks current dir if if "./.." used
48
122
 
49
123
  RETURN
50
 
    #  length of new name
 
124
    #  length of new name   
51
125
*/
52
126
 
53
 
static size_t cleanup_dirname(register char *to, const char *from)
 
127
size_t cleanup_dirname(register char *to, const char *from)
54
128
{
55
129
  register size_t length;
56
130
  register char * pos;
58
132
  register char * start;
59
133
  char parent[5],                               /* for "FN_PARENTDIR" */
60
134
       buff[FN_REFLEN+1],*end_parentdir;
 
135
#ifdef BACKSLASH_MBTAIL
 
136
  CHARSET_INFO *fs= fs_character_set();
 
137
#endif
61
138
 
62
139
  start=buff;
63
140
  from_ptr= from;
65
142
  if ((pos=strrchr(from_ptr,FN_DEVCHAR)) != 0)
66
143
  {                                             /* Skip device part */
67
144
    length=(size_t) (pos-from_ptr)+1;
68
 
    start= strncpy(buff,from_ptr,length);
69
 
    start+= strlen(from_ptr);
70
 
    from_ptr+=length;
 
145
    start=my_stpncpy(buff,from_ptr,length); from_ptr+=length;
71
146
  }
72
147
#endif
73
148
 
74
149
  parent[0]=FN_LIBCHAR;
75
 
  length= (size_t)((strcpy(parent+1,FN_PARENTDIR)+strlen(FN_PARENTDIR))-parent);
 
150
  length=(size_t) (my_stpcpy(parent+1,FN_PARENTDIR)-parent);
76
151
  for (pos=start ; (*pos= *from_ptr++) != 0 ; pos++)
77
152
  {
78
153
#ifdef BACKSLASH_MBTAIL
102
177
              pos+=length+1;                    /* Don't unpack ~/.. */
103
178
              continue;
104
179
            }
105
 
            pos= strcpy(buff,home_dir)+strlen(home_dir)-1;      /* Unpacks ~/.. */
 
180
            pos=my_stpcpy(buff,home_dir)-1;     /* Unpacks ~/.. */
106
181
            if (*pos == FN_LIBCHAR)
107
182
              pos--;                            /* home ended with '/' */
108
183
          }
109
184
          if (*pos == FN_CURLIB && (pos == start || pos[-1] == FN_LIBCHAR))
110
185
          {
111
 
            if (getcwd(curr_dir,FN_REFLEN))
 
186
            if (my_getwd(curr_dir,FN_REFLEN,MYF(0)))
112
187
            {
113
188
              pos+=length+1;                    /* Don't unpack ./.. */
114
189
              continue;
115
190
            }
116
 
            pos= strcpy(buff,curr_dir)+strlen(curr_dir)-1;      /* Unpacks ./.. */
 
191
            pos=my_stpcpy(buff,curr_dir)-1;     /* Unpacks ./.. */
117
192
            if (*pos == FN_LIBCHAR)
118
193
              pos--;                            /* home ended with '/' */
119
194
          }
122
197
            pos--;
123
198
          if (pos[1] == FN_HOMELIB || memcmp(pos,parent,length) == 0)
124
199
          {                                     /* Don't remove ~user/ */
125
 
            pos= strcpy(end_parentdir+1,parent)+strlen(parent);
 
200
            pos=my_stpcpy(end_parentdir+1,parent);
126
201
            *pos=FN_LIBCHAR;
127
202
            continue;
128
203
          }
148
223
      }
149
224
    }
150
225
  }
151
 
  (void) strcpy(to,buff);
 
226
  (void) my_stpcpy(to,buff);
152
227
  return((size_t) (pos-buff));
153
228
} /* cleanup_dirname */
154
229
 
155
230
 
156
231
/*
157
232
  On system where you don't have symbolic links, the following
158
 
  code will allow you to create a file:
 
233
  code will allow you to create a file: 
159
234
  directory-name.sym that should contain the real path
160
235
  to the directory.  This will be used if the directory name
161
236
  doesn't exists
164
239
 
165
240
bool my_use_symdir=0;   /* Set this if you want to use symdirs */
166
241
 
 
242
#ifdef USE_SYMDIR
 
243
void symdirget(char *dir)
 
244
{
 
245
  char buff[FN_REFLEN];
 
246
  char *pos= strchr(dir, '\0');
 
247
  if (dir[0] && pos[-1] != FN_DEVCHAR && my_access(dir, F_OK))
 
248
  {
 
249
    File file;
 
250
    size_t length;
 
251
    char temp= *(--pos);            /* May be "/" or "\" */
 
252
    my_stpcpy(pos,".sym");
 
253
    file= my_open(dir, O_RDONLY, MYF(0));
 
254
    *pos++=temp; *pos=0;          /* Restore old filename */
 
255
    if (file >= 0)
 
256
    {
 
257
      if ((length= my_read(file, buff, sizeof(buff), MYF(0))) > 0)
 
258
      {
 
259
        for (pos= buff + length ;
 
260
             pos > buff && (iscntrl(pos[-1]) || isspace(pos[-1])) ;
 
261
             pos --);
 
262
 
 
263
        /* Ensure that the symlink ends with the directory symbol */
 
264
        if (pos == buff || pos[-1] != FN_LIBCHAR)
 
265
          *pos++=FN_LIBCHAR;
 
266
 
 
267
        strmake(dir,buff, (size_t) (pos-buff));
 
268
      }
 
269
      my_close(file, MYF(0));
 
270
    }
 
271
  }
 
272
}
 
273
#endif /* USE_SYMDIR */
 
274
 
167
275
 
168
276
/*
169
277
  Fixes a directroy name so that can be used by open()
213
321
        if (tilde_expansion[h_length-1] == FN_LIBCHAR)
214
322
          h_length--;
215
323
        if (buff+h_length < suffix)
216
 
          memmove(buff+h_length, suffix, length);
 
324
          memcpy(buff+h_length, suffix, length);
217
325
        else
218
326
          bmove_upp((unsigned char*) buff+h_length+length, (unsigned char*) suffix+length, length);
219
 
        memmove(buff, tilde_expansion, h_length);
 
327
        memcpy(buff, tilde_expansion, h_length);
220
328
      }
221
329
    }
222
330
  }
 
331
#ifdef USE_SYMDIR
 
332
  if (my_use_symdir)
 
333
    symdirget(buff);
 
334
#endif
223
335
  return(system_filename(to,buff));     /* Fix for open */
224
336
} /* unpack_dirname */
225
337
 
231
343
{
232
344
  if (path[0][0] == FN_LIBCHAR)
233
345
    return home_dir;                    /* ~/ expanded to home */
234
 
  char *str,save;
235
 
  struct passwd *user_entry;
 
346
#ifdef HAVE_GETPWNAM
 
347
  {
 
348
    char *str,save;
 
349
    struct passwd *user_entry;
236
350
 
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;
 
351
    if (!(str=strchr(*path,FN_LIBCHAR)))
 
352
      str= strchr(*path, '\0');
 
353
    save= *str; *str= '\0';
 
354
    user_entry=getpwnam(*path);
 
355
    *str=save;
 
356
    endpwent();
 
357
    if (user_entry)
 
358
    {
 
359
      *path=str;
 
360
      return user_entry->pw_dir;
 
361
    }
247
362
  }
 
363
#endif
248
364
  return NULL;
249
365
}
250
366
 
275
391
  n_length=unpack_dirname(buff,buff);
276
392
  if (n_length+strlen(from+length) < FN_REFLEN)
277
393
  {
278
 
    (void) strcpy(buff+n_length,from+length);
 
394
    (void) my_stpcpy(buff+n_length,from+length);
279
395
    length= system_filename(to,buff);           /* Fix to usably filename */
280
396
  }
281
397
  else
288
404
        /* Used before system command's like open(), create() .. */
289
405
        /* Returns used length of to; total length should be FN_REFLEN */
290
406
 
291
 
static size_t system_filename(char * to, const char *from)
 
407
size_t system_filename(char * to, const char *from)
292
408
{
293
 
  return strlen(strncpy(to,from,FN_REFLEN-1));
 
409
#ifndef FN_C_BEFORE_DIR
 
410
  return (size_t) (strmake(to,from,FN_REFLEN-1)-to);
 
411
#else   /* VMS */
 
412
 
 
413
        /* change 'dev:lib/xxx' to 'dev:[lib]xxx' */
 
414
        /* change 'dev:xxx' to 'dev:xxx' */
 
415
        /* change './xxx' to 'xxx' */
 
416
        /* change './lib/' or lib/ to '[.lib]' */
 
417
        /* change '/x/y/z to '[x.y]x' */
 
418
        /* change 'dev:/x' to 'dev:[000000]x' */
 
419
 
 
420
  int libchar_found;
 
421
  size_t length;
 
422
  char * to_pos,from_pos,pos;
 
423
  char buff[FN_REFLEN];
 
424
 
 
425
  libchar_found=0;
 
426
  (void) my_stpcpy(buff,from);                   /* If to == from */
 
427
  from_pos= buff;
 
428
  if ((pos=strrchr(from_pos,FN_DEVCHAR)))       /* Skip device part */
 
429
  {
 
430
    pos++;
 
431
    to_pos=my_stpncpy(to,from_pos,(size_t) (pos-from_pos));
 
432
    from_pos=pos;
 
433
  }
 
434
  else
 
435
    to_pos=to;
 
436
 
 
437
  if (from_pos[0] == FN_CURLIB && from_pos[1] == FN_LIBCHAR)
 
438
    from_pos+=2;                                /* Skip './' */
 
439
  if (strchr(from_pos,FN_LIBCHAR))
 
440
  {
 
441
    *(to_pos++) = FN_C_BEFORE_DIR;
 
442
    if (strstr(from_pos,FN_ROOTDIR) == from_pos)
 
443
    {
 
444
      from_pos+=strlen(FN_ROOTDIR);             /* Actually +1 but... */
 
445
      if (! strchr(from_pos,FN_LIBCHAR))
 
446
      {                                         /* No dir, use [000000] */
 
447
        to_pos=my_stpcpy(to_pos,FN_C_ROOT_DIR);
 
448
        libchar_found++;
 
449
      }
 
450
    }
 
451
    else
 
452
      *(to_pos++)=FN_C_DIR_SEP;                 /* '.' gives current dir */
 
453
 
 
454
    while ((pos=strchr(from_pos,FN_LIBCHAR)))
 
455
    {
 
456
      if (libchar_found++)
 
457
        *(to_pos++)=FN_C_DIR_SEP;               /* Add '.' between dirs */
 
458
      if (strstr(from_pos,FN_PARENTDIR) == from_pos &&
 
459
          from_pos+strlen(FN_PARENTDIR) == pos)
 
460
        to_pos=my_stpcpy(to_pos,FN_C_PARENT_DIR);       /* Found '../' */
 
461
      else
 
462
        to_pos=my_stpncpy(to_pos,from_pos,(size_t) (pos-from_pos));
 
463
      from_pos=pos+1;
 
464
    }
 
465
    *(to_pos++)=FN_C_AFTER_DIR;
 
466
  }
 
467
  length= (size_t) (my_stpcpy(to_pos,from_pos)-to);
 
468
  return(length);
 
469
#endif
294
470
} /* system_filename */
295
471
 
296
472
 
302
478
  char buff[FN_REFLEN];
303
479
  if (from == to)
304
480
  {                                             /* Dirname may destroy from */
305
 
    strcpy(buff,from);
 
481
    my_stpcpy(buff,from);
306
482
    from=buff;
307
483
  }
308
484
  length= dirname_part(to, from, &to_length);   /* Copy dirname & fix chars */
309
 
  (void) strcpy(to + to_length,from+length);
 
485
  (void) my_stpcpy(to + to_length,from+length);
310
486
  return (to);
311
487
} /* intern_filename */
312
 
 
313
 
} /* namespace internal */
314
 
} /* namespace drizzled */