Do not check for or interpret tilde in file paths. mpictor/rbertucat
This commit is contained in:
+2
-81
@@ -92,10 +92,7 @@ DirObj::~DirObj() {
|
||||
//
|
||||
// Returns a real path.
|
||||
// if 'path' is a null string ("\0") or nil (0) return ./
|
||||
// otherwise return the path remaining after the following is done:
|
||||
// send the remaining path after the first '/' in the last occurence
|
||||
// of '//' to InterpTilde() where the last tilde will be expanded
|
||||
// to a full path.
|
||||
// otherwise return the path.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -105,7 +102,7 @@ const char * DirObj::RealPath( const char * path ) {
|
||||
if( path == 0 || *path == '\0' ) {
|
||||
realpath = "./";
|
||||
} else {
|
||||
realpath = InterpTilde( InterpSlashSlash( path ) );
|
||||
realpath = path;
|
||||
}
|
||||
return realpath;
|
||||
}
|
||||
@@ -179,20 +176,6 @@ boolean DirObj::IsADirectory( const char * path ) {
|
||||
return stat( path, &st ) == 0 && ( st.st_mode & S_IFMT ) == S_IFDIR;
|
||||
}
|
||||
|
||||
//////////////////////////////// Home() ///////////////////////////////////////
|
||||
//
|
||||
// Return the home directory (complete path) for user with login name 'name'.
|
||||
// If 'name' is nil, it returns the home directory of the logged-in user who
|
||||
// ran the program. Returns nil on error - (name not in password file).
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const char * DirObj::Home( const char * name ) {
|
||||
struct passwd * pw =
|
||||
( name == nil ) ? getpwuid( getuid() ) : getpwnam( ( char * )name );
|
||||
return ( pw == nil ) ? nil : pw->pw_dir;
|
||||
}
|
||||
|
||||
//////////////////////////////// Normalize() //////////////////////////////////
|
||||
//
|
||||
// Normalize path in order in the following way:
|
||||
@@ -222,7 +205,6 @@ const char * DirObj::Normalize( const char * path ) {
|
||||
buf = InterpSlashSlash( path );
|
||||
buf = ElimDot( buf );
|
||||
buf = ElimDotDot( buf );
|
||||
buf = InterpTilde( buf );
|
||||
|
||||
if( *buf == '\0' ) {
|
||||
strcpy( newpath, "./" );
|
||||
@@ -381,67 +363,6 @@ const char * DirObj::ElimDotDot( const char * path ) {
|
||||
return newpath;
|
||||
}
|
||||
|
||||
//////////////////////////////// InterpTilde() ////////////////////////////////
|
||||
//
|
||||
// if 'path' has no tilde or an error happens when expanding the tilde,
|
||||
// return 'path'.
|
||||
// if 'path' has a tilde, it must be at the beginning of 'path' or
|
||||
// follow a '/'
|
||||
// if a 'name' follows tilde, return a new path consisting of the
|
||||
// last ~ in 'path' expanded to the 'name's home directory
|
||||
// followed by the remaining path after name (if such exists)
|
||||
// if no 'name' follows tilde, return a new path consisting of the
|
||||
// last ~ in 'path' expanded to the home directory of the
|
||||
// logged-in user who ran the process followed by the remaining
|
||||
// path after ~ (if such exists)
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const char * DirObj::InterpTilde( const char * path ) {
|
||||
static char realpath[MAXPATHLEN + 1];
|
||||
const char * beg = strrchr( path, '~' );
|
||||
boolean validTilde = beg != NULL && ( beg == path || *( beg - 1 ) == '/' );
|
||||
|
||||
if( validTilde ) {
|
||||
const char * end = strchr( beg, '/' );
|
||||
int length = ( end == nil ) ? strlen( beg ) : end - beg;
|
||||
const char * expandedTilde = ExpandTilde( beg, length );
|
||||
|
||||
if( expandedTilde == nil ) {
|
||||
validTilde = 0;
|
||||
} else {
|
||||
strcpy( realpath, expandedTilde );
|
||||
if( end != nil ) {
|
||||
strcat( realpath, end );
|
||||
}
|
||||
}
|
||||
}
|
||||
return validTilde ? realpath : path;
|
||||
}
|
||||
|
||||
//////////////////////////////// ExpandTilde() ////////////////////////////////
|
||||
//
|
||||
// 'tildeName' must start with a tilde.
|
||||
// if a name immediately follows ~ return a path expanded to the
|
||||
// name's home directory.
|
||||
// if tildeName is only a ~ or if a '/' immediately follows ~
|
||||
// return a path expanded to the home directory of the logged-in
|
||||
// user who ran the program.
|
||||
// return nil on error (if the name is not in the password file)
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const char * DirObj::ExpandTilde( const char * tildeName, int length ) {
|
||||
const char * name = nil;
|
||||
|
||||
if( length > 1 ) {
|
||||
static char buf[MAXNAMLEN + 1];
|
||||
strncpy( buf, tildeName + 1, length - 1 );
|
||||
buf[length - 1] = '\0';
|
||||
name = buf;
|
||||
}
|
||||
return Home( name );
|
||||
}
|
||||
|
||||
//////////////////////////////// CheckIndex() /////////////////////////////////
|
||||
//
|
||||
|
||||
@@ -13,13 +13,6 @@
|
||||
* and is not subject to copyright.
|
||||
*/
|
||||
|
||||
/* $Id: dirobj.h,v 3.0.1.3 1997/11/05 22:33:45 sauderd DP3.1 $ */
|
||||
|
||||
#ifdef __O3DB__
|
||||
#include <OpenOODB.h>
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* DirObj - This object contains a list of files in a directory.
|
||||
* Can be used for interpreting a tilde, checking paths etc.
|
||||
@@ -31,8 +24,6 @@
|
||||
* David Sauder
|
||||
*/
|
||||
|
||||
//#include <InterViews/filebrowser.h>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// These header files are a pain between compilers etc ... and
|
||||
@@ -50,11 +41,6 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
////extern _G_uid_t getuid _G_ARGS((void));
|
||||
//#ifdef HAVE_STAT_H
|
||||
//#include <stat.h>
|
||||
//#endif
|
||||
|
||||
#ifdef HAVE_SYSENT_H
|
||||
#include <sysent.h>
|
||||
#endif
|
||||
@@ -64,30 +50,8 @@
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
//#ifdef __GNUC__
|
||||
//#include <unistd.h> // needed to declare getuid() with new GNU 2.3.3
|
||||
////extern _G_uid_t getuid _G_ARGS((void));
|
||||
//#include <stat.h>
|
||||
|
||||
//#else
|
||||
////#ifdef __OBJECTCENTER__
|
||||
////#ifdef __SUNCPLUSPLUS__
|
||||
////#ifdef __xlC__
|
||||
//#include <sysent.h>
|
||||
//#include <sys/stat.h>
|
||||
//#endif
|
||||
|
||||
#include <pwd.h>
|
||||
//#include <InterViews/Std/os/auth.h>
|
||||
//#include <InterViews/Std/os/fs.h>
|
||||
// the below is good for g++
|
||||
// if getting it from /usr/include change to __string_h
|
||||
#ifndef _std_h
|
||||
#include <string.h>
|
||||
#endif
|
||||
#include <sys/param.h>
|
||||
//#include <InterViews/Std/sys/dir.h>
|
||||
//#include <InterViews/Std/sys/stat.h>
|
||||
|
||||
#include <scldir.h>
|
||||
|
||||
@@ -116,12 +80,9 @@ public:
|
||||
|
||||
boolean IsADirectory(const char*);
|
||||
private:
|
||||
const char* Home(const char* x = nil);
|
||||
const char* ElimDot(const char*);
|
||||
const char* ElimDotDot(const char*);
|
||||
const char* InterpSlashSlash(const char*);
|
||||
const char* InterpTilde(const char*);
|
||||
const char* ExpandTilde(const char*, int);
|
||||
const char* RealPath(const char*);
|
||||
|
||||
boolean Reset(const char*);
|
||||
|
||||
+2
-51
@@ -71,7 +71,6 @@
|
||||
#include "conf.h"
|
||||
#include "express/basic.h"
|
||||
#include <ctype.h>
|
||||
#include <pwd.h>
|
||||
#include <stdlib.h>
|
||||
#include <setjmp.h>
|
||||
#include <errno.h>
|
||||
@@ -137,46 +136,6 @@ typedef struct Dir {
|
||||
char * leaf;
|
||||
} Dir;
|
||||
|
||||
/* expand ~ in-place, blindly assuming argument has extra space */
|
||||
/* if translation is valid, return original address, else return 0 */
|
||||
/* note: returns result in static buffer! */
|
||||
/* assume arg begins at first char past tilde */
|
||||
static
|
||||
char *
|
||||
tilde_translate( char * name ) {
|
||||
static char output[1024];
|
||||
struct passwd * pwd;
|
||||
char * new;
|
||||
char * slash;
|
||||
|
||||
/* we'll need to put back string beginning at slash+1 */
|
||||
slash = strchr( name, '/' );
|
||||
if( slash ) {
|
||||
*slash = '\0';
|
||||
}
|
||||
|
||||
/* "~" "~/" */
|
||||
if( ( name[0] == '\0' ) || ( name == slash ) ) {
|
||||
/* get our own home dir */
|
||||
new = getenv( "HOME" );
|
||||
} else {
|
||||
/* "~name" */
|
||||
pwd = getpwnam( name );
|
||||
new = ( pwd ? pwd->pw_dir : 0 );
|
||||
}
|
||||
if( !new ) {
|
||||
ERRORreport( ERROR_tilde_expansion_failed, name - 1 );
|
||||
return( 0 );
|
||||
}
|
||||
if( slash ) {
|
||||
sprintf( output, "%s/%s", new, slash + 1 );
|
||||
} else {
|
||||
strcpy( output, new );
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
EXPRESS_PATHinit() {
|
||||
char * p;
|
||||
@@ -231,16 +190,8 @@ EXPRESS_PATHinit() {
|
||||
continue;
|
||||
}
|
||||
|
||||
/* if does not begin with a tilde */
|
||||
if( start[0] != '~' ) {
|
||||
length = ( p - 1 ) - start;
|
||||
} else {
|
||||
start = tilde_translate( start + 1 );
|
||||
if( !start ) {
|
||||
break;
|
||||
}
|
||||
length = strlen( start );
|
||||
}
|
||||
//removed tilde logic; below line is all that's left
|
||||
length = ( p - 1 ) - start;
|
||||
|
||||
/* if slash present at end, don't add another */
|
||||
slash = strrchr( start, '/' );
|
||||
|
||||
Reference in New Issue
Block a user