2 * Copyright (c) 1980 Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL>
36 * added Native Language Support
37 * 1999-09-19 Bruno Haible <haible@clisp.cons.org>
38 * modified to work correctly in multi-byte locales
50 #include "closestream.h"
53 COLRM removes unwanted columns from a file
54 Jeff Schriebman UC Berkeley 11-74
57 static void __attribute__((__noreturn__
)) usage(void)
60 fprintf(out
, _("\nUsage:\n"
61 " %s [startcol [endcol]]\n"),
62 program_invocation_short_name
);
64 fputs(USAGE_SEPARATOR
, out
);
65 fputs(_("Filter out the specified columns.\n"), out
);
67 fputs(USAGE_OPTIONS
, out
);
68 printf(USAGE_HELP_OPTIONS(16));
69 fprintf(out
, _("%s reads from standard input and writes to standard output\n\n"),
70 program_invocation_short_name
);
71 printf(USAGE_MAN_TAIL("colrm(1)"));
75 static int process_input(unsigned long first
, unsigned long last
)
88 w
= ((ct
+ 8) & ~7) - ct
;
90 w
= (ct
? ct
- 1 : 0) - ct
;
103 if (!first
|| ct
< first
) {
110 for (i
= ct
- w
+ 1; i
< first
; i
++)
113 /* Loop getting rid of characters */
114 while (!last
|| ct
< last
) {
125 ct
= ct
? ct
- 1 : 0;
136 /* Output last of the line */
145 if (padding
== 0 && last
< ct
) {
146 for (i
= last
; i
< ct
; i
++)
155 int main(int argc
, char **argv
)
157 unsigned long first
= 0, last
= 0;
160 static const struct option longopts
[] = {
161 {"version", no_argument
, NULL
, 'V'},
162 {"help", no_argument
, NULL
, 'h'},
166 setlocale(LC_ALL
, "");
167 bindtextdomain(PACKAGE
, LOCALEDIR
);
169 atexit(close_stdout
);
172 getopt_long(argc
, argv
, "bfhl:pxVH", longopts
,
176 print_version(EXIT_SUCCESS
);
180 errtryhelp(EXIT_FAILURE
);
184 first
= strtoul_or_err(*++argv
, _("first argument"));
186 last
= strtoul_or_err(*++argv
, _("second argument"));
188 while (process_input(first
, last
))