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