17 #include "mysys_priv.h"
23 static char * expand_tilde(
char **path);
29 void pack_dirname(
char *
to,
const char *from)
32 size_t d_length,length,UNINIT_VAR(buff_length);
35 DBUG_ENTER(
"pack_dirname");
37 (void) intern_filename(to,from);
40 if ((start=strrchr(to,FN_DEVCHAR)) != 0)
46 if (!(cwd_err= my_getwd(buff,FN_REFLEN,MYF(0))))
48 buff_length= strlen(buff);
49 d_length= (size_t) (start-to);
51 (buff_length == d_length && !memcmp(buff,start,d_length))) &&
52 *start != FN_LIBCHAR && *start)
54 bchange((uchar*) to, d_length, (uchar*) buff, buff_length, strlen(to)+1);
58 if ((d_length= cleanup_dirname(to,to)) != 0)
63 length= strlen(home_dir);
64 if (home_dir[length-1] == FN_LIBCHAR)
67 if (length > 1 && length < d_length)
69 if (memcmp(to,home_dir,length) == 0 && to[length] == FN_LIBCHAR)
72 (void) strmov_overlapp(to+1,to+length);
77 if (length > 1 && length < buff_length)
79 if (memcmp(buff,home_dir,length) == 0 && buff[length] == FN_LIBCHAR)
82 (void) strmov_overlapp(buff+1,buff+length);
85 if (is_prefix(to,buff))
89 (void) strmov_overlapp(to,to+length);
99 DBUG_PRINT(
"exit",(
"to: '%s'",to));
124 size_t cleanup_dirname(
register char *to,
const char *from)
128 reg3
char * from_ptr;
131 buff[FN_REFLEN+1],*end_parentdir;
132 #ifdef BACKSLASH_MBTAIL
135 DBUG_ENTER(
"cleanup_dirname");
136 DBUG_PRINT(
"enter",(
"from: '%s'",from));
139 from_ptr=(
char *) from;
141 if ((pos=strrchr(from_ptr,FN_DEVCHAR)) != 0)
143 length=(size_t) (pos-from_ptr)+1;
144 start=strnmov(buff,from_ptr,length); from_ptr+=length;
148 parent[0]=FN_LIBCHAR;
149 length=(size_t) (strmov(parent+1,FN_PARENTDIR)-parent);
150 for (pos=start ; (*pos= *from_ptr++) != 0 ; pos++)
152 #ifdef BACKSLASH_MBTAIL
154 if (use_mb(fs) && (l= my_ismbchar(fs, from_ptr - 1, from_ptr + 2)))
156 for (l-- ; l ; *++pos= *from_ptr++, l--);
163 if (*pos == FN_LIBCHAR)
165 if ((
size_t) (pos-start) > length && memcmp(pos-length,parent,length) == 0)
171 if (*pos == FN_HOMELIB && (pos == start || pos[-1] == FN_LIBCHAR))
178 pos=strmov(buff,home_dir)-1;
179 if (*pos == FN_LIBCHAR)
182 if (*pos == FN_CURLIB && (pos == start || pos[-1] == FN_LIBCHAR))
184 if (my_getwd(curr_dir,FN_REFLEN,MYF(0)))
189 pos=strmov(buff,curr_dir)-1;
190 if (*pos == FN_LIBCHAR)
194 while (pos >= start && *pos != FN_LIBCHAR)
196 if (pos[1] == FN_HOMELIB ||
197 (pos >= start && memcmp(pos, parent, length) == 0))
199 pos=strmov(end_parentdir+1,parent);
205 else if ((
size_t) (pos-start) == length-1 &&
206 !memcmp(start,parent+1,length-1))
208 else if (pos-start > 0 && pos[-1] == FN_LIBCHAR)
210 #ifdef FN_NETWORK_DRIVES
215 else if (pos-start > 1 && pos[-1] == FN_CURLIB && pos[-2] == FN_LIBCHAR)
217 else if (pos > buff+1 && pos[-1] == FN_HOMELIB && pos[-2] == FN_LIBCHAR)
221 start=buff; pos=buff+1;
225 (void) strmov(to,buff);
226 DBUG_PRINT(
"exit",(
"to: '%s'",to));
227 DBUG_RETURN((
size_t) (pos-buff));
240 my_bool my_use_symdir=0;
243 void symdirget(
char *dir, my_bool *is_symdir)
245 char buff[FN_REFLEN+1];
246 char *pos=strend(dir);
247 if (dir[0] && pos[-1] != FN_DEVCHAR && my_access(dir, F_OK))
253 file= my_open(dir, O_RDONLY, MYF(0));
257 if ((length= my_read(file, buff,
sizeof(buff) - 1, MYF(0))) > 0)
259 for (pos= buff + length ;
260 pos > buff && (iscntrl(pos[-1]) || isspace(pos[-1])) ;
264 if (pos == buff || pos[-1] != FN_LIBCHAR)
267 strmake(dir,buff, (
size_t) (pos-buff));
271 my_close(file, MYF(0));
294 size_t normalize_dirname(
char *to,
const char *from)
297 char buff[FN_REFLEN];
298 DBUG_ENTER(
"normalize_dirname");
304 (void) intern_filename(buff, from);
305 length= strlen(buff);
308 buff[length - 1] != FN_DEVCHAR &&
310 buff[length - 1] != FN_LIBCHAR && buff[length - 1] !=
'/')
313 if (length >=
sizeof (buff) - 1)
314 length=
sizeof (buff) - 2;
315 buff[length]= FN_LIBCHAR;
316 buff[length + 1]=
'\0';
319 length=cleanup_dirname(to, buff);
344 size_t unpack_dirname(
char * to,
const char *from, my_bool *is_symdir)
346 size_t length, h_length;
347 char buff[FN_REFLEN+1+4],*suffix,*tilde_expansion;
348 DBUG_ENTER(
"unpack_dirname");
352 length= normalize_dirname(buff, from);
354 if (buff[0] == FN_HOMELIB)
356 suffix=buff+1; tilde_expansion=expand_tilde(&suffix);
359 length-= (size_t) (suffix-buff)-1;
360 if (length+(h_length= strlen(tilde_expansion)) <= FN_REFLEN)
362 if ((h_length > 0) && (tilde_expansion[h_length-1] == FN_LIBCHAR))
364 if (buff+h_length < suffix)
365 bmove(buff+h_length,suffix,length);
367 bmove_upp((uchar*) buff+h_length+length, (uchar*) suffix+length, length);
368 bmove(buff,tilde_expansion,h_length);
374 symdirget(buff, is_symdir);
376 DBUG_RETURN(system_filename(to,buff));
383 static char * expand_tilde(
char **path)
385 if (path[0][0] == FN_LIBCHAR)
390 struct passwd *user_entry;
392 if (!(str=strchr(*path,FN_LIBCHAR)))
394 save= *str; *str=
'\0';
395 user_entry=getpwnam(*path);
401 return user_entry->pw_dir;
426 size_t unpack_filename(
char * to,
const char *from)
428 size_t length, n_length, buff_length;
429 char buff[FN_REFLEN];
431 DBUG_ENTER(
"unpack_filename");
433 length=dirname_part(buff, from, &buff_length);
434 n_length=unpack_dirname(buff, buff, ¬_used);
435 if (n_length+strlen(from+length) < FN_REFLEN)
437 (void) strmov(buff+n_length,from+length);
438 length= system_filename(to,buff);
441 length= system_filename(to,from);
450 size_t system_filename(
char *to,
const char *from)
452 return (
size_t) (strmake(to,from,FN_REFLEN-1)-
to);
457 char *intern_filename(
char *to,
const char *from)
459 size_t length, to_length;
460 char buff[FN_REFLEN];
463 (void) strnmov(buff, from, FN_REFLEN);
466 length= dirname_part(to, from, &to_length);
467 (void) strnmov(to + to_length, from + length, FN_REFLEN - to_length);