subroutine get_terminal_nCols(n, stat)
  implicit none
  integer, intent(out) :: nCols
  integer, intent(out) :: stat

  character(32), pointer :: s
  integer, pointer :: cl
  integer, pointer :: cstat
  integer :: ios
  !-------------------------------------------------------------
  stat = 0

  allocate(s)
  allocate(cl)
  allocate(cstat)

  call c_system_tput_cols(s, cl, cstat)

  if( cstat == 0 )then
    read(s(:cl),*,iostat=ios) nCols
    if( ios /= 0 )then
      nCols = 0
      stat = 1
    endif
  else
    nCols = 0
    stat = 1
  endif

  deallocate(s)
  deallocate(cl)
  deallocate(cstat)
  !-------------------------------------------------------------
end subroutine get_terminal_nCols