1
/* Copyright (C) 2000 MySQL AB
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.
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.
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 */
16
#include "mysys_priv.h"
18
#include "my_static.h"
20
static char *find_file_in_path(char *to,const char *name);
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 */
33
char * my_path(char * to, const char *progname,
34
const char *own_pathname_part)
36
char *start, *end, *prog;
38
DBUG_ENTER("my_path");
40
start=to; /* Return this */
41
if (progname && (dirname_part(to, progname, &to_length) ||
42
find_file_in_path(to,progname) ||
43
((prog=getenv("_")) != 0 &&
44
dirname_part(to, prog, &to_length))))
46
VOID(intern_filename(to,to));
47
if (!test_if_hard_path(to))
49
if (!my_getwd(curr_dir,FN_REFLEN,MYF(0)))
50
bchange((uchar*) to, 0, (uchar*) curr_dir, strlen(curr_dir), strlen(to)+1);
55
if ((end = getenv("MY_BASEDIR_VERSION")) == 0 &&
56
(end = getenv("MY_BASEDIR")) == 0)
58
#ifdef DEFAULT_BASEDIR
59
end= (char*) DEFAULT_BASEDIR;
64
VOID(intern_filename(to,end));
66
if (to != start && to[-1] != FN_LIBCHAR)
68
VOID(strmov(to,own_pathname_part));
70
DBUG_PRINT("exit",("to: '%s'",start));
75
/* test if file without filename is found in path */
76
/* Returns to if found and to has dirpart if found, else NullS */
80
static char *find_file_in_path(char *to, const char *name)
82
char *path,*pos,dir[2];
85
if (!(path=getenv("PATH")))
87
dir[0]=FN_LIBCHAR; dir[1]=0;
88
#ifdef PROGRAM_EXTENSION
90
ext=PROGRAM_EXTENSION;
93
for (pos=path ; (pos=strchr(pos,PATH_SEP)) ; path= ++pos)
97
strxmov(strnmov(to,path,(uint) (pos-path)),dir,name,ext,NullS);
100
to[(uint) (pos-path)+1]=0; /* Return path only */
105
return NullS; /* File not found */