~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_getwd.cc

  • Committer: Monty Taylor
  • Date: 2008-11-11 01:17:35 UTC
  • mto: This revision was merged to the branch mainline in revision 584.
  • Revision ID: monty@inaugust.com-20081111011735-ct447w151fbbubcu
Removed my_getwd.
Moved replication files.
Moved more stuff out of server_includes.h.

Show diffs side-by-side

added added

removed removed

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