~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_getwd.c

  • Committer: Toru Maesaka
  • Date: 2008-12-10 18:33:08 UTC
  • mto: (670.1.11 devel)
  • mto: This revision was merged to the branch mainline in revision 672.
  • Revision ID: dev@torum.net-20081210183308-mb6a5x0sbl1dhc5c
Replaced MySQL's my_stpncpy() with libc and c++ calls

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