usr.bin/col/col.c
void
flush_line(LINE *l)
{
	CHAR *c, *endc;
	int nchars, last_col, this_col;

	last_col = 0;
	nchars = l->l_line_len;

	if (l->l_needs_sort) {
		static CHAR *sorted;
		static int count_size, *count, i, save, sorted_size, tot;

		/*
		 * Do an O(n) sort on l->l_line by column being careful to
		 * preserve the order of characters in the same column.
		 */
		if (l->l_lsize > sorted_size) {
			sorted_size = l->l_lsize;
			if ((sorted = realloc(sorted,
			    (unsigned)sizeof(CHAR) * sorted_size)) == NULL)
				err(1, NULL);
		}
		if (l->l_max_col >= count_size) {
			count_size = l->l_max_col + 1;
			if ((count = realloc(count,
			    (unsigned)sizeof(int) * count_size)) == NULL)
				err(1, NULL);
		}
		memset(count, 0, sizeof(int) * l->l_max_col + 1);
		for (i = nchars, c = l->l_line; --i >= 0; c++)
			count[c->c_column]++;

		/*
		 * calculate running total (shifted down by 1) to use as
		 * indices into new line.
		 */
		for (tot = 0, i = 0; i <= l->l_max_col; i++) {
			save = count[i];
			count[i] = tot;
			tot += save;
		}

		for (i = nchars, c = l->l_line; --i >= 0; c++)
			sorted[count[c->c_column]++] = *c;
		c = sorted;
	} else
		c = l->l_line;
	while (nchars > 0) {
		this_col = c->c_column;
		endc = c;
		do {
			++endc;
		} while (--nchars > 0 && this_col == endc->c_column);

		/* if -b only print last character */
		if (no_backspaces)
			c = endc - 1;

		if (this_col > last_col) {
			int nspace = this_col - last_col;

			if (compress_spaces && nspace > 1) {
				while (1) {
					int tab_col, tab_size;

					tab_col = (last_col + 8) & ~7;
					if (tab_col > this_col)
						break;
					tab_size = tab_col - last_col;
					if (tab_size == 1)
						PUTC(' ');
					else
						PUTC('\t');
					nspace -= tab_size;
					last_col = tab_col;
				}
			}
			while (--nspace >= 0)
				PUTC(' ');
			last_col = this_col;
		}
		last_col++;

		for (;;) {
			if (c->c_set != last_set) {
				switch (c->c_set) {
				case CS_NORMAL:
					PUTC('\017');
					break;
				case CS_ALTERNATE:
					PUTC('\016');
				}
				last_set = c->c_set;
			}
			PUTC(c->c_char);
			if (++c >= endc)
				break;
			PUTC('\b');
		}
	}
}
/*
 * Write a line to stdout taking care of space to tab conversion (-h flag)
 * and character set shifts.
 */
