AUSAM/source/S/detab.c

Compare this file to the similar file:
Show the results in this format:

/*
 *	detab [infile [outfile] ]
 *		This program replaces all tabs by blanks
 *		in infile placing result in outfile.
 *		May be used as a filter.
 *		also see 'entab'
 */
#define EOF	-1
#define EOLN	'\n'
#define CR	'\r'
#define FF	'\f'
#define TAB	'\t'
#define BS	'\b'

char ibuf[518], obuf[518];

main(argc, argv)
char *argv[];
{
	register c, ocol;

	if(argc > 3)
	{
		prints(2, "Usage: detab [ infile [ outfile ] ]\n");
		return 1;
	}
	if(argc > 1)
	{
		if(fopen(argv[1], ibuf) == -1)
		{
			perror( argv[1] );
			return 1;
		}
	}
	else
	{
		ibuf[0] = 0;	/* use standard input */
	}
	if(argc > 2)
	{
		if(fcreat(argv[2], obuf) == -1)
		{
			perror( argv[2] );
			return 1;
		}
	}
	else
	{
		obuf[0] = 1;	/* use standard output */
	}
	/* here beginneth the program */ 

	ocol = 0;
	while((c = getc(ibuf)) != EOF)
	{
		switch(c)
		{
		case EOLN:
		case CR:
		case FF:
			put( c );
			ocol = 0;
			break;
		case TAB:
			do
			{
				put( ' ' );
				ocol++;
			}
			while((ocol%8) != 0);
			break;
		case BS:
			put( c );
			ocol--;
			break;
		default:
			put( c );
			ocol++;
			break;
		}
	}
	fflush(obuf);
	return 0;
}
put( c )
char c;
{
	extern errno;

	putc( c , obuf );
	if( errno )
	{
		perror("write");
		exit(1);
	}
}