www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000283

When Created: 05/25/1999 22:00:28
Against DJGPP version: 2.02
By whom: _pm_@yahoo.com
Abstract: searchpath() doesn't work with filenames that contain slashes
Hi, there is a bug in the searchpath() function, defined in <dir.h>.  If a filename
containing a slash is supplied as the parameter to this function, the function
always returns whatever was typed as existing in the filesystem.  The function
only works properly when non-slashed filenames relative to the current directory are passed as params.  This is not good.

Sample program:

#include <stdio.h>
#include <dir.h>

int main(int argc, char **argv) {
   printf("The file was: %s", searchpath(argv[1]));

   return 0;
}


If I type  "test.exe lala" and no directory or file named lala exists in the current
directory, the following is printed to the screen: The file was: (null).

However, if I type "test.exe lala\fkdhfkdhkhj", the following is printed:
The file was: lala\fkdhfkdhkh.  This also works with the forward slash, and with
names containing a drive spec.  In other words, it does this with ANY filename
containing ANY type of slash.

If I'm doing something wrong please let me know.  Otherwise, this is a bad bug.

Solution added: 06/03/1999 06:00:35
By whom: eliz@is.elta.co.il
This is indeed a bug, it is corrected in CVS and will be in v2.03.

Here's a source-level patch:

*** src/libc/dos/dir/srchpath.c~0       Sat Aug 31 22:09:32 1996                
--- src/libc/dos/dir/srchpath.c Thu Jun  3 12:10:30 1999                        
***************                                                                 
*** 4,9 ****                                                                    
--- 4,10 ----                                                                   
  #include <string.h>                                                           
  #include <stdlib.h>                                                           
  #include <stdio.h>                                                            
+ #include <errno.h>                                                            
  #include <unistd.h>                                                           
  #include <ctype.h>                                                            
  #include <limits.h>                                                           
*************** searchpath(const char *file)                                    
*** 29,34 ****                                                                  
--- 30,41 ----                                                                  
    static char found[PATH_MAX];                                                
    static char *path;                                                          
                                                                                
+   if (!file || !*file)                                                        
+     {                                                                         
+       errno = file ? ENOENT : EINVAL;                                         
+       return NULL;                                                            
+     }                                                                         
+                                                                               
    memset(found, 0, sizeof(found));                                            
                                                                                
    /* Get $PATH and store it for reuse.  */                                    
*************** searchpath(const char *file)                                    
*** 91,99 ****                                                                  
      else                                                                      
        path[1] = 0;                                                            
    }                                                                           
!   if (strpbrk (file, "/\\:") != 0)                                            
    {                                                                           
!     strcpy(found, file);                                                      
      return found;                                                             
    }                                                                           
    else                                                                        
--- 98,126 ----                                                                 
      else                                                                      
        path[1] = 0;                                                            
    }                                                                           
!                                                                               
!   /* Borland's version (as of BC 3.1) always disregards the leading           
!      directories and only looks at the file-name component.  So, for          
!      example, "foo/bar/baz.exe" could find "c:/bin/baz.exe".  But that        
!      doesn't seem right, so we don't follow their lead here.  */              
!                                                                               
!   /* If the file name includes slashes or the drive letter, maybe they        
!      already have a good name.  */                                            
!   if (strpbrk (file, "/\\:") != 0 && __file_exists(file))                     
    {                                                                           
!     if (file[0] == '/' || file[0] == '\\' || file[1] == ':'                   
!       || (file[0] == '.'                                                      
!           && (file[1] == '/' || file[1] == '\\'                               
!               || (file[1] == '.'                                              
!                   && (file[2] == '/' || file[2] == '\\')))))                  
!       /* Either absolute file name or it begins with a "./".  */              
!       strcpy(found, file);                                                    
!     else                                                                      
!     {                                                                         
!       /* Relative file name: add "./".  */                                    
!       strcpy(found, "./");                                                    
!       strcat(found, file);                                                    
!     }                                                                         
      return found;                                                             
    }                                                                           
    else                                                                        
*************** searchpath(const char *file)                                    
*** 124,130 ****                                                                
        test_dir = dp + 1;                                                      
      } while (*test_dir != 0);                                                 
    }                                                                           
!                                                                               
    return NULL;                                                                
  }                                                                             
                                                                                
--- 151,160 ----                                                                
        test_dir = dp + 1;                                                      
      } while (*test_dir != 0);                                                 
    }                                                                           
!                                                                               
!   /* FIXME: perhaps now that we failed to find it, we should try the          
!      basename alone, like BC does?  But let somebody complain about           
!      this first... ;-) */                                                     
    return NULL;                                                                
  }

Fixed in version on 06/03/1999 06:00:14
By whom: eliz@is.elta.co.il



  webmaster     delorie software   privacy  
  Copyright © 2010   by DJ Delorie     Updated Jul 2010