A program to list files from bottom to top

Bruce Fowler brf at link.UUCP
Fri Sep 6 12:32:37 AEST 1985


$ cat spoon >/dev/bug	# Gag on that, line eater!

	O.K ... I CAN'T STAND IT ANYMORE ... ALL THAT CREATIVITY!

	Here is the *ultimate* way to invert the lines in a file:
		
		$ tip <filename> | tip

	Enjoy!
						Bruce Fowler
^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v
    "The Twilight Zone - Love it or Leave it..."

 ____  ==> [ unzip here ]
|____)-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-8-
/* tip.c - tip section of text sideways (90 degrees counterclockwise) */

#include	<stdio.h>
#define	TXTSIDE	80	/* number of rows and columns maximum */
int incval;		/* column increment, 1 or 2 */
int padflag;		/* to keep leading blank lines */

main(argc, argv )
int argc;
char *argv[];
{
	char *p;	/* for flag arg analysis */
	int nrfiles;	/* how many files processed */
	FILE *fp;	/* files to process */

	/* analyze flags and do each file argument */
	incval = 1;
	padflag = 0;
	nrfiles = 0;
	while (--argc > 0 ) {   /* process each argument */
		p = *++argv;
		if (*p == '-' ) {	/* suck up flags */
			while (*++p != '\0' ) switch (*p ) {
			/* s and d are opposite */
			case 'd':	/* pretend input is double spaced */
				incval = 2;
				break;
			case 's':	/* stop double spacing */
				incval = 1;
				break;
			/* p and n are opposite */
			case 'p':	/* keep padding at left (top) */
				padflag = 1;
				break;
			case 'n':	/* left justify output */
				padflag = 0;
				break;
			default:
				fputs("usage: tip [-dspn] [<file>] ...\n",
					stderr );
				exit(1 );
			}
		} else {
			nrfiles++;	/* file name provided */
			if ((fp = fopen(*argv,"r" )) == NULL ) {
				 fputs("tip: can't open ", stderr );
				 fputs(*argv, stderr );
				 putc('\n', stderr );
			} else {
				tip(fp ); /* process new file */
				fclose(fp );
			}
		}
	}
	if (nrfiles == 0 ) {  /* no file args -- process std input */
		tip(stdin);
	}
}

tip(input )		/* writes to stdout - truncates and pads as necessary */
	FILE *input;
{
	char pix[TXTSIDE][TXTSIDE];
	int len[TXTSIDE];	/* size of useful data in each line */
	int i,j;		/* row and column, what else? */
	int c;		/* as in Current Character */
	int jinc;		/* row increment */

	/* initialize array of linesizes */
	for (i=0; i<TXTSIDE; i++ ) len[i] = -1;
	/* set text array to blanks */
	memset(pix, ' ', TXTSIDE*TXTSIDE );
	/* read input, spread chars down left edge */
	c = ' ';
	jinc = (padflag) ? incval : 0;
	for (j=0; j<TXTSIDE && c!=EOF; j+=jinc ) {
		/* get a character to chew on */
		c = getc(input );
		for (i=0; i<TXTSIDE && c!='\n' && c!=EOF; i++ ) {
			if (c > ' ' && c <= '~' ) {
				pix[i][j] = c;
				if (len[i]<j ) len[i] = j;
				jinc = incval;
			}
			/* adjust row position to next tab stop */
			else if (c == '\t' ) i += 8 - (i + 9 ) & 07;
			c = getc(input );
		}
		/* throw away end of long lines */
		while (c!='\n' && c!=EOF ) c = getc(input );
	}
	/* trim off trailing blank lines */
	for (i=0; i<TXTSIDE && len[i]==(-1); i++ ) len[i] = (-2);
	/* trim off leading blank lines */
	for (i=TXTSIDE-1; i>=0 && len[i]<0; i-- );
	/* print out the result */
	for ( ; i>=0 && len[i]!=(-2); i-- ) {
		for (j=0; j<=len[i]; j++ ) putchar(pix[i][j] );
		putchar('\n' );
	}
}



More information about the Comp.sources.unix mailing list