2.2 Using the unixDirectory Fragment

A program using the unixDirectory fragment will have the following structure:

INCLUDE '~beta/unixlib/unixDirectory'
--- program: descriptor ---
(# ...
   ud: @unixDirectory;
do ...
   'unixDirectory.bet' -> ud.findEntry(# ... do ... #);
#)

2.2.1 Listing Unix Directory Example

Program showing a simple use of UNIX directory: The directory with the path given as argument is scanned, and the names of all the entries are printed with an indication of what type of entry it is. This is done using the select pattern of scanEntries. This is a more efficient strategy than using found.entry.isFile etc., possibly correcting for the case that 'd' is not current working directory.

If the path given is not a directory, an exception will be raised:

Program 3: listunix.bet

ORIGIN '~beta/unixlib/unixdirectory';
-- PROGRAM: descriptor --

(* Program showing a simple use of UNIX directory: The directory with the path 
 * given as argument is scanned, and the names of all the entries are printed
 * with an indication of what type of entry it is.
 * This is done using the 'select' pattern of 'scanentries'. This is a more
 * efficient startegy than using 'found.entry.isFile' etc., possibly 
 * correcting for the case that 'd' is not current working directory.
 * If the path given is not a directory, an exception will be raised.
 *)

(# arg: ^text; 
   d: @UnixDirectory; 
   nl: @boolean; 
   full: @boolean;
   usage:
     (# do 'Usage: ' -> puttext; 
        1->arguments->puttext; 
        ' [-f] path' -> putline;
        stop;
     #);
do (* Parse command line *)
   (if noOfArguments
    // 1 then '.' -> d.name
    // 2 then
       2 -> arguments -> arg[];
       (if '-f' -> arg.equal
        // true then
           true -> full;
           '.' -> d.name;
        // false then
           arg[] -> d.name;
       if)
    // 3 then
       2 -> arguments -> arg[];
       (if '-f' -> arg.equal
        // true then
           true -> full;
        // false then usage;
       if);
       3 -> arguments -> d.name;
    else
       usage;
   if);
   
   (* Scan directory *)
   newline;
   'The content of \''-> puttext;
   d.name -> puttext;
   '\' is: ' -> putline;
   d.scanEntries
   (# 
   do false -> found.followlinks;
      select
      (# whenFile::<
           (# do 'File:          ' -> puttext; #);
         whenDir::<
           (# do 'Directory:     ' -> puttext; #);
         whenSocket::<  
           (# do 'Socket:        ' -> puttext; #);
         whenSymboliclink::< 
           (# do 'Link:          ' -> puttext; #);
         whenCharSpecial::<  
           (# do 'Char special:  ' -> puttext; #);
         whenBlockSpecial::< 
           (# do 'Block special: ' -> puttext; #);
         whenOther::<
           (# do '(Unknown):     ' -> puttext; #);
      #);
      (if full
       // true then foundFullPath -> putline; 
       // false then found.path -> putline; 
      if);
   #);
   newline;
#)


© 1990-2004 Mjølner Informatics
[Modified: Wednesday October 18th 2000 at 13:55]