mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysys / my_getwd.c
blobdc9718d710dcb2c3695375dcf5d1fe3d65bff494
1 /*
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"
21 #include <m_string.h>
22 #include "mysys_err.h"
23 #ifdef HAVE_GETWD
24 #include <sys/param.h>
25 #endif
26 #if defined(__WIN__)
27 #include <m_ctype.h>
28 #include <dos.h>
29 #include <direct.h>
30 #endif
32 /* Gets current working directory in buff.
34 SYNPOSIS
35 my_getwd()
36 buf Buffer to store result. Can be curr_dir[].
37 size Size of buffer
38 MyFlags Flags
40 NOTES
41 Directory is allways ended with FN_LIBCHAR
43 RESULT
44 0 ok
45 # error
48 int my_getwd(char * buf, size_t size, myf MyFlags)
50 char * pos;
51 DBUG_ENTER("my_getwd");
52 DBUG_PRINT("my",("buf: 0x%lx size: %u MyFlags %d",
53 (long) buf, (uint) size, MyFlags));
55 if (size < 1)
56 return(-1);
58 if (curr_dir[0]) /* Current pos is saved here */
59 VOID(strmake(buf,&curr_dir[0],size-1));
60 else
62 #if defined(HAVE_GETCWD)
63 if (size < 2)
64 return(-1);
65 if (!getcwd(buf,(uint) (size-2)) && MyFlags & MY_WME)
67 my_errno=errno;
68 my_error(EE_GETWD,MYF(ME_BELL+ME_WAITTANG),errno);
69 return(-1);
71 #elif defined(HAVE_GETWD)
73 char pathname[MAXPATHLEN];
74 getwd(pathname);
75 strmake(buf,pathname,size-1);
77 #elif defined(VMS)
78 if (size < 2)
79 return(-1);
80 if (!getcwd(buf,size-2,1) && MyFlags & MY_WME)
82 my_errno=errno;
83 my_error(EE_GETWD,MYF(ME_BELL+ME_WAITTANG),errno);
84 return(-1);
86 intern_filename(buf,buf);
87 #else
88 #error "No way to get current directory"
89 #endif
90 if (*((pos=strend(buf))-1) != FN_LIBCHAR) /* End with FN_LIBCHAR */
92 pos[0]= FN_LIBCHAR;
93 pos[1]=0;
95 (void) strmake(&curr_dir[0],buf, (size_t) (FN_REFLEN-1));
97 DBUG_RETURN(0);
98 } /* my_getwd */
101 /* Set new working directory */
103 int my_setwd(const char *dir, myf MyFlags)
105 int res;
106 size_t length;
107 char *start, *pos;
108 #if defined(VMS)
109 char buff[FN_REFLEN];
110 #endif
111 DBUG_ENTER("my_setwd");
112 DBUG_PRINT("my",("dir: '%s' MyFlags %d", dir, MyFlags));
114 start=(char *) dir;
115 if (! dir[0] || (dir[0] == FN_LIBCHAR && dir[1] == 0))
116 dir=FN_ROOTDIR;
117 #ifdef VMS
119 pos=strmov(buff,dir);
120 if (pos[-1] != FN_LIBCHAR)
122 pos[0]=FN_LIBCHAR; /* Mark as directory */
123 pos[1]=0;
125 system_filename(buff,buff); /* Change to VMS format */
126 dir=buff;
128 #endif /* VMS */
129 if ((res=chdir((char*) dir)) != 0)
131 my_errno=errno;
132 if (MyFlags & MY_WME)
133 my_error(EE_SETWD,MYF(ME_BELL+ME_WAITTANG),start,errno);
135 else
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';
147 else
148 curr_dir[0]='\0'; /* Don't save name */
150 DBUG_RETURN(res);
151 } /* my_setwd */
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)
163 return (TRUE);
164 #ifdef FN_DEVCHAR
165 return (strchr(dir_name,FN_DEVCHAR) != 0);
166 #else
167 return FALSE;
168 #endif
169 } /* test_if_hard_path */
173 Test if a name contains an (absolute or relative) path.
175 SYNOPSIS
176 has_path()
177 name The name to test.
179 RETURN
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,'/'))
189 #endif
190 #ifdef FN_DEVCHAR
191 || test(strchr(name, FN_DEVCHAR))
192 #endif