~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"
17
#include <m_string.h>
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
{
29
  char dev[FN_REFLEN], buff[FN_REFLEN], *pos, *startpos;
30
  const char *ext;
31
  register size_t length;
32
  size_t dev_length;
33
  DBUG_ENTER("fn_format");
34
  DBUG_PRINT("enter",("name: %s  dir: %s  extension: %s  flag: %d",
35
		       name,dir,extension,flag));
36
37
  /* Copy and skip directory */
38
  name+=(length=dirname_part(dev, (startpos=(char *) name), &dev_length));
39
  if (length == 0 || (flag & MY_REPLACE_DIR))
40
  {
41
    /* Use given directory */
42
    convert_dirname(dev,dir,NullS);		/* Fix to this OS */
43
  }
44
  else if ((flag & MY_RELATIVE_PATH) && !test_if_hard_path(dev))
45
  {
46
    /* Put 'dir' before the given path */
47
    strmake(buff,dev,sizeof(buff)-1);
48
    pos=convert_dirname(dev,dir,NullS);
49
    strmake(pos,buff,sizeof(buff)-1- (int) (pos-dev));
50
  }
51
52
  if (flag & MY_PACK_FILENAME)
53
    pack_dirname(dev,dev);			/* Put in ./.. and ~/.. */
54
  if (flag & MY_UNPACK_FILENAME)
55
    (void) unpack_dirname(dev,dev);		/* Replace ~/.. with dir */
56
57
  if (!(flag & MY_APPEND_EXT) &&
58
      (pos= (char*) strchr(name,FN_EXTCHAR)) != NullS)
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)
82
      return NullS;
83
    tmp_length= strlength(startpos);
84
    DBUG_PRINT("error",("dev: '%s'  ext: '%s'  length: %u",dev,ext,
85
                        (uint) length));
86
    (void) strmake(to,startpos,min(tmp_length,FN_REFLEN-1));
87
  }
88
  else
89
  {
90
    if (to == startpos)
91
    {
92
      bmove(buff,(uchar*) name,length);		/* Save name for last copy */
93
      name=buff;
94
    }
95
    pos=strmake(strmov(to,dev),name,length);
96
    (void) strmov(pos,ext);			/* Don't convert extension */
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)
103
    (void) my_realpath(to, to, MYF(flag & MY_RESOLVE_SYMLINKS ?
104
				   MY_RESOLVE_LINK: 0));
105
  else if (flag & MY_RESOLVE_SYMLINKS)
106
  {
107
    strmov(buff,to);
108
    (void) my_readlink(to, buff, MYF(0));
109
  }
110
  DBUG_RETURN(to);
111
} /* fn_format */
112
113
114
/*
115
  strlength(const string str)
116
  Return length of string with end-space:s not counted.
117
*/
118
119
size_t strlength(const char *str)
120
{
121
  register const char * pos;
122
  register const char * found;
123
  DBUG_ENTER("strlength");
124
125
  pos= found= str;
126
127
  while (*pos)
128
  {
129
    if (*pos != ' ')
130
    {
131
      while (*++pos && *pos != ' ') {};
132
      if (!*pos)
133
      {
134
	found=pos;			/* String ends here */
135
	break;
136
      }
137
    }
138
    found=pos;
139
    while (*++pos == ' ') {};
140
  }
141
  DBUG_RETURN((size_t) (found - str));
142
} /* strlength */