~drizzle-trunk/drizzle/development

1 by brian
clean slate
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"
212.5.18 by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype.
17
#include <mystrings/m_string.h>
53.2.7 by Monty Taylor
Changes so that removal of duplicate curr_dir from my_sys.h work.
18
#include "my_static.h"
722.1.4 by Monty Taylor
Removed all the setting of DEFS everywhere. Use configmake.h to get the values
19
#include <drizzled/configmake.h>
629.1.1 by Monty Taylor
More solaris fixes.
20
#include <stdlib.h>
670.3.1 by Toru Maesaka
Replaced MySQL's my_stpncpy() with libc and c++ calls
21
#include <sstream>
1 by brian
clean slate
22
670.3.3 by Toru Maesaka
Added namespacing for std to .cc files that needed it
23
using namespace std;
24
1 by brian
clean slate
25
static char *find_file_in_path(char *to,const char *name);
26
27
	/* Finds where program can find it's files.
28
	   pre_pathname is found by first locking at progname (argv[0]).
29
	   if progname contains path the path is returned.
30
	   else if progname is found in path, return it
31
	   else if progname is given and POSIX environment variable "_" is set
32
	   then path is taken from "_".
33
	   If filename doesn't contain a path append MY_BASEDIR_VERSION or
34
	   MY_BASEDIR if defined, else append "/my/running".
35
	   own_path_name_part is concatinated to result.
36
	   my_path puts result in to and returns to */
37
38
char * my_path(char * to, const char *progname,
39
               const char *own_pathname_part)
40
{
722.1.4 by Monty Taylor
Removed all the setting of DEFS everywhere. Use configmake.h to get the values
41
  char *start, *prog;
42
  const char *end;
1 by brian
clean slate
43
  size_t to_length;
44
45
  start=to;					/* Return this */
46
  if (progname && (dirname_part(to, progname, &to_length) ||
47
		   find_file_in_path(to,progname) ||
48
		   ((prog=getenv("_")) != 0 &&
49
                    dirname_part(to, prog, &to_length))))
50
  {
398.1.10 by Monty Taylor
Actually removed VOID() this time.
51
    intern_filename(to,to);
1 by brian
clean slate
52
    if (!test_if_hard_path(to))
53
    {
575.4.6 by Monty Taylor
Removed my_getwd.
54
      if (!getcwd(curr_dir,FN_REFLEN))
481 by Brian Aker
Remove all of uchar.
55
	bchange((unsigned char*) to, 0, (unsigned char*) curr_dir, strlen(curr_dir), strlen(to)+1);
1 by brian
clean slate
56
    }
57
  }
58
  else
59
  {
60
    if ((end = getenv("MY_BASEDIR_VERSION")) == 0 &&
61
	(end = getenv("MY_BASEDIR")) == 0)
62
    {
722.1.4 by Monty Taylor
Removed all the setting of DEFS everywhere. Use configmake.h to get the values
63
      end= PREFIX;
1 by brian
clean slate
64
    }
398.1.10 by Monty Taylor
Actually removed VOID() this time.
65
    intern_filename(to,end);
376 by Brian Aker
strend remove
66
    to= strchr(to, '\0');
1 by brian
clean slate
67
    if (to != start && to[-1] != FN_LIBCHAR)
68
      *to++ = FN_LIBCHAR;
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
69
    strcpy(to,own_pathname_part);
1 by brian
clean slate
70
  }
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
71
  return(start);
1 by brian
clean slate
72
} /* my_path */
73
74
75
	/* test if file without filename is found in path */
461 by Monty Taylor
Removed NullS. bu-bye.
76
	/* Returns to if found and to has dirpart if found, else NULL */
1 by brian
clean slate
77
78
#define PATH_SEP ':'
79
80
static char *find_file_in_path(char *to, const char *name)
81
{
673.1.1 by Patrick
Fix to mf_path.cc, error was passing a NULL. Changed to '\0'
82
  char *path, *pos, dir[2];
1 by brian
clean slate
83
  const char *ext="";
670.3.3 by Toru Maesaka
Added namespacing for std to .cc files that needed it
84
  ostringstream sstream;
1 by brian
clean slate
85
86
  if (!(path=getenv("PATH")))
461 by Monty Taylor
Removed NullS. bu-bye.
87
    return NULL;
1 by brian
clean slate
88
  dir[0]=FN_LIBCHAR; dir[1]=0;
89
#ifdef PROGRAM_EXTENSION
90
  if (!fn_ext(name)[0])
91
    ext=PROGRAM_EXTENSION;
92
#endif
93
94
  for (pos=path ; (pos=strchr(pos,PATH_SEP)) ; path= ++pos)
95
  {
96
    if (path != pos)
97
    {
673.1.1 by Patrick
Fix to mf_path.cc, error was passing a NULL. Changed to '\0'
98
      sstream << path << dir << name << ext  << '\0';
670.3.1 by Toru Maesaka
Replaced MySQL's my_stpncpy() with libc and c++ calls
99
      strncpy(to, sstream.str().c_str(), sstream.str().length());
1 by brian
clean slate
100
      if (!access(to,F_OK))
101
      {
670.3.1 by Toru Maesaka
Replaced MySQL's my_stpncpy() with libc and c++ calls
102
        to[(uint) (pos-path)+1]=0;	/* Return path only */
103
        return to;
1 by brian
clean slate
104
      }
105
    }
106
  }
461 by Monty Taylor
Removed NullS. bu-bye.
107
  return NULL;				/* File not found */
1 by brian
clean slate
108
}
575.4.6 by Monty Taylor
Removed my_getwd.
109
110
	/* Test if hard pathname */
111
	/* Returns true if dirname is a hard path */
112
113
bool test_if_hard_path(register const char *dir_name)
114
{
115
  if (dir_name[0] == FN_HOMELIB && dir_name[1] == FN_LIBCHAR)
116
    return (home_dir != NULL && test_if_hard_path(home_dir));
117
  if (dir_name[0] == FN_LIBCHAR)
118
    return (true);
119
  return false;
120
} /* test_if_hard_path */
121