/*
** checksparse v1.0
**
** Checks wether a file contains blocks of zeros
**
** Copyright (C) 11.6.2001 by Andreas Ley <Andreas.Ley@rz.uni-karlsruhe.de>
**
** 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., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** This program has been tested on a HP9000/715 with HP-UX A.09.07
** In this environment, neither lint -u nor gcc -Wall -pedantic -fsyntax-only
** produce any messages. If you encounter any errors or need to make any
** changes to port it to another platform, please contact me.
**
** Version history
**
** Version 1.0 - 11.6.2001
**	Initial version
*/

static char copyright[] = "@(#)Copyright (C) 2001 by Andreas Ley (Andreas.Ley@rz.uni-karlsruhe.de)";
static char sccsid[] = "@(#)checksparse 1.0 - Checks wether a file contains blocks of zeros";


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/vfs.h>

#ifndef FALSE
#define	FALSE	(0)		/* This is the naked Truth */
#define	TRUE	(1)		/* and this is the Light */
#endif


char	*image;
int	debug=0,count=FALSE,countbytes=FALSE;


static void usage()
{
	(void)fprintf(stderr,"Usage: %s [-c|-C] filename [...]\n",image);
	(void)fprintf(stderr,"-c  count empty blocks\n",image);
	(void)fprintf(stderr,"-C  count bytes in empty blocks\n",image);
	exit(1);
}


int main(argc,argv)
int	argc;
char	*argv[];
{
	int		c;
	extern char	*optarg;
	extern int	optind;
	int		fd;
	struct statfs	buf;
	char		*buffer;
	size_t		cnt,block,empty;

	if ((image=strrchr(argv[0],'/')))
		image++;
	else
		image=argv[0];

	while ((c=getopt(argc,argv,"DCcvh?"))!=EOF)
		switch ((char)c) {
		case 'D':
			debug++;
			break;
		case 'C':
			countbytes=TRUE;
		/*FALLTHROUGH*/
		case 'c':
			count=TRUE;
			break;
		case 'v':
			(void)fprintf(stderr,"%s\n",sccsid+4);
			(void)fprintf(stderr,"%s\n",copyright+4);
			exit(0);
			break;
		case 'h':
			(void)fprintf(stderr,"%s\n",sccsid+4);
			(void)fprintf(stderr,"%s\n",copyright+4);
		/*FALLTHROUGH*/
		case '?':
			usage();
		}

	if (optind==argc)
		usage();

	for (;optind<argc;optind++) {
		if ((fd=open(argv[optind],O_RDONLY))<0) {
			(void)fprintf(stderr,"%s: cannot open ",image);
			perror(argv[optind]);
			continue;
		}
		if (fstatfs(fd,&buf)) {
			(void)fprintf(stderr,"%s: cannot statfs ",image);
			perror(argv[optind]);
			exit(1);
		}
		if (!(buffer=malloc(buf.f_bsize))) {
			(void)fprintf(stderr,"%s: cannot malloc %ld bytes: ",image,buf.f_bsize);
			perror("");
			exit(1);
		}
		if (debug)
			(void)fprintf(stderr,"%s: block size %ld\n",argv[optind],buf.f_bsize);
		block=0;
		empty=0;
		while (read(fd,buffer,buf.f_bsize)==buf.f_bsize) {
			for (cnt=0;cnt<buf.f_bsize&&!buffer[cnt];cnt++);
			if (cnt==buf.f_bsize) {
				empty++;
				if (debug)
					(void)fprintf(stderr,"%s: block %ld is empty\n",argv[optind],block);
				if (!count)
					break;
			}
			block++;
		}
		(void)free(buffer);
		(void)close(fd);
		if (empty)
			if (count)
				(void)printf("%ld %s\n",countbytes?empty*buf.f_bsize:empty,argv[optind]);
			else
				(void)printf("%s\n",argv[optind]);
	}

	return(0);
}

