2 Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 /* my_setwd() and my_getwd() works with intern_filenames !! */
20 #include "mysys_priv.h"
22 #include "mysys_err.h"
24 #include <sys/param.h>
32 /* Gets current working directory in buff.
36 buf Buffer to store result. Can be curr_dir[].
41 Directory is allways ended with FN_LIBCHAR
48 int my_getwd(char * buf
, size_t size
, myf MyFlags
)
51 DBUG_ENTER("my_getwd");
52 DBUG_PRINT("my",("buf: 0x%lx size: %u MyFlags %d",
53 (long) buf
, (uint
) size
, MyFlags
));
58 if (curr_dir
[0]) /* Current pos is saved here */
59 VOID(strmake(buf
,&curr_dir
[0],size
-1));
62 #if defined(HAVE_GETCWD)
65 if (!getcwd(buf
,(uint
) (size
-2)) && MyFlags
& MY_WME
)
68 my_error(EE_GETWD
,MYF(ME_BELL
+ME_WAITTANG
),errno
);
71 #elif defined(HAVE_GETWD)
73 char pathname
[MAXPATHLEN
];
75 strmake(buf
,pathname
,size
-1);
80 if (!getcwd(buf
,size
-2,1) && MyFlags
& MY_WME
)
83 my_error(EE_GETWD
,MYF(ME_BELL
+ME_WAITTANG
),errno
);
86 intern_filename(buf
,buf
);
88 #error "No way to get current directory"
90 if (*((pos
=strend(buf
))-1) != FN_LIBCHAR
) /* End with FN_LIBCHAR */
95 (void) strmake(&curr_dir
[0],buf
, (size_t) (FN_REFLEN
-1));
101 /* Set new working directory */
103 int my_setwd(const char *dir
, myf MyFlags
)
109 char buff
[FN_REFLEN
];
111 DBUG_ENTER("my_setwd");
112 DBUG_PRINT("my",("dir: '%s' MyFlags %d", dir
, MyFlags
));
115 if (! dir
[0] || (dir
[0] == FN_LIBCHAR
&& dir
[1] == 0))
119 pos
=strmov(buff
,dir
);
120 if (pos
[-1] != FN_LIBCHAR
)
122 pos
[0]=FN_LIBCHAR
; /* Mark as directory */
125 system_filename(buff
,buff
); /* Change to VMS format */
129 if ((res
=chdir((char*) dir
)) != 0)
132 if (MyFlags
& MY_WME
)
133 my_error(EE_SETWD
,MYF(ME_BELL
+ME_WAITTANG
),start
,errno
);
137 if (test_if_hard_path(start
))
138 { /* Hard pathname */
139 pos
= strmake(&curr_dir
[0],start
,(size_t) FN_REFLEN
-1);
140 if (pos
[-1] != FN_LIBCHAR
)
142 length
=(uint
) (pos
-(char*) curr_dir
);
143 curr_dir
[length
]=FN_LIBCHAR
; /* must end with '/' */
144 curr_dir
[length
+1]='\0';
148 curr_dir
[0]='\0'; /* Don't save name */
155 /* Test if hard pathname */
156 /* Returns 1 if dirname is a hard path */
158 int test_if_hard_path(register const char *dir_name
)
160 if (dir_name
[0] == FN_HOMELIB
&& dir_name
[1] == FN_LIBCHAR
)
161 return (home_dir
!= NullS
&& test_if_hard_path(home_dir
));
162 if (dir_name
[0] == FN_LIBCHAR
)
165 return (strchr(dir_name
,FN_DEVCHAR
) != 0);
169 } /* test_if_hard_path */
173 Test if a name contains an (absolute or relative) path.
177 name The name to test.
180 TRUE name contains a path.
181 FALSE name does not contain a path.
184 my_bool
has_path(const char *name
)
186 return test(strchr(name
, FN_LIBCHAR
))
187 #if FN_LIBCHAR != '/'
188 || test(strchr(name
,'/'))
191 || test(strchr(name
, FN_DEVCHAR
))