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