By default, Bugzilla does not search the list of RESOLVED bugs.
You can force it to do so by putting the upper-case word ALL in front of your search query, e.g.: ALL tdelibs
We recommend searching for bugs this way, as you may discover that your bug has already been resolved and fixed in a later release.

Bug 168

Summary: set the working directory to the same directory as the executable when running an executable directly in konqueror
Product: TDE Reporter: Whistler <whistler_wmz>
Component: tdelibsAssignee: Timothy Pearson <kb9vqf>
Status: RESOLVED FIXED    
Severity: minor CC: bugwatch, darrella
Priority: P5    
Version: 3.5.11 [Trinity]   
Hardware: All   
OS: Linux   
Compiler Version: TDE Version String:
Application Version: Application Name:

Description Whistler 2010-04-05 03:22:33 CDT
currently trying to run an executable or script by double-click an executable in konqueror will not change the working directory for the executable, i.e., the program will run at the working directory of konqueror despite of where the executable/script itself is located.

for example,

cd /a/b/c/d/e
/usr/bin/konqueror

...and any executable started within the opened konqueror window will have /a/b/c/d/e as working directory.

This behavior doesn't allow simple running of many programs which are not distributed as rpm/deb packages but intended to extract & run directly, as well as many scripts which do something with the files within the working directory. Although it's possible to write a script or create a launcher, it would be annoying and not quite friendly.

here's a patch to fix this:

for kdelibs/kio/kio/krun.cpp:

pid_t KRun::runCommand( const QString& cmd, const QString &execName, const
QString & iconName )
{
  kdDebug(7010) << "runCommand " << cmd << "," << execName << endl;
  KProcess * proc = new KProcess;
  proc->setUseShell(true);
  *proc << cmd;
  KService::Ptr service = KService::serviceByDesktopName( binaryName( execName,
true ) );
+ QStringList args = KShell::splitArgs( cmd );
+ for (QStringList::ConstIterator it = args.begin(); it != args.end(); ++it)
+   if (!(*it).contains('='))
+     proc->setWorkingDirectory((*it).mid(0, (*it).findRev('/')));
  return runCommandInternal( proc, service.data(), binaryName( execName, false
), execName, iconName );
}
Comment 1 Timothy Pearson 2010-04-05 12:53:43 CDT
Uploaded to SVN in revision 1111449.  Packages for Lucid should be automatically built by the end of the day.

Thanks for reporting (and for the patch!)
Comment 2 Timothy Pearson 2010-04-06 11:58:50 CDT
Patch still needs some cleanup apparently.  Can you suggest a non-loop-based patch?

> On Monday 05 April 2010, Timothy Pearson wrote:
> > +  for (QStringList::ConstIterator it = args.begin(); it != args.end();
> > ++it) +  if (!(*it).contains('='))
> > +    proc->setWorkingDirectory((*it).mid(0, (*it).findRev('/')));
> 
> Why a loop?
> 
> Exec=/foo/bar -e /tmp/blah
> will start in /tmp ?
> 
> There's already a method to extract the binary name from the Exec line, better 
> use that.
> 
> -- 
> David Faure, faure@kde.org, http://www.davidfaure.fr
> Sponsored by Nokia to work on KDE, incl. Konqueror (http://www.konqueror.org).


Thanks!
Comment 3 Whistler 2010-04-08 11:45:04 CDT
well I think the KRun::binaryName() method only returns the filename of the binary rather than the full path, which could not be used here...
e.g., returns "ls" for "AAAA=bbbb CCCC=dddd /bin/ls /usr/lib"
I'm not really completely familiar with the code so I could be wrong though.

(Comment #2)
> Patch still needs some cleanup apparently.  Can you suggest a non-loop-based
> patch?
> 
> > On Monday 05 April 2010, Timothy Pearson wrote:
> > > +  for (QStringList::ConstIterator it = args.begin(); it != args.end();
> > > ++it) +  if (!(*it).contains('='))
> > > +    proc->setWorkingDirectory((*it).mid(0, (*it).findRev('/')));
> > 
> > Why a loop?
> > 
> > Exec=/foo/bar -e /tmp/blah
> > will start in /tmp ?
> > 
> > There's already a method to extract the binary name from the Exec line, better 
> > use that.
Comment 4 Whistler 2010-04-08 11:51:35 CDT
> Exec=/foo/bar -e /tmp/blah
> will start in /tmp ?

oops, I got this wrong, sorry...

QStringList args = KShell::splitArgs( cmd );
for (QStringList::ConstIterator it = args.begin(); it != args.end(); ++it) {
  if (!(*it).contains('=')) {
    int pos = (*it).findRev('/');
    if (pos != -1) {
       proc->setWorkingDirectory((*it).mid(0, pos));
    }
    break;
  }
}
Comment 5 Whistler 2010-04-11 05:25:00 CDT
d*mn, looks like I was stupid and did overlook the "removePath" parameter of KRun::binaryName() function...
sorry for that, hope I can get it correctly this time :)

  QString bin = binaryName( cmd, false );
  int pos = bin.findRev( '/' );
  if (pos != -1) {
    proc->setWorkingDirectory( bin.mid(0, pos) );
  }


(Comment #3)
> well I think the KRun::binaryName() method only returns the filename of the
> binary rather than the full path, which could not be used here...
> e.g., returns "ls" for "AAAA=bbbb CCCC=dddd /bin/ls /usr/lib"
Comment 6 Timothy Pearson 2010-04-13 20:21:23 CDT
Uploaded to SVN in revision 1114572.