1 | /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | |
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. |
6 | |
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. |
11 | |
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
15 | |
16 | #include "mysys_priv.h" |
17 | #include <m_string.h> |
18 | |
19 | /* |
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. |
24 | */ |
25 | |
26 | char * fn_format(char * to, const char *name, const char *dir, |
27 | const char *extension, uint flag) |
28 | { |
29 | char dev[FN_REFLEN], buff[FN_REFLEN], *pos, *startpos; |
30 | const char *ext; |
31 | reg1 size_t length; |
32 | size_t dev_length; |
33 | DBUG_ENTER("fn_format" ); |
34 | DBUG_ASSERT(name != NULL); |
35 | DBUG_ASSERT(extension != NULL); |
36 | DBUG_PRINT("enter" ,("name: %s dir: %s extension: %s flag: %d" , |
37 | name,dir,extension,flag)); |
38 | |
39 | /* Copy and skip directory */ |
40 | name+=(length=dirname_part(dev, (startpos=(char *) name), &dev_length)); |
41 | if (length == 0 || (flag & MY_REPLACE_DIR)) |
42 | { |
43 | /* Use given directory */ |
44 | convert_dirname(dev,dir,NullS); /* Fix to this OS */ |
45 | } |
46 | else if ((flag & MY_RELATIVE_PATH) && !test_if_hard_path(dev)) |
47 | { |
48 | /* Put 'dir' before the given path */ |
49 | strmake_buf(buff, dev); |
50 | pos=convert_dirname(dev,dir,NullS); |
51 | strmake(pos,buff,sizeof(buff)-1- (int) (pos-dev)); |
52 | } |
53 | |
54 | if (flag & MY_PACK_FILENAME) |
55 | pack_dirname(dev,dev); /* Put in ./.. and ~/.. */ |
56 | if (flag & MY_UNPACK_FILENAME) |
57 | (void) unpack_dirname(dev,dev); /* Replace ~/.. with dir */ |
58 | |
59 | if (!(flag & MY_APPEND_EXT) && |
60 | (pos= (char*) strchr(name,FN_EXTCHAR)) != NullS) |
61 | { |
62 | if ((flag & MY_REPLACE_EXT) == 0) /* If we should keep old ext */ |
63 | { |
64 | length=strlength(name); /* Use old extension */ |
65 | ext = "" ; |
66 | } |
67 | else |
68 | { |
69 | length= (size_t) (pos-(char*) name); /* Change extension */ |
70 | ext= extension; |
71 | } |
72 | } |
73 | else |
74 | { |
75 | length=strlength(name); /* No ext, use the now one */ |
76 | ext=extension; |
77 | } |
78 | |
79 | if (strlen(dev)+length+strlen(ext) >= FN_REFLEN || length >= FN_LEN ) |
80 | { |
81 | /* To long path, return original or NULL */ |
82 | size_t tmp_length; |
83 | if (flag & MY_SAFE_PATH) |
84 | DBUG_RETURN(NullS); |
85 | tmp_length= strlength(startpos); |
86 | DBUG_PRINT("error" ,("dev: '%s' ext: '%s' length: %u" ,dev,ext, |
87 | (uint) length)); |
88 | (void) strmake(to,startpos,MY_MIN(tmp_length,FN_REFLEN-1)); |
89 | } |
90 | else |
91 | { |
92 | if (to == startpos) |
93 | { |
94 | bmove(buff,(uchar*) name,length); /* Save name for last copy */ |
95 | name=buff; |
96 | } |
97 | pos=strmake(strmov(to,dev),name,length); |
98 | (void) strmov(pos,ext); /* Don't convert extension */ |
99 | } |
100 | if (flag & MY_RETURN_REAL_PATH) |
101 | (void) my_realpath(to, to, MYF(0)); |
102 | else if (flag & MY_RESOLVE_SYMLINKS) |
103 | { |
104 | strmov(buff,to); |
105 | (void) my_readlink(to, buff, MYF(0)); |
106 | } |
107 | DBUG_RETURN(to); |
108 | } /* fn_format */ |
109 | |
110 | |
111 | /* |
112 | strlength(const string str) |
113 | Return length of string with end-space:s not counted. |
114 | */ |
115 | |
116 | size_t strlength(const char *str) |
117 | { |
118 | reg1 const char * pos; |
119 | reg2 const char * found; |
120 | DBUG_ENTER("strlength" ); |
121 | |
122 | pos= found= str; |
123 | |
124 | while (*pos) |
125 | { |
126 | if (*pos != ' ') |
127 | { |
128 | while (*++pos && *pos != ' ') {}; |
129 | if (!*pos) |
130 | { |
131 | found=pos; /* String ends here */ |
132 | break; |
133 | } |
134 | } |
135 | found=pos; |
136 | while (*++pos == ' ') {}; |
137 | } |
138 | DBUG_RETURN((size_t) (found - str)); |
139 | } /* strlength */ |
140 | |