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.



Example output
For the list of paths:

/foo/bar
/foo/bar/1
/foo/bar/2
/foo/bar/a
/foo/bar/b



When the program is executed in   `/foo`,   it should print:

bar

and when the program is executed in   `/foo/bar`,   it should print:

1
2
a
b



 
program LsCommand;
 
{$APPTYPE CONSOLE}
 
 
 
uses
  System.SysUtils,
  System.IoUtils;
 
procedure Ls(folder: string = '.');
var
  offset: Integer;
  fileName: string;
 
  // simulate unix results in windows
 
  function ToUnix(path: string): string;
  begin
    Result := path.Replace('/', PathDelim, [rfReplaceAll])
  end;
 
begin
  folder := IncludeTrailingPathDelimiter(ToUnix(folder));
  offset := length(folder);
 
  for fileName in TDirectory.GetFileSystemEntries(folder, '*') do
    writeln(^I, ToUnix(fileName).Substring(offset));
end;
 
begin
  writeln('cd foo'#10'ls');
  ls('foo');
 
  writeln(#10'cd bar'#10'ls');
  ls('foo/bar');
 
  {$IFNDEF LINUX} readln; {$ENDIF}
end.