~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to mysys/mf_path.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

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; 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
static char *find_file_in_path(char *to,const char *name);
 
20
 
 
21
        /* Finds where program can find it's files.
 
22
           pre_pathname is found by first locking at progname (argv[0]).
 
23
           if progname contains path the path is returned.
 
24
           else if progname is found in path, return it
 
25
           else if progname is given and POSIX environment variable "_" is set
 
26
           then path is taken from "_".
 
27
           If filename doesn't contain a path append MY_BASEDIR_VERSION or
 
28
           MY_BASEDIR if defined, else append "/my/running".
 
29
           own_path_name_part is concatinated to result.
 
30
           my_path puts result in to and returns to */
 
31
 
 
32
char * my_path(char * to, const char *progname,
 
33
               const char *own_pathname_part)
 
34
{
 
35
  char *start, *end, *prog;
 
36
  size_t to_length;
 
37
  DBUG_ENTER("my_path");
 
38
 
 
39
  start=to;                                     /* Return this */
 
40
  if (progname && (dirname_part(to, progname, &to_length) ||
 
41
                   find_file_in_path(to,progname) ||
 
42
                   ((prog=getenv("_")) != 0 &&
 
43
                    dirname_part(to, prog, &to_length))))
 
44
  {
 
45
    VOID(intern_filename(to,to));
 
46
    if (!test_if_hard_path(to))
 
47
    {
 
48
      if (!my_getwd(curr_dir,FN_REFLEN,MYF(0)))
 
49
        bchange((uchar*) to, 0, (uchar*) curr_dir, strlen(curr_dir), strlen(to)+1);
 
50
    }
 
51
  }
 
52
  else
 
53
  {
 
54
    if ((end = getenv("MY_BASEDIR_VERSION")) == 0 &&
 
55
        (end = getenv("MY_BASEDIR")) == 0)
 
56
    {
 
57
#ifdef DEFAULT_BASEDIR
 
58
      end= (char*) DEFAULT_BASEDIR;
 
59
#else
 
60
      end= (char*) "/my/";
 
61
#endif
 
62
    }
 
63
    VOID(intern_filename(to,end));
 
64
    to=strend(to);
 
65
    if (to != start && to[-1] != FN_LIBCHAR)
 
66
      *to++ = FN_LIBCHAR;
 
67
    VOID(strmov(to,own_pathname_part));
 
68
  }
 
69
  DBUG_PRINT("exit",("to: '%s'",start));
 
70
  DBUG_RETURN(start);
 
71
} /* my_path */
 
72
 
 
73
 
 
74
        /* test if file without filename is found in path */
 
75
        /* Returns to if found and to has dirpart if found, else NullS */
 
76
 
 
77
#if defined(__WIN__)
 
78
#define F_OK 0
 
79
#define PATH_SEP ';'
 
80
#define PROGRAM_EXTENSION ".exe"
 
81
#elif defined(__NETWARE__)
 
82
#define PATH_SEP ';'
 
83
#define PROGRAM_EXTENSION ".nlm"
 
84
#else
 
85
#define PATH_SEP ':'
 
86
#endif
 
87
 
 
88
static char *find_file_in_path(char *to, const char *name)
 
89
{
 
90
  char *path,*pos,dir[2];
 
91
  const char *ext="";
 
92
 
 
93
  if (!(path=getenv("PATH")))
 
94
    return NullS;
 
95
  dir[0]=FN_LIBCHAR; dir[1]=0;
 
96
#ifdef PROGRAM_EXTENSION
 
97
  if (!fn_ext(name)[0])
 
98
    ext=PROGRAM_EXTENSION;
 
99
#endif
 
100
 
 
101
  for (pos=path ; (pos=strchr(pos,PATH_SEP)) ; path= ++pos)
 
102
  {
 
103
    if (path != pos)
 
104
    {
 
105
      strxmov(strnmov(to,path,(uint) (pos-path)),dir,name,ext,NullS);
 
106
      if (!access(to,F_OK))
 
107
      {
 
108
        to[(uint) (pos-path)+1]=0;      /* Return path only */
 
109
        return to;
 
110
      }
 
111
    }
 
112
  }
 
113
#ifdef __WIN__
 
114
  to[0]=FN_CURLIB;
 
115
  strxmov(to+1,dir,name,ext,NullS);
 
116
  if (!access(to,F_OK))                 /* Test in current dir */
 
117
  {
 
118
    to[2]=0;                            /* Leave ".\" */
 
119
    return to;
 
120
  }
 
121
#endif
 
122
  return NullS;                         /* File not found */
 
123
}