~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_format.cc

Merge of Jay

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
 
 
19
#include <algorithm>
 
20
 
 
21
using namespace std;
 
22
 
19
23
/*
20
24
  Formats a filename with possible replace of directory of extension
21
25
  Function can handle the case where 'to' == 'name'
24
28
*/
25
29
 
26
30
char * fn_format(char * to, const char *name, const char *dir,
27
 
                    const char *extension, uint flag)
 
31
                    const char *extension, uint32_t flag)
28
32
{
29
33
  char dev[FN_REFLEN], buff[FN_REFLEN], *pos;
30
34
  const char *startpos = name;
37
41
  if (length == 0 || (flag & MY_REPLACE_DIR))
38
42
  {
39
43
    /* Use given directory */
40
 
    convert_dirname(dev,dir,NullS);             /* Fix to this OS */
 
44
    convert_dirname(dev,dir,NULL);              /* Fix to this OS */
41
45
  }
42
46
  else if ((flag & MY_RELATIVE_PATH) && !test_if_hard_path(dev))
43
47
  {
44
48
    /* Put 'dir' before the given path */
45
 
    strmake(buff,dev,sizeof(buff)-1);
46
 
    pos=convert_dirname(dev,dir,NullS);
47
 
    strmake(pos,buff,sizeof(buff)-1- (int) (pos-dev));
 
49
    strncpy(buff,dev,sizeof(buff)-1);
 
50
    pos=convert_dirname(dev,dir,NULL);
 
51
    strncpy(pos,buff,sizeof(buff)-1- (int) (pos-dev));
48
52
  }
49
53
 
50
 
  if (flag & MY_PACK_FILENAME)
51
 
    pack_dirname(dev,dev);                      /* Put in ./.. and ~/.. */
52
54
  if (flag & MY_UNPACK_FILENAME)
53
55
    (void) unpack_dirname(dev,dev);             /* Replace ~/.. with dir */
54
56
 
55
57
  if (!(flag & MY_APPEND_EXT) &&
56
 
      (pos= (char*) strchr(name,FN_EXTCHAR)) != NullS)
 
58
      (pos= (char*) strchr(name,FN_EXTCHAR)) != NULL)
57
59
  {
58
60
    if ((flag & MY_REPLACE_EXT) == 0)           /* If we should keep old ext */
59
61
    {
77
79
    /* To long path, return original or NULL */
78
80
    size_t tmp_length;
79
81
    if (flag & MY_SAFE_PATH)
80
 
      return NullS;
81
 
    tmp_length= strlength(startpos);
82
 
    (void) strmake(to,startpos,min(tmp_length,FN_REFLEN-1));
 
82
      return NULL;
 
83
    tmp_length= min(strlength(startpos), (size_t)(FN_REFLEN-1));
 
84
    strncpy(to,startpos,tmp_length);
 
85
    to[tmp_length]= '\0';
83
86
  }
84
87
  else
85
88
  {
86
89
    if (to == startpos)
87
90
    {
88
 
      memcpy(buff, name, length); /* Save name for last copy */
 
91
      memmove(buff, name, length); /* Save name for last copy */
89
92
      name=buff;
90
93
    }
91
 
    pos=strmake(stpcpy(to,dev),name,length);
92
 
    (void) stpcpy(pos,ext);                     /* Don't convert extension */
 
94
    char *tmp= strcpy(to, dev) + strlen(dev);
 
95
    pos= strncpy(tmp,name,length) + length;
 
96
    (void) strcpy(pos,ext);                     /* Don't convert extension */
93
97
  }
94
98
  /*
95
99
    If MY_RETURN_REAL_PATH and MY_RESOLVE_SYMLINK is given, only do
96
100
    realpath if the file is a symbolic link
97
101
  */
98
102
  if (flag & MY_RETURN_REAL_PATH)
99
 
    (void) my_realpath(to, to, MYF(flag & MY_RESOLVE_SYMLINKS ?
100
 
                                   MY_RESOLVE_LINK: 0));
 
103
  {
 
104
    struct stat stat_buff;
 
105
    char rp_buff[PATH_MAX];
 
106
    if ((!flag & MY_RESOLVE_SYMLINKS) || 
 
107
       (!lstat(to,&stat_buff) && S_ISLNK(stat_buff.st_mode)))
 
108
    {
 
109
      if (!realpath(to,rp_buff))
 
110
        my_load_path(rp_buff, to, NULL);
 
111
      rp_buff[FN_REFLEN-1]= '\0';
 
112
      strcpy(to,rp_buff);
 
113
    }
 
114
  }
101
115
  else if (flag & MY_RESOLVE_SYMLINKS)
102
116
  {
103
 
    stpcpy(buff,to);
104
 
    (void) my_readlink(to, buff, MYF(0));
 
117
    strcpy(buff,to);
 
118
    ssize_t sym_link_size= readlink(buff,to,FN_REFLEN-1);
 
119
    if (sym_link_size >= 0)
 
120
      to[sym_link_size]= '\0';
105
121
  }
106
122
  return(to);
107
123
} /* fn_format */