~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_pack.cc

  • Committer: Brian Aker
  • Date: 2009-02-12 22:45:08 UTC
  • Revision ID: brian@tangent.org-20090212224508-mrd9jwgn1zjdpqdk
Minor refactoring (we will need to disconnect the code from the include
file).

Show diffs side-by-side

added added

removed removed

Lines of Context:
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)
142
61
  if ((pos=strrchr(from_ptr,FN_DEVCHAR)) != 0)
143
62
  {                                             /* Skip device part */
144
63
    length=(size_t) (pos-from_ptr)+1;
145
 
    start=my_stpncpy(buff,from_ptr,length); from_ptr+=length;
 
64
    start= strncpy(buff,from_ptr,length);
 
65
    start+= strlen(from_ptr);
 
66
    from_ptr+=length;
146
67
  }
147
68
#endif
148
69
 
149
70
  parent[0]=FN_LIBCHAR;
150
 
  length=(size_t) (my_stpcpy(parent+1,FN_PARENTDIR)-parent);
 
71
  length= (size_t)((strcpy(parent+1,FN_PARENTDIR)+strlen(FN_PARENTDIR))-parent);
151
72
  for (pos=start ; (*pos= *from_ptr++) != 0 ; pos++)
152
73
  {
153
74
#ifdef BACKSLASH_MBTAIL
177
98
              pos+=length+1;                    /* Don't unpack ~/.. */
178
99
              continue;
179
100
            }
180
 
            pos=my_stpcpy(buff,home_dir)-1;     /* Unpacks ~/.. */
 
101
            pos= strcpy(buff,home_dir)+strlen(home_dir)-1;      /* Unpacks ~/.. */
181
102
            if (*pos == FN_LIBCHAR)
182
103
              pos--;                            /* home ended with '/' */
183
104
          }
184
105
          if (*pos == FN_CURLIB && (pos == start || pos[-1] == FN_LIBCHAR))
185
106
          {
186
 
            if (my_getwd(curr_dir,FN_REFLEN,MYF(0)))
 
107
            if (getcwd(curr_dir,FN_REFLEN))
187
108
            {
188
109
              pos+=length+1;                    /* Don't unpack ./.. */
189
110
              continue;
190
111
            }
191
 
            pos=my_stpcpy(buff,curr_dir)-1;     /* Unpacks ./.. */
 
112
            pos= strcpy(buff,curr_dir)+strlen(curr_dir)-1;      /* Unpacks ./.. */
192
113
            if (*pos == FN_LIBCHAR)
193
114
              pos--;                            /* home ended with '/' */
194
115
          }
197
118
            pos--;
198
119
          if (pos[1] == FN_HOMELIB || memcmp(pos,parent,length) == 0)
199
120
          {                                     /* Don't remove ~user/ */
200
 
            pos=my_stpcpy(end_parentdir+1,parent);
 
121
            pos= strcpy(end_parentdir+1,parent)+strlen(parent);
201
122
            *pos=FN_LIBCHAR;
202
123
            continue;
203
124
          }
223
144
      }
224
145
    }
225
146
  }
226
 
  (void) my_stpcpy(to,buff);
 
147
  (void) strcpy(to,buff);
227
148
  return((size_t) (pos-buff));
228
149
} /* cleanup_dirname */
229
150
 
230
151
 
231
152
/*
232
153
  On system where you don't have symbolic links, the following
233
 
  code will allow you to create a file: 
 
154
  code will allow you to create a file:
234
155
  directory-name.sym that should contain the real path
235
156
  to the directory.  This will be used if the directory name
236
157
  doesn't exists
249
170
    File file;
250
171
    size_t length;
251
172
    char temp= *(--pos);            /* May be "/" or "\" */
252
 
    my_stpcpy(pos,".sym");
 
173
    strcpy(pos,".sym");
253
174
    file= my_open(dir, O_RDONLY, MYF(0));
254
175
    *pos++=temp; *pos=0;          /* Restore old filename */
255
176
    if (file >= 0)
256
177
    {
257
178
      if ((length= my_read(file, buff, sizeof(buff), MYF(0))) > 0)
258
179
      {
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));
 
180
        for (pos= buff + length ;
 
181
             pos > buff && (iscntrl(pos[-1]) || isspace(pos[-1])) ;
 
182
             pos --);
 
183
 
 
184
        /* Ensure that the symlink ends with the directory symbol */
 
185
        if (pos == buff || pos[-1] != FN_LIBCHAR)
 
186
          *pos++=FN_LIBCHAR;
 
187
 
 
188
        strncpy(dir,buff, FN_REFLEN-1);
268
189
      }
269
190
      my_close(file, MYF(0));
