~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"
1 by brian
clean slate
19
20
static char *find_file_in_path(char *to,const char *name);
21
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 */
32
33
char * my_path(char * to, const char *progname,
34
               const char *own_pathname_part)
35
{
36
  char *start, *end, *prog;
37
  size_t to_length;
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
  {
398.1.10 by Monty Taylor
Actually removed VOID() this time.
45
    intern_filename(to,to);
1 by brian
clean slate
46
    if (!test_if_hard_path(to))
47
    {
48
      if (!my_getwd(curr_dir,FN_REFLEN,MYF(0)))
481 by Brian Aker
Remove all of uchar.
49
	bchange((unsigned char*) to, 0, (unsigned char*) curr_dir, strlen(curr_dir), strlen(to)+1);
1 by brian
clean slate
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
    }
398.1.10 by Monty Taylor
Actually removed VOID() this time.
63
    intern_filename(to,end);
376 by Brian Aker
strend remove
64
    to= strchr(to, '\0');
1 by brian
clean slate
65
    if (to != start && to[-1] != FN_LIBCHAR)
66
      *to++ = FN_LIBCHAR;
419 by Monty
Merged up to 408 of stdint-includes-fix.
67
    my_stpcpy(to,own_pathname_part);
1 by brian
clean slate
68
  }
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
69
  return(start);
1 by brian
clean slate
70
} /* my_path */
71
72
73
	/* test if file without filename is found in path */
461 by Monty Taylor
Removed NullS. bu-bye.
74
	/* Returns to if found and to has dirpart if found, else NULL */
1 by brian
clean slate
75
76
#define PATH_SEP ':'
77
78
static char *find_file_in_path(char *to, const char *name)
79
{
80
  char *path,*pos,dir[2];
81
  const char *ext="";
82
83
  if (!(path=getenv("PATH")))
461 by Monty Taylor
Removed NullS. bu-bye.
84
    return NULL;
1 by brian
clean slate
85
  dir[0]=FN_LIBCHAR; dir[1]=0;
86
#ifdef PROGRAM_EXTENSION
87
  if (!fn_ext(name)[0])
88
    ext=PROGRAM_EXTENSION;
89
#endif
90
91
  for (pos=path ; (pos=strchr(pos,PATH_SEP)) ; path= ++pos)
92
  {
93
    if (path != pos)
94
    {
461 by Monty Taylor
Removed NullS. bu-bye.
95
      strxmov(my_stpncpy(to,path,(uint) (pos-path)),dir,name,ext,NULL);
1 by brian
clean slate
96
      if (!access(to,F_OK))
97
      {
98
	to[(uint) (pos-path)+1]=0;	/* Return path only */
99
	return to;
100
      }
101
    }
102
  }
461 by Monty Taylor
Removed NullS. bu-bye.
103
  return NULL;				/* File not found */
1 by brian
clean slate
104
}