Liberty BASIC Community Forum
« [RC] Unix/ls »

Welcome Guest. Please Login or Register.
Jul 25th, 2017, 5:44pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 sticky  Author  Topic: [RC] Unix/ls  (Read 265 times)
tsh73
Moderator
ImageImageImageImageImage


member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1675
xx [RC] Unix/ls
« Thread started on: Oct 24th, 2016, 07:31am »

[RC] Unix/ls
Quote:
*Task*
Write a program that will list everything in the current folder, similar to:

* the Unix utility “ls” [1] or
* the Windows terminal command “DIR”

The output must be sorted, but printing extended details and producing multi-column output is not required.


Now I have trouble with actually using StartupDir$
It was introduced in 4.5, right?

Here's code that makes example files/folders (under c:\)
Code:
'making directory structure for test
'   /foo/bar
'   /foo/bar/1
'   /foo/bar/2
'   /foo/bar/a
'   /foo/bar/b
res = mkdir("c:\foo")
print res
'0 OK,
'183. Cannot create file when that file already exists
'- directory already created
res = mkdir("c:\foo\bar")
print res

path$="c:\foo\bar\"
open path$+"1" for output as #1: close #1
open path$+"2" for output as #1: close #1
open path$+"a" for output as #1: close #1
open path$+"b" for output as #1: close #1 

Next, here's the program that shows directories and files in a current directory
Code:
'[RC] Unix/ls

print "DefaultDir$"
print DefaultDir$
print "StartupDir$"
print StartupDir$


dim info$(10, 10)
'files DefaultDir$, info$()
files StartupDir$, info$()

nFiles = val(info$(0, 0))
nFolders = val(info$(0, 1))

for i = 1 to nFolders
    print info$(nFiles+i, 1)
next

for i = 1 to nFiles
    print info$(i, 0)
next

print "Press any key to continue"
dummy$=input$(1) 


Now I make EXE, name it say test1.tkn , test1.exe
Now I try to run if from command line from c:\foo, c:\foo\bar.
1) I have to provide full path to TKN as well, or EXE could not find it
2) even if I run it as
Code:
 full_path\test1.exe full_path\test1.tkn 

somehow I have StartupDir$ equal to DefaultDir$, equal to "full_path" where programs lies - and no reference to current directory ir run from (c:\foo etc)

Does it work for you? Please share.
« Last Edit: Oct 24th, 2016, 07:33am by tsh73 » User IP Logged

damned Dog in the Manger
tsh73
Moderator
ImageImageImageImageImage


member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1675
xx Re: [RC] Unix/ls
« Reply #1 on: Oct 24th, 2016, 07:39am »

One that sorts as required (if it will work that is)
Code:
'[RC] Unix/ls

print "DefaultDir$"
print DefaultDir$
print "StartupDir$"
print StartupDir$


dim info$(10, 10)
'files DefaultDir$, info$()
files StartupDir$, info$()

nFiles = val(info$(0, 0))
nFolders = val(info$(0, 1))

n = nFiles+nFolders
dim fnames$(n)

for i = 1 to nFiles
    fnames$(i) = info$(i, 0)
next


for i = 1 to nFolders
    fnames$(nFiles+i) = info$(nFiles+i, 1)
next

sort fnames$(), 1, n

for i = 1 to n
    print fnames$(i)
next    

print "Press any key to continue"
dummy$=input$(1)
 
User IP Logged

damned Dog in the Manger
Jack Kelly
New Member
Image


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 17
xx Re: [RC] Unix/ls
« Reply #2 on: Oct 24th, 2016, 11:18am »

Hi Anatoly,

A couple of things first. Thanks for correcting the link in my Debugging Techniques article. I don't know how I got it so screwed up, but I appreciate your noticing it and fixing it.

And thanks for inviting me back to the LB Forum on behalf of all the moderators and administrators. It's nice to be wanted, and even nicer coming from you.

I don't see any difficulties in your 'files' code. Maybe you sould define your specs more clearly. What to you WANT to happen when your program is run under various circumstances. Do you always want to print the files and folders in the directory where you run the program? Or do you want to specify the directory when you run the program, in the code or in the command line? StartupDir$ is always where the program resides. DefaultDir$ can be assigned to anywhere you want. Actually I don't think it's reliable to use until you assign it.

