[Python-checkins] CVS: python/dist/src/Modules getpath.c,1.38,1.39

Guido van Rossum gvanrossum@users.sourceforge.net
Fri, 28 Sep 2001 13:00:31 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv21822

Modified Files:
	getpath.c 
Log Message:
Be more rigorous about making pathnames absolute, to address SF bug
#424002.

Refactor init_path_from_argv0() and rename to copy_absolute(); add
absolutize() which does the same in-place.

Clean up whitespace (leading tabs -> spaces, delete trailing
spaces/tabs).


Index: getpath.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/getpath.c,v
retrieving revision 1.38
retrieving revision 1.39
diff -C2 -d -r1.38 -r1.39
*** getpath.c	2001/08/15 01:14:40	1.38
--- getpath.c	2001/09/28 20:00:29	1.39
***************
*** 114,118 ****
  #ifndef PYTHONPATH
  #define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
! 	      EXEC_PREFIX "/lib/python" VERSION "/lib-dynload"
  #endif
  
--- 114,118 ----
  #ifndef PYTHONPATH
  #define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
!               EXEC_PREFIX "/lib/python" VERSION "/lib-dynload"
  #endif
  
***************
*** 120,124 ****
  #define LANDMARK "os.py"
  #endif
!  
  static char prefix[MAXPATHLEN+1];
  static char exec_prefix[MAXPATHLEN+1];
--- 120,124 ----
  #define LANDMARK "os.py"
  #endif
! 
  static char prefix[MAXPATHLEN+1];
  static char exec_prefix[MAXPATHLEN+1];
***************
*** 138,142 ****
  
  static int
