~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_path.cc

  • Committer: Monty Taylor
  • Date: 2008-12-08 01:15:27 UTC
  • mto: This revision was merged to the branch mainline in revision 670.
  • Revision ID: monty@inaugust.com-20081208011527-lq9m47jsmiiqn999
Replaced my hacked up m4/ac_system_extensions.m4 with the one from gnulib.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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"
 
17
#include <mystrings/m_string.h>
 
18
#include "my_static.h"
 
19
#include <stdlib.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
char * my_path(char * to, const char *progname,
 
35
               const char *own_pathname_part)
 
36
{
 
37
  char *start, *end, *prog;
 
38
  size_t to_length;
 
39
 
 
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))))
 
45
  {
 
46
    intern_filename(to,to);
 
47
    if (!test_if_hard_path(to))
 
48
    {
 
49
      if (!getcwd(curr_dir,FN_REFLEN))
 
50
        bchange((unsigned char*) to, 0, (unsigned char*) curr_dir, strlen(curr_dir), strlen(to)+1);
 
51
    }
 
52
  }
 
53
  else
 
54
  {
 
55
    if ((end = getenv("MY_BASEDIR_VERSION")) == 0 &&
 
56
        (end = getenv("MY_BASEDIR")) == 0)
 
57
    {
 
58
#ifdef DEFAULT_BASEDIR
 
59
      end= (char*) DEFAULT_BASEDIR;
 
60
#else
 
61
      end= (char*) "/my/";
 
62
#endif
 
63
    }
 
64
    intern_filename(to,end);
 
65
    to= strchr(to, '\0');
 
66
    if (to != start && to[-1] != FN_LIBCHAR)
 
67
      *to++ = FN_LIBCHAR;
 
68
    strcpy(to,own_pathname_part);
 
69
  }
 
70
  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 NULL */
 
76
 
 
77
#define PATH_SEP ':'
 
78
 
 
79
static char *find_file_in_path(char *to, const char *name)
 
80
{
 
81
  char *path,*pos,dir[2];
 
82
  const char *ext="";
 
83
 
 
84
  if (!(path=getenv("PATH")))
 
85
    return NULL;
 
86
  dir[0]=FN_LIBCHAR; dir[1]=0;
 
87
#ifdef PROGRAM_EXTENSION
 
88
  if (!fn_ext(name)[0])
 
89
    ext=PROGRAM_EXTENSION;
 
90
#endif
 
91
 
 
92
  for (pos=path ; (pos=strchr(pos,PATH_SEP)) ; path= ++pos)
 
93
  {
 
94
    if (path != pos)
 
95
    {
 
96
      strxmov(my_stpncpy(to,path,(uint) (pos-path)),dir,name,ext,NULL);
 
97
      if (!access(to,F_OK))
 
98
      {
 
99
        to[(uint) (pos-path)+1]=0;      /* Return path only */
 
100
        return to;
 
101
      }
 
102
    }
 
103
  }
 
104
  return NULL;                          /* File not found */
 
105
}
 
106
 
 
107
        /* Test if hard pathname */
 
108
        /* Returns true if dirname is a hard path */
 
109
 
 
110
bool test_if_hard_path(register const char *dir_name)
 
111
{
 
112
  if (dir_name[0] == FN_HOMELIB && dir_name[1] == FN_LIBCHAR)
 
113
    return (home_dir != NULL && test_if_hard_path(home_dir));
 
114
  if (dir_name[0] == FN_LIBCHAR)
 
115
    return (true);
 
116
  return false;
 
117
} /* test_if_hard_path */
 
118