home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Media Share 9
/
MEDIASHARE_09.ISO
/
clarion
/
untab.zip
/
UNTAB.C
< prev
next >
Wrap
Text File
|
1989-04-24
|
1KB
|
59 lines
/*--------------------- untab.c -----------------------------------
*
* Chuck Eckenroed (208) 385-0702 April 1989
*
* This program reads a file and replaces all occurances of
* the tab charter (0x09) with the proper number of spaces (1 to 8)
* to remove 8 character tab spacing.
*
*/
#include <stdio.h>
#define TEMPFILE "$untab$.$$$"
#define TABSIZE 8
main(argc,argv)
int argc;
char *argv[];
{
FILE *in_file, *tmp_file;
char ch;
int i,col = 0,tab_cnt;
if (argc != 2)
{printf("Format: C>untab filename.ext"); exit();}
if ((in_file=fopen(argv[1],"rb")) == NULL)
{perror("Open error"); exit();}
if ((tmp_file=fopen(TEMPFILE,"wb")) == NULL)
{perror("Temp file open error"); exit();}
while((ch=getc(in_file)) != EOF) {
switch(ch) {
case '\t':
ch = ' ';
tab_cnt = (next_tab(col) - col);
for (i= 0; i< tab_cnt - 1; i++)
putc(ch, tmp_file);
col += i;
break;
case '\r' :
case '\n' :
col = 0;
col--;
break;
} /* end switch */
putc(ch, tmp_file);
col++;
} /* end while */
fclose(in_file);
fclose(tmp_file);
remove(argv[1]);
rename(TEMPFILE,argv[1]);
}
next_tab(int x)
{
return (x / TABSIZE + 1) * TABSIZE;
}