1
by brian
clean slate |
1 |
/* Copyright (C) 2000 MySQL AB
|
2 |
||
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.
|
|
6 |
||
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.
|
|
11 |
||
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 */
|
|
15 |
||
16 |
/* my_setwd() and my_getwd() works with intern_filenames !! */
|
|
17 |
||
18 |
#include "mysys_priv.h" |
|
19 |
#include <m_string.h> |
|
20 |
#include "mysys_err.h" |
|
21 |
#ifdef HAVE_GETWD
|
|
22 |
#include <sys/param.h> |
|
23 |
#endif
|
|
24 |
||
25 |
/* Gets current working directory in buff.
|
|
26 |
||
27 |
SYNPOSIS
|
|
28 |
my_getwd()
|
|
29 |
buf Buffer to store result. Can be curr_dir[].
|
|
30 |
size Size of buffer
|
|
31 |
MyFlags Flags
|
|
32 |
||
33 |
NOTES
|
|
34 |
Directory is allways ended with FN_LIBCHAR
|
|
35 |
||
36 |
RESULT
|
|
37 |
0 ok
|
|
38 |
# error
|
|
39 |
*/
|
|
40 |
||
41 |
int my_getwd(char * buf, size_t size, myf MyFlags) |
|
42 |
{
|
|
43 |
char * pos; |
|
44 |
DBUG_ENTER("my_getwd"); |
|
45 |
DBUG_PRINT("my",("buf: 0x%lx size: %u MyFlags %d", |
|
46 |
(long) buf, (uint) size, MyFlags)); |
|
47 |
||
48 |
if (curr_dir[0]) /* Current pos is saved here */ |
|
49 |
VOID(strmake(buf,&curr_dir[0],size-1)); |
|
50 |
else
|
|
51 |
{
|
|
52 |
#if defined(HAVE_GETCWD)
|
|
53 |
if (!getcwd(buf,size-2) && MyFlags & MY_WME) |
|
54 |
{
|
|
55 |
my_errno=errno; |
|
56 |
my_error(EE_GETWD,MYF(ME_BELL+ME_WAITTANG),errno); |
|
57 |
return(-1); |
|
58 |
}
|
|
59 |
#elif defined(HAVE_GETWD)
|
|
60 |
{
|
|
61 |
char pathname[MAXPATHLEN]; |
|
62 |
getwd(pathname); |
|
63 |
strmake(buf,pathname,size-1); |
|
64 |
}
|
|
65 |
#elif defined(VMS)
|
|
66 |
if (!getcwd(buf,size-2,1) && MyFlags & MY_WME) |
|
67 |
{
|
|
68 |
my_errno=errno; |
|
69 |
my_error(EE_GETWD,MYF(ME_BELL+ME_WAITTANG),errno); |
|
70 |
return(-1); |
|
71 |
}
|
|
72 |
intern_filename(buf,buf); |
|
73 |
#else
|
|
74 |
#error "No way to get current directory"
|
|
75 |
#endif
|
|
76 |
if (*((pos=strend(buf))-1) != FN_LIBCHAR) /* End with FN_LIBCHAR */ |
|
77 |
{
|
|
78 |
pos[0]= FN_LIBCHAR; |
|
79 |
pos[1]=0; |
|
80 |
}
|
|
81 |
(void) strmake(&curr_dir[0],buf, (size_t) (FN_REFLEN-1)); |
|
82 |
}
|
|
83 |
DBUG_RETURN(0); |
|
84 |
} /* my_getwd */ |
|
85 |
||
86 |
||
87 |
/* Set new working directory */
|
|
88 |
||
89 |
int my_setwd(const char *dir, myf MyFlags) |
|
90 |
{
|
|
91 |
int res; |
|
92 |
size_t length; |
|
93 |
char *start, *pos; |
|
94 |
#if defined(VMS)
|
|
95 |
char buff[FN_REFLEN]; |
|
96 |
#endif
|
|
97 |
DBUG_ENTER("my_setwd"); |
|
98 |
DBUG_PRINT("my",("dir: '%s' MyFlags %d", dir, MyFlags)); |
|
99 |
||
100 |
start=(char *) dir; |
|
101 |
if (! dir[0] || (dir[0] == FN_LIBCHAR && dir[1] == 0)) |
|
102 |
dir=FN_ROOTDIR; |
|
103 |
#ifdef VMS
|
|
104 |
{
|
|
105 |
pos=strmov(buff,dir); |
|
106 |
if (pos[-1] != FN_LIBCHAR) |
|
107 |
{
|
|
108 |
pos[0]=FN_LIBCHAR; /* Mark as directory */ |
|
109 |
pos[1]=0; |
|
110 |
}
|
|
111 |
system_filename(buff,buff); /* Change to VMS format */ |
|
112 |
dir=buff; |
|
113 |
}
|
|
114 |
#endif /* VMS */ |
|
115 |
if ((res=chdir((char*) dir)) != 0) |
|
116 |
{
|
|
117 |
my_errno=errno; |
|
118 |
if (MyFlags & MY_WME) |
|
119 |
my_error(EE_SETWD,MYF(ME_BELL+ME_WAITTANG),start,errno); |
|
120 |
}
|
|
121 |
else
|
|
122 |
{
|
|
123 |
if (test_if_hard_path(start)) |
|
124 |
{ /* Hard pathname */ |
|
125 |
pos= strmake(&curr_dir[0],start,(size_t) FN_REFLEN-1); |
|
126 |
if (pos[-1] != FN_LIBCHAR) |
|
127 |
{
|
|
128 |
length=(uint) (pos-(char*) curr_dir); |
|
129 |
curr_dir[length]=FN_LIBCHAR; /* must end with '/' */ |
|
130 |
curr_dir[length+1]='\0'; |
|
131 |
}
|
|
132 |
}
|
|
133 |
else
|
|
134 |
curr_dir[0]='\0'; /* Don't save name */ |
|
135 |
}
|
|
136 |
DBUG_RETURN(res); |
|
137 |
} /* my_setwd */ |
|
138 |
||
139 |
||
140 |
||
141 |
/* Test if hard pathname */
|
|
142 |
/* Returns 1 if dirname is a hard path */
|
|
143 |
||
144 |
int test_if_hard_path(register const char *dir_name) |
|
145 |
{
|
|
146 |
if (dir_name[0] == FN_HOMELIB && dir_name[1] == FN_LIBCHAR) |
|
147 |
return (home_dir != NullS && test_if_hard_path(home_dir)); |
|
148 |
if (dir_name[0] == FN_LIBCHAR) |
|
149 |
return (TRUE); |
|
150 |
#ifdef FN_DEVCHAR
|
|
151 |
return (strchr(dir_name,FN_DEVCHAR) != 0); |
|
152 |
#else
|
|
153 |
return FALSE; |
|
154 |
#endif
|
|
155 |
} /* test_if_hard_path */ |
|
156 |
||
157 |
||
158 |
/*
|
|
159 |
Test if a name contains an (absolute or relative) path.
|
|
160 |
||
161 |
SYNOPSIS
|
|
162 |
has_path()
|
|
163 |
name The name to test.
|
|
164 |
||
165 |
RETURN
|
|
166 |
TRUE name contains a path.
|
|
167 |
FALSE name does not contain a path.
|
|
168 |
*/
|
|
169 |
||
170 |
my_bool has_path(const char *name) |
|
171 |
{
|
|
172 |
return test(strchr(name, FN_LIBCHAR)) |
|
173 |
#if FN_LIBCHAR != '/'
|
|
174 |
|| test(strchr(name,'/')) |
|
175 |
#endif
|
|
176 |
#ifdef FN_DEVCHAR
|
|
177 |
|| test(strchr(name, FN_DEVCHAR)) |
|
178 |
#endif
|
|
179 |
;
|
|
180 |
}
|