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"
27
static char * expand_tilde(char * *path);
29
/* Pack a dirname ; Changes HOME to ~/ and current dev to ./ */
30
/* from is a dirname (from dirname() ?) ending with FN_LIBCHAR */
31
/* to may be == from */
33
void pack_dirname(char * to, const char *from)
36
size_t d_length,length,buff_length= 0;
39
DBUG_ENTER("pack_dirname");
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 && !bcmp(buff,start,d_length))) &&
56
*start != FN_LIBCHAR && *start)
57
{ /* Put current dir before */
58
bchange((uchar*) to, d_length, (uchar*) 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 (bcmp(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 (bcmp(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 */
103
DBUG_PRINT("exit",("to: '%s'",to));
109
remove unwanted chars from dirname
114
from Dirname to fix. May be same as to
117
"/../" removes prev dir
118
"/~/" removes all before ~
119
//" is same as "/", except on Win32 at start of a file
121
Unpacks home_dir if "~/.." used
122
Unpacks current dir if if "./.." used
128
size_t cleanup_dirname(register char *to, const char *from)
130
register size_t length;
132
register char * from_ptr;
133
register char * start;
134
char parent[5], /* for "FN_PARENTDIR" */
135
buff[FN_REFLEN+1],*end_parentdir;
136
#ifdef BACKSLASH_MBTAIL
137
CHARSET_INFO *fs= fs_character_set();
139
DBUG_ENTER("cleanup_dirname");
140
DBUG_PRINT("enter",("from: '%s'",from));
143
from_ptr=(char *) from;
145
if ((pos=strrchr(from_ptr,FN_DEVCHAR)) != 0)
146
{ /* Skip device part */
147
length=(size_t) (pos-from_ptr)+1;
148
start=strnmov(buff,from_ptr,length); from_ptr+=length;
152
parent[0]=FN_LIBCHAR;
153
length=(size_t) (strmov(parent+1,FN_PARENTDIR)-parent);
154
for (pos=start ; (*pos= *from_ptr++) != 0 ; pos++)
156
#ifdef BACKSLASH_MBTAIL
158
if (use_mb(fs) && (l= my_ismbchar(fs, from_ptr - 1, from_ptr + 2)))
160
for (l-- ; l ; *++pos= *from_ptr++, l--);
161
start= pos + 1; /* Don't look inside multi-byte char */
167
if (*pos == FN_LIBCHAR)
169
if ((size_t) (pos-start) > length && bcmp(pos-length,parent,length) == 0)
170
{ /* If .../../; skip prev */
175
if (*pos == FN_HOMELIB && (pos == start || pos[-1] == FN_LIBCHAR))
179
pos+=length+1; /* Don't unpack ~/.. */
182
pos=strmov(buff,home_dir)-1; /* Unpacks ~/.. */
183
if (*pos == FN_LIBCHAR)
184
pos--; /* home ended with '/' */
186
if (*pos == FN_CURLIB && (pos == start || pos[-1] == FN_LIBCHAR))
188
if (my_getwd(curr_dir,FN_REFLEN,MYF(0)))
190
pos+=length+1; /* Don't unpack ./.. */
193
pos=strmov(buff,curr_dir)-1; /* Unpacks ./.. */
194
if (*pos == FN_LIBCHAR)
195
pos--; /* home ended with '/' */
198
while (pos >= start && *pos != FN_LIBCHAR) /* remove prev dir */
200
if (pos[1] == FN_HOMELIB || bcmp(pos,parent,length) == 0)
201
{ /* Don't remove ~user/ */
202
pos=strmov(end_parentdir+1,parent);
208
else if ((size_t) (pos-start) == length-1 &&
209
!bcmp(start,parent+1,length-1))
210
start=pos; /* Starts with "../" */
211
else if (pos-start > 0 && pos[-1] == FN_LIBCHAR)
213
#ifdef FN_NETWORK_DRIVES
216
pos--; /* Remove dupplicate '/' */
218
else if (pos-start > 1 && pos[-1] == FN_CURLIB && pos[-2] == FN_LIBCHAR)
219
pos-=2; /* Skip /./ */
220
else if (pos > buff+1 && pos[-1] == FN_HOMELIB && pos[-2] == FN_LIBCHAR)
221
{ /* Found ..../~/ */
224
start=buff; pos=buff+1;
228
(void) strmov(to,buff);
229
DBUG_PRINT("exit",("to: '%s'",to));
230
DBUG_RETURN((size_t) (pos-buff));
231
} /* cleanup_dirname */
235
On system where you don't have symbolic links, the following
236
code will allow you to create a file:
237
directory-name.sym that should contain the real path
238
to the directory. This will be used if the directory name
243
my_bool my_use_symdir=0; /* Set this if you want to use symdirs */
246
void symdirget(char *dir)
248
char buff[FN_REFLEN];
249
char *pos=strend(dir);
250
if (dir[0] && pos[-1] != FN_DEVCHAR && my_access(dir, F_OK))
254
char temp= *(--pos); /* May be "/" or "\" */
256
file= my_open(dir, O_RDONLY, MYF(0));
257
*pos++=temp; *pos=0; /* Restore old filename */
260
if ((length= my_read(file, buff, sizeof(buff), MYF(0))) > 0)
262
for (pos= buff + length ;
263
pos > buff && (iscntrl(pos[-1]) || isspace(pos[-1])) ;
266
/* Ensure that the symlink ends with the directory symbol */
267
if (pos == buff || pos[-1] != FN_LIBCHAR)
270
strmake(dir,buff, (size_t) (pos-buff));
272
my_close(file, MYF(0));
276
#endif /* USE_SYMDIR */
280
Fixes a directroy name so that can be used by open()
284
to result-buffer, FN_REFLEN characters. may be == from
285
from 'Packed' directory name (may contain ~)
288
Make that last char of to is '/' if from not empty and
289
from doesn't end in FN_DEVCHAR
290
Uses cleanup_dirname and changes ~/.. to home_dir/..
292
Changes a UNIX filename to system filename (replaces / with \ on windows)
295
Length of new directory name (= length of to)
298
size_t unpack_dirname(char * to, const char *from)
300
size_t length, h_length;
301
char buff[FN_REFLEN+1+4],*suffix,*tilde_expansion;
302
DBUG_ENTER("unpack_dirname");
304
(void) intern_filename(buff,from); /* Change to intern name */
305
length= strlen(buff); /* Fix that '/' is last */
308
buff[length-1] != FN_DEVCHAR &&
310
buff[length-1] != FN_LIBCHAR && buff[length-1] != '/')
312
buff[length]=FN_LIBCHAR;
313
buff[length+1]= '\0';
316
length=cleanup_dirname(buff,buff);
317
if (buff[0] == FN_HOMELIB)
319
suffix=buff+1; tilde_expansion=expand_tilde(&suffix);
322
length-= (size_t) (suffix-buff)-1;
323
if (length+(h_length= strlen(tilde_expansion)) <= FN_REFLEN)
325
if (tilde_expansion[h_length-1] == FN_LIBCHAR)
327
if (buff+h_length < suffix)
328
bmove(buff+h_length,suffix,length);
330
bmove_upp((uchar*) buff+h_length+length, (uchar*) suffix+length, length);
331
bmove(buff,tilde_expansion,h_length);
339
DBUG_RETURN(system_filename(to,buff)); /* Fix for open */
340
} /* unpack_dirname */
343
/* Expand tilde to home or user-directory */
344
/* Path is reset to point at FN_LIBCHAR after ~xxx */
346
static char * expand_tilde(char * *path)
348
if (path[0][0] == FN_LIBCHAR)
349
return home_dir; /* ~/ expanded to home */
353
struct passwd *user_entry;
355
if (!(str=strchr(*path,FN_LIBCHAR)))
357
save= *str; *str= '\0';
358
user_entry=getpwnam(*path);
364
return user_entry->pw_dir;
373
Fix filename so it can be used by open, create
377
to Store result here. Must be at least of size FN_REFLEN.
378
from Filename in unix format (with ~)
385
~ will only be expanded if total length < FN_REFLEN
389
size_t unpack_filename(char * to, const char *from)
391
size_t length, n_length, buff_length;
392
char buff[FN_REFLEN];
393
DBUG_ENTER("unpack_filename");
395
length=dirname_part(buff, from, &buff_length);/* copy & convert dirname */
396
n_length=unpack_dirname(buff,buff);
397
if (n_length+strlen(from+length) < FN_REFLEN)
399
(void) strmov(buff+n_length,from+length);
400
length= system_filename(to,buff); /* Fix to usably filename */
403
length= system_filename(to,from); /* Fix to usably filename */
405
} /* unpack_filename */
408
/* Convert filename (unix standard) to system standard */
409
/* Used before system command's like open(), create() .. */
410
/* Returns used length of to; total length should be FN_REFLEN */
412
size_t system_filename(char * to, const char *from)
414
#ifndef FN_C_BEFORE_DIR
415
return (size_t) (strmake(to,from,FN_REFLEN-1)-to);
418
/* change 'dev:lib/xxx' to 'dev:[lib]xxx' */
419
/* change 'dev:xxx' to 'dev:xxx' */
420
/* change './xxx' to 'xxx' */
421
/* change './lib/' or lib/ to '[.lib]' */
422
/* change '/x/y/z to '[x.y]x' */
423
/* change 'dev:/x' to 'dev:[000000]x' */
427
char * to_pos,from_pos,pos;
428
char buff[FN_REFLEN];
429
DBUG_ENTER("system_filename");
432
(void) strmov(buff,from); /* If to == from */
434
if ((pos=strrchr(from_pos,FN_DEVCHAR))) /* Skip device part */
437
to_pos=strnmov(to,from_pos,(size_t) (pos-from_pos));
443
if (from_pos[0] == FN_CURLIB && from_pos[1] == FN_LIBCHAR)
444
from_pos+=2; /* Skip './' */
445
if (strchr(from_pos,FN_LIBCHAR))
447
*(to_pos++) = FN_C_BEFORE_DIR;
448
if (strinstr(from_pos,FN_ROOTDIR) == 1)
450
from_pos+=strlen(FN_ROOTDIR); /* Actually +1 but... */
451
if (! strchr(from_pos,FN_LIBCHAR))
452
{ /* No dir, use [000000] */
453
to_pos=strmov(to_pos,FN_C_ROOT_DIR);
458
*(to_pos++)=FN_C_DIR_SEP; /* '.' gives current dir */
460
while ((pos=strchr(from_pos,FN_LIBCHAR)))
463
*(to_pos++)=FN_C_DIR_SEP; /* Add '.' between dirs */
464
if (strinstr(from_pos,FN_PARENTDIR) == 1 &&
465
from_pos+strlen(FN_PARENTDIR) == pos)
466
to_pos=strmov(to_pos,FN_C_PARENT_DIR); /* Found '../' */
468
to_pos=strnmov(to_pos,from_pos,(size_t) (pos-from_pos));
471
*(to_pos++)=FN_C_AFTER_DIR;
473
length= (size_t) (strmov(to_pos,from_pos)-to);
474
DBUG_PRINT("exit",("name: '%s'",to));
477
} /* system_filename */
480
/* Fix a filename to intern (UNIX format) */
482
char *intern_filename(char *to, const char *from)
484
size_t length, to_length;
485
char buff[FN_REFLEN];
487
{ /* Dirname may destroy from */
491
length= dirname_part(to, from, &to_length); /* Copy dirname & fix chars */
492
(void) strmov(to + to_length,from+length);
494
} /* intern_filename */