01 June 2020, 21:19 | #1 |
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? |
01 June 2020, 22:12 | #2 |
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 |
02 June 2020, 00:01 | #3 | |
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 962
|
Thank you. This looks exactly what I am looking for.
Quote:
|
|
02 June 2020, 22:04 | #4 |
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); } |
03 June 2020, 10:44 | #5 |
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. Last edited by Sim085; 03 June 2020 at 11:50. |
03 June 2020, 12:56 | #6 |
Registered User
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.
|
03 June 2020, 13:22 | #7 |
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 962
|
Thank you Thomas
That is some background I sure don't have Last edited by Sim085; 03 June 2020 at 13:53. |
03 June 2020, 13:54 | #8 |
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.
|
03 June 2020, 15:52 | #9 |
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 |
03 June 2020, 18:31 | #10 | |
Registered User
Join Date: Jan 2019
Location: Germany
Posts: 3,233
|
Quote:
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: |
|
03 June 2020, 20:52 | #11 | |
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 962
|
What does "pure" mean?
Quote:
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. |
|
03 June 2020, 22:39 | #12 |
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. |
03 June 2020, 22:44 | #13 |
Registered User
Join Date: Jan 2002
Location: Germany
Posts: 7,002
|
|
04 June 2020, 01:08 | #14 |
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. |
04 June 2020, 12:42 | #15 |
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. |
04 June 2020, 13:42 | #16 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
|
04 June 2020, 23:40 | #17 |
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; } |
05 June 2020, 00:08 | #18 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
Oops. Yeah, fixed in this one. Sorry.
|
05 June 2020, 00:19 | #19 |
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 962
|
|
08 June 2020, 12:02 | #20 | |
bye
Join Date: Jun 2016
Location: Some / Where
Posts: 680
|
Quote:
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 |
|
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 |
|
|