#ifdef	ournix
#include "ournix.h"
#endif
#ifndef	LINT	/* { */
char sccsID[] = 
	"@(#) notail.c V1.0 Copyright Julian H. Stacey, Munich, 5 July 1991.\n" ;
#endif		/* } */

/* strip trailing \n s from files */

#include	<stdio.h>
#include <stdlib.h>
#include <string.h>
#include	<sys/types.h>
#include 	<sys/stat.h>
#ifdef ns32000	/* { */
#define BSD
#endif		/* } */
#ifdef	BSD
#include	<sys/file.h>
/* #include	<sys/signal.h> */
#else
/* #include	<signal.h> */
#endif
#ifndef	BSD	/* { */
#include	<fcntl.h>
#endif		/* } */

#include <unistd.h>


#define STRADR	&
// extern char *malloc() ;
char	**ARGV ;
typedef	char	FLAG ;
FLAG	verbose = 0 ;

void zapname(name)
	char	*name ;
	{
	register int	i, j ;

	if (!verbose) 	return ;
	i = strlen(name) ;
#define	BS	8
	for(j = i ; j ; j--) putchar(BS) ;
	for(j = i ; j ; j--) putchar(' ') ;
	for(j = i ; j ; j--) putchar(BS) ;
	}

int do_it(name)
	char	*name ;
	{
	/* input file variables */
	int orig_size, new_size ;
	char *array ;
	char	*pp ;
	int	fd ;
	struct stat stat_buf ;

	if (verbose)
		{
		printf("%s", name) ;
		(void) fflush(stdout) ;
		}
	/* (void) signal(SIGINT, SIG_DFL) ; */
	if (stat(name, STRADR stat_buf) ) 
		{
		zapname(name);
		fprintf(stderr, "%s: Cannot stat '%s'\n",*ARGV, name) ;
		return(1) ;
		}
	/*skip 0 size files*/
	if (!(new_size = orig_size = (int)(stat_buf.st_size))) 
		{
		zapname(name);
		return(0) ;
		}
	if ((fd = open(name,
#ifdef	MSDOS	/* { */
			O_BINARY |
#endif		/* } */
				O_RDWR) ) < 0 )
		{
		zapname(name);
		fprintf(stderr, "%s: Cannot open '%s'\n",*ARGV, name) ;
		return(1) ;
		}
	if ((array = malloc((unsigned)orig_size)) == (char *)0) 
		{
		zapname(name);
		fprintf(stderr, "%s: Cannot malloc(%d) for '%s'\n",*ARGV, 
			orig_size, name) ;
		return(1) ;
		}
	if (read(fd, array, orig_size) != orig_size)
		{
		zapname(name);
		fprintf(stderr, "%s: Cannot read '%s'\n",*ARGV, name) ;
		return(1) ;
		}
	/* go back to beginning */
	if (close(fd) < 0)
		{
		zapname(name);
		fprintf(stderr, "%s: Cannot close '%s'\n",*ARGV, name) ;
		return(1) ;
		}

	/* scan back from end of array till a non \n is found */
	for ( pp = array + orig_size ;
		(--pp >= array) && (*pp == '\n') ;
		new_size-- ) ;
	/* allow one \n if existant */
	if (new_size < orig_size) new_size++ ;
	if (new_size == orig_size) 
		{
		free(array);
		zapname(name);
		return(0) ;
		}

	/* re open to truncate original */
	/* (void) signal(SIGINT, SIG_IGN) ; */
	if ((fd = open(name,
#ifdef	MSDOS	/* { */
			O_BINARY |
#endif		/* } */
				O_RDWR | O_TRUNC ) ) < 0 )
		{
		zapname(name);
		fprintf(stderr, "%s: Cannot open '%s'\n",*ARGV, name) ;
		return(1) ;
		}

	if (write(fd, array, new_size) != new_size)
		{
		free(array) ;
		zapname(name);
		fprintf(stderr, "%s: Cannot write '%s'\n",*ARGV, name) ;
		return(1) ;
		}
	free(array) ;
	if (close(fd) < 0)
		{
		zapname(name);
		fprintf(stderr, "%s: Cannot close '%s'\n",*ARGV, name) ;
		return(1) ;
		}
	zapname(name);
	return(0) ;
	}

int main(argc, argv)	
	int argc ;
	char **argv ;
	{
	int	cur_fd ;
	int	errors = 0 ;

	ARGV = argv ;
#ifdef	VSL	/* { */
#include	"../../include/vsl.h"
#endif		/* } */
#ifdef DEBUG
	printf("%s",sccsID) ;
#endif
	if (argc == 1) /* no args ; copy standard input */
		{
		fprintf(stderr, "%s: Pipe usage not allowed.\n",*ARGV) ; 
		exit(1) ;
		}

	while (*++argv) errors += do_it(*argv) ;
	exit(errors) ;
	}


