with Interfaces.C.Strings, POSIX.IO, POSIX.Process_Primitives;
with EUP.Text_IO, Fork, Wait;

procedure Who_To_file is
   procedure Exec_No_Arguments (Command : in Interfaces.C.Strings.chars_ptr;
                                Name    : in Interfaces.C.Strings.chars_ptr;
                                The_End : in Interfaces.C.Strings.chars_ptr);
   pragma Import (C, Exec_No_Arguments, "execlp");

   use type Interfaces.C.int;

   Process_ID   : Interfaces.C.int;
   File         : POSIX.IO.File_Descriptor;
   Child_Status : aliased Interfaces.C.int;
begin
   EUP.Text_IO.Put_Line
     (File => EUP.Text_IO.Standard_Input,
      Item => String'("About to write the output from 'who' to a file."));

   Process_ID := Fork;
   if Process_ID = -1 then
      EUP.Text_IO.Put_Line (File => EUP.Text_IO.Standard_Error,
                            Item => String'("Calling fork() failed."));
      POSIX.Process_Primitives.Exit_Process (Status => 1);
   elsif Process_ID = 0 then
      POSIX.IO.Close (POSIX.IO.Standard_Output);
      EUP.Text_IO.Create (File => File,
                          Name => String'("userlist"));
      Exec_No_Arguments (Interfaces.C.Strings.New_String ("who"),
                         Interfaces.C.Strings.New_String ("who"),
                         Interfaces.C.Strings.Null_Ptr);
   else
      Process_ID := Wait (Child_Status'Access);
      EUP.Text_IO.Put_Line
        (File => EUP.Text_IO.Standard_Input,
         Item => String'("Done running 'who'.  Results in 'userlist'."));
   end if;
exception
   when others => null;
end Who_To_File;

