mf_path.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:3k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Library General Public
  5.    License as published by the Free Software Foundation; either
  6.    version 2 of the License, or (at your option) any later version.
  7.    
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU Library General Public
  14.    License along with this library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16.    MA 02111-1307, USA */
  17. #include "mysys_priv.h"
  18. #include <m_string.h>
  19. static char *find_file_in_path(char *to,const char *name);
  20. /* Finds where program can find it's files.
  21.    pre_pathname is found by first locking at progname (argv[0]).
  22.    if progname contains path the path is returned.
  23.    else if progname is found in path, return it
  24.    else if progname is given and POSIX environment variable "_" is set
  25.    then path is taken from "_".
  26.    If filename doesn't contain a path append MY_BASEDIR_VERSION or
  27.    MY_BASEDIR if defined, else append "/my/running".
  28.    own_path_name_part is concatinated to result.
  29.    my_path puts result in to and returns to */
  30. my_string my_path(my_string to, const char *progname,
  31.   const char *own_pathname_part)
  32. {
  33.   my_string start,end,prog;
  34.   DBUG_ENTER("my_path");
  35.   start=to; /* Return this */
  36.   if (progname && (dirname_part(to, progname) ||
  37.    find_file_in_path(to,progname) ||
  38.    ((prog=getenv("_")) != 0 && dirname_part(to,prog))))
  39.   {
  40.     VOID(intern_filename(to,to));
  41.     if (!test_if_hard_path(to))
  42.     {
  43.       if (!my_getwd(curr_dir,FN_REFLEN,MYF(0)))
  44. bchange(to,0,curr_dir, (uint) strlen(curr_dir), (uint) strlen(to)+1);
  45.     }
  46.   }
  47.   else
  48.   {
  49.     if ((end = getenv("MY_BASEDIR_VERSION")) == 0 &&
  50. (end = getenv("MY_BASEDIR")) == 0)
  51.     {
  52. #ifdef DEFAULT_BASEDIR
  53.       end= (char*) DEFAULT_BASEDIR;
  54. #else
  55.       end= (char*) "/my/";
  56. #endif
  57.     }
  58.     VOID(intern_filename(to,end));
  59.     to=strend(to);
  60.     if (to != start && to[-1] != FN_LIBCHAR)
  61.       *to++ = FN_LIBCHAR;
  62.     VOID(strmov(to,own_pathname_part));
  63.   }
  64.   DBUG_PRINT("exit",("to: '%s'",start));
  65.   DBUG_RETURN(start);
  66. } /* my_path */
  67. /* test if file without filename is found in path */
  68. /* Returns to if found and to has dirpart if found, else NullS */
  69. #if defined(MSDOS) || defined(__WIN__) || defined(__EMX__)
  70. #define F_OK 0
  71. #define PATH_SEP ';'
  72. #define PROGRAM_EXTENSION ".exe"
  73. #else
  74. #define PATH_SEP ':'
  75. #endif
  76. static char *find_file_in_path(char *to, const char *name)
  77. {
  78.   char *path,*pos,dir[2];
  79.   const char *ext="";
  80.   if (!(path=getenv("PATH")))
  81.     return NullS;
  82.   dir[0]=FN_LIBCHAR; dir[1]=0;
  83. #ifdef PROGRAM_EXTENSION
  84.   if (!fn_ext(name)[0])
  85.     ext=PROGRAM_EXTENSION;
  86. #endif
  87.   for (pos=path ; (pos=strchr(pos,PATH_SEP)) ; path= ++pos)
  88.   {
  89.     if (path != pos)
  90.     {
  91.       strxmov(strnmov(to,path,(uint) (pos-path)),dir,name,ext,NullS);
  92.       if (!access(to,F_OK))
  93.       {
  94. to[(uint) (pos-path)+1]=0; /* Return path only */
  95. return to;
  96.       }
  97.     }
  98.   }
  99. #ifdef __WIN__
  100.   to[0]=FN_CURLIB;
  101.   strxmov(to+1,dir,name,ext,NullS);
  102.   if (!access(to,F_OK)) /* Test in current dir */
  103.   {
  104.     to[2]=0; /* Leave "." */
  105.     return to;
  106.   }
  107. #endif
  108.   return NullS; /* File not found */
  109. }