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