English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 01 June 2020, 21:19   #1
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
How to read file from executable directory?

I am using fopen to open a file to read this. To this I pass the name of the file I want to read. However the problem is the the path of the file to be read is relative to the location from where the executable is executed.

For example if I have the following:

System:SayHello/SayHelloExe
System:SayHello/HelloMessage.txt

If I go to System:SayHello to run SayHelloExe then it works fine. However if for example I go to System: and run SayHelloExe as follows "System:SayHello/SayHelloExe" then SayHelloExe will look for HelloMessage.txt in System: rather than in System:SayHello/SayHelloExe

One way I can think of to solve this if I could get the location of SayHelloExe when this is executing from args[0]. However I noticed that if I move SayHelloExe to System:C and run this from console as just SayHelloExe then there is no path location from where this is executing.

Is there a standard way this is done?
Sim085 is offline  
Old 01 June 2020, 22:12   #2
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
If you are targeting OS>=2.0 then the dynamic assign PROGDIR: would probably fit your needs.
http://www.amigareport.com/ar311/progdir.html
alkis is offline  
Old 02 June 2020, 00:01   #3
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
Thank you. This looks exactly what I am looking for.



Quote:
Originally Posted by alkis View Post
If you are targeting OS>=2.0 then the dynamic assign PROGDIR: would probably fit your needs.
http://www.amigareport.com/ar311/progdir.html
Sim085 is offline  
Old 02 June 2020, 22:04   #4
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
I do not know if anyone knows why this is happening. I am now loading my text file from "PROGDIR:HelloMessage.txt". The strange thing is that this works fine when I execute my program from shell. However when I use ZShell it does not work.

For example;

Given I have my executable and text file in "bin/", if I execute "bin/SayHello" from Shell it works fine but if I execute "bin/SayHello" from ZShell the file is not found. The solution works fine from both Shell and ZShell if I execute directly from "bin/". The solution also works fine if I copy it and the text file to "System:C". I then can run from both Shell and ZShell.

Has anyone ever faced this problem and knows why in ZShell running a solution in a sub-directory results in PROGDIR not being read correctly?

Code I am using to test this is as follows:

Code:
int main(int argc, char *argv[])
int main()
{
    char *path = "PROGDIR:HelloMessage.txt";
    FILE *f = (FILE *)fopen(path, "r");
    char *text = (char *)malloc(sizeof(char) * 255);
    fread(text, 1, 255, f);
    printf("%s\n\n",text);
}
Quote:
Originally Posted by Sim085 View Post
Thank you. This looks exactly what I am looking for.
Sim085 is offline  
Old 03 June 2020, 10:44   #5
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
Here is a screenshot of what I mean.



Above is same solution which I copied to System:C (together with the HelloMessage.txt) executed from Shell and ZShell. Using the full path to execute the solution works in Shell but not ZShell. Executing the solution directly works on both Shell and ZShell.

Would anyone know why the different behavior between ZShell and Shell when executing the solution using the full path System:C/SayHello?
In the end isn't it the same application being executed regardless of from where this has been triggered?

Is it a problem with the code? or code looks ok?

btw - please ignore the "Fail Limit" error at the end.
Attached Thumbnails
Click image for larger version

Name:	behavior.png
Views:	507
Size:	19.3 KB
ID:	67583  

Last edited by Sim085; 03 June 2020 at 11:50.
Sim085 is offline  
Old 03 June 2020, 12:56   #6
thomas
Registered User
 
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 7,002
I think PROGDIR: depends on the pr_HomeDir field in the CLI structure. This is a new field in OS 2.0 and as ZShell runs on 1.3, too, it probably does not set this field.
thomas is offline  
Old 03 June 2020, 13:22   #7
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
Thank you Thomas

That is some background I sure don't have

Quote:
Originally Posted by thomas View Post
I think PROGDIR: depends on the pr_HomeDir field in the CLI structure. This is a new field in OS 2.0 and as ZShell runs on 1.3, too, it probably does not set this field.

Last edited by Sim085; 03 June 2020 at 13:53.
Sim085 is offline  
Old 03 June 2020, 13:54   #8
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
Is there a way how to get the value of PROGDIR so that if this returns empty I can apply some workaround condition? I can try to open and if I get nothing I apply the work around. However was thinking if there is a more cleaner way to do it.
Sim085 is offline  
Old 03 June 2020, 15:52   #9
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
ZShell does:

Code:
	lea	pr_CurrentDir(a3),a3	(A3)=ptr to current dir
	move.l	(a3),d3			d3=current dir at beginning
	move.l	d3,d4			d4=PROGDIR:
	...
	...
	...
	cmp.w	#36,kickver(a5)
	blo.s	.A
	move.l	thistask(a5),a3
	move.l	d4,pr_HomeDir(a3)	init PROGDIR:
.A	move.l	d0,temp1(a5)		save the segment
so from the looks of it, it accounts for OS>=2.0 but set pr_HomeDir to pr_CurrentDir for some reason.
alkis is offline  
Old 03 June 2020, 18:31   #10
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,233
Quote:
Originally Posted by alkis View Post
So from the looks of it, it accounts for OS>=2.0 but set pr_HomeDir to pr_CurrentDir for some reason.

Without context, one cannot say whether this is correct or not. It depends on what the current directory is at the time of execution.



