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>
18
18
#include "my_static.h"
28
23
static char * expand_tilde(char * *path);
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 */
34
void pack_dirname(char * to, const char *from)
37
size_t d_length,length,buff_length= 0;
41
(void) intern_filename(to,from); /* Change to intern name */
44
if ((start=strrchr(to,FN_DEVCHAR)) != 0) /* Skip device part */
50
if (!(cwd_err= my_getwd(buff,FN_REFLEN,MYF(0))))
52
buff_length= strlen(buff);
53
d_length= (size_t) (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);
62
if ((d_length= cleanup_dirname(to,to)) != 0)
67
length= strlen(home_dir);
68
if (home_dir[length-1] == FN_LIBCHAR)
69
length--; /* Don't test last '/' */
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)
75
to[0]=FN_HOMELIB; /* Filename begins with ~ */
76
(void) strmov_overlapp(to+1,to+length);
80
{ /* Test if cwd is ~/... */
81
if (length > 1 && length < buff_length)
83
if (memcmp(buff,home_dir,length) == 0 && buff[length] == FN_LIBCHAR)
86
(void) strmov_overlapp(buff+1,buff+length);
89
if (is_prefix(to,buff))
93
(void) strmov_overlapp(to,to+length); /* Remove everything before */
96
to[0]= FN_CURLIB; /* Put ./ instead of cwd */
108
27
remove unwanted chars from dirname
142
58
if ((pos=strrchr(from_ptr,FN_DEVCHAR)) != 0)
143
59
{ /* Skip device part */
144
60
length=(size_t) (pos-from_ptr)+1;
145
start=my_stpncpy(buff,from_ptr,length); from_ptr+=length;
61
start= strncpy(buff,from_ptr,length);
62
start+= strlen(from_ptr);
149
67
parent[0]=FN_LIBCHAR;
150
length=(size_t) (my_stpcpy(parent+1,FN_PARENTDIR)-parent);
68
length= (size_t)((strcpy(parent+1,FN_PARENTDIR)+strlen(FN_PARENTDIR))-parent);
151
69
for (pos=start ; (*pos= *from_ptr++) != 0 ; pos++)
153
71
#ifdef BACKSLASH_MBTAIL
177
95
pos+=length+1; /* Don't unpack ~/.. */
180
pos=my_stpcpy(buff,home_dir)-1; /* Unpacks ~/.. */
98
pos= strcpy(buff,home_dir)+strlen(home_dir)-1; /* Unpacks ~/.. */
181
99
if (*pos == FN_LIBCHAR)
182
100
pos--; /* home ended with '/' */
184
102
if (*pos == FN_CURLIB && (pos == start || pos[-1] == FN_LIBCHAR))
186
if (my_getwd(curr_dir,FN_REFLEN,MYF(0)))
104
if (getcwd(curr_dir,FN_REFLEN))
188
106
pos+=length+1; /* Don't unpack ./.. */
191
pos=my_stpcpy(buff,curr_dir)-1; /* Unpacks ./.. */
109
pos= strcpy(buff,curr_dir)+strlen(curr_dir)-1; /* Unpacks ./.. */
192
110
if (*pos == FN_LIBCHAR)
193
111
pos--; /* home ended with '/' */
226
(void) my_stpcpy(to,buff);
144
(void) strcpy(to,buff);
227
145
return((size_t) (pos-buff));
228
146
} /* cleanup_dirname */
232
150
On system where you don't have symbolic links, the following
233
code will allow you to create a file:
151
code will allow you to create a file:
234
152
directory-name.sym that should contain the real path
235
153
to the directory. This will be used if the directory name
240
158
bool my_use_symdir=0; /* Set this if you want to use symdirs */
243
void symdirget(char *dir)
245
char buff[FN_REFLEN];
246
char *pos= strchr(dir, '\0');
247
if (dir[0] && pos[-1] != FN_DEVCHAR && my_access(dir, F_OK))
251
char temp= *(--pos); /* May be "/" or "\" */
252
my_stpcpy(pos,".sym");
253
file= my_open(dir, O_RDONLY, MYF(0));
254
*pos++=temp; *pos=0; /* Restore old filename */
257
if ((length= my_read(file, buff, sizeof(buff), MYF(0))) > 0)
259
for (pos= buff + length ;
260
pos > buff && (iscntrl(pos[-1]) || isspace(pos[-1])) ;
263
/* Ensure that the symlink ends with the directory symbol */
264
if (pos == buff || pos[-1] != FN_LIBCHAR)
267
strmake(dir,buff, (size_t) (pos-buff));
269
my_close(file, MYF(0));
273
#endif /* USE_SYMDIR */
277
162
Fixes a directroy name so that can be used by open()
321
206
if (tilde_expansion[h_length-1] == FN_LIBCHAR)
323
208
if (buff+h_length < suffix)
324
memcpy(buff+h_length, suffix, length);
209
memmove(buff+h_length, suffix, length);
326
211
bmove_upp((unsigned char*) buff+h_length+length, (unsigned char*) suffix+length, length);
327
memcpy(buff, tilde_expansion, h_length);
212
memmove(buff, tilde_expansion, h_length);
335
216
return(system_filename(to,buff)); /* Fix for open */
336
217
} /* unpack_dirname */
391
272
n_length=unpack_dirname(buff,buff);
392
273
if (n_length+strlen(from+length) < FN_REFLEN)
394
(void) my_stpcpy(buff+n_length,from+length);
275
(void) strcpy(buff+n_length,from+length);
395
276
length= system_filename(to,buff); /* Fix to usably filename */
407
288
size_t system_filename(char * to, const char *from)
409
290
#ifndef FN_C_BEFORE_DIR
410
return (size_t) (strmake(to,from,FN_REFLEN-1)-to);
291
return strlen(strncpy(to,from,FN_REFLEN-1));
413
294
/* change 'dev:lib/xxx' to 'dev:[lib]xxx' */
423
304
char buff[FN_REFLEN];
426
(void) my_stpcpy(buff,from); /* If to == from */
307
(void) strcpy(buff,from); /* If to == from */
428
309
if ((pos=strrchr(from_pos,FN_DEVCHAR))) /* Skip device part */
431
to_pos=my_stpncpy(to,from_pos,(size_t) (pos-from_pos));
312
to_pos= strncpy(to,from_pos,(size_t) (pos-from_pos));
444
326
from_pos+=strlen(FN_ROOTDIR); /* Actually +1 but... */
445
327
if (! strchr(from_pos,FN_LIBCHAR))
446
328
{ /* No dir, use [000000] */
447
to_pos=my_stpcpy(to_pos,FN_C_ROOT_DIR);
329
to_pos= strcpy(to_pos,FN_C_ROOT_DIR)+strlen(FN_C_ROOT_DIR);
454
336
while ((pos=strchr(from_pos,FN_LIBCHAR)))
456
338
if (libchar_found++)
457
*(to_pos++)=FN_C_DIR_SEP; /* Add '.' between dirs */
339
*(to_pos++)=FN_C_DIR_SEP; /* Add '.' between dirs */
458
340
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 '../' */
341
from_pos+strlen(FN_PARENTDIR) == pos) {
342
to_pos= strcpy(to_pos,FN_C_PARENT_DIR); /* Found '../' */
343
to_pos+= strlen(FN_C_PARENT_DIR);
462
to_pos=my_stpncpy(to_pos,from_pos,(size_t) (pos-from_pos));
347
to_pos= strncpy(to_pos,from_pos,(size_t) (pos-from_pos));
348
to_pos+= strlen(to_pos);
465
352
*(to_pos++)=FN_C_AFTER_DIR;
467
length= (size_t) (my_stpcpy(to_pos,from_pos)-to);
355
strcpy(to_pos, from_pos);
470
359
} /* system_filename */