--  write0.adb
--
--  Purpose: Send messages to another terminal
--   Method: Open the other terminal for output, then copy from
--           Standard_Input to that terminal.
--    Shows: A terminal is just a file supporting regular I/O.
--    Usage: ./write0 ttyname

with Ada.Characters.Latin_1;
with Ada.Command_Line;
with POSIX.IO;

procedure write0 is
   use Ada.Command_Line;
   use POSIX;
   use POSIX.IO;
   Terminal      : File_Descriptor;
   Buffer        : IO_Buffer (1 .. 1024);
   Bytes_Read    : Natural;
   Bytes_Written : Natural;
begin
   if Argument_Count = 1 then
      Terminal := Open (Name => To_POSIX_String (Argument (1)),
                        Mode => Write_Only);
      loop
         NONSTANDARD_Read
           (File   => Standard_Input,
            Buffer => Buffer,
            Last   => Bytes_Read);
         NONSTANDARD_Write
           (File   => Terminal,
            Buffer => Buffer (Buffer'First .. Bytes_Read),
            Last   => Bytes_Written);
      end loop;
      Close (File => Terminal);
   else
      NONSTANDARD_Write
        (File   => Standard_Output,
         Buffer => "Usage: write0 ttyname" & POSIX.LF,
         Last   => Bytes_Written);
   end if;
exception
   when others =>
      null;
end write0;