What the AmigaOs shell does is that it sets pr_HomeDir (and hence PROGDIR to the directory that contains the executable, or script. It is set to NIL for resident commands. Thus beware: If a program is pure, it also means that it cannot access data relative to PROGIDR:
Thomas Richter is offline  
Old 03 June 2020, 20:52   #11
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
What does "pure" mean?

Quote:
Originally Posted by Thomas Richter View Post
Thus beware: If a program is pure, it also means that it cannot access data relative to PROGIDR:
From what I can see, given the solution is placed in System:C from ZShell it will not work if executed as follows:

System> System:C/SayHello

or

System> C/SayHello

But will work if executed directly:

System>SayHello

I can also see that if the solution is placed in some other sub-directory, example located in System:MyProg/SayHello on ZShell it will not work if run as follows:

System>System:MyProg/SayHello

but will work if I first cd to MyProg

System>cd MyProg
System:MyProg>SayHello


On AmigaShell it always works fine.

Quote:
Originally Posted by Thomas Richter View Post
Without context, one cannot say whether this is correct or not.
Sim085 is offline  
Old 03 June 2020, 22:39   #12
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Absolute and relative path calling the exe doesn't find the datafile, unless you place the datafile in the current directory. (Tested)

I guess that behavior confirms my original interpretation.
alkis is offline  
Old 03 June 2020, 22:44   #13
thomas
Registered User
 
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 7,002
Quote:
Originally Posted by Sim085 View Post
What does "pure" mean?
https://en.wikipedia.org/wiki/Reentrancy_(computing)
thomas is offline  
Old 04 June 2020, 01:08   #14
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Can you give this zshell a try. I think I've patched the issue.

Source code included.
I use : vasmm68k_mot -nosym -kick1hunks -Fhunkexe -o zshell ZShell.s
to assemble it.

Last edited by alkis; 05 June 2020 at 00:09. Reason: removed zip. Fixed version a few posts below.
alkis is offline  
Old 04 June 2020, 12:42   #15
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
Hi alkis, Many thanks for this. Yes looks like this version works fine. I have attached screenshot below:



I can see you also added the result of last command issued in the brackets.

Quote:
Originally Posted by alkis View Post
Can you give this zshell a try. I think I've patched the issue.
Attached Thumbnails
Click image for larger version

Name:	now.png
Views:	419
Size:	4.9 KB
ID:	67596  
Sim085 is offline  
Old 04 June 2020, 13:42   #16
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Quote:
Originally Posted by Sim085 View Post
Hi alkis, Many thanks for this. Yes looks like this version works fine.
Great!

Quote:
Originally Posted by Sim085 View Post
I can see you also added the result of last command issued in the brackets.
Yeah, I like this.

You can
Code:
prompt "%P >"
if you don't like it.
alkis is offline  
Old 04 June 2020, 23:40   #17
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
Unfortunately I just discovered that the fix for PROGDIR seems to somehow has an effect on the command line arguments. In summary, the binary name is missing from argv[0].

Code:
int main(int argc, char *argv[])
{
    printf("\n\nargv0 = %s\n\n", argv[0]);
    return 0;
}
Attached Thumbnails
Click image for larger version

Name:	newerror.png
Views:	429
Size:	8.8 KB
ID:	67603  
Sim085 is offline  
Old 05 June 2020, 00:08   #18
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Oops. Yeah, fixed in this one. Sorry.
Attached Files
File Type: zip zshell.zip (102.9 KB, 104 views)
alkis is offline  
Old 05 June 2020, 00:19   #19
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
Works great. Amazing.

Quote:
Originally Posted by alkis View Post
Oops. Yeah, fixed in this one. Sorry.
Sim085 is offline  
Old 08 June 2020, 12:02   #20
bebbo
bye
 
Join Date: Jun 2016
Location: Some / Where
Posts: 680
Quote:
Originally Posted by Sim085 View Post
I am using fopen to open a file to read this. To this I pass the name of the file I want to read. However the problem is the the path of the file to be read is relative to the location from where the executable is executed.

For example if I have the following:

System:SayHello/SayHelloExe
System:SayHello/HelloMessage.txt

If I go to System:SayHello to run SayHelloExe then it works fine. However if for example I go to System: and run SayHelloExe as follows "System:SayHello/SayHelloExe" then SayHelloExe will look for HelloMessage.txt in System: rather than in System:SayHello/SayHelloExe

One way I can think of to solve this if I could get the location of SayHelloExe when this is executing from args[0]. However I noticed that if I move SayHelloExe to System:C and run this from console as just SayHelloExe then there is no path location from where this is executing.

Is there a standard way this is done?

The standard way:


use current directory = curdir
use program name = argv[0] = prog


if prog is absolute then
use prog
else
combine curdir with prog
endif


---
example


curdir = dh1:foo/bar, prog = exe
-> dh1:/foo/bar/exe


curdir = dh1:foo/bar, prog = /faa/exe
-> dh1:/foo/faa/exe

curdir = dh1:foo/bar, prog = dh0:exe
-> dh0:exe
bebbo is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Config file directory paths hexaae support.WinUAE 6 24 June 2018 17:28
Limit file size directory Seiya support.WinUAE 2 19 February 2018 19:29
Executable script file/arguments shrub3056 support.Apps 1 26 July 2011 18:08
File not executable amigapd support.Apps 3 22 February 2011 17:59
File not executable ancalimon support.WinUAE 8 15 February 2008 17:18

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 00:49.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.15320 seconds with 14 queries