~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
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
16
#include "config.h"
17
18
#include "drizzled/internal/my_sys.h"
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
19
20
#include <fcntl.h>
21
22
#ifdef HAVE_SYS_STAT_H
23
# include <sys/stat.h>
24
#endif
25
26
#include <algorithm>
27
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
28
#include "drizzled/internal/m_string.h"
1 by brian
clean slate
29
1067.4.10 by Nathan Williams
Converted all cmin/cmax usages in the mysys directory to std::min/max.
30
using namespace std;
31
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
32
namespace drizzled
33
{
34
namespace internal
35
{
36
1 by brian
clean slate
37
/*
38
  Formats a filename with possible replace of directory of extension
39
  Function can handle the case where 'to' == 'name'
40
  For a description of the flag values, consult my_sys.h
41
  The arguments should be in unix format.
42
*/
43
44
char * fn_format(char * to, const char *name, const char *dir,
482 by Brian Aker
Remove uint.
45
		    const char *extension, uint32_t flag)
1 by brian
clean slate
46
{
266.6.6 by Andy Lester
fixing some constants
47
  char dev[FN_REFLEN], buff[FN_REFLEN], *pos;
48
  const char *startpos = name;
1 by brian
clean slate
49
  const char *ext;
50
  register size_t length;
51
  size_t dev_length;
52
53
  /* Copy and skip directory */
266.6.6 by Andy Lester
fixing some constants
54
  name+=(length=dirname_part(dev, startpos, &dev_length));
1 by brian
clean slate
55
  if (length == 0 || (flag & MY_REPLACE_DIR))
56
  {
57
    /* Use given directory */
461 by Monty Taylor
Removed NullS. bu-bye.
58
    convert_dirname(dev,dir,NULL);		/* Fix to this OS */
1 by brian
clean slate
59
  }
60
  else if ((flag & MY_RELATIVE_PATH) && !test_if_hard_path(dev))
61
  {
62
    /* Put 'dir' before the given path */
629.5.2 by Toru Maesaka
Second pass of replacing MySQL's strmake() with libc calls
63
    strncpy(buff,dev,sizeof(buff)-1);
461 by Monty Taylor
Removed NullS. bu-bye.
64
    pos=convert_dirname(dev,dir,NULL);
629.5.2 by Toru Maesaka
Second pass of replacing MySQL's strmake() with libc calls
65
    strncpy(pos,buff,sizeof(buff)-1- (int) (pos-dev));
1 by brian
clean slate
66
  }
67
68
  if (flag & MY_UNPACK_FILENAME)
69
    (void) unpack_dirname(dev,dev);		/* Replace ~/.. with dir */
70
71
  if (!(flag & MY_APPEND_EXT) &&
461 by Monty Taylor
Removed NullS. bu-bye.
72
      (pos= (char*) strchr(name,FN_EXTCHAR)) != NULL)
1 by brian
clean slate
73
  {
74
    if ((flag & MY_REPLACE_EXT) == 0)		/* If we should keep old ext */
75
    {
76
      length=strlength(name);			/* Use old extension */
77
      ext = "";
78
    }
79
    else
80
    {
81
      length= (size_t) (pos-(char*) name);	/* Change extension */
82
      ext= extension;
83
    }
84
  }
85
  else
86
  {
87
    length=strlength(name);			/* No ext, use the now one */
88
    ext=extension;
89
  }
90
91
  if (strlen(dev)+length+strlen(ext) >= FN_REFLEN || length >= FN_LEN )
92
  {
93
    /* To long path, return original or NULL */
94
    size_t tmp_length;
95
    if (flag & MY_SAFE_PATH)
461 by Monty Taylor
Removed NullS. bu-bye.
96
      return NULL;
1067.4.10 by Nathan Williams
Converted all cmin/cmax usages in the mysys directory to std::min/max.
97
    tmp_length= min(strlength(startpos), (size_t)(FN_REFLEN-1));
629.5.2 by Toru Maesaka
Second pass of replacing MySQL's strmake() with libc calls
98
    strncpy(to,startpos,tmp_length);
99
    to[tmp_length]= '\0';
1 by brian
clean slate
100
  }
101
  else
102
  {
103
    if (to == startpos)
104
    {
629.3.4 by Kristian Nielsen
Take Mats'es changes from bmove()->memcpy(), and fix all of them to be
105
      memmove(buff, name, length); /* Save name for last copy */
1 by brian
clean slate
106
      name=buff;
107
    }
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
108
    char *tmp= strcpy(to, dev) + strlen(dev);
109
    pos= strncpy(tmp,name,length) + length;
110
    (void) strcpy(pos,ext);			/* Don't convert extension */
1 by brian
clean slate
111
  }
112
  /*
113
    If MY_RETURN_REAL_PATH and MY_RESOLVE_SYMLINK is given, only do
114
    realpath if the file is a symbolic link
115
  */
116
  if (flag & MY_RETURN_REAL_PATH)
1060.2.5 by Eric Lambert
-removed my_realpath from my_symlink and replaced it with calls to posix realpath
117
  {
118
    struct stat stat_buff;
119
    char rp_buff[PATH_MAX];
120
    if ((!flag & MY_RESOLVE_SYMLINKS) || 
1060.2.7 by Eric Lambert
-it appears that the result from symlink() in ha_archive can not be
121
       (!lstat(to,&stat_buff) && S_ISLNK(stat_buff.st_mode)))
1060.2.5 by Eric Lambert
-removed my_realpath from my_symlink and replaced it with calls to posix realpath
122
    {
123
      if (!realpath(to,rp_buff))
124
        my_load_path(rp_buff, to, NULL);
125
      rp_buff[FN_REFLEN-1]= '\0';
126
      strcpy(to,rp_buff);
127
    }
128
  }
1 by brian
clean slate
129
  else if (flag & MY_RESOLVE_SYMLINKS)
130
  {
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
131
    strcpy(buff,to);
1060.2.1 by Eric Lambert
-replace calls to my_readlink with readlink
132
    ssize_t sym_link_size= readlink(buff,to,FN_REFLEN-1);
133
    if (sym_link_size >= 0)
134
      to[sym_link_size]= '\0';
1 by brian
clean slate
135
  }
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
136
  return(to);
1 by brian
clean slate
137
} /* fn_format */
138
139
140
/*
141
  strlength(const string str)
142
  Return length of string with end-space:s not counted.
143
*/
144
145
size_t strlength(const char *str)
146
{
147
  register const char * pos;
148
  register const char * found;
149
150
  pos= found= str;
151
152
  while (*pos)
153
  {
154
    if (*pos != ' ')
155
    {
156
      while (*++pos && *pos != ' ') {};
157
      if (!*pos)
158
      {
159
	found=pos;			/* String ends here */
160
	break;
161
      }
162
    }
163
    found=pos;
164
    while (*++pos == ' ') {};
165
  }
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
166
  return((size_t) (found - str));
1 by brian
clean slate
167
} /* strlength */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
168
169
} /* namespace internal */
170
} /* namespace drizzled */