Category : Word Processors
Archive   : TABEDIT.ZIP
Filename : DETAB.C

 
Output of file : DETAB.C contained in archive : TABEDIT.ZIP
/*
* detab - convert tabs to spaces
*
* usage: detab [ -tN ] [ file(s) ]
*
* -tN Set tab stops at every N columns. Default is
* 8 unless the TABS environment variable is set.
*
* Output is placed on the standard output, and may be sent
* to a file using ">" on the command line.
*
* Tab stops may also be set using an environment variable.
* The DOS command SET TABS=4 would set tabs at every 4th
* column, rather than using the normal default of 8.
*
*/

#include

/* !!!!!!!! EDIT THESE IF OPTIONS OR COMMAND NAME CHANGES !!!!!!!! */
char me[] = "detab";
char usage[] = "usage: detab [ -tN ] file(s)\n";

#define DEFTABS 8 /* default tabs every 8th column */
#define MAXTABS 120 /* maximum tabs every 120th column */
#define YES 1
#define NO 0

FILE *fopen();
char *getenv();

short tabstop; /* tab column increment */

main(argc,argv)
int argc;
register char **argv;
{
FILE *fp; /* input file pointer */
char *stops; /* pointers to tab stop arg */

argv++;

tabstop = DEFTABS;
if ( (stops=getenv("TABS")) != NULL ) {
if ( (tabstop = atoi(stops)) <= 0 )
tabstop = DEFTABS;
}

/* Process arguments */
while ( *argv != NULL && **argv == '-') {
switch ( (*argv)[1] ) {
case 't':
stops = &(*argv)[2];
tabstop = atoi(stops);
break;
default:
fprintf(stderr, "%s: unknown option \"%s\"\n",
me, *argv);
fputs(usage, stderr);
exit(1);
}
++argv;
}

if ( tabstop < 1 || tabstop >= MAXTABS ) {
fprintf(stderr, "%s: can't use tab stops -- %s\n", me, stops);
exit(1);
}

if ( *argv == NULL ) { /* no args, use std input */
deprint(stdin);
} else {
for ( ; *argv != NULL ; ++argv ) {
if ( strcmp(*argv,"-") == 0 ) {
deprint(stdin);
} else if ( (fp = fopen(*argv, "r") ) == NULL) {
fprintf(stderr,"%s: can't open \"%s\"\n",
me, *argv);
exit(1);
} else {
deprint(fp);
fclose(fp);
}
}
}

exit(0);
}

deprint(fp)
FILE *fp;

{
int padcnt; /* blank padding to tabstops */
int colpos; /* current column position */
register int ch; /* current character */

colpos = 1;
while ( (ch = getc(fp)) != EOF ) {
if ( ch == '\t' ) {
padcnt = colpos-1; /* blank pad to next stop */
padcnt %= tabstop;
padcnt = tabstop - padcnt;
colpos += padcnt; /* add count to col counter */
while ( --padcnt >= 0 )
putc(' ', stdout);
} else {
if ( ch == '\n' ) /* reset col pos on newline */
colpos = 0;
putc(ch, stdout);
colpos++;
}
}

}



  3 Responses to “Category : Word Processors
Archive   : TABEDIT.ZIP
Filename : DETAB.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/