#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

/* This program makes a little character spinner. */
/* e.g. - \ | / - \ | / ...                       */
/* It is useful for keeping links alive, etc.     */

/* Usage: spinner [<delay_in_sec>]                */
/* The delay is the time between each tick of the */
/* "spin". If no delay is provide a default is    */
/* used.                                          */

/* This is obviously a trivial prog. I found it   */
/* useful for ssh keepalives through firewalls    */
/* that timeout idle connections. Hopefully you   */
/* will too...                                    */

/* You can compile this with:                     */
/*                                                */
/* gcc -O2 spinner.c -o spinner                   */
/*                                                */
/* This will generate a binary called "spinner".  */
/* Try executing it with "./spinner 0" or just    */
/* plain "./spinner" for the default delay.       */
/* If you use this to keep alive a connection use */
/* a delay of 2-30 secs. as not to waste          */
/* bandwidth.                                     */

/*
spinner.c
vers 1.0
    Copyright (C) 2002 Joe Laffey

    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 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
*/


/* deafult delay in seconds */
#define	DEFAULT_DELAY	2
#define MAXINT                (~((unsigned int)1 << ((8 * sizeof(int)) - 1)))

int main(int argc, char** argv)
{
	/* The spinning characters */
	char chars[] = { '-', '\\', '|','/','-','\\','|','/' };

	/* length of above */
	int numChars = sizeof(chars);

	/* count.. */
	int i;

	/* time of sleep in secs */

	int time;
	if(argc != 2)
	{
		time = DEFAULT_DELAY;	
		printf("No delay specified. Using %d secs.\n", time);
	
	}	
	else
	{
		time = atoi(argv[1]);
	}

	/* startup */
	printf("Spinning: \\");
	
	while(1)
	{
        	if( i < MAXINT )
			i++;
		else
			i = 0;
        	
		/* Print backspace and the char */
		printf("%c%c", '\b',chars[i%numChars]);
        	fflush(stdout);
        	sleep(time);
	}
	
	return(0);
}


