1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "mysys_priv.h"
20
Formats a filename with possible replace of directory of extension
21
Function can handle the case where 'to' == 'name'
22
For a description of the flag values, consult my_sys.h
23
The arguments should be in unix format.
26
char * fn_format(char * to, const char *name, const char *dir,
27
const char *extension, uint flag)
29
char dev[FN_REFLEN], buff[FN_REFLEN], *pos, *startpos;
31
register size_t length;
33
DBUG_ENTER("fn_format");
34
DBUG_PRINT("enter",("name: %s dir: %s extension: %s flag: %d",
35
name,dir,extension,flag));
37
/* Copy and skip directory */
38
name+=(length=dirname_part(dev, (startpos=(char *) name), &dev_length));
39
if (length == 0 || (flag & MY_REPLACE_DIR))
41
/* Use given directory */
42
convert_dirname(dev,dir,NullS); /* Fix to this OS */
44
else if ((flag & MY_RELATIVE_PATH) && !test_if_hard_path(dev))
46
/* Put 'dir' before the given path */
47
strmake(buff,dev,sizeof(buff)-1);
48
pos=convert_dirname(dev,dir,NullS);
49
strmake(pos,buff,sizeof(buff)-1- (int) (pos-dev));
52
if (flag & MY_PACK_FILENAME)
53
pack_dirname(dev,dev); /* Put in ./.. and ~/.. */
54
if (flag & MY_UNPACK_FILENAME)
55
(void) unpack_dirname(dev,dev); /* Replace ~/.. with dir */
57
if (!(flag & MY_APPEND_EXT) &&
58
(pos= (char*) strchr(name,FN_EXTCHAR)) != NullS)
60
if ((flag & MY_REPLACE_EXT) == 0) /* If we should keep old ext */
62
length=strlength(name); /* Use old extension */
67
length= (size_t) (pos-(char*) name); /* Change extension */
73
length=strlength(name); /* No ext, use the now one */
77
if (strlen(dev)+length+strlen(ext) >= FN_REFLEN || length >= FN_LEN )
79
/* To long path, return original or NULL */
81
if (flag & MY_SAFE_PATH)
83
tmp_length= strlength(startpos);
84
DBUG_PRINT("error",("dev: '%s' ext: '%s' length: %u",dev,ext,
86
(void) strmake(to,startpos,min(tmp_length,FN_REFLEN-1));
92
bmove(buff,(uchar*) name,length); /* Save name for last copy */
95
pos=strmake(strmov(to,dev),name,length);
96
(void) strmov(pos,ext); /* Don't convert extension */
99
If MY_RETURN_REAL_PATH and MY_RESOLVE_SYMLINK is given, only do
100
realpath if the file is a symbolic link
102
if (flag & MY_RETURN_REAL_PATH)
103
(void) my_realpath(to, to, MYF(flag & MY_RESOLVE_SYMLINKS ?
104
MY_RESOLVE_LINK: 0));
105
else if (flag & MY_RESOLVE_SYMLINKS)
108
(void) my_readlink(to, buff, MYF(0));
115
strlength(const string str)
116
Return length of string with end-space:s not counted.
119
size_t strlength(const char *str)
121
register const char * pos;
122
register const char * found;
123
DBUG_ENTER("strlength");
131
while (*++pos && *pos != ' ') {};
134
found=pos; /* String ends here */
139
while (*++pos == ' ') {};
141
DBUG_RETURN((size_t) (found - str));