-#include <string.h>
-#include <stdio.h>
-#include <termios.h>
-
-/* This implementation of getch() is from here - */
-/* http://wesley.vidiqatch.org/ */
-/* Thanks, Wesley! */
-static struct termios old, new;
-
-/* Initialize new terminal i/o settings */
-void initTermios(int echo) {
- tcgetattr(0, &old); /* grab old terminal i/o settings */
- new = old; /* make new settings same as old settings */
- new.c_lflag &= ~ICANON; /* disable buffered i/o */
- new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
- tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
-}
-
-
-/* Restore old terminal i/o settings */
-void resetTermios(void) {
- tcsetattr(0, TCSANOW, &old);
-}
-
-
-/* Read 1 character - echo defines echo mode */
-char getch_(int echo) {
- char ch;
- initTermios(echo);
- ch = getchar();
- resetTermios();
- return ch;
-}
-
-
-/* Read 1 character without echo */
-char getch(void) {
- return getch_(0);
-}
-
-
-/* Read 1 character with echo */
-char getche(void) {
- return getch_(1);
-}
-
-
-
-/* Helper function, to let us see if a file */
-/* exists in the current directory. */
-int fexists(char *fname)
-{
- FILE *fptr;
- fptr = fopen(fname, "r") ;
- if ( !fptr ) return -1 ; /* File does not exist in dir. */
- fclose(fptr);
- return 0; /* File DOES exist in dir. */
-}
-
-
-/* Helper function to chop newlines off the lines read in. */
-/* Without this being done, an extra newline is inserted */
-/* (which is usually not what is wanted). */
-char *chop(char *s)
-{
- s[strcspn(s,"\n")] = '\0';
- return s;
-}
-
-
-/* An array to store the command-history file in. */
-/* Only 20 lines are read. */
-char hist[20][80];
-
-
-
-/* Read the file into the array of strings. */
-void readfile(char *fname)
-{
- int retval = fexists(fname);
-
- int i;
- if (retval == 0) {
- /* File exists, so open it. */
- /* We open it in read-write mode so we can */
- /* append new commands to it. */
- FILE *fptr;
- fptr = fopen(fname, "rw");
-
- for(i=0; i<20; i++)
- {
- chop(fgets(hist[i], 80, fptr) );
- }
-
- } /* retval == 0 */
-
- else puts("Error! File does not exist. \n");
-
-}
-
-
-/* Helper function to print the command-history file. */
-void printfile(void)
-{
- int j;
-
- for(j=0; j<20; j++)
- {
- puts(hist[j]);
- }
-}
-
-
-
-
-/* Helper function. Print a previous command-line WITHOUT */
-/* a newline. */
-void putline(char *str)
-{
- char *line = chop(str);
- printf("%s", line);
-}
-