xref: /netbsd-src/usr.bin/cut/x_cut.c (revision b3af16c2d31163ee0a628f02fa5678c39260d98a)
1*b3af16c2Schristos /*	$NetBSD: x_cut.c,v 1.2 2007/07/02 18:41:04 christos Exp $	*/
294c137d5Shubertf 
394c137d5Shubertf /*
494c137d5Shubertf  * Copyright (c) 1989, 1993
594c137d5Shubertf  *	The Regents of the University of California.  All rights reserved.
694c137d5Shubertf  *
794c137d5Shubertf  * This code is derived from software contributed to Berkeley by
894c137d5Shubertf  * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
994c137d5Shubertf  *
1094c137d5Shubertf  * Redistribution and use in source and binary forms, with or without
1194c137d5Shubertf  * modification, are permitted provided that the following conditions
1294c137d5Shubertf  * are met:
1394c137d5Shubertf  * 1. Redistributions of source code must retain the above copyright
1494c137d5Shubertf  *    notice, this list of conditions and the following disclaimer.
1594c137d5Shubertf  * 2. Redistributions in binary form must reproduce the above copyright
1694c137d5Shubertf  *    notice, this list of conditions and the following disclaimer in the
1794c137d5Shubertf  *    documentation and/or other materials provided with the distribution.
1894c137d5Shubertf  * 3. Neither the name of the University nor the names of its contributors
1994c137d5Shubertf  *    may be used to endorse or promote products derived from this software
2094c137d5Shubertf  *    without specific prior written permission.
2194c137d5Shubertf  *
2294c137d5Shubertf  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2394c137d5Shubertf  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2494c137d5Shubertf  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2594c137d5Shubertf  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2694c137d5Shubertf  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2794c137d5Shubertf  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2894c137d5Shubertf  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2994c137d5Shubertf  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3094c137d5Shubertf  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3194c137d5Shubertf  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3294c137d5Shubertf  * SUCH DAMAGE.
3394c137d5Shubertf  */
3494c137d5Shubertf 
3594c137d5Shubertf /*
3694c137d5Shubertf  *  This file is #include'd twice from cut.c, to generate both
3794c137d5Shubertf  *  single- and multibyte versions of the same code.
3894c137d5Shubertf  *
3994c137d5Shubertf  *  In cut.c #define:
4094c137d5Shubertf  *   CUT_BYTE=0 to define b_cut (singlebyte), and
4194c137d5Shubertf  *   CUT_BYTE=1 to define c_cut (multibyte).
4294c137d5Shubertf  *
4394c137d5Shubertf  */
4494c137d5Shubertf 
4594c137d5Shubertf #if (CUT_BYTE == 1)
4694c137d5Shubertf #  define CUT_FN 		b_cut
4794c137d5Shubertf #  define CUT_CH_T 		int
4894c137d5Shubertf #  define CUT_GETC 		getc
4994c137d5Shubertf #  define CUT_EOF 		EOF
5094c137d5Shubertf #  define CUT_PUTCHAR		putchar
5194c137d5Shubertf #else
5294c137d5Shubertf #  define CUT_FN 		c_cut
5394c137d5Shubertf #  define CUT_CH_T 		wint_t
5494c137d5Shubertf #  define CUT_GETC 		getwc
5594c137d5Shubertf #  define CUT_EOF		WEOF
5694c137d5Shubertf #  define CUT_PUTCHAR		putwchar
5794c137d5Shubertf #endif
5894c137d5Shubertf 
5994c137d5Shubertf 
6094c137d5Shubertf /* ARGSUSED */
6194c137d5Shubertf void
CUT_FN(FILE * fp,const char * fname __unused)62*b3af16c2Schristos CUT_FN(FILE *fp, const char *fname __unused)
6394c137d5Shubertf {
6494c137d5Shubertf 	CUT_CH_T ch;
6594c137d5Shubertf 	int col;
6694c137d5Shubertf 	char *pos;
6794c137d5Shubertf 
6894c137d5Shubertf 	ch = 0;
6994c137d5Shubertf 	for (;;) {
7094c137d5Shubertf 		pos = positions + 1;
7194c137d5Shubertf 		for (col = maxval; col; --col) {
7294c137d5Shubertf 			if ((ch = CUT_GETC(fp)) == EOF)
7394c137d5Shubertf 				return;
7494c137d5Shubertf 			if (ch == '\n')
7594c137d5Shubertf 				break;
7694c137d5Shubertf 			if (*pos++)
7794c137d5Shubertf 				(void)CUT_PUTCHAR(ch);
7894c137d5Shubertf 		}
7994c137d5Shubertf 		if (ch != '\n') {
8094c137d5Shubertf 			if (autostop)
8194c137d5Shubertf 				while ((ch = CUT_GETC(fp)) != CUT_EOF && ch != '\n')
8294c137d5Shubertf 					(void)CUT_PUTCHAR(ch);
8394c137d5Shubertf 			else
8494c137d5Shubertf 				while ((ch = CUT_GETC(fp)) != CUT_EOF && ch != '\n');
8594c137d5Shubertf 		}
8694c137d5Shubertf 		(void)CUT_PUTCHAR('\n');
8794c137d5Shubertf 	}
8894c137d5Shubertf }
8994c137d5Shubertf 
9094c137d5Shubertf #undef CUT_FN
9194c137d5Shubertf #undef CUT_CH_T
9294c137d5Shubertf #undef CUT_GETC
9394c137d5Shubertf #undef CUT_EOF
9494c137d5Shubertf #undef CUT_PUTCHAR
9594c137d5Shubertf 
96