1 /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
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, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    Without limiting anything contained in the foregoing, this file,
15    which is part of C Driver for MySQL (Connector/C), is also subject to the
16    Universal FOSS Exception, version 1.0, a copy of which can be found at
17    http://oss.oracle.com/licenses/universal-foss-exception.
18 
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License, version 2.0, for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
27 
28 #include "mysys_priv.h"
29 #include <m_string.h>
30 
31 static char *find_file_in_path(char *to,const char *name);
32 
33 	/* Finds where program can find it's files.
34 	   pre_pathname is found by first locking at progname (argv[0]).
35 	   if progname contains path the path is returned.
36 	   else if progname is found in path, return it
37 	   else if progname is given and POSIX environment variable "_" is set
38 	   then path is taken from "_".
39 	   If filename doesn't contain a path append MY_BASEDIR_VERSION or
40 	   MY_BASEDIR if defined, else append "/my/running".
41 	   own_path_name_part is concatinated to result.
42 	   my_path puts result in to and returns to */
43 
my_path(char * to,const char * progname,const char * own_pathname_part)44 char * my_path(char * to, const char *progname,
45                const char *own_pathname_part)
46 {
47   char *start, *end, *prog;
48   size_t to_length;
49   DBUG_ENTER("my_path");
50 
51   start=to;					/* Return this */
52   if (progname && (dirname_part(to, progname, &to_length) ||
53 		   find_file_in_path(to,progname) ||
54 		   ((prog=getenv("_")) != 0 &&
55                     dirname_part(to, prog, &to_length))))
56   {
57     (void) intern_filename(to,to);
58     if (!test_if_hard_path(to))
59     {
60       if (!my_getwd(curr_dir,FN_REFLEN,MYF(0)))
61 	bchange((uchar*) to, 0, (uchar*) curr_dir, strlen(curr_dir), strlen(to)+1);
62     }
63   }
64   else
65   {
66     if ((end = getenv("MY_BASEDIR_VERSION")) == 0 &&
67 	(end = getenv("MY_BASEDIR")) == 0)
68     {
69 #ifdef DEFAULT_BASEDIR
70       end= (char*) DEFAULT_BASEDIR;
71 #else
72       end= (char*) "/my/";
73 #endif
74     }
75     (void) intern_filename(to,end);
76     to=strend(to);
77     if (to != start && to[-1] != FN_LIBCHAR)
78       *to++ = FN_LIBCHAR;
79     (void) strmov(to,own_pathname_part);
80   }
81   DBUG_PRINT("exit",("to: '%s'",start));
82   DBUG_RETURN(start);
83 } /* my_path */
84 
85 
86 	/* test if file without filename is found in path */
87 	/* Returns to if found and to has dirpart if found, else NullS */
88 
89 #if defined(__WIN__)
90 #define F_OK 0
91 #define PATH_SEP ';'
92 #define PROGRAM_EXTENSION ".exe"
93 #else
94 #define PATH_SEP ':'
95 #endif
96 
find_file_in_path(char * to,const char * name)97 static char *find_file_in_path(char *to, const char *name)
98 {
99   char *path,*pos,dir[2];
100   const char *ext="";
101 
102   if (!(path=getenv("PATH")))
103     return NullS;
104   dir[0]=FN_LIBCHAR; dir[1]=0;
105 #ifdef PROGRAM_EXTENSION
106   if (!fn_ext(name)[0])
107     ext=PROGRAM_EXTENSION;
108 #endif
109 
110   for (pos=path ; (pos=strchr(pos,PATH_SEP)) ; path= ++pos)
111   {
112     if (path != pos)
113     {
114       strxmov(strnmov(to,path,(uint) (pos-path)),dir,name,ext,NullS);
115       if (!access(to,F_OK))
116       {
117 	to[(uint) (pos-path)+1]=0;	/* Return path only */
118 	return to;
119       }
120     }
121   }
122 #ifdef __WIN__
123   to[0]=FN_CURLIB;
124   strxmov(to+1,dir,name,ext,NullS);
125   if (!access(to,F_OK))			/* Test in current dir */
126   {
127     to[2]=0;				/* Leave ".\" */
128     return to;
129   }
130 #endif
131   return NullS;				/* File not found */
132 }
133