How to resolve the algorithm Unix/ls step by step in the Smalltalk programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Unix/ls step by step in the Smalltalk programming language
Table of Contents
Problem Statement
Write a program that will list everything in the current folder, similar to:
The output must be sorted, but printing extended details and producing multi-column output is not required.
For the list of paths:
When the program is executed in /foo
, it should print:
and when the program is executed in /foo/bar
, it should print:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Unix/ls step by step in the Smalltalk programming language
Source code in the smalltalk programming language
Stdout printCR: ( PipeStream outputFromCommand:'ls' )
Stdout printCR:('.' asFilename directoryContents sort asStringWith:Character cr)
'.' asFilename directoryContents sort do:#printCR
dir := '.' asFilename.
dir directoryContentsAsFilenames sort do:[:fn |
|line|
"
generate a line of the form of ls -l:
drwxrwxrwx user group size date time name
where year is printed if not current, time of day otherwise
"
line := String streamContents:[:s |
|accessRights|
s nextPut:(fn isDirectory ifTrue:[$d] ifFalse:[$-]).
accessRights := fn symbolicAccessRights.
#( readUser writeUser executeUser
readGroup writeGroup executeGroup
readOthers writeOthers executeOthers
)
with:'rwxrwxrwx'
do:[:eachRight :charToPrint |
s nextPut:((accessRights includes:eachRight) ifTrue:[charToPrint] ifFalse:[$-])
].
(OperatingSystem getUserNameFromID:fn info uid) printOn:s leftPaddedTo:10.
(OperatingSystem getGroupNameFromID:fn info gid) printOn:s leftPaddedTo:10.
fn fileSize printOn:s leftPaddedTo:12.
fn modificationTime year = Date today year ifTrue:[
fn modificationTime printOn:s format:' %(dayPadded) %(ShortMonthName) %h:%m'.
] ifFalse:[
fn modificationTime asDate printOn:s format:' %(dayPadded) %(ShortMonthName) %y'.
].
s space.
s nextPutAll:fn baseName
].
line printCR
].
You may also check:How to resolve the algorithm Unix/ls step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Stack step by step in the BASIC programming language
You may also check:How to resolve the algorithm Lah numbers step by step in the VBScript programming language
You may also check:How to resolve the algorithm Fork step by step in the Factor programming language
You may also check:How to resolve the algorithm Approximate equality step by step in the ALGOL 68 programming language