I'll stay tuned to your [RC] solution of this task...

Jack
User IP Logged

tsh73
Moderator
ImageImageImageImageImage


member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1675
xx Re: [RC] Unix/ls
« Reply #3 on: Oct 24th, 2016, 3:38pm »

The only problem with this code is that I doesn't work for me.
I did:
- put it in LB 4.50 (Liberty Basic Release 4.5.0)
- made an application by Run | Create application menu
- run cmd
- cd c:\foo\bar
- run made program via full path, like
c:\full_path_to_exe\prog.exe

Now, it runs and does not ask me about path to TKN. Really good.
BUT
I expect DefaultDir$ to be full_path_to_exe, and StartupDir$ to be "c:\foo\bar"
- but I got
Code:
DefaultDir$
full_path_to_exe
StartupDir$
full_path_to_exe\
 

AND no trace of c:\foo\bar
AND this holds for Windows XP, Windows 7 and Windows 2000.

So. Does StartupDir$ works at all? Since noone complained, it should be. Then how am I to use it?
User IP Logged

damned Dog in the Manger
Chris Iverson
Administrator
ImageImageImageImageImage


member is offline

Avatar

20% Cooler


Homepage PM

Gender: Male
Posts: 2271
xx Re: [RC] Unix/ls
« Reply #4 on: Oct 24th, 2016, 4:28pm »

What happens if you add the LB path to the PATH, and run it just like a normal command, instead of specifying the full path?

Code:
set PATH=C:\full_path_to_exe;%PATH%
prog.exe 
User IP Logged

"Do you believe in destiny?" - Pyrrha Nikos, RWBY
"With what wish will your Soul Gem shine?" - Kyubey, Puella Magi Madoka Magica
cundo
Guru
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 624
xx Re: [RC] Unix/ls
« Reply #5 on: Oct 24th, 2016, 4:46pm »

I think StartupDir$ holds the path to the LB exe. Well, that is what I get if I try this Code:
Print StartupDir$,DefaultDir$ 


User IP Logged

cundo
tsh73
Moderator
ImageImageImageImageImage


member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1675
xx Re: [RC] Unix/ls
« Reply #6 on: Oct 24th, 2016, 10:44pm »

Chris,
Quote:
What happens if you add the LB path to the PATH, and run it just like a normal command, instead of specifying the full path?

Code:set PATH=C:\full_path_to_exe;%PATH%
prog.exe

under XP I got error.log with text
Quote:
Error log timestamp 10/25/2016 6:38:19
Message 2005:
Could not find the image file: lb_ls.exe
User IP Logged

damned Dog in the Manger
tsh73
Moderator
ImageImageImageImageImage


member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1675
xx Re: [RC] Unix/ls
« Reply #7 on: Oct 25th, 2016, 03:44am »

