~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to mysys/mf_path.c

  • Committer: bk at mysql
  • Date: 2000-07-31 19:29:14 UTC
  • Revision ID: sp1r-bk@work.mysql.com-20000731192914-08846
Import changeset

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
 
2
   
 
3
   This library is free software; you can redistribute it and/or
 
4
   modify it under the terms of the GNU Library General Public
 
5
   License as published by the Free Software Foundation; either
 
6
   version 2 of the License, or (at your option) any later version.
 
7
   
 
8
   This library 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 GNU
 
11
   Library General Public License for more details.
 
12
   
 
13
   You should have received a copy of the GNU Library General Public
 
14
   License along with this library; if not, write to the Free
 
15
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
16
   MA 02111-1307, USA */
 
17
 
 
18
#include "mysys_priv.h"
 
19
#include <m_string.h>
 
20
 
 
21
static char *find_file_in_path(char *to,const char *name);
 
22
 
 
23
        /* Finds where program can find it's files.
 
24
           pre_pathname is found by first locking at progname (argv[0]).
 
25
           if progname contains path the path is returned.
 
26
           else if progname is found in path, return it
 
27
           else if progname is given and POSIX environment variable "_" is set
 
28
           then path is taken from "_".
 
29
           If filename doesn't contain a path append MY_BASEDIR_VERSION or
 
30
           MY_BASEDIR if defined, else append "/my/running".
 
31
           own_path_name_part is concatinated to result.
 
32
           my_path puts result in to and returns to */
 
33
 
 
34
my_string my_path(my_string to, const char *progname,
 
35
                  const char *own_pathname_part)
 
36
{
 
37
  my_string start,end,prog;
 
38
  DBUG_ENTER("my_path");
 
39
 
 
40
  start=to;                                     /* Return this */
 
41
  if (progname && (dirname_part(to, progname) ||
 
42
                   find_file_in_path(to,progname) ||
 
43
                   ((prog=getenv("_")) != 0 && dirname_part(to,prog))))
 
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(to,0,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(MSDOS) || defined(__WIN__)
 
78
#define F_OK 0
 
79
#define PATH_SEP ';'
 
80
#define PROGRAM_EXTENSION ".exe"
 
81
#else
 
82
#define PATH_SEP ':'
 
83
#endif
 
84
 
 
85
static char *find_file_in_path(char *to, const char *name)
 
86
{
 
87
  char *path,*pos,dir[2];
 
88
  const char *ext="";
 
89
 
 
90
  if (!(path=getenv("PATH")))
 
91
    return NullS;
 
92
  dir[0]=FN_LIBCHAR; dir[1]=0;
 
93
#ifdef PROGRAM_EXTENSION
 
94
  if (!fn_ext(name)[0])
 
95
    ext=PROGRAM_EXTENSION;
 
96
#endif
 
97
 
 
98
  for (pos=path ; (pos=strchr(pos,PATH_SEP)) ; path= ++pos)
 
99
  {
 
100
    if (path != pos)
 
101
    {
 
102
      strxmov(strnmov(to,path,(uint) (pos-path)),dir,name,ext,NullS);
 
103
      if (!access(to,F_OK))
 
104
      {
 
105
        to[(uint) (pos-path)+1]=0;      /* Return path only */
 
106
        return to;
 
107
      }
 
108
    }
 
109
  }
 
110
#ifdef __WIN__
 
111
  to[0]=FN_CURLIB;
 
112
  strxmov(to+1,dir,name,ext,NullS);
 
113
  if (!access(to,F_OK))                 /* Test in current dir */
 
114
  {
 
115
    to[2]=0;                            /* Leave ".\" */
 
116
    return to;
 
117
  }
 
118
#endif
 
119
  return NullS;                         /* File not found */
 
120
}