Write a line to stdout taking care of space to tab conversion (-h flag)
and character set shifts.
Write a line to stdout taking care of space to tab conversion ( - h flag ) and character set shifts .
void
flush_line(LINE *l)
{
	CHAR *c, *endc;
	int nchars, last_col, this_col;
	last_col = 0;
	nchars = l->l_line_len;
	if (l->l_needs_sort) {
		static CHAR *sorted;
		static int count_size, *count, i, save, sorted_size, tot;
		if (l->l_lsize > sorted_size) {
			sorted_size = l->l_lsize;
			if ((sorted = realloc(sorted,
			    (unsigned)sizeof(CHAR) * sorted_size)) == NULL)
				err(1, NULL);
		}
		if (l->l_max_col >= count_size) {
			count_size = l->l_max_col + 1;
			if ((count = realloc(count,
			    (unsigned)sizeof(int) * count_size)) == NULL)
				err(1, NULL);
		}
		memset(count, 0, sizeof(int) * l->l_max_col + 1);
		for (i = nchars, c = l->l_line; --i >= 0; c++)
			count[c->c_column]++;
		for (tot = 0, i = 0; i <= l->l_max_col; i++) {
			save = count[i];
			count[i] = tot;
			tot += save;
		}
		for (i = nchars, c = l->l_line; --i >= 0; c++)
			sorted[count[c->c_column]++] = *c;
		c = sorted;
	} else
		c = l->l_line;
	while (nchars > 0) {
		this_col = c->c_column;
		endc = c;
		do {
			++endc;
		} while (--nchars > 0 && this_col == endc->c_column);
		if (no_backspaces)
			c = endc - 1;
		if (this_col > last_col) {
			int nspace = this_col - last_col;
			if (compress_spaces && nspace > 1) {
				while (1) {
					int tab_col, tab_size;
					tab_col = (last_col + 8) & ~7;
					if (tab_col > this_col)
						break;
					tab_size = tab_col - last_col;
					if (tab_size == 1)
						PUTC(' ');
					else
						PUTC('\t');
					nspace -= tab_size;
					last_col = tab_col;
				}
			}
			while (--nspace >= 0)
				PUTC(' ');
			last_col = this_col;
		}
		last_col++;
		for (;;) {
			if (c->c_set != last_set) {
				switch (c->c_set) {
				case CS_NORMAL:
					PUTC('\017');
					break;
				case CS_ALTERNATE:
					PUTC('\016');
				}
				last_set = c->c_set;
			}
			PUTC(c->c_char);
			if (++c >= endc)
				break;
			PUTC('\b');
		}
	}
}
void flush_line ( LINE * l ) { CHAR * c , * endc ; int nchars , last_col , this_col ; last_col = 0 ; nchars = l -> l_line_len ; if ( l -> l_needs_sort ) { static CHAR * sorted ; static int count_size , * count , i , save , sorted_size , tot ; if ( l -> l_lsize > sorted_size ) { sorted_size = l -> l_lsize ; if ( ( sorted = realloc ( sorted , ( unsigned ) sizeof ( CHAR ) * sorted_size ) ) == NULL ) err ( 1 , NULL ) ; } if ( l -> l_max_col >= count_size ) { count_size = l -> l_max_col + 1 ; if ( ( count = realloc ( count , ( unsigned ) sizeof ( int ) * count_size ) ) == NULL ) err ( 1 , NULL ) ; } memset ( count , 0 , sizeof ( int ) * l -> l_max_col + 1 ) ; for ( i = nchars , c = l -> l_line ; -- i >= 0 ; c ++ ) count [ c -> c_column ] ++ ; for ( tot = 0 , i = 0 ; i <= l -> l_max_col ; i ++ ) { save = count [ i ] ; count [ i ] = tot ; tot += save ; } for ( i = nchars , c = l -> l_line ; -- i >= 0 ; c ++ ) sorted [ count [ c -> c_column ] ++ ] = * c ; c = sorted ; } else c = l -> l_line ; while ( nchars > 0 ) { this_col = c -> c_column ; endc = c ; do { ++ endc ; } while ( -- nchars > 0 && this_col == endc -> c_column ) ; if ( no_backspaces ) c = endc - 1 ; if ( this_col > last_col ) { int nspace = this_col - last_col ; if ( compress_spaces && nspace > 1 ) { while ( 1 ) { int tab_col , tab_size ; tab_col = ( last_col + 8 ) & ~ 7 ; if ( tab_col > this_col ) break ; tab_size = tab_col - last_col ; if ( tab_size == 1 ) PUTC ( ' ' ) ; else PUTC ( ' \t ' ) ; nspace -= tab_size ; last_col = tab_col ; } } while ( -- nspace >= 0 ) PUTC ( ' ' ) ; last_col = this_col ; } last_col ++ ; for ( ; ; ) { if ( c -> c_set != last_set ) { switch ( c -> c_set ) { case CS_NORMAL : PUTC ( ' \017 ' ) ; break ; case CS_ALTERNATE : PUTC ( ' \016 ' ) ; } last_set = c -> c_set ; } PUTC ( c -> c_char ) ; if ( ++ c >= endc ) break ; PUTC ( ' \b ' ) ; } } }
Write a line to stdout taking care of space to tab conversion (-h flag)
 and character set shifts.
Write a line to stdout taking care of space to tab conversion ( - h flag ) and character set shifts .
/*
		 * Do an O(n) sort on l->l_line by column being careful to
		 * preserve the order of characters in the same column.
		 */ /*
		 * calculate running total (shifted down by 1) to use as
		 * indices into new line.
		 */ /* if -b only print last character */