/* tab2space - Convert all tabs in a file to spaces. */ /* */ /* This program scans through the input file for tab characters */ /* and converts them to the proper number of spaces based on the */ /* input tab stop size. */ /* */ /* Usage: tab2space TABSIZE file_name */ /* */ /* where TABSIZE is the number of spaces or columns for each */ /* tab stop. */ /* file_name is the name of the input file to be */ /* converted. */ /* */ /* Output: The converted file is sent to standard output <stdout> */ /* */ /* Example: If TABSIZE = 4 then the tab stops are set as follows: */ /* */ /* 0123456789012345678901234567890 */ /* t t t t t t t */ /* */ /* tab2space 4 foo.txt > foo.new.txt */ /* */ /* The tabs in "foo.txt" are written to "foo.new.txt" using */ /* using 4 spaces per tab stop. */ // David Edwards <edwards@300M.us> Tue Jan 9 04:49:40 EST 2001 // Make with: gcc [-ggdb] -o tab2space tab2space.c // Or with any ANSI C compiler... /********************** REVISION HISTORY **************************** 01-09-01: Original release *********************************************************************/ #define BUFSIZE 5120 // Maximum line length #include <stdio.h> #include <stdlib.h> int main(int argc,char **argv) { FILE *fin; int i, j, col, spaces, tabsize; char line[BUFSIZE]; /********************************* Initialization Portion of Program **********************************/ // Error handling if (argc != 3) { fprintf(stderr,"\nConvert tab stops to spaces." " /DEE 01-09-01 Rev 1.0\n" "\n Usage: tab2space TABSIZE filename\n" "\n Output: Standard output file with tabs converted to spaces." "\n Tab stops at multiples of TABSIZE are maintained.\n\n"); return 1; } // Open the input file if ((fin = fopen(argv[2], "rt")) == NULL) { fprintf(stderr, "\nCannot open input file: %s\n",argv[2]); return 1; } // Read in TABSIZE tabsize = atoi(argv[1]); if (tabsize < 1) { fprintf(stderr, "\nTABSIZE must greater or equal to one.\n"); return 1; } /********************************* Write the result to stdout **********************************/ while (fgets(line,BUFSIZE,fin) != NULL) { // i=index into line, col=column number on page for (i=0,col=0;line[i] != 0;i++) { if (line[i] != '\t') { putchar(line[i]); col++; } else { spaces = tabsize - col % tabsize; // printf("%d %d",col, spaces); col += spaces; for (j=1;j<=spaces;j++) putchar(' '); } } } fclose(fin); return 0; }