Wrong CWD when running Wine programs from file manager,

New to Debian (Or Linux in general)? Ask your questions here!

Wrong CWD when running Wine programs from file manager,

Postby bicyclesonthemoon » 2018-05-14 17:47

Hello,
I was not completely sure if this's place is really in the beginner section, but my feeling is that I'm missing something obvious here that everybody else knows.

I have Debian 9 with LXDE.

I wrote a simple program and compiled it for Windows with mingw.
When testing the program I discovered that if I run the program by double clicking it (wrich opens it in Wine) then the current working directory was my home directory (instead of the directory where the exe is located and where it was clicked from the file manager).

I prepared a simple test program:
Code: Select all
#include <stdio.h>
#include <unistd.h>

int main (int argc, char **argv){
   char cwd[1024];
   FILE* file;
   getcwd(cwd, sizeof(cwd));
   file=fopen("Z:\\b\\pro\\cwdtest\\cwd.txt","w");
   if (file != NULL) {
      fprintf(file,"%s\n",cwd);
      fclose (file);
   }
   printf("%s\n",cwd);
}
which would write to some file its current working directory.
Some context: file is cwdwin.c in directory /b/pro/cwdtest and my home directory is /b
compiled with
Code: Select all
b@balt3:~/pro/cwdtest$ i686-w64-mingw32-g++ -o cwdwin.exe cwdwin.c
b@balt3:~/pro/cwdtest$
and ran ot twice:
Once from the command line:
Code: Select all
b@balt3:~/pro/cwdtest$ wine cwdwin.exe
Z:\b\pro\cwdtest
b@balt3:~/pro/cwdtest$
and the CWD was "Z:\b\pro\cwdtest".
and once by double-clicking cwdwin.exe from the file manager in directory /b/pro/cwdtest and the CWD was "Z:\b".

So it is true: When I run the program from command line the CWD is the directory from where I call it but if I click it from file manager CWD is my home directory.

Why is it like this?
How can I change it?

I wanted to find out if the problem is with Wine or with the file manager.
A very simple test: prepare a similar test program but this time compile it natively and not for windows.
I made:
Code: Select all
#include <stdio.h>
#include <unistd.h>

int main (int argc, char **argv){
   char cwd[1024];
   FILE* file;
   getcwd(cwd, sizeof(cwd));
   file=fopen("/b/pro/cwdtest/cwd.txt","w");
   if (file != NULL) {
      fprintf(file,"%s\n",cwd);
      fclose (file);
   }
   printf("%s\n",cwd);
}
I compiled
Code: Select all
b@balt3:~/pro/cwdtest$ g++ -o cwd cwd.c
b@balt3:~/pro/cwdtest$

I ran it from command line
Code: Select all
b@balt3:~/pro/cwdtest$ ./cwd
/b/pro/cwdtest
b@balt3:~/pro/cwdtest$
and like expected the CWD was "/b/pro/cwdtest"
I tried to run it by double clicking it from the file manager but I couldn't.
I detected another problem:
I'm not able to run native programs by double clicking on them. Not only this one but any program too.
When I click it it just asks me what program to open it with. And apparently it says that this is a "shared library" and not an executable file
(but it is an executable program and the execute bit is set). In Debian Wheezy I was able to run all programs by clicking them. On Stretch I can't.
I can run executable #! scripts, however. After clicking it says it's an executable script and I can chose to run or open it or cancel. Similar window appeared in Wheezy when clicking executable programs.
But here it thinks it's a shared library and doesn't give me this choice which is annoying.
Is this a MIME problem?
How do I solve it?
So because of this additional problem I was unable to determine if my original problem is fault of Wine or file manager.

So my actual problems are:

1. Why if I run Wine programs from file manager then CWD is set to my home directory? How to fix this?

2. Why I can't run normal programs from file manager? How to fix this?

I'll just add that this is a relatively recently installed system and so far only things I did with it was moving home directory from /home/b to /b, instaling some packets from the package manager, browsing some internet and writing some programs so there is low probability that I broke this by myself by some stupid actions.

Thanks in advance and please tell me if I have to provide additional information.
Last edited by bicyclesonthemoon on 2018-05-14 20:36, edited 1 time in total.
bicyclesonthemoon
 
Posts: 6
Joined: 2018-03-06 22:00

Re: Wrong CWD when running Wine programs from file manager,

Postby debiman » 2018-05-14 18:21

