~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
#include "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
19
/*
20
  Formats a filename with possible replace of directory of extension
21
  Function can handle the case where 'to' == 'name'
22
  For a description of the flag values, consult my_sys.h
23
  The arguments should be in unix format.
24
*/
25
26
char * fn_format(char * to, const char *name, const char *dir,
27
		    const char *extension, uint flag)
28
{
266.6.6 by Andy Lester
fixing some constants
29
  char dev[FN_REFLEN], buff[FN_REFLEN], *pos;
30
  const char *startpos = name;
1 by brian
clean slate
31
  const char *ext;
32
  register size_t length;
33
  size_t dev_length;
34
35
  /* Copy and skip directory */
266.6.6 by Andy Lester
fixing some constants
36
  name+=(length=dirname_part(dev, startpos, &dev_length));
1 by brian
clean slate
37
  if (length == 0 || (flag & MY_REPLACE_DIR))
38
  {
39
    /* Use given directory */
40
    convert_dirname(dev,dir,NullS);		/* Fix to this OS */
41
  }
42
  else if ((flag & MY_RELATIVE_PATH) && !test_if_hard_path(dev))
43
  {
44
    /* Put 'dir' before the given path */
45
    strmake(buff,dev,sizeof(buff)-1);
46
    pos=convert_dirname(dev,dir,NullS);
47
    strmake(pos,buff,sizeof(buff)-1- (int) (pos-dev));
48
  }
49
50
  if (flag & MY_PACK_FILENAME)
51
    pack_dirname(dev,dev);			/* Put in ./.. and ~/.. */
52
  if (flag & MY_UNPACK_FILENAME)
53
    (void) unpack_dirname(dev,dev);		/* Replace ~/.. with dir */
54
55
  if (!(flag & MY_APPEND_EXT) &&
56
      (pos= (char*) strchr(name,FN_EXTCHAR)) != NullS)
57
  {
58
    if ((flag & MY_REPLACE_EXT) == 0)		/* If we should keep old ext */
59
    {
60
      length=strlength(name);			/* Use old extension */
61
      ext = "";
62
    }
63
    else
64
    {
65
      length= (size_t) (pos-(char*) name);	/* Change extension */
66
      ext= extension;
67
    }
68
  }
69
  else
70
  {
71
    length=strlength(name);			/* No ext, use the now one */
72
    ext=extension;
73
  }
74
75
  if (strlen(dev)+length+strlen(ext) >= FN_REFLEN || length >= FN_LEN )
76
  {
77
    /* To long path, return original or NULL */
78
    size_t tmp_length;
79
    if (flag & MY_SAFE_PATH)
80
      return NullS;
81
    tmp_length= strlength(startpos);
82
    (void) strmake(to,startpos,min(tmp_length,FN_REFLEN-1));
83
  }
84
  else
85
  {
86
    if (to == startpos)
87
    {
212.6.14 by Mats Kindahl
Removing redundant use of casts in mysys for memcmp(), memcpy(), memset(), and memmove().
88
      memcpy(buff, name, length); /* Save name for last copy */
1 by brian
clean slate
89
      name=buff;
90
    }
266.1.21 by Monty Taylor
Removed references to strmov and strnmov
91
    pos=strmake(stpcpy(to,dev),name,length);
92
    (void) stpcpy(pos,ext);			/* Don't convert extension */
1 by brian
clean slate
93
  }
94
  /*
95
    If MY_RETURN_REAL_PATH and MY_RESOLVE_SYMLINK is given, only do
96
    realpath if the file is a symbolic link
97
  */
98
  if (flag & MY_RETURN_REAL_PATH)
99
    (void) my_realpath(to, to, MYF(flag & MY_RESOLVE_SYMLINKS ?
100
				   MY_RESOLVE_LINK: 0));
101
  else if (flag & MY_RESOLVE_SYMLINKS)
102
  {
266.1.21 by Monty Taylor
Removed references to strmov and strnmov
103
    stpcpy(buff,to);
1 by brian
clean slate
104
    (void) my_readlink(to, buff, MYF(0));
105
  }
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
106
  return(to);
1 by brian
clean slate
107
} /* fn_format */
108
109
110
/*
111
  strlength(const string str)
112
  Return length of string with end-space:s not counted.
113
*/
114
115
size_t strlength(const char *str)
116
{
117
  register const char * pos;
118
  register const char * found;
119
120
  pos= found= str;
121
122
  while (*pos)
123
  {
124
    if (*pos != ' ')
125
    {
126
      while (*++pos && *pos != ' ') {};
127
      if (!*pos)
128
      {
129
	found=pos;			/* String ends here */
130
	break;
131
      }
132
    }
133
    found=pos;
134
    while (*++pos == ' ') {};
135
  }
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
136
  return((size_t) (found - str));
1 by brian
clean slate
137
} /* strlength */