-- who1.adb - a first verions of the who program
--            open, read UTMP file, show results

with Ada.Direct_IO, Ada.Text_IO, POSIX, POSIX.C;

with EUP.Strings, EUP.UTMP;

procedure Who is
   package Time_Text_IO is new Ada.Text_IO.Integer_IO (POSIX.C.time_t);

   procedure Show (Item : in     EUP.UTMP.Instance) is
      use Ada.Text_IO;
      use EUP.Strings;
      use Time_Text_IO;
   begin
      Put (Format (Item.User, Width => 8));
      Put (" ");
      Put (Format (Item.Line, Width => 8));
      Put (" ");
      Put (Item.TV.tv_sec, Width => 10);
      Put (" ");
      Put ("(" & To_Ada_String (Item.Host) & ")");
      New_Line;
   end Show;

   package UTMP_IO is new Ada.Direct_IO (EUP.UTMP.Instance);
   use UTMP_IO;
   Current_Record : EUP.UTMP.Instance;
   UTMP_File      : UTMP_IO.File_Type;
begin
   Open (File => UTMP_File,
         Name => EUP.UTMP.File_Name,
         Mode => In_File);

   while not End_Of_File (UTMP_File) loop
      Read (File => UTMP_File,
            Item => Current_Record);
      Show (Item => Current_Record);
   end loop;

   Close (File => UTMP_File);
end Who;

