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 */
16
#include "mysys_priv.h"
16
#include "mysys/mysys_priv.h"
17
17
#include <mystrings/m_string.h>
20
24
Formats a filename with possible replace of directory of extension
21
25
Function can handle the case where 'to' == 'name'
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)
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))
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 */
42
46
else if ((flag & MY_RELATIVE_PATH) && !test_if_hard_path(dev))
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));
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 */
55
57
if (!(flag & MY_APPEND_EXT) &&
56
(pos= (char*) strchr(name,FN_EXTCHAR)) != NullS)
58
(pos= (char*) strchr(name,FN_EXTCHAR)) != NULL)
58
60
if ((flag & MY_REPLACE_EXT) == 0) /* If we should keep old ext */
77
79
/* To long path, return original or NULL */
79
81
if (flag & MY_SAFE_PATH)
81
tmp_length= strlength(startpos);
82
(void) strmake(to,startpos,min(tmp_length,FN_REFLEN-1));
83
tmp_length= min(strlength(startpos), (size_t)(FN_REFLEN-1));
84
strncpy(to,startpos,tmp_length);
86
89
if (to == startpos)
88
memcpy(buff, name, length); /* Save name for last copy */
91
memmove(buff, name, length); /* Save name for last copy */
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 */
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
98
102
if (flag & MY_RETURN_REAL_PATH)
99
(void) my_realpath(to, to, MYF(flag & MY_RESOLVE_SYMLINKS ?
100
MY_RESOLVE_LINK: 0));
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)))
109
if (!realpath(to,rp_buff))
110
my_load_path(rp_buff, to, NULL);
111
rp_buff[FN_REFLEN-1]= '\0';
101
115
else if (flag & MY_RESOLVE_SYMLINKS)
104
(void) my_readlink(to, buff, MYF(0));
118
ssize_t sym_link_size= readlink(buff,to,FN_REFLEN-1);
119
if (sym_link_size >= 0)
120
to[sym_link_size]= '\0';
107
123
} /* fn_format */