text: /*
   * $Id: termios_tcdrain.c,v 1.4 2006-11-16 14:39:23 clib2devs Exp $
  */
  
  #ifndef    _TERMIOS_HEADERS_H
  #include "termios_headers.h"
  #endif /* _TERMIOS_HEADERS_H */
  
  #if !defined(Flush)
  #define Flush(fh) FFlush(fh)
  #endif /* !Flush */
  
  int
  tcdrain(int file_descriptor) {
      int result = ERROR;
      struct fd *fd;
  
      ENTER();
  
      SHOWVALUE(file_descriptor);
  
      if (__check_abort_enabled)
          __check_abort();
  
      __stdio_lock();
  
      fd = __get_file_descriptor(file_descriptor);
      if (fd == NULL) {
          __set_errno(EBADF);
          goto out;
      }
  
      __fd_lock(fd);
  
      if (FLAG_IS_SET(fd->fd_Flags, FDF_TERMIOS)) {
          struct termios *tios;
          BPTR file;
  
          tios = fd->fd_Aux;
  
          switch (tios->type) {
              case TIOST_CONSOLE:
  
                  file = __resolve_fd_file(fd);
                  if (file == ZERO) {
                      __set_errno(EBADF);
                      goto out;
                  }
  
                  /* This also discards any buffered input, but it does
                     not appear possible to drain the output buffer
                     otherwise. (?) */
                  if (CANNOT Flush(file))
                      goto out;
  
                  break;
              default: /* TODO: Serial port support. */
                  __set_errno(ENXIO);
                  goto out;
          }
  
          result = OK;
      } else {
          result = fdatasync(file_descriptor); /* If called on a "regular" file. */
      }
  
  out:
  
      __fd_unlock(fd);
      __stdio_unlock();
  
      RETURN(result);
      return (result);
  }
  