~drizzle-trunk/drizzle/development

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