bicyclesonthemoon wrote:So it is true: When I run the program from command line the CWD is the directory from where I call it but if I click it from file manager CWD is my home directory.

this (the latter) has something to do with the .desktop file that is associated with launching the app.
you need to read up on https://freedesktop.org/wiki/Specificat ... ntry-spec/
User avatar
debiman
 
Posts: 2209
Joined: 2013-03-12 07:18

Re: Wrong CWD when running Wine programs from file manager,

Postby bicyclesonthemoon » 2018-05-14 20:34

I did some reading and I found this:
https://bugs.freedesktop.org/show_bug.cgi?id=97226
So it looks like my problem #2 (not being able to launch executable files from file manager) is caused by the fact that in Debian 9 the default form of executable files (compiled by me or already present in the system) is PIE (whatever that means) which is not distinguished by the MIME system from a shared executable.
Did I get it right?

So if I understand it correctly it's not that there is something wrong just for me but generally, on this version of Debian the functionality of running programs by clicking them in the file manager is broken and apparently not much is done about this.

I just cannot believe how this is even possible that such a basic and important functionality of the OS is missing and everyone allows it to be like this.

I just confirmed it by compiling it with -no-pie; I was able to run it by clicking then.
the CWD was "/b/pro/cwdtest" which means that my problem #1 (wrong CWD) is happening only for Wine.

So, if mime system is not able to distinguish some executable files from shared libraries, can I make the file manager treat shared libraries the same way as executables, so that clicking them will give me an option to run them? how? Can something bad happen it this is done?
bicyclesonthemoon
 
Posts: 6
Joined: 2018-03-06 22:00

Re: Wrong CWD when running Wine programs from file manager,

Postby debiman » 2018-05-15 17:04

i think you're misunderstanding & overinterpreting something there.

what i meant is sth along the lines that it is possible to tell .desktop files which working directory to use.
or maybe not, i'm not so sure now.
imo, the way .desktop files and the associated applications work, it does not make much sense to always use the current working directory (where the application is called from), like with shell utilities.
you can however pass the current directory as an argument to your program.

your whole example looks like a proof-of-concept sort of thing, or an excercise, that has no practical value.

i'd just file what you learned and move on to the next excercise.
User avatar
debiman
 
Posts: 2209
Joined: 2013-03-12 07:18

Re: Wrong CWD when running Wine programs from file manager,

Postby bw123 » 2018-05-15 18:40

Here is some more reading...
https://www.debian.org/releases/stable/ ... ow-default

bicyclesonthemoon wrote:...So if I understand it correctly it's not that there is something wrong just for me but generally, on this version of Debian the functionality of running programs by clicking them in the file manager is broken and apparently not much is done about this.
...


What do you mean by "the" file manager. I use mc, and it does just fine if I want to click a file that is marked as executable whether PIE or not? I checked dolphin, and it does report PIE ELF as "shared library" but they can be opened with xterm -e and work fine. Maybe slow down and explore your filemanager a little more... or try another, there are many.

good luck.

p.s. I have zero knowledge of wine, so I can't comment on it.

another edit: before someone beats me to it
http://linux.oneandoneis2.org/LNW.htm
Last edited by bw123 on 2018-05-15 19:46, edited 1 time in total.
User avatar
bw123
 
Posts: 2987
Joined: 2011-05-09 06:02
Location: TN_USA

Re: Wrong CWD when running Wine programs from file manager,

Postby bicyclesonthemoon » 2018-05-15 19:45

"the" file manager here is PCManFM, which is the default for Debian with LXDE.
mc which I also use doesn't have problems.

"xterm -e" indeed allows me to run executable files from pcmanfm but then the CWD is changed to my home directory too.
So it looks like this is fault of PCManFM that the CWD is changed. Like you said.
So far did not find out if this can be changed.
bicyclesonthemoon
 
Posts: 6
Joined: 2018-03-06 22:00

Re: Wrong CWD when running Wine programs from file manager,

Postby bw123 » 2018-05-15 19:50

Your c looks good and you obviously know how to research things, so if it's a bug file it (with a workaround or patch if possible) and if it's a config issue tweak it.
User avatar
bw123
 
Posts: 2987
Joined: 2011-05-09 06:02
Location: TN_USA


Return to Beginners Questions

Who is online

Users browsing this forum: 4D696B65, jmgibson1981 and 1 guest

fashionable