1 /* vi: set sw=4 ts=4: */
3 * cut implementation for busybox
5 * Copyright (c) Michael J. Holme
7 * This version of cut is adapted from Minix cut and was modified
8 * by Erik Andersen <andersee@debian.org> to be used in busybox.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Original copyright notice is retained at the end of this file.
28 #include <sys/types.h>
34 #define BB_DECLARE_EXTERN
38 #define MAX_FIELD 80 /* Pointers to the beginning of each field
39 * are stored in columns[], if a line holds
40 * more than MAX_FIELD columns the array
41 * boundary is exceed. But unlikely at 80 */
43 #define MAX_ARGS 32 /* Maximum number of fields following -f or
45 int args[MAX_ARGS * 2];
48 /* Lots of new defines, should easen maintainance... */
49 #define DUMP_STDIN 0 /* define for mode: no options */
50 #define OPTIONF 1 /* define for mode: option -f */
51 #define OPTIONC 2 /* define for mode: option -c */
52 #define OPTIONB 3 /* define for mode: option -b */
53 #define NOTSET 0 /* option not selected */
54 #define SET 1 /* option selected */
55 #define OPTIONS 1 /*define option -s */
56 /* Defines for the warnings */
57 #define DELIMITER_NOT_APPLICABLE 0
58 #define OVERRIDING_PREVIOUS_MODE 1
59 #define OPTION_NOT_APPLICABLE 2
60 #define UNKNOWN_OPTION 3
61 #define FILE_NOT_READABLE 4
62 /* Defines for the fatal errors */
63 #define SYNTAX_ERROR 101
64 #define POSITION_ERROR 102
65 #define LINE_TO_LONG_ERROR 103
66 #define RANGE_ERROR 104
67 #define MAX_FIELDS_EXEEDED_ERROR 105
68 #define MAX_ARGS_EXEEDED_ERROR 106
71 int mode; /* 0 = dump stdin to stdout, 1=-f, 2=-c */
72 char delim = '\t'; /* default delimiting character */
76 int option = 0; /* for -s option */
78 int cut_main(int argc, char **argv);
79 void warn(int warn_number, char *option);
80 void cuterror(int err);
84 void warn(int warn_number, char *option)
86 static char *warn_msg[] = {
87 "Option -%s allowed only with -f\n",
88 "-%s overrides earlier option\n",
89 "-%s not allowed in current mode\n",
93 errorMsg(warn_msg[warn_number], option);
94 exit_status = warn_number + 1;
98 void cuterror(int err)
100 static char *err_mes[] = {
102 "position must be >0\n",
103 "line longer than BUFSIZ\n",
104 "range must not decrease from left to right\n",
105 "MAX_FIELD exceeded\n",
106 "MAX_ARGS exceeded\n"
109 errorMsg(err_mes[err - 101]);
122 if (num_args == MAX_ARGS)
123 cuterror(MAX_ARGS_EXEEDED_ERROR);
124 if (!isdigit(line[i]) && line[i] != '-')
125 cuterror(SYNTAX_ERROR);
128 args[arg_ptr + 1] = BUFSIZ;
131 while (line[i] != ',' && line[i] != 0) {
132 if (isdigit(line[i])) {
134 while (isdigit(line[i]))
135 args[arg_ptr] = 10 * args[arg_ptr] + line[i++] - '0';
137 cuterror(POSITION_ERROR);
140 if (line[i] == '-') {
146 if (flag && arg_ptr & 1)
147 args[arg_ptr] = args[arg_ptr - 1];
148 if (args[num_args * 2] > args[num_args * 2 + 1])
149 cuterror(RANGE_ERROR);
151 arg_ptr = num_args * 2;
159 int i, j, length, maxcol=0;
160 char *columns[MAX_FIELD];
162 while (fgets(line, BUFSIZ, fd)) {
164 length = strlen(line) - 1;
165 *(line + length) = 0;
171 columns[maxcol++] = line;
172 for (i = 0; i < length; i++) {
173 if (*(line + i) == delim) {
175 if (maxcol == MAX_FIELD)
176 cuterror(MAX_FIELDS_EXEEDED_ERROR);
177 columns[maxcol] = line + i + 1;
182 for (i = 0; i < num_args; i++) {
183 for (j = args[i * 2]; j <= args[i * 2 + 1]; j++)
186 printf("%s", columns[j - 1]);
188 if (i != num_args - 1 || j != args[i * 2 + 1])
192 } else if (option != OPTIONS) {
197 for (i = 0; i < num_args; i++) {
198 for (j = args[i * 2];
199 j <= (args[i * 2 + 1] >
200 length ? length : args[i * 2 + 1]); j++)
201 putchar(*(line + j - 1));
209 const char cut_usage[] =
210 "cut [OPTION]... [FILE]...\n"
211 #ifndef BB_FEATURE_TRIVIAL_HELP
212 "\nPrints selected fields from each input FILE to standard output.\n\n"
214 "\t-b LIST\tOutput only bytes from LIST\n"
215 "\t-c LIST\tOutput only characters from LIST\n"
216 "\t-d CHAR\tUse CHAR instead of tab as the field delimiter\n"
217 "\t-s\tOnly output Lines if the include DELIM\n"
218 "\t-f N\tPrint only these fields\n"
223 int cut_main(int argc, char **argv)
226 int numberFilenames = 0;
228 if (argc == 1 || strcmp(argv[1], dash_dash_help)==0)
232 if (argv[i][0] == '-') {
233 switch (argv[i++][1]) {
235 if (mode == OPTIONC || mode == OPTIONB)
236 warn(DELIMITER_NOT_APPLICABLE, "d");
238 delim = argv[i++][0];
240 cuterror(SYNTAX_ERROR);
243 sprintf(line, "%s", argv[i++]);
244 if (mode == OPTIONC || mode == OPTIONB)
245 warn(OVERRIDING_PREVIOUS_MODE, "f");
249 sprintf(line, "%s", argv[i++]);
250 if (mode == OPTIONF || mode == OPTIONC)
251 warn(OVERRIDING_PREVIOUS_MODE, "b");
255 sprintf(line, "%s", argv[i++]);
256 if (mode == OPTIONF || mode == OPTIONB)
257 warn(OVERRIDING_PREVIOUS_MODE, "c");
264 case '\0': /* - means: read from stdin */
267 case 'n': /* needed for Posix, but no effect here */
269 warn(OPTION_NOT_APPLICABLE, "n");
272 warn(UNKNOWN_OPTION, &(argv[i - 1][1]));
280 /* Here follow the checks, if the selected options are reasonable. */
281 if (mode == OPTIONB) /* since in Minix char := byte */
284 if (mode != OPTIONF && option == OPTIONS)
285 warn(DELIMITER_NOT_APPLICABLE,"s");
287 if (numberFilenames != 0) {
290 if (argv[i][0] == '-') {
291 switch (argv[i][1]) {
312 if ((fd = fopen(argv[i++], "r")) == NULL) {
313 warn(FILE_NOT_READABLE, argv[i - 1]);
328 /* cut - extract columns from a file or stdin. Author: Michael J. Holme
330 * Copyright 1989, Michael John Holme, All rights reserved.
331 * This code may be freely distributed, provided that this notice
334 * V1.1: 6th September 1989
336 * Bugs, criticisms, etc,
338 * JANET sq79@uk.ac.liv
339 * ARPA sq79%liv.ac.uk@nsfnet-relay.ac.uk
340 * UUCP ...!mcvax!ukc!liv.ac.uk!sq79
341 *-------------------------------------------------------------------------
342 * Changed for POSIX1003.2/Draft10 conformance
343 * Thomas Brupbacher (tobr@mw.lpc.ethz.ch), September 1990.
345 * - separation of error messages ( stderr) and output (stdout).
346 * - support for -b and -n (no effect, -b acts as -c)
348 *-------------------------------------------------------------------------
352 * Copyright (c) 1987,1997, Prentice Hall
353 * All rights reserved.
355 * Redistribution and use of the MINIX operating system in source and
356 * binary forms, with or without modification, are permitted provided
357 * that the following conditions are met:
359 * Redistributions of source code must retain the above copyright
360 * notice, this list of conditions and the following disclaimer.
362 * Redistributions in binary form must reproduce the above
363 * copyright notice, this list of conditions and the following
364 * disclaimer in the documentation and/or other materials provided
365 * with the distribution.
367 * Neither the name of Prentice Hall nor the names of the software
368 * authors or contributors may be used to endorse or promote
369 * products derived from this software without specific prior
370 * written permission.
372 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
373 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
374 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
375 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
376 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
377 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
378 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
379 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
380 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
381 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
382 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
383 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.