/*******************************************************************************/
/*                                                                             */
/*  Copyright 2004 Pascal Gloor <pascal.gloor@spale.com>                       */
/*                                                                             */
/*  Licensed under the Apache License, Version 2.0 (the "License");            */
/*  you may not use this file except in compliance with the License.           */
/*  You may obtain a copy of the License at                                    */
/*                                                                             */
/*     http://www.apache.org/licenses/LICENSE-2.0                              */
/*                                                                             */
/*  Unless required by applicable law or agreed to in writing, software        */
/*  distributed under the License is distributed on an "AS IS" BASIS,          */
/*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
/*  See the License for the specific language governing permissions and        */
/*  limitations under the License.                                             */
/*                                                                             */
/*******************************************************************************/

/* Compile:
 *
 * cc -ansi -pedantic -Wall -W -O2 -o zrm zrm.c
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>



static char zero[8192] = { 0 };

void del_file(char *file);

int main(int argc, char *argv[])
{
	int i;
	size_t maxlen = 0;
	char format[20];

	if ( argc < 2 )
	{
		printf("ZeroRM v1.0 by Pascal Gloor\n");
		printf("usage: %s <file> [file] ...\n",argv[0]);
		printf("note : it can only delete 'files'.\n");
		printf("     : it does NOT delete files with multiple hardlinks.\n");
		exit(EXIT_FAILURE);
	}
	for(i=1; i<argc; i++)
	{
		if ( strlen(argv[i]) > maxlen )
			maxlen = strlen(argv[i]);
	}

	sprintf(format,"%%-%is : ",maxlen);

	for(i=1; i<argc; i++)
	{
		printf(format,argv[i]);
		del_file(argv[i]);
		printf("\n");
		fflush(stdout);
	}
	return 0;
}

void del_file(char *file)
{
	int fd;
	int pos = 0;
	struct stat st;
	struct flock lk;

	lk.l_start = 0;
	lk.l_len = 0;
	lk.l_pid = getpid();
	lk.l_type = F_WRLCK;
	lk.l_whence = 0;


	if ( stat(file, &st) != 0 )
	{
		printf("error(%s)",strerror(errno));
		return;
	}

	if ( st.st_nlink > 1 )
	{
		printf("error(multiple hardlinks)");
		return;
	}

	if ( ( fd = open(file, O_WRONLY) ) == -1 )
	{
		printf("error(%s)",strerror(errno));
		return;
	}

	if ( fcntl(fd, F_SETLK, &lk) == -1 )
	{
		printf("error(%s)",strerror(errno));
		return;
	}

	while(pos<st.st_size)
	{
		int len = 1;

		if ( st.st_size - pos > 8192 )
			len = 8192;
		if ( st.st_size - pos > 1024 )
			len = 1024;
		if ( st.st_size - pos > 128 )
			len = 128;
		if ( st.st_size - pos > 16 )
			len = 16;

		if ( (pos += write(fd, zero, len)) == -1 )
			break;
	}

	close(fd);

	if ( unlink(file) != 0 )
	{
		printf("error(%s)",strerror(errno));
		return;
	}

	printf("ok");

	return;
}