~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_pack.cc

Merged in latest plugin-slot-reorg.

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 "mysys_priv.h"
 
16
#include "mysys/mysys_priv.h"
17
17
#include <mystrings/m_string.h>
18
18
#include "my_static.h"
19
19
#ifdef HAVE_PWD_H
20
20
#include <pwd.h>
21
21
#endif
22
 
#ifdef VMS
23
 
#include <rms.h>
24
 
#include <iodef.h>
25
 
#include <descrip.h>
26
 
#endif /* VMS */
27
22
 
28
23
static char * expand_tilde(char * *path);
29
24
 
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
 
 
106
25
 
107
26
/*
108
27
  remove unwanted chars from dirname
121
40
  Unpacks current dir if if "./.." used
122
41
 
123
42
  RETURN
124
 
    #  length of new name   
 
43
    #  length of new name
125
44
*/
126
45
 
127
46
size_t cleanup_dirname(register char *to, const char *from)
132
51
  register char * start;
133
52
  char parent[5],                               /* for "FN_PARENTDIR" */
134
53
       buff[FN_REFLEN+1],*end_parentdir;
135
 
#ifdef BACKSLASH_MBTAIL
136
 
  CHARSET_INFO *fs= fs_character_set();
137
 
#endif
138
54
 
139
55
  start=buff;
140
56
  from_ptr= from;
142
58
  if ((pos=strrchr(from_ptr,FN_DEVCHAR)) != 0)
143
59
  {                                             /* Skip device part */
144
60
    length=(size_t) (pos-from_ptr)+1;
145
 
    start=my_stpncpy(buff,from_ptr,length); from_ptr+=length;
 
61
    start= strncpy(buff,from_ptr,length);
 
62
    start+= strlen(from_ptr);
 
63
    from_ptr+=length;
146
64
  }
147
65
#endif
148
66
 
149
67
  parent[0]=FN_LIBCHAR;
150
 
  length=(size_t) (my_stpcpy(parent+1,FN_PARENTDIR)-parent);
 
68
  length= (size_t)((strcpy(parent+1,FN_PARENTDIR)+strlen(FN_PARENTDIR))-parent);
151
69
  for (pos=start ; (*pos= *from_ptr++) != 0 ; pos++)
152
70
  {
153
71
#ifdef BACKSLASH_MBTAIL
177
95
              pos+=length+1;                    /* Don't unpack ~/.. */
178
96
              continue;
179
97
            }
180
 
            pos=my_stpcpy(buff,home_dir)-1;     /* Unpacks ~/.. */
 
98
            pos= strcpy(buff,home_dir)+strlen(home_dir)-1;      /* Unpacks ~/.. */
181
99
            if (*pos == FN_LIBCHAR)
182
100
              pos--;                            /* home ended with '/' */
183
101
          }
184
102
          if (*pos == FN_CURLIB && (pos == start || pos[-1] == FN_LIBCHAR))
185
103
          {
186
 
            if (my_getwd(curr_dir,FN_REFLEN,MYF(0)))
 
104
            if (getcwd(curr_dir,FN_REFLEN))
187
105
            {
188
106
              pos+=length+1;                    /* Don't unpack ./.. */
189
107
              continue;
190
108
            }
191
 
            pos=my_stpcpy(buff,curr_dir)-1;     /* Unpacks ./.. */
 
109
            pos= strcpy(buff,curr_dir)+strlen(curr_dir)-1;      /* Unpacks ./.. */
192
110
            if (*pos == FN_LIBCHAR)
193
111
              pos--;                            /* home ended with '/' */
194
112
          }
197
115
            pos--;
198
116
          if (pos[1] == FN_HOMELIB || memcmp(pos,parent,length) == 0)
199
117
          {                                     /* Don't remove ~user/ */
200
 
            pos=my_stpcpy(end_parentdir+1,parent);
 
118
            pos= strcpy(end_parentdir+1,parent)+strlen(parent);
201
119
            *pos=FN_LIBCHAR;
202
120
            continue;
203
121
          }
223
141
      }
224
142
    }
225
143
  }
226
 
  (void) my_stpcpy(to,buff);
 
144
  (void) strcpy(to,buff);
227
145
  return((size_t) (pos-buff));
228
146
} /* cleanup_dirname */
229
147
 
230
148
 
231
149
/*
232
150
  On system where you don't have symbolic links, the following
233
 
  code will allow you to create a file: 
 
151
  code will allow you to create a file:
234
152
  directory-name.sym that should contain the real path
235
153
  to the directory.  This will be used if the directory name
236
154
  doesn't exists
239
157
 
240
158
bool my_use_symdir=0;   /* Set this if you want to use symdirs */
241
159
 
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
 
 
275
160
 
