~vlad-lesin/percona-server/mysql-5.0.33-original

« back to all changes in this revision

Viewing changes to mysys/mf_path.c

  • Committer: Vlad Lesin
  • Date: 2012-07-31 09:21:34 UTC
  • Revision ID: vladislav.lesin@percona.com-20120731092134-zfodx022b7992wsi
VirginĀ 5.0.33

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; either version 2 of the License, or
 
6
   (at your option) any later version.
 
7
 
 
8
   This program is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
   GNU General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU General Public License
 
14
   along with this program; if not, write to the Free Software
 
15
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
16
 
 
17
#include "mysys_priv.h"
 
18
#include <m_string.h>
 
19
 
 
20
static char *find_file_in_path(char *to,const char *name);
 
21
 
 
22
        /* Finds where program can find it's files.
 
23
           pre_pathname is found by first locking at progname (argv[0]).
 
24
           if progname contains path the path is returned.
 
25
           else if progname is found in path, return it
 
26
           else if progname is given and POSIX environment variable "_" is set
 
27
           then path is taken from "_".
 
28
           If filename doesn't contain a path append MY_BASEDIR_VERSION or
 
29
           MY_BASEDIR if defined, else append "/my/running".
 
30
           own_path_name_part is concatinated to result.
 
31
           my_path puts result in to and returns to */
 
32
 
 
33
my_string my_path(my_string to, const char *progname,
 
34
                  const char *own_pathname_part)
 
35
{
 
36
  my_string start,end,prog;
 
37
  DBUG_ENTER("my_path");
 
38
 
 
39
  start=to;                                     /* Return this */
 
40
  if (progname && (dirname_part(to, progname) ||
 
41
                   find_file_in_path(to,progname) ||
 
42
                   ((prog=getenv("_")) != 0 && dirname_part(to,prog))))
 
43
  {
 
44
    VOID(intern_filename(to,to));
 
45
    if (!test_if_hard_path(to))
 
46
    {
 
47
      if (!my_getwd(curr_dir,FN_REFLEN,MYF(0)))
 
48
        bchange(to,0,curr_dir, (uint) strlen(curr_dir), (uint) strlen(to)+1);
 
49
    }
 
50
  }
 
51
  else
 
52
  {
 
53
    if ((end = getenv("MY_BASEDIR_VERSION")) == 0 &&
 
54
        (end = getenv("MY_BASEDIR")) == 0)
 
55
    {
 
56
#ifdef DEFAULT_BASEDIR
 
57
      end= (char*) DEFAULT_BASEDIR;
 
58
#else
 
59
      end= (char*) "/my/";
 
60
#endif
 
61
    }
 
62
    VOID(intern_filename(to,end));
 
63
    to=strend(to);
 
64
    if (to != start && to[-1] != FN_LIBCHAR)
 
65
      *to++ = FN_LIBCHAR;
 
66
    VOID(strmov(to,own_pathname_part));
 
67
  }
 
68
  DBUG_PRINT("exit",("to: '%s'",start));
 
69
  DBUG_RETURN(start);
 
70
} /* my_path */
 
71
 
 
72
 
 
73
        /* test if file without filename is found in path */
 
74
        /* Returns to if found and to has dirpart if found, else NullS */
 
75
 
 
76
#if defined(MSDOS) || defined(__WIN__) || defined(__EMX__) || defined(OS2)
 
77
#define F_OK 0
 
78
#define PATH_SEP ';'
 
79
#define PROGRAM_EXTENSION ".exe"
 
80
#elif defined(__NETWARE__)
 
81
#define PATH_SEP ';'
 
82
#define PROGRAM_EXTENSION ".nlm"
 
83
#else
 
84
#define PATH_SEP ':'
 
85
#endif
 
86
 
 
87
static char *find_file_in_path(char *to, const char *name)
 
88
{
 
89
  char *path,*pos,dir[2];
 
90
  const char *ext="";
 
91
 
 
92
  if (!(path=getenv("PATH")))
 
93
    return NullS;
 
94
  dir[0]=FN_LIBCHAR; dir[1]=0;
 
95
#ifdef PROGRAM_EXTENSION
 
96
  if (!fn_ext(name)[0])
 
97
    ext=PROGRAM_EXTENSION;
 
98
#endif
 
99
 
 
100
  for (pos=path ; (pos=strchr(pos,PATH_SEP)) ; path= ++pos)
 
101
  {
 
102
    if (path != pos)
 
103
    {
 
104
      strxmov(strnmov(to,path,(uint) (pos-path)),dir,name,ext,NullS);
 
105
      if (!access(to,F_OK))
 
106
      {
 
107
        to[(uint) (pos-path)+1]=0;      /* Return path only */
 
108
        return to;
 
109
      }
 
110
    }
 
111
  }
 
112
#ifdef __WIN__
 
113
  to[0]=FN_CURLIB;
 
114
  strxmov(to+1,dir,name,ext,NullS);
 
115
  if (!access(to,F_OK))                 /* Test in current dir */
 
116
  {
 
117
    to[2]=0;                            /* Leave ".\" */
 
118
    return to;
 
119
  }
 
120
#endif
 
121
  return NullS;                         /* File not found */
 
122
}