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"
17
#include <mystrings/m_string.h>
18
#include "drizzled/internal/my_sys.h"
22
#include "drizzled/internal/m_string.h"
18
23
#include "my_static.h"
28
30
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((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 (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 */
31
static size_t system_filename(char * to, const char *from);
108
34
remove unwanted chars from dirname
142
65
if ((pos=strrchr(from_ptr,FN_DEVCHAR)) != 0)
143
66
{ /* Skip device part */
144
67
length=(size_t) (pos-from_ptr)+1;
145
start=stpncpy(buff,from_ptr,length); from_ptr+=length;
68
start= strncpy(buff,from_ptr,length);
69
start+= strlen(from_ptr);
149
74
parent[0]=FN_LIBCHAR;
150
length=(size_t) (stpcpy(parent+1,FN_PARENTDIR)-parent);
75
length= (size_t)((strcpy(parent+1,FN_PARENTDIR)+strlen(FN_PARENTDIR))-parent);
151
76
for (pos=start ; (*pos= *from_ptr++) != 0 ; pos++)
153
78
#ifdef BACKSLASH_MBTAIL
155
80
if (use_mb(fs) && (l= my_ismbchar(fs, from_ptr - 1, from_ptr + 2)))
157
82
for (l-- ; l ; *++pos= *from_ptr++, l--);
177
102
pos+=length+1; /* Don't unpack ~/.. */
180
pos=stpcpy(buff,home_dir)-1; /* Unpacks ~/.. */
105
pos= strcpy(buff,home_dir)+strlen(home_dir)-1; /* Unpacks ~/.. */
181
106
if (*pos == FN_LIBCHAR)
182
107
pos--; /* home ended with '/' */
184
109
if (*pos == FN_CURLIB && (pos == start || pos[-1] == FN_LIBCHAR))
186
if (my_getwd(curr_dir,FN_REFLEN,MYF(0)))
111
if (getcwd(curr_dir,FN_REFLEN))
188
113
pos+=length+1; /* Don't unpack ./.. */
191
pos=stpcpy(buff,curr_dir)-1; /* Unpacks ./.. */
116
pos= strcpy(buff,curr_dir)+strlen(curr_dir)-1; /* Unpacks ./.. */
192
117
if (*pos == FN_LIBCHAR)
193
118
pos--; /* home ended with '/' */
226
(void) stpcpy(to,buff);
151
(void) strcpy(to,buff);
227
152
return((size_t) (pos-buff));
228
153
} /* cleanup_dirname */
232
157
On system where you don't have symbolic links, the following
233
code will allow you to create a file:
158
code will allow you to create a file:
234
159
directory-name.sym that should contain the real path
235
160
to the directory. This will be used if the directory name
240
165
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=strend(dir);
247
if (dir[0] && pos[-1] != FN_DEVCHAR && my_access(dir, F_OK))
251
char temp= *(--pos); /* May be "/" or "\" */
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
169
Fixes a directroy name so that can be used by open()
321
213
if (tilde_expansion[h_length-1] == FN_LIBCHAR)
323
215
if (buff+h_length < suffix)
324
memcpy(buff+h_length, suffix, length);
216
memmove(buff+h_length, suffix, length);
326
bmove_upp((uchar*) buff+h_length+length, (uchar*) suffix+length, length);
327
memcpy(buff, tilde_expansion, h_length);
218
bmove_upp((unsigned char*) buff+h_length+length, (unsigned char*) suffix+length, length);
219
memmove(buff, tilde_expansion, h_length);
335
223
return(system_filename(to,buff)); /* Fix for open */
336
224
} /* unpack_dirname */
344
232
if (path[0][0] == FN_LIBCHAR)
345
233
return home_dir; /* ~/ expanded to home */
235
struct passwd *user_entry;
237
if (!(str=strchr(*path,FN_LIBCHAR)))
238
str= strchr(*path, '\0');
239
save= *str; *str= '\0';
240
user_entry=getpwnam(*path);
349
struct passwd *user_entry;
351
if (!(str=strchr(*path,FN_LIBCHAR)))
353
save= *str; *str= '\0';
354
user_entry=getpwnam(*path);
360
return user_entry->pw_dir;
246
return user_entry->pw_dir;
391
275
n_length=unpack_dirname(buff,buff);
392
276
if (n_length+strlen(from+length) < FN_REFLEN)
394
(void) stpcpy(buff+n_length,from+length);
278
(void) strcpy(buff+n_length,from+length);
395
279
length= system_filename(to,buff); /* Fix to usably filename */
404
288
/* Used before system command's like open(), create() .. */
405
289
/* Returns used length of to; total length should be FN_REFLEN */
407
size_t system_filename(char * to, const char *from)
291
static size_t system_filename(char * to, const char *from)
409
#ifndef FN_C_BEFORE_DIR
410
return (size_t) (strmake(to,from,FN_REFLEN-1)-to);
413
/* change 'dev:lib/xxx' to 'dev:[lib]xxx' */
414
/* change 'dev:xxx' to 'dev:xxx' */
415
/* change './xxx' to 'xxx' */
416
/* change './lib/' or lib/ to '[.lib]' */
417
/* change '/x/y/z to '[x.y]x' */
418
/* change 'dev:/x' to 'dev:[000000]x' */
422
char * to_pos,from_pos,pos;
423
char buff[FN_REFLEN];
426
(void) stpcpy(buff,from); /* If to == from */
428
if ((pos=strrchr(from_pos,FN_DEVCHAR))) /* Skip device part */
431
to_pos=stpncpy(to,from_pos,(size_t) (pos-from_pos));
437
if (from_pos[0] == FN_CURLIB && from_pos[1] == FN_LIBCHAR)
438
from_pos+=2; /* Skip './' */
439
if (strchr(from_pos,FN_LIBCHAR))
441
*(to_pos++) = FN_C_BEFORE_DIR;
442
if (strstr(from_pos,FN_ROOTDIR) == from_pos)
444
from_pos+=strlen(FN_ROOTDIR); /* Actually +1 but... */
445
if (! strchr(from_pos,FN_LIBCHAR))
446
{ /* No dir, use [000000] */
447
to_pos=stpcpy(to_pos,FN_C_ROOT_DIR);
452
*(to_pos++)=FN_C_DIR_SEP; /* '.' gives current dir */
454
while ((pos=strchr(from_pos,FN_LIBCHAR)))
457
*(to_pos++)=FN_C_DIR_SEP; /* Add '.' between dirs */
458
if (strstr(from_pos,FN_PARENTDIR) == from_pos &&
459
from_pos+strlen(FN_PARENTDIR) == pos)
460
to_pos=stpcpy(to_pos,FN_C_PARENT_DIR); /* Found '../' */
462
to_pos=stpncpy(to_pos,from_pos,(size_t) (pos-from_pos));
465
*(to_pos++)=FN_C_AFTER_DIR;
467
length= (size_t) (stpcpy(to_pos,from_pos)-to);
293
return strlen(strncpy(to,from,FN_REFLEN-1));
470
294
} /* system_filename */
478
302
char buff[FN_REFLEN];
480
304
{ /* Dirname may destroy from */
484
308
length= dirname_part(to, from, &to_length); /* Copy dirname & fix chars */
485
(void) stpcpy(to + to_length,from+length);
309
(void) strcpy(to + to_length,from+length);
487
311
} /* intern_filename */
313
} /* namespace internal */
314
} /* namespace drizzled */