xref: /minix3/usr.bin/cut/x_cut.c (revision d091a404e823cfd2b9b9be18cc8c1e852e55370a)
1*d091a404SThomas Cort /*	$NetBSD: x_cut.c,v 1.2 2007/07/02 18:41:04 christos Exp $	*/
2*d091a404SThomas Cort 
3*d091a404SThomas Cort /*
4*d091a404SThomas Cort  * Copyright (c) 1989, 1993
5*d091a404SThomas Cort  *	The Regents of the University of California.  All rights reserved.
6*d091a404SThomas Cort  *
7*d091a404SThomas Cort  * This code is derived from software contributed to Berkeley by
8*d091a404SThomas Cort  * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
9*d091a404SThomas Cort  *
10*d091a404SThomas Cort  * Redistribution and use in source and binary forms, with or without
11*d091a404SThomas Cort  * modification, are permitted provided that the following conditions
12*d091a404SThomas Cort  * are met:
13*d091a404SThomas Cort  * 1. Redistributions of source code must retain the above copyright
14*d091a404SThomas Cort  *    notice, this list of conditions and the following disclaimer.
15*d091a404SThomas Cort  * 2. Redistributions in binary form must reproduce the above copyright
16*d091a404SThomas Cort  *    notice, this list of conditions and the following disclaimer in the
17*d091a404SThomas Cort  *    documentation and/or other materials provided with the distribution.
18*d091a404SThomas Cort  * 3. Neither the name of the University nor the names of its contributors
19*d091a404SThomas Cort  *    may be used to endorse or promote products derived from this software
20*d091a404SThomas Cort  *    without specific prior written permission.
21*d091a404SThomas Cort  *
22*d091a404SThomas Cort  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*d091a404SThomas Cort  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*d091a404SThomas Cort  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*d091a404SThomas Cort  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*d091a404SThomas Cort  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*d091a404SThomas Cort  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*d091a404SThomas Cort  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*d091a404SThomas Cort  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*d091a404SThomas Cort  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*d091a404SThomas Cort  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*d091a404SThomas Cort  * SUCH DAMAGE.
33*d091a404SThomas Cort  */
34*d091a404SThomas Cort 
35*d091a404SThomas Cort /*
36*d091a404SThomas Cort  *  This file is #include'd twice from cut.c, to generate both
37*d091a404SThomas Cort  *  single- and multibyte versions of the same code.
38*d091a404SThomas Cort  *
39*d091a404SThomas Cort  *  In cut.c #define:
40*d091a404SThomas Cort  *   CUT_BYTE=0 to define b_cut (singlebyte), and
41*d091a404SThomas Cort  *   CUT_BYTE=1 to define c_cut (multibyte).
42*d091a404SThomas Cort  *
43*d091a404SThomas Cort  */
44*d091a404SThomas Cort 
45*d091a404SThomas Cort #if (CUT_BYTE == 1)
46*d091a404SThomas Cort #  define CUT_FN 		b_cut
47*d091a404SThomas Cort #  define CUT_CH_T 		int
48*d091a404SThomas Cort #  define CUT_GETC 		getc
49*d091a404SThomas Cort #  define CUT_EOF 		EOF
50*d091a404SThomas Cort #  define CUT_PUTCHAR		putchar
51*d091a404SThomas Cort #else
52*d091a404SThomas Cort #  define CUT_FN 		c_cut
53*d091a404SThomas Cort #  define CUT_CH_T 		wint_t
54*d091a404SThomas Cort #  define CUT_GETC 		getwc
55*d091a404SThomas Cort #  define CUT_EOF		WEOF
56*d091a404SThomas Cort #  define CUT_PUTCHAR		putwchar
57*d091a404SThomas Cort #endif
58*d091a404SThomas Cort 
59*d091a404SThomas Cort 
60*d091a404SThomas Cort /* ARGSUSED */
61*d091a404SThomas Cort void
CUT_FN(FILE * fp,const char * fname __unused)62*d091a404SThomas Cort CUT_FN(FILE *fp, const char *fname __unused)
63*d091a404SThomas Cort {
64*d091a404SThomas Cort 	CUT_CH_T ch;
65*d091a404SThomas Cort 	int col;
66*d091a404SThomas Cort 	char *pos;
67*d091a404SThomas Cort 
68*d091a404SThomas Cort 	ch = 0;
69*d091a404SThomas Cort 	for (;;) {
70*d091a404SThomas Cort 		pos = positions + 1;
71*d091a404SThomas Cort 		for (col = maxval; col; --col) {
72*d091a404SThomas Cort 			if ((ch = CUT_GETC(fp)) == EOF)
73*d091a404SThomas Cort 				return;
74*d091a404SThomas Cort 			if (ch == '\n')
75*d091a404SThomas Cort 				break;
76*d091a404SThomas Cort 			if (*pos++)
77*d091a404SThomas Cort 				(void)CUT_PUTCHAR(ch);
78*d091a404SThomas Cort 		}
79*d091a404SThomas Cort 		if (ch != '\n') {
80*d091a404SThomas Cort 			if (autostop)
81*d091a404SThomas Cort 				while ((ch = CUT_GETC(fp)) != CUT_EOF && ch != '\n')
82*d091a404SThomas Cort 					(void)CUT_PUTCHAR(ch);
83*d091a404SThomas Cort 			else
84*d091a404SThomas Cort 				while ((ch = CUT_GETC(fp)) != CUT_EOF && ch != '\n');
85*d091a404SThomas Cort 		}
86*d091a404SThomas Cort 		(void)CUT_PUTCHAR('\n');
87*d091a404SThomas Cort 	}
88*d091a404SThomas Cort }
89*d091a404SThomas Cort 
90*d091a404SThomas Cort #undef CUT_FN
91*d091a404SThomas Cort #undef CUT_CH_T
92*d091a404SThomas Cort #undef CUT_GETC
93*d091a404SThomas Cort #undef CUT_EOF
94*d091a404SThomas Cort #undef CUT_PUTCHAR
95*d091a404SThomas Cort 
96