12
12
You should have received a copy of the GNU General Public License
13
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 */
18
#include "drizzled/internal/my_sys.h"
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 "my_static.h"
22
#include "drizzled/internal/m_string.h"
23
#include "my_static.h"
30
28
static char * expand_tilde(char * *path);
31
static size_t system_filename(char * to, const char *from);
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 */
34
108
remove unwanted chars from dirname
65
142
if ((pos=strrchr(from_ptr,FN_DEVCHAR)) != 0)
66
143
{ /* Skip device part */
67
144
length=(size_t) (pos-from_ptr)+1;
68
start= strncpy(buff,from_ptr,length);
69
start+= strlen(from_ptr);
145
start=my_stpncpy(buff,from_ptr,length); from_ptr+=length;
74
149
parent[0]=FN_LIBCHAR;
75
length= (size_t)((strcpy(parent+1,FN_PARENTDIR)+strlen(FN_PARENTDIR))-parent);
150
length=(size_t) (my_stpcpy(parent+1,FN_PARENTDIR)-parent);
76
151
for (pos=start ; (*pos= *from_ptr++) != 0 ; pos++)
78
153
#ifdef BACKSLASH_MBTAIL
102
177
pos+=length+1; /* Don't unpack ~/.. */
105
pos= strcpy(buff,home_dir)+strlen(home_dir)-1; /* Unpacks ~/.. */
180
pos=my_stpcpy(buff,home_dir)-1; /* Unpacks ~/.. */
106
181
if (*pos == FN_LIBCHAR)
107
182
pos--; /* home ended with '/' */
109
184
if (*pos == FN_CURLIB && (pos == start || pos[-1] == FN_LIBCHAR))
111
if (getcwd(curr_dir,FN_REFLEN))
186
if (my_getwd(curr_dir,FN_REFLEN,MYF(0)))
113
188
pos+=length+1; /* Don't unpack ./.. */
116
pos= strcpy(buff,curr_dir)+strlen(curr_dir)-1; /* Unpacks ./.. */
191
pos=my_stpcpy(buff,curr_dir)-1; /* Unpacks ./.. */
117
192
if (*pos == FN_LIBCHAR)
118
193
pos--; /* home ended with '/' */
151
(void) strcpy(to,buff);
226
(void) my_stpcpy(to,buff);
152
227
return((size_t) (pos-buff));
153
228
} /* cleanup_dirname */
157
232
On system where you don't have symbolic links, the following
158
code will allow you to create a file:
233
code will allow you to create a file:
159
234
directory-name.sym that should contain the real path
160
235
to the directory. This will be used if the directory name
165
240
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 */
169
277
Fixes a directroy name so that can be used by open()
213
321
if (tilde_expansion[h_length-1] == FN_LIBCHAR)
215
323
if (buff+h_length < suffix)
216
memmove(buff+h_length, suffix, length);
324
memcpy(buff+h_length, suffix, length);
218
326
bmove_upp((unsigned char*) buff+h_length+length, (unsigned char*) suffix+length, length);
219
memmove(buff, tilde_expansion, h_length);
327
memcpy(buff, tilde_expansion, h_length);
223
335
return(system_filename(to,buff)); /* Fix for open */
224
336
} /* unpack_dirname */
232
344
if (path[0][0] == FN_LIBCHAR)
233
345
return home_dir; /* ~/ expanded to home */
235
struct passwd *user_entry;
349
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);
246
return user_entry->pw_dir;
351
if (!(str=strchr(*path,FN_LIBCHAR)))
352
str= strchr(*path, '\0');
353
save= *str; *str= '\0';
354
user_entry=getpwnam(*path);
360
return user_entry->pw_dir;
288
404
/* Used before system command's like open(), create() .. */
289
405
/* Returns used length of to; total length should be FN_REFLEN */
291
static size_t system_filename(char * to, const char *from)
407
size_t system_filename(char * to, const char *from)
293
return strlen(strncpy(to,from,FN_REFLEN-1));
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) my_stpcpy(buff,from); /* If to == from */
428
if ((pos=strrchr(from_pos,FN_DEVCHAR))) /* Skip device part */
431
to_pos=my_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=my_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=my_stpcpy(to_pos,FN_C_PARENT_DIR); /* Found '../' */
462
to_pos=my_stpncpy(to_pos,from_pos,(size_t) (pos-from_pos));
465
*(to_pos++)=FN_C_AFTER_DIR;
467
length= (size_t) (my_stpcpy(to_pos,from_pos)-to);
294
470
} /* system_filename */
302
478
char buff[FN_REFLEN];
304
480
{ /* Dirname may destroy from */
481
my_stpcpy(buff,from);
308
484
length= dirname_part(to, from, &to_length); /* Copy dirname & fix chars */
309
(void) strcpy(to + to_length,from+length);
485
(void) my_stpcpy(to + to_length,from+length);
311
487
} /* intern_filename */
313
} /* namespace internal */
314
} /* namespace drizzled */