/* 
 * growspd, shows how fast a file is growing.
 * quick hack by peter k, peter@brokep.com
 * please listen to techno. it _will_ make your life better.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "growspeed.h"

int main (int argc, char **argv)
{
	char		*prgname;
	char		*file;
	struct stat	*fileStat;
	int		cursize=0, errspd=0, curspd=0, overallspd=0;
	int		i=1;			/* i=0 would result in dbz */

	prgname = (char *) malloc(strlen(argv[0]+1));
	strcpy(prgname, argv[0]);

	if (argc == 1)
		arg_error(prgname, "Argument required.", 1);
	if (argc > 2)
		arg_error(prgname, "Wrong arguments.", 1);

	file = (char *) malloc(strlen(argv[1]+1));
	strcpy(file,argv[1]);
	if (strcasecmp("-h",file)==0)
		arg_error(prgname, "Asked for help.", 1);

	fileStat = malloc(sizeof(struct stat));
	errspd = 1;
	while (errspd>-1)
	{
		if (i>1)
			printf("\033[1A\033[K");
		errspd = stat(file, fileStat);
		if (errspd<0) { perror(file); exit(1); }
		if (S_ISREG(fileStat->st_mode)==0)
			arg_error(prgname, "growspd cannot handle this filetype.", 0);
		cursize=(int) fileStat->st_size;
		if (i==1) { overallspd=cursize; curspd=cursize; }
		printf("%s - %ld Kb (current %ld Kb/s, overall %ld Kb/s)\n", file, (cursize/1024), ((cursize-curspd)/1024), ((((cursize-overallspd)/1024))/i));
		curspd=cursize;
		sleep(1);
		i++;
	}
	free(fileStat);
	free(file);
	exit(0);
}

void arg_error(char *prgname, char *str, int usage)
{
	if (usage == 1)
		printf("Usage: %s <filename>\n", prgname);

	printf("Error: %s\n", str);
	exit(1);
}


syntax highlighted by Code2HTML, v. 0.9.1