Just for a record
StartupDir$ was in in LB 4.04
I test it now and it works in 4.04 (yesterday somehow it didn't)
Then in cmd in folder
Code:
C:\foo\bar 

command
Code:
"C:\Progs\Liberty BASIC v4.04\newExe\lb_ls.exe" "C:\Progs\Liberty BASIC v4.04\newExe\lb_ls.tkn" 

produces
Code:
DefaultDir$
C:\Progs\Liberty BASIC v4.04\newExe
StartupDir$
C:\foo\bar
1
2
a
b
Press any key to continue 


Does StartupDir$ got broken in LB 4.5?
« Last Edit: Oct 25th, 2016, 03:46am by tsh73 » User IP Logged

damned Dog in the Manger
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5366
xx Re: [RC] Unix/ls
« Reply #8 on: Oct 25th, 2016, 04:52am »

Running 4.50 I created the c:\foo structure.

If I save my .bas to c:\foo and run it with the IDE I get

DefaultDir$
C:\foo
StartupDir$
C:\Program Files (x86)\Liberty BASIC v4.5.0\

Exactly the response I expected.

If I create the .exe in c:\foo and run the .exe I get

DefaultDir$
C:\foo
StartupDir$
C:\foo\

Exactly the response I expected.

So I don't think there is anything wrong with DefaultDir$ which returns the current windows working directory Or StartupDir$ which returns the Liberty.exe starting place, normally

C:\Program Files (x86)\Liberty BASIC v4.5.0\

But not if you have placed it elsewhere.


Should we not be allowing the user to choose the current directory? We could do that with the path$ template in the files command. That way they can negotiate and list any directory they wish. I Need to read the RC task.

Edit: Seems to imply the program is in the current directory, so a simple .bas files program would complete the task.
« Last Edit: Oct 25th, 2016, 04:57am by Rod » User IP Logged

tsh73
Moderator
ImageImageImageImageImage


member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1675
xx Re: [RC] Unix/ls
« Reply #9 on: Oct 25th, 2016, 05:46am »

It's no point to have program in a current directory.
Program (EXE) should lie elsewhere (c:\utils etc)
but should be run from c:\foo folder
like
Code:
c:\foo> c:\utils\lb_ls.exe 

What it outputs then?
User IP Logged

damned Dog in the Manger
Jack Kelly
New Member
Image


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 17
xx Re: [RC] Unix/ls
« Reply #10 on: Oct 25th, 2016, 09:28am »

Don't forget that a desktop or start menu shortcut can tell windows where the program resides (DefaultDir$) and what the current directory should be (StartupDir$).
User IP Logged

Chris Iverson
Administrator
ImageImageImageImageImage


member is offline

Avatar

20% Cooler


Homepage PM

Gender: Male
Posts: 2271
xx Re: [RC] Unix/ls
« Reply #11 on: Oct 25th, 2016, 7:42pm »

Here's the results of testing from the command line.

This is the exact code I used:

Code:
print "DefaultDir$"
print DefaultDir$
print "StartupDir$"
print StartupDir$
input a 


Compiled to application prog.exe, saved under C:\test.

Started command line, opens at default path(%USERPROFILE%, in my case, C:\Users\cjnoo).


Command entered:

Code:
C:\Users\cjnoo>C:\test\prog 


Result, error:

---------------------------
Application load aborted
---------------------------
File not found: prog.TKN
---------------------------
OK
---------------------------


Next attempt:
Code:
C:\Users\cjnoo>C:\test\prog C:\test\prog.tkn 


Result: success.
Returned message:

DefaultDir$
C:\test
StartupDir$
C:\Users\cjnoo
?

Kind of irked about this one, actually. It can see the application folder is C:\test, it should be able to auto-detect the TKN of the same name. Still, StartupDir$ lists the folder that I launched from.

Another test, out of curiosity:
Code:
C:\Users\cjnoo>..\..\test\prog ..\..\test\prog.tkn 


Results: successful
Returned message:

DefaultDir$
C:\Users\cjnoo\..\..\test
StartupDir$
C:\Users\cjnoo
?


Kind of found it amusing that it passed the ..'s straight through.



One more test: running as a default program, as part of PATH, launched from the command line.

Code:
C:\Users\cjnoo>set PATH=C:\test;%PATH%

C:\Users\cjnoo>prog 


Result: hang for about 10 seconds, and then crash.

Crash message:
---------------------------
Smalltalk/V
---------------------------
Could not find the image file: prog.exe
---------------------------
OK
---------------------------



ERROR.LOG created in C:\Users\cjnoo. Contents:



Error log timestamp 10/25/2016 19:37:35
Message 2005:
Could not find the image file: prog.exe



Running this:
Code:
C:\Users\cjnoo>prog C:\test\prog.tkn 


Came back with the same error.
User IP Logged

"Do you believe in destiny?" - Pyrrha Nikos, RWBY
"With what wish will your Soul Gem shine?" - Kyubey, Puella Magi Madoka Magica
tsh73
Moderator
ImageImageImageImageImage


member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1675
xx Re: [RC] Unix/ls
« Reply #12 on: Oct 26th, 2016, 12:26am »

Chris,
was it LB 4.04?
User IP Logged

damned Dog in the Manger
Chris Iverson
Administrator
ImageImageImageImageImage


member is offline

Avatar

20% Cooler


Homepage PM

Gender: Male
Posts: 2271
xx Re: [RC] Unix/ls
« Reply #13 on: Oct 26th, 2016, 10:16am »

Oops, should've specified.

Tested, with the same results, in both LB Pro v4.04 and LB v4.5.
User IP Logged

"Do you believe in destiny?" - Pyrrha Nikos, RWBY
"With what wish will your Soul Gem shine?" - Kyubey, Puella Magi Madoka Magica
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls