/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#include "io.h"

int remove(const char *path) {
/* -----------------------------------------------------------------------------
 * Remove a file more safely 
 *
 * @param  const char *, file name
 * @return int
 * -------------------------------------------------------------------------- */

  if ((path)
  && (strlen(path) > 0)
  && (!access(path, F_OK))) {
    return unlink(path);
  }

  return 0;

}

void reset_input_mode (void) {
/* -----------------------------------------------------------------------------
 * Reset terminal parameter
 *
 * -------------------------------------------------------------------------- */
  tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
}

void set_input_mode_off() {
/* -----------------------------------------------------------------------------
 * Remove ECHO
 *
 * -------------------------------------------------------------------------- */
  struct termios tattr;

  /* We must be sure to be on a tty */
  if (!isatty (STDIN_FILENO)) {
    fprintf(stderr, "Not a terminal.\n");
    exit (EXIT_FAILURE);
  }

  /* Save attributes */
  tcgetattr (STDIN_FILENO, &saved_attributes);
  atexit (reset_input_mode);

  /* Clear ICANON and ECHO. */
  tcgetattr (STDIN_FILENO, &tattr);
  tattr.c_lflag &= ~ICANON;
  tattr.c_lflag &= ~ECHO;
  /*tattr.c_cc[VMIN] = 1;
  tattr.c_cc[VTIME] = 0;*/
  tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
}

void set_input_mode_on() {
/* -----------------------------------------------------------------------------
 * Restore ECHO
 *
 * -------------------------------------------------------------------------- */
  struct termios tattr;

  /* We must be sure to be on a tty */
  if (!isatty (STDIN_FILENO)) {
    fprintf(stderr, "Not a terminal.\n");
    exit (EXIT_FAILURE);
  }

  /* Reinit ICANON and ECHO. */
  tcgetattr (STDIN_FILENO, &tattr);
  tattr.c_lflag |= ICANON;
  tattr.c_lflag |= ECHO;
  /*tattr.c_cc[VMIN] = 1;
  tattr.c_cc[VTIME] = 0;*/
  tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
}


syntax highlighted by Code2HTML, v. 0.9.1