270
191
    }
321
242
        if (tilde_expansion[h_length-1] == FN_LIBCHAR)
322
243
          h_length--;
323
244
        if (buff+h_length < suffix)
324
 
          memcpy(buff+h_length, suffix, length);
 
245
          memmove(buff+h_length, suffix, length);
325
246
        else
326
247
          bmove_upp((unsigned char*) buff+h_length+length, (unsigned char*) suffix+length, length);
327
 
        memcpy(buff, tilde_expansion, h_length);
 
248
        memmove(buff, tilde_expansion, h_length);
328
249
      }
329
250
    }
330
251
  }
391
312
  n_length=unpack_dirname(buff,buff);
392
313
  if (n_length+strlen(from+length) < FN_REFLEN)
393
314
  {
394
 
    (void) my_stpcpy(buff+n_length,from+length);
 
315
    (void) strcpy(buff+n_length,from+length);
395
316
    length= system_filename(to,buff);           /* Fix to usably filename */
396
317
  }
397
318
  else
407
328
size_t system_filename(char * to, const char *from)
408
329
{
409
330
#ifndef FN_C_BEFORE_DIR
410
 
  return (size_t) (strmake(to,from,FN_REFLEN-1)-to);
 
331
  return strlen(strncpy(to,from,FN_REFLEN-1));
411
332
#else   /* VMS */
412
333
 
413
334
        /* change 'dev:lib/xxx' to 'dev:[lib]xxx' */
423
344
  char buff[FN_REFLEN];
424
345
 
425
346
  libchar_found=0;
426
 
  (void) my_stpcpy(buff,from);                   /* If to == from */
 
347
  (void) strcpy(buff,from);                      /* If to == from */
427
348
  from_pos= buff;
428
349
  if ((pos=strrchr(from_pos,FN_DEVCHAR)))       /* Skip device part */
429
350
  {
430
351
    pos++;
431
 
    to_pos=my_stpncpy(to,from_pos,(size_t) (pos-from_pos));
 
352
    to_pos= strncpy(to,from_pos,(size_t) (pos-from_pos));
 
353
    to_pos+= strlen(to);
432
354
    from_pos=pos;
433
355
  }
434
356
  else
444
366
      from_pos+=strlen(FN_ROOTDIR);             /* Actually +1 but... */
445
367
      if (! strchr(from_pos,FN_LIBCHAR))
446
368
      {                                         /* No dir, use [000000] */
447
 
        to_pos=my_stpcpy(to_pos,FN_C_ROOT_DIR);
 
369
        to_pos= strcpy(to_pos,FN_C_ROOT_DIR)+strlen(FN_C_ROOT_DIR);
448
370
        libchar_found++;
449
371
      }
450
372
    }
454
376
    while ((pos=strchr(from_pos,FN_LIBCHAR)))
455
377
    {
456
378
      if (libchar_found++)
457
 
        *(to_pos++)=FN_C_DIR_SEP;               /* Add '.' between dirs */
 
379
        *(to_pos++)=FN_C_DIR_SEP;               /* Add '.' between dirs */
458
380
      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 '../' */
 
381
          from_pos+strlen(FN_PARENTDIR) == pos) {
 
382
        to_pos= strcpy(to_pos,FN_C_PARENT_DIR); /* Found '../' */
 
383
        to_pos+= strlen(FN_C_PARENT_DIR);
 
384
      }
461
385
      else
462
 
        to_pos=my_stpncpy(to_pos,from_pos,(size_t) (pos-from_pos));
 
386
      {
 
387
        to_pos= strncpy(to_pos,from_pos,(size_t) (pos-from_pos));
 
388
        to_pos+= strlen(to_pos);
 
389
      }
463
390
      from_pos=pos+1;
464
391
    }
465
392
    *(to_pos++)=FN_C_AFTER_DIR;
466
393
  }
467
 
  length= (size_t) (my_stpcpy(to_pos,from_pos)-to);
 
394
 
 
395
  strcpy(to_pos, from_pos);
 
396
  length= strlen(to);
468
397
  return(length);
469
398
#endif
470
399
} /* system_filename */
478
407
  char buff[FN_REFLEN];
479
408
  if (from == to)
480
409
  {                                             /* Dirname may destroy from */
481
 
    my_stpcpy(buff,from);
 
410
    strcpy(buff,from);
482
411
    from=buff;
483
412
  }
484
413
  length= dirname_part(to, from, &to_length);   /* Copy dirname & fix chars */
485
 
  (void) my_stpcpy(to + to_length,from+length);
 
414
  (void) strcpy(to + to_length,from+length);
486
415
  return (to);
487
416
} /* intern_filename */