Author |
Topic: Re: [RC] Unix/ls (Read 898 times) |
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: [RC] Unix/ls
« Thread started on: Oct 24th, 2016, 4:40pm » |
|
Anatoly wrote at the LB Community Forum:
Quote:somehow I have StartupDir$ equal to DefaultDir$, equal to "full_path" where programs lies - and no reference to current directory it run from (c:\foo etc)
Does it work for you? Please share. |
|
I'd like to share there, but I can't . This is my understanding:
DefaultDir$ points to the directory in which your program is stored. So if you are loading resource files like sprites or music you can specify their location with respect to DefaultDir$ and ensure they will be found. In Anatoly's case this would be full_path.
StartupDir$ (introduced in LB 4.04) points to the directory from which your program is run, i.e. the 'current directory' at the time. In Anatoly's case this would be c:\foo. I've checked what LBB does, and it seems to correspond with the above. It's obviously important that you can discover in your program where it was run from, because you may want it to read or write files in that directory - or, in the case of the specific Rosetta Code task, list the files there.
Richard.
|
|
Logged
|
|
|
|
tsh73
Full Member
member is offline
Gender:
Posts: 210
|
|
Re: [RC] Unix/ls
« Reply #1 on: Oct 24th, 2016, 8:49pm » |
|
Yes I think it should work the same as you said. Yes LBB works the same you (and I) think. Actually I tested code with LBB after start having problems with LB. I still think I did something wrong since noone complained about StartupDir$.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: [RC] Unix/ls
« Reply #2 on: Oct 24th, 2016, 9:56pm » |
|
on Oct 24th, 2016, 8:49pm, tsh73 wrote:I still think I did something wrong since noone complained about StartupDir$. |
|
To make StartupDir$ work correctly in LB 4.04 execute your program using a .bat file, not by running the 'compiled' .exe. So in your case create a batch file (for example ls.bat) containing the following command, copied from your original post:
Code:full_path\test1.exe full_path\test1.tkn (you can add command-line parameters if necessary).
Now you should find that if you run ls StartupDir$ will contain the directory from which you ran it.
Richard.
|
|
Logged
|
|
|
|
Jack Kelly
Full Member
member is offline
Gender:
Posts: 106
|
|
Re: [RC] Unix/ls
« Reply #3 on: Oct 25th, 2016, 11:16am » |
|
I've been looking with some confusion at Anatoly's trials and tribulations over directories. I'm running XP (still...). When I start LBB and run 'print DefaultDir$' I get
Code:C:\DOCUME~1\Jack\LOKALE~1\Temp
Where does this come from? I was never aware of such a 'Temp' directory, much less ever having used it. But it has never presented any problem to my knowledge.
|
|
Logged
|
|
|
|
tsh73
Full Member
member is offline
Gender:
Posts: 210
|
|
Re: [RC] Unix/ls
« Reply #4 on: Oct 25th, 2016, 11:38am » |
|
Quote:But it has never presented any problem to my knowledge. |
|
*snorted by a hot tea*
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: [RC] Unix/ls
« Reply #5 on: Oct 25th, 2016, 11:47am » |
|
on Oct 25th, 2016, 11:16am, Jack Kelly wrote:I've been looking with some confusion at Anatoly's trials and tribulations over directories. |
|
Everything that Anatoly has said is accurate, as far as I know.
Quote:Where does this come from? I was never aware of such a 'Temp' directory, much less ever having used it. |
|
How long have you been using Windows? Not being aware of the 'temp' directory is, shall we say, surprising (hint: it's the directory which corresponds to the %temp% environment variable)! If you never clear it out it must be full of a huge amount of garbage by now.
Incidentally the description of DefaultDir$ and StartupDir$ that you posted at the LB forum is entirely incorrect.
Richard.
|
|
|
|
tsh73
Full Member
member is offline
Gender:
Posts: 210
|
|
Re: [RC] Unix/ls
« Reply #6 on: Oct 25th, 2016, 12:19pm » |
|
Richard, I've tried to search - but I did not found something like "then program run from IDE, LBB creates bbcwrun6.exe in %TEMP% folder and runs it. It gets deleted then you quit IDE" (this is my impression of how things work) Is it documented somewhere? Is there something that is useful to know about it?
Jack, Quote:But it has never presented any problem to my knowledge. |
|
Actually things you don't aware of never presents a problem. Problem happens then you meet that thing you never knew was there.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: [RC] Unix/ls
« Reply #7 on: Oct 25th, 2016, 2:04pm » |
|
on Oct 25th, 2016, 12:19pm, tsh73 wrote:Is it documented somewhere? |
|
No.
Quote:Is there something that is useful to know about it? |
|
No (unless you know different).
The temp directory is guaranteed to exist and is guaranteed to be writable. Many (probably most) applications make use of it as somewhere to store files temporarily - the clue is in the name! In general they do not document this usage since it is of no relevance to the end-user.
Typically, unless you have cleared it out recently, the temp directory will contain hundreds if not thousands of files, often with names and contents that give no clue to their origin or purpose. This is how it has always been.
https://en.wikipedia.org/wiki/Temporary_folder
Richard.
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: [RC] Unix/ls
« Reply #8 on: Oct 25th, 2016, 3:02pm » |
|
on Oct 24th, 2016, 9:56pm, Richard Russell wrote:Now you should find that StartupDir$ will contain the directory from which you ran it. |
|
Like you I am getting inconsistent results, which is disconcerting. I suspect one of the reasons may be that I am using the same directory to test both LB 4.04 and LB 4.50 executables, but of course when you 'compile' a program in LB it copies a whole load of DLLs and SLLs into that directory. So compiling a program in LB 4.5.0 can potentially affect how the '4.04 version' seems to work, and compiling a program in LB 4.04 can affect how the '4.5.0 version' seems to work!
To avoid that potential conflict I am now compiling the LB 4.04 and LB 4.5.0 versions to different directories: c:\temp\404 and c:\temp\450 respectively. Having done that I seem to be getting more consistent results, but unfortunately those results seem to be suggesting that StartupDir$ is broken in LB 4.5.0!
The test program I am using is this:
Code: cd$ = space$(_MAX_PATH)
calldll #kernel32, "GetCurrentDirectoryA", _MAX_PATH as long, cd$ as ptr, r as long
cd$ = left$(cd$, r)
print "Current Directory = "; cd$
print "DefaultDir$ = "; DefaultDir$
print "StartupDir$ = "; StartupDir$
print "Press any key to quit"
dummy$ = input$(1)
end If I compile it with LB 4.04 and run it from the c:\foo\bar directory this is what I get:
LB 4.04 Code:Current Directory = C:\temp\404
DefaultDir$ = C:\temp\404
StartupDir$ = C:\foo\bar
Press any key to quit but if I compile it with LB 4.5.0 this is the result:
LB 4.5.0 Code:Current Directory = C:\temp\450
DefaultDir$ = C:\temp\450
StartupDir$ = C:\temp\450\
Press any key to quit So if this is genuinely indicating a bug in LB 4.5.0 it's another reason (on top of the memory allocation bug) not to use it.
Richard.
|
|
|
|
Jack Kelly
Full Member
member is offline
Gender:
Posts: 106
|
|
Re: [RC] Unix/ls
« Reply #9 on: Oct 27th, 2016, 1:03pm » |
|
I sincerely apologize for jumping to conclusions and posting incorrect information on the LB Forum thread regarding DefaultDir$ and StartupDir$. Since then I have done more research and experimentation regarding directories in Liberty BASIC programs (at least under LBB).
DefaultDir$ I have found no instance when DefaultDir$ is initially different than the directory where the executed .exe file resides. Nor did I find any instance when it is different than the 'current directory'.
Note: A while back Richard told us that it could be different if the program did a 'calldll #kernel32, "SetCurrentDirectoryA"' or changed DefaultDir$ in a subroutine 'byref'. But neither of these procedures is normally done in an ordinary program.
I found in all cases that changing DefaultDir$ also changes the current directory accordingly.
StartupDir$ I found that StartupDir$ is initially always the same as DefaultDir$ except in two circumstances.
The first is when the program is executed in DOS, or by a DOS .BAT file, and the current directory is different than the directory where the .exe file resides. In this case, StartupDir$ is set to the current directory at the time the program is executed. This procedure is not, however, normally done by the user of a program.
The second circumstance is when the program is executed from a Windows shortcut file. In this case StartupDir$ is set to the 'Start in' field in the shortcut. This quite a normal method for a user to run a program. If the programmer chooses to honor a user's startup directory request from a shortcut file, and if it makes sense to do so, he or she must execute a 'DefaultDir$=StartupDir$' statement, probably early in the program.
Current Directory The current directory of a program may be obtained from the value of DefaultDir$, or by 'calldll #kernel32, "GetCurrentDirectoryA"'. The latter is the absolute, gospel truth, straight from the mouth of Windows.
The current directory may be manually verified by using a 'files' statement without a path parameter. Unfortunately the drive and path are not reported in array$(0,2) and array$(0,3). Or by observing where a file is created with an 'open for output' statement without a specified path.
I used the following program for my research and experimentation.
Code:dim DirectoryInfo$(0,0)
files DefaultDir$, DirectoryInfo$()
print "DefaultDir$ = "; DirectoryInfo$(0,2); DirectoryInfo$(0,3)
print "Subdirectory Count = "; DirectoryInfo$(0,1)
print "File Count = "; DirectoryInfo$(0,0)
print
files StartupDir$, DirectoryInfo$()
print "StartupDir$ = "; DirectoryInfo$(0,2); DirectoryInfo$(0,3)
print "Subdirectory Count = "; DirectoryInfo$(0,1)
print "File Count = "; DirectoryInfo$(0,0)
print
cd$ = space$(_MAX_PATH)
calldll #kernel32, "GetCurrentDirectoryA", _MAX_PATH as long, cd$ as ptr, r as long
cd$ = left$(cd$, r)
files "", "*", DirectoryInfo$()
print "Current Directory = "; cd$
print "Subdirectory Count = "; DirectoryInfo$(0,1)
print "File Count = "; DirectoryInfo$(0,0)
print
input "New 'DefaultDir$'? "; DD$
if DD$<>"" then
DefaultDir$=DD$
cd$ = space$(_MAX_PATH)
calldll #kernel32, "GetCurrentDirectoryA", _MAX_PATH as long, cd$ as ptr, r as long
cd$ = left$(cd$, r)
print "Current Directory = "; cd$
end if
open "AnatolyTest.dat" for output as #1
close #1
print "Confirm location of 'AnatolyTest.dat'."
input "Press any key to delete 'AnatolyTest.dat' and end the program: "; x$
kill "AnatolyTest.dat"
print "Program complete."
end
Please let me know if any of my conclusions are incorrect. And if you conclude conclusively that they are different in LB45.
Jack
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: [RC] Unix/ls
« Reply #10 on: Oct 27th, 2016, 1:52pm » |
|
on Oct 27th, 2016, 1:03pm, Jack Kelly wrote:Nor did I find any instance when it is different than the 'current directory'. |
|
That agrees with my findings too, although I would want to emphasise that running the program changes the current directory; DefaultDir$ isn't (necessarily) the same as what the current directory was before the program was run (that's what becomes StartupDir$).
In practice it means that whilst you are encouraged to specify DefaultDir$ as the path whenever you try to access a resource file (music, bitmap, sprite etc.) it is not normally necessary to do so because it's where a file - with no path specified - will be assumed to be anyway!
Quote:StartupDir$ is set to the current directory at the time the program is executed. |
|
That's what is supposed to happen, but in my tests it worked correctly when using LB 4.04 or LBB but not when using LB 4.5.0. However Chris Iverson over at the LB forum seems to be finding that it works correctly in both versions.
So there remains an unsolved mystery. Both Anatoly and I have seen inconsistent behavior, without (as yet) any adequate explanation for it. Can I encourage you to run your program in LB 4.5.0 too and report back.
Quote:The second circumstance is when the program is executed from a Windows shortcut file. In this case StartupDir$ is set to the 'Start in' field in the shortcut. |
|
I don't think this is really a "second circumstance" because the 'Start in' property of the shortcut sets the Current Directory, so really it's the same as you stated above.
Richard.
|
|
Logged
|
|
|
|
tsh73
Full Member
member is offline
Gender:
Posts: 210
|
|
Re: [RC] Unix/ls
« Reply #11 on: Oct 27th, 2016, 2:19pm » |
|
Quote:The first is when the program is executed in DOS, or by a DOS .BAT file, and the current directory is different than the directory where the .exe file resides. In this case, StartupDir$ is set to the current directory at the time the program is executed. This procedure is not, however, normally done by the user of a program |
| About >> This procedure is not, however, normally done by the user of a program I really read task as "recreate UNIX ls" which is program executed from a command line. Really, command line still has its uses.
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: [RC] Unix/ls
« Reply #12 on: Oct 27th, 2016, 3:49pm » |
|
on Oct 27th, 2016, 2:19pm, tsh73 wrote:I really read task as "recreate UNIX ls" which is program executed from a command line. |
|
Of course in LBB you can create a console application and make it look exactly like ls or dir. In LB 4 the best you can do is to display the output in a window; it's not clear from the task description at RC whether that qualifies as a solution or not.
Richard.
|
|
Logged
|
|
|
|
Jack Kelly
Full Member
member is offline
Gender:
Posts: 106
|
|
Re: [RC] Unix/ls
« Reply #13 on: Feb 7th, 2017, 06:55am » |
|
Ah, ha. That explains my confusion, Richard. A while back I mentioned that when I start LBB, DefaultDir$ is set to my Windows 'Temp' directory. Anatoly thought that was because LBB referenced that directory during its initilization. Is there any way I can make LBB set DefaultDir$ to the directory where I started it?
on Oct 25th, 2016, 11:16am, Jack Kelly wrote:I've been looking with some confusion at Anatoly's trials and tribulations over directories. I'm running XP (still...). When I start LBB and run 'print DefaultDir$' I get
Code:C:\DOCUME~1\Jack\LOKALE~1\Temp
Where does this come from? I was never aware of such a 'Temp' directory, much less ever having used it. But it has never presented any problem to my knowledge. |
|
Thanks, Jack
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: [RC] Unix/ls
« Reply #14 on: Feb 7th, 2017, 1:53pm » |
|
on Feb 7th, 2017, 06:55am, Jack Kelly wrote:Is there any way I can make LBB set DefaultDir$ to the directory where I started it? |
|
I don't think so, but there's a good reason. When you run an unsaved BASIC program, LBB has to store a temporary copy of it somewhere because it's run in a separate process. The temporary directory is pretty much the only place in the entire Windows filing system that is guaranteed to exist and guaranteed to be writable.
If LBB were instead to attempt to set DefaultDir$ to your startup directory, that's where it would have to put the temporary copy of your unsaved program. There's no certainly that it could save anything there (for example it might be a directory under C:\Program Files (x86)\ which are non-writable without admin privileges) nor that it should.
So LBB takes a safe course. If you haven't saved your program, so you haven't explicitly said "here is a directory that I am happy for you to store files in" it uses the one place that it definitely can: the temporary directory.
When you run a program from the LB 4 IDE I think it runs 'in process' and does not need to be saved somewhere temporarily (BBC BASIC for Windows works that way too). But LBB does need to save it first.
Richard.
|
|
|
|
|