MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_getwd.c
1 /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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 #if defined(__WIN__)
25 #include <m_ctype.h>
26 #include <dos.h>
27 #include <direct.h>
28 #endif
29 
30 /* Gets current working directory in buff.
31 
32  SYNPOSIS
33  my_getwd()
34  buf Buffer to store result. Can be curr_dir[].
35  size Size of buffer
36  MyFlags Flags
37 
38  NOTES
39  Directory is allways ended with FN_LIBCHAR
40 
41  RESULT
42  0 ok
43  # error
44 */
45 
46 int my_getwd(char * buf, size_t size, myf MyFlags)
47 {
48  char * pos;
49  DBUG_ENTER("my_getwd");
50  DBUG_PRINT("my",("buf: 0x%lx size: %u MyFlags %d",
51  (long) buf, (uint) size, MyFlags));
52 
53  if (size < 1)
54  DBUG_RETURN(-1);
55 
56  if (curr_dir[0]) /* Current pos is saved here */
57  (void) strmake(buf,&curr_dir[0],size-1);
58  else
59  {
60 #if defined(HAVE_GETCWD)
61  if (size < 2)
62  DBUG_RETURN(-1);
63  if (!getcwd(buf,(uint) (size-2)) && MyFlags & MY_WME)
64  {
65  char errbuf[MYSYS_STRERROR_SIZE];
66  my_errno=errno;
67  my_error(EE_GETWD, MYF(ME_BELL+ME_WAITTANG),
68  errno, my_strerror(errbuf, sizeof(errbuf), errno));
69  DBUG_RETURN(-1);
70  }
71 #elif defined(HAVE_GETWD)
72  {
73  char pathname[MAXPATHLEN];
74  getwd(pathname);
75  strmake(buf,pathname,size-1);
76  }
77 #else
78 #error "No way to get current directory"
79 #endif
80  if (*((pos=strend(buf))-1) != FN_LIBCHAR) /* End with FN_LIBCHAR */
81  {
82  pos[0]= FN_LIBCHAR;
83  pos[1]=0;
84  }
85  (void) strmake(&curr_dir[0],buf, (size_t) (FN_REFLEN-1));
86  }
87  DBUG_RETURN(0);
88 } /* my_getwd */
89 
90 
91 /* Set new working directory */
92 
93 int my_setwd(const char *dir, myf MyFlags)
94 {
95  int res;
96  size_t length;
97  char *start, *pos;
98  DBUG_ENTER("my_setwd");
99  DBUG_PRINT("my",("dir: '%s' MyFlags %d", dir, MyFlags));
100 
101  start=(char *) dir;
102  if (! dir[0] || (dir[0] == FN_LIBCHAR && dir[1] == 0))
103  dir=FN_ROOTDIR;
104  if ((res=chdir((char*) dir)) != 0)
105  {
106  my_errno=errno;
107  if (MyFlags & MY_WME)
108  {
109  char errbuf[MYSYS_STRERROR_SIZE];
110  my_error(EE_SETWD, MYF(ME_BELL+ME_WAITTANG), start,
111  errno, my_strerror(errbuf, sizeof(errbuf), errno));
112  }
113  }
114  else
115  {
116  if (test_if_hard_path(start))
117  { /* Hard pathname */
118  pos= strmake(&curr_dir[0],start,(size_t) FN_REFLEN-1);
119  if (pos[-1] != FN_LIBCHAR)
120  {
121  length=(uint) (pos-(char*) curr_dir);
122  curr_dir[length]=FN_LIBCHAR; /* must end with '/' */
123  curr_dir[length+1]='\0';
124  }
125  }
126  else
127  curr_dir[0]='\0'; /* Don't save name */
128  }
129  DBUG_RETURN(res);
130 } /* my_setwd */
131 
132 
133 
134  /* Test if hard pathname */
135  /* Returns 1 if dirname is a hard path */
136 
137 int test_if_hard_path(register const char *dir_name)
138 {
139  if (dir_name[0] == FN_HOMELIB && dir_name[1] == FN_LIBCHAR)
140  return (home_dir != NullS && test_if_hard_path(home_dir));
141  if (dir_name[0] == FN_LIBCHAR)
142  return (TRUE);
143 #ifdef FN_DEVCHAR
144  return (strchr(dir_name,FN_DEVCHAR) != 0);
145 #else
146  return FALSE;
147 #endif
148 } /* test_if_hard_path */
149 
150 
151 /*
152  Test if a name contains an (absolute or relative) path.
153 
154  SYNOPSIS
155  has_path()
156  name The name to test.
157 
158  RETURN
159  TRUE name contains a path.
160  FALSE name does not contain a path.
161 */
162 
163 my_bool has_path(const char *name)
164 {
165  return test(strchr(name, FN_LIBCHAR))
166 #if FN_LIBCHAR != '/'
167  || test(strchr(name,'/'))
168 #endif
169 #ifdef FN_DEVCHAR
170  || test(strchr(name, FN_DEVCHAR))
171 #endif
172  ;
173 }