~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mf_path.cc

  • Committer: Monty Taylor
  • Date: 2008-12-18 07:42:01 UTC
  • mto: This revision was merged to the branch mainline in revision 714.
  • Revision ID: monty@inaugust.com-20081218074201-4m7d63t72qfuplwx
Fixed max() problem for solaris

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
#include <sstream>
 
21
 
 
22
using namespace std;
 
23
 
 
24
static char *find_file_in_path(char *to,const char *name);
 
25
 
 
26
        /* Finds where program can find it's files.
 
27
           pre_pathname is found by first locking at progname (argv[0]).
 
28
           if progname contains path the path is returned.
 
29
           else if progname is found in path, return it
 
30
           else if progname is given and POSIX environment variable "_" is set
 
31
           then path is taken from "_".
 
32
           If filename doesn't contain a path append MY_BASEDIR_VERSION or
 
33
           MY_BASEDIR if defined, else append "/my/running".
 
34
           own_path_name_part is concatinated to result.
 
35
           my_path puts result in to and returns to */
 
36
 
 
37
char * my_path(char * to, const char *progname,
 
38
               const char *own_pathname_part)
 
39
{
 
40
  char *start, *end, *prog;
 
41
  size_t to_length;
 
42
 
 
43
  start=to;                                     /* Return this */
 
44
  if (progname && (dirname_part(to, progname, &to_length) ||
 
45
                   find_file_in_path(to,progname) ||
 
46
                   ((prog=getenv("_")) != 0 &&
 
47
                    dirname_part(to, prog, &to_length))))
 
48
  {
 
49
    intern_filename(to,to);
 
50
    if (!test_if_hard_path(to))
 
51
    {
 
52
      if (!getcwd(curr_dir,FN_REFLEN))
 
53
        bchange((unsigned char*) to, 0, (unsigned char*) curr_dir, strlen(curr_dir), strlen(to)+1);
 
54
    }
 
55
  }
 
56
  else
 
57
  {
 
58
    if ((end = getenv("MY_BASEDIR_VERSION")) == 0 &&
 
59
        (end = getenv("MY_BASEDIR")) == 0)
 
60
    {
 
61
#ifdef DEFAULT_BASEDIR
 
62
      end= (char*) DEFAULT_BASEDIR;
 
63
#else
 
64
      end= (char*) "/my/";
 
65
#endif
 
66
    }
 
67
    intern_filename(to,end);
 
68
    to= strchr(to, '\0');
 
69
    if (to != start && to[-1] != FN_LIBCHAR)
 
70
      *to++ = FN_LIBCHAR;
 
71
    strcpy(to,own_pathname_part);
 
72
  }
 
73
  return(start);
 
74
} /* my_path */
 
75
 
 
76
 
 
77
        /* test if file without filename is found in path */
 
78
        /* Returns to if found and to has dirpart if found, else NULL */
 
79
 
 
80
#define PATH_SEP ':'
 
81
 
 
82
static char *find_file_in_path(char *to, const char *name)
 
83
{
 
84
  char *path, *pos, dir[2];
 
85
  const char *ext="";
 
86
  ostringstream sstream;
 
87
 
 
88
  if (!(path=getenv("PATH")))
 
89
    return NULL;
 
90
  dir[0]=FN_LIBCHAR; dir[1]=0;
 
91
#ifdef PROGRAM_EXTENSION
 
92
  if (!fn_ext(name)[0])
 
93
    ext=PROGRAM_EXTENSION;
 
94
#endif
 
95
 
 
96
  for (pos=path ; (pos=strchr(pos,PATH_SEP)) ; path= ++pos)
 
97
  {
 
98
    if (path != pos)
 
99
    {
 
100
      sstream << path << dir << name << ext  << '\0';
 
101
      strncpy(to, sstream.str().c_str(), sstream.str().length());
 
102
      if (!access(to,F_OK))
 
103
      {
 
104
        to[(uint) (pos-path)+1]=0;      /* Return path only */
 
105
        return to;
 
106
      }
 
107
    }
 
108
  }
 
109
  return NULL;                          /* File not found */
 
110
}
 
111
 
 
112
        /* Test if hard pathname */
 
113
        /* Returns true if dirname is a hard path */
 
114
 
 
115
bool test_if_hard_path(register const char *dir_name)
 
116
{
 
117
  if (dir_name[0] == FN_HOMELIB && dir_name[1] == FN_LIBCHAR)
 
118
    return (home_dir != NULL && test_if_hard_path(home_dir));
 
119
  if (dir_name[0] == FN_LIBCHAR)
 
120
    return (true);
 
121
  return false;
 
122
} /* test_if_hard_path */
 
123