! isfile(char *filename)		/* Is file, not directory */
  {
      struct stat buf;
--- 138,142 ----
  
  static int
! isfile(char *filename)          /* Is file, not directory */
  {
      struct stat buf;
***************
*** 150,154 ****
  
  static int
! ismodule(char *filename)	/* Is module -- check for .pyc/.pyo too */
  {
      if (isfile(filename))
--- 150,154 ----
  
  static int
! ismodule(char *filename)        /* Is module -- check for .pyc/.pyo too */
  {
      if (isfile(filename))
***************
*** 166,170 ****
  
  static int
! isxfile(char *filename)		/* Is executable file */
  {
      struct stat buf;
--- 166,170 ----
  
  static int
! isxfile(char *filename)         /* Is executable file */
  {
      struct stat buf;
***************
*** 180,184 ****
  
  static int
! isdir(char *filename)			/* Is directory */
  {
      struct stat buf;
--- 180,184 ----
  
  static int
! isdir(char *filename)                   /* Is directory */
  {
      struct stat buf;
***************
*** 214,240 ****
  }
  
! /* init_path_from_argv0 requires that path be allocated at least
!    MAXPATHLEN + 1 bytes and that argv0_path be no more than MAXPATHLEN
!    bytes. 
! */
  static void
! init_path_from_argv0(char *path, char *argv0_path)
  {
!     if (argv0_path[0] == '/')
! 	strcpy(path, argv0_path);
!     else if (argv0_path[0] == '.') {
! 	getcwd(path, MAXPATHLEN);
! 	if (argv0_path[1] == '/') 
! 	    joinpath(path, argv0_path + 2);
! 	else
! 	    joinpath(path, argv0_path);
!     }
      else {
! 	getcwd(path, MAXPATHLEN);
! 	joinpath(path, argv0_path);
      }
  }
  
! /* search_for_prefix requires that argv0_path be no more than MAXPATHLEN 
     bytes long.
  */
--- 214,245 ----
  }
  
! /* copy_absolute requires that path be allocated at least
!    MAXPATHLEN + 1 bytes and that p be no more than MAXPATHLEN bytes. */
  static void
! copy_absolute(char *path, char *p)
  {
!     if (p[0] == SEP)
!         strcpy(path, p);
      else {
!         getcwd(path, MAXPATHLEN);
!         if (p[0] == '.' && p[1] == SEP)
!             p += 2;
!         joinpath(path, p);
      }
  }
  
! /* absolutize() requires that path be allocated at least MAXPATHLEN+1 bytes. */
! static void
! absolutize(char *path)
! {
!     char buffer[MAXPATHLEN + 1];
! 
!     if (path[0] == SEP)
!         return;
!     copy_absolute(buffer, path);
!     strcpy(path, buffer);
! }
! 
! /* search_for_prefix requires that argv0_path be no more than MAXPATHLEN
     bytes long.
  */
***************
*** 272,276 ****
  
      /* Search from argv0_path, until root is found */
!     init_path_from_argv0(prefix, argv0_path);
      do {
          n = strlen(prefix);
--- 277,281 ----
  
      /* Search from argv0_path, until root is found */
!     copy_absolute(prefix, argv0_path);
      do {
          n = strlen(prefix);
***************
*** 296,300 ****
  
  /* search_for_exec_prefix requires that argv0_path be no more than
!    MAXPATHLEN bytes long.  
  */
  static int
--- 301,305 ----
  
  /* search_for_exec_prefix requires that argv0_path be no more than
!    MAXPATHLEN bytes long.
  */
  static int
***************
*** 325,329 ****
  
      /* Search from argv0_path, until root is found */
!     init_path_from_argv0(exec_prefix, argv0_path);
      do {
          n = strlen(exec_prefix);
--- 330,334 ----
  
      /* Search from argv0_path, until root is found */
!     copy_absolute(exec_prefix, argv0_path);
      do {
          n = strlen(exec_prefix);
***************
*** 369,373 ****
      NSModule pythonModule;
  #endif
! 	
  #ifdef WITH_NEXT_FRAMEWORK
      pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize"));
--- 374,378 ----
      NSModule pythonModule;
  #endif
! 
  #ifdef WITH_NEXT_FRAMEWORK
      pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize"));
***************
*** 388,398 ****
          joinpath(argv0_path, LANDMARK);
          if (!ismodule(argv0_path)) {
!         	/* We are in the build directory so use the name of the 
!         	   executable - we know that the absolute path is passed */
!         	strncpy(progpath, prog, MAXPATHLEN);
          }
          else {
!         	/* Use the location of the library as the progpath */
! 	        strncpy(progpath, buf, MAXPATHLEN);
          }
      }
--- 393,403 ----
          joinpath(argv0_path, LANDMARK);
          if (!ismodule(argv0_path)) {
!                 /* We are in the build directory so use the name of the
!                    executable - we know that the absolute path is passed */
!                 strncpy(progpath, prog, MAXPATHLEN);
          }
          else {
!                 /* Use the location of the library as the progpath */
!                 strncpy(progpath, buf, MAXPATHLEN);
          }
      }
***************
*** 401,414 ****
             (even though NSNameOfModule() probably does the same thing.) */
  #endif
! 	
! 	/* If there is no slash in the argv0 path, then we have to
! 	 * assume python is on the user's $PATH, since there's no
! 	 * other way to find a directory to start the search from.  If
! 	 * $PATH isn't exported, you lose.
! 	 */
! 	if (strchr(prog, SEP))
              strncpy(progpath, prog, MAXPATHLEN);
! 	else if (path) {
! 	    int bufspace = MAXPATHLEN;
              while (1) {
                  char *delim = strchr(path, DELIM);
--- 406,418 ----
             (even though NSNameOfModule() probably does the same thing.) */
  #endif
! 
!         /* If there is no slash in the argv0 path, then we have to
!          * assume python is on the user's $PATH, since there's no
!          * other way to find a directory to start the search from.  If
!          * $PATH isn't exported, you lose.
!          */
!         if (strchr(prog, SEP))
              strncpy(progpath, prog, MAXPATHLEN);
!         else if (path) {
              while (1) {
                  char *delim = strchr(path, DELIM);
***************
*** 416,427 ****
                  if (delim) {
                      size_t len = delim - path;
! 		    if (len > bufspace)
! 			len = bufspace;
                      strncpy(progpath, path, len);
                      *(progpath + len) = '\0';
- 		    bufspace -= len;
                  }
                  else
!                     strncpy(progpath, path, bufspace);
  
                  joinpath(progpath, prog);
--- 420,430 ----
                  if (delim) {
                      size_t len = delim - path;
!                     if (len > MAXPATHLEN)
!                         len = MAXPATHLEN;
                      strncpy(progpath, path, len);
                      *(progpath + len) = '\0';
                  }
                  else
!                     strncpy(progpath, path, MAXPATHLEN);
  
                  joinpath(progpath, prog);
***************
*** 435,441 ****
                  path = delim + 1;
              }
! 	}
! 	else
              progpath[0] = '\0';
  #ifdef WITH_NEXT_FRAMEWORK
      }
--- 438,446 ----
                  path = delim + 1;
              }
!         }
!         else
              progpath[0] = '\0';
+         if (progpath[0] != SEP)
+             absolutize(progpath);
  #ifdef WITH_NEXT_FRAMEWORK
      }
***************
*** 443,447 ****
  
      strncpy(argv0_path, progpath, MAXPATHLEN);
! 	
  #if HAVE_READLINK
      {
--- 448,452 ----
  
      strncpy(argv0_path, progpath, MAXPATHLEN);
! 
  #if HAVE_READLINK
      {
***************
*** 452,457 ****
              tmpbuffer[linklen] = '\0';
              if (tmpbuffer[0] == SEP)
! 		/* tmpbuffer should never be longer than MAXPATHLEN,
! 		   but extra check does not hurt */
                  strncpy(argv0_path, tmpbuffer, MAXPATHLEN);
              else {
--- 457,462 ----
              tmpbuffer[linklen] = '\0';
              if (tmpbuffer[0] == SEP)
!                 /* tmpbuffer should never be longer than MAXPATHLEN,
!                    but extra check does not hurt */
                  strncpy(argv0_path, tmpbuffer, MAXPATHLEN);
              else {
***************
*** 473,477 ****
          if (!Py_FrozenFlag)
              fprintf(stderr,
!                     "Could not find platform independent libraries <prefix>\n");
          strncpy(prefix, PREFIX, MAXPATHLEN);
          joinpath(prefix, lib_python);
--- 478,482 ----
          if (!Py_FrozenFlag)
              fprintf(stderr,
!                 "Could not find platform independent libraries <prefix>\n");
          strncpy(prefix, PREFIX, MAXPATHLEN);
          joinpath(prefix, lib_python);
***************
*** 479,487 ****
      else
          reduce(prefix);
! 	
      if (!(efound = search_for_exec_prefix(argv0_path, home))) {
          if (!Py_FrozenFlag)
              fprintf(stderr,
!                     "Could not find platform dependent libraries <exec_prefix>\n");
          strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN);
          joinpath(exec_prefix, "lib/lib-dynload");
--- 484,492 ----
      else
          reduce(prefix);
! 
      if (!(efound = search_for_exec_prefix(argv0_path, home))) {
          if (!Py_FrozenFlag)
              fprintf(stderr,
!                 "Could not find platform dependent libraries <exec_prefix>\n");
          strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN);
          joinpath(exec_prefix, "lib/lib-dynload");