276
161
/*
277
162
  Fixes a directroy name so that can be used by open()
321
206
        if (tilde_expansion[h_length-1] == FN_LIBCHAR)
322
207
          h_length--;
323
208
        if (buff+h_length < suffix)
324
 
          memcpy(buff+h_length, suffix, length);
 
209
          memmove(buff+h_length, suffix, length);
325
210
        else
326
211
          bmove_upp((unsigned char*) buff+h_length+length, (unsigned char*) suffix+length, length);
327
 
        memcpy(buff, tilde_expansion, h_length);
 
212
        memmove(buff, tilde_expansion, h_length);
328
213
      }
329
214
    }
330
215
  }
331
 
#ifdef USE_SYMDIR
332
 
  if (my_use_symdir)
333
 
    symdirget(buff);
334
 
#endif
335
216
  return(system_filename(to,buff));     /* Fix for open */
336
217
} /* unpack_dirname */
337
218
 
391
272
  n_length=unpack_dirname(buff,buff);
392
273
  if (n_length+strlen(from+length) < FN_REFLEN)
393
274
  {
394
 
    (void) my_stpcpy(buff+n_length,from+length);
 
275
    (void) strcpy(buff+n_length,from+length);
395
276
    length= system_filename(to,buff);           /* Fix to usably filename */
396
277
  }
397
278
  else
407
288
size_t system_filename(char * to, const char *from)
408
289
{
409
290
#ifndef FN_C_BEFORE_DIR
410
 
  return (size_t) (strmake(to,from,FN_REFLEN-1)-to);
 
291
  return strlen(strncpy(to,from,FN_REFLEN-1));
411
292
#else   /* VMS */
412
293
 
413
294
        /* change 'dev:lib/xxx' to 'dev:[lib]xxx' */
423
304
  char buff[FN_REFLEN];
424
305
 
425
306
  libchar_found=0;
426
 
  (void) my_stpcpy(buff,from);                   /* If to == from */
 
307
  (void) strcpy(buff,from);                      /* If to == from */
427
308
  from_pos= buff;
428
309
  if ((pos=strrchr(from_pos,FN_DEVCHAR)))       /* Skip device part */
429
310
  {
430
311
    pos++;
431
 
    to_pos=my_stpncpy(to,from_pos,(size_t) (pos-from_pos));
 
312
    to_pos= strncpy(to,from_pos,(size_t) (pos-from_pos));
 
313
    to_pos+= strlen(to);
432
314
    from_pos=pos;
433
315
  }
434
316
  else
444
326
      from_pos+=strlen(FN_ROOTDIR);             /* Actually +1 but... */
445
327
      if (! strchr(from_pos,FN_LIBCHAR))
446
328
      {                                         /* No dir, use [000000] */
447
 
        to_pos=my_stpcpy(to_pos,FN_C_ROOT_DIR);
 
329
        to_pos= strcpy(to_pos,FN_C_ROOT_DIR)+strlen(FN_C_ROOT_DIR);
448
330
        libchar_found++;
449
331
      }
450
332
    }
454
336
    while ((pos=strchr(from_pos,FN_LIBCHAR)))
455
337
    {
456
338
      if (libchar_found++)
457
 
        *(to_pos++)=FN_C_DIR_SEP;               /* Add '.' between dirs */
 
339
        *(to_pos++)=FN_C_DIR_SEP;               /* Add '.' between dirs */
458
340
      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 '../' */
 
341
          from_pos+strlen(FN_PARENTDIR) == pos) {
 
342
        to_pos= strcpy(to_pos,FN_C_PARENT_DIR); /* Found '../' */
 
343
        to_pos+= strlen(FN_C_PARENT_DIR);
 
344
      }
461
345
      else
462
 
        to_pos=my_stpncpy(to_pos,from_pos,(size_t) (pos-from_pos));
 
346
      {
 
347
        to_pos= strncpy(to_pos,from_pos,(size_t) (pos-from_pos));
 
348
        to_pos+= strlen(to_pos);
 
349
      }
463
350
      from_pos=pos+1;
464
351
    }
465
352
    *(to_pos++)=FN_C_AFTER_DIR;
466
353
  }
467
 
  length= (size_t) (my_stpcpy(to_pos,from_pos)-to);
 
354
 
 
355
  strcpy(to_pos, from_pos);
 
356
  length= strlen(to);
468
357
  return(length);
469
358
#endif
470
359
} /* system_filename */
478
367
  char buff[FN_REFLEN];
479
368
  if (from == to)
480
369
  {                                             /* Dirname may destroy from */
481
 
    my_stpcpy(buff,from);
 
370
    strcpy(buff,from);
482
371
    from=buff;
483
372
  }
484
373
  length= dirname_part(to, from, &to_length);   /* Copy dirname & fix chars */
485
 
  (void) my_stpcpy(to + to_length,from+length);
 
374
  (void) strcpy(to + to_length,from+length);
486
375
  return (to);
487
376
} /* intern_filename */