[BusyBox] Bugfix cut.c
Arne Bernin
arne at matrix.loopback.org
Sun Jun 25 22:46:19 UTC 2000
Hi,
while trying to build in the -s option to cut i found a bug in processing
lines without the delimeter inside. cut used to print parts of the line
again when requesting multible columns with -f (like -f 1,2).
I'll include a patch to fix this and also enable the -s switch for printing
only lines with the delimiter.
Also fixed a segmentation fault when using cut like this:
cut -f 1 -d: , it now prints an error message (syntax error).
--arne
----------------------------------------------------------------------
Arne Bernin
-------------- next part --------------
Index: cut.c
===================================================================
RCS file: /usr/local/cvsroot/busybox/cut.c,v
retrieving revision 1.1
retrieving revision 1.3
diff -c -r1.1 -r1.3
*** cut.c 2000/06/25 22:23:49 1.1
--- cut.c 2000/06/25 22:37:19 1.3
***************
*** 52,65 ****
#define OPTIONB 3 /* define for mode: option -b */
#define NOTSET 0 /* option not selected */
#define SET 1 /* option selected */
!
/* Defines for the warnings */
#define DELIMITER_NOT_APPLICABLE 0
#define OVERRIDING_PREVIOUS_MODE 1
#define OPTION_NOT_APPLICABLE 2
#define UNKNOWN_OPTION 3
#define FILE_NOT_READABLE 4
-
/* Defines for the fatal errors */
#define SYNTAX_ERROR 101
#define POSITION_ERROR 102
--- 52,64 ----
#define OPTIONB 3 /* define for mode: option -b */
#define NOTSET 0 /* option not selected */
#define SET 1 /* option selected */
! #define OPTIONS 1 /*define option -s */
/* Defines for the warnings */
#define DELIMITER_NOT_APPLICABLE 0
#define OVERRIDING_PREVIOUS_MODE 1
#define OPTION_NOT_APPLICABLE 2
#define UNKNOWN_OPTION 3
#define FILE_NOT_READABLE 4
/* Defines for the fatal errors */
#define SYNTAX_ERROR 101
#define POSITION_ERROR 102
***************
*** 75,80 ****
--- 74,80 ----
char *name;
char line[BUFSIZ];
int exit_status;
+ int option = 0; /* for -s option */
int cut_main(int argc, char **argv);
void warn(int warn_number, char *option);
***************
*** 85,91 ****
void warn(int warn_number, char *option)
{
static char *warn_msg[] = {
! "%s: Option -d allowed only with -f\n",
"%s: -%s overrides earlier option\n",
"%s: -%s not allowed in current mode\n",
"%s: Cannot open %s\n"
--- 85,91 ----
void warn(int warn_number, char *option)
{
static char *warn_msg[] = {
! "%s: Option -%s allowed only with -f\n",
"%s: -%s overrides earlier option\n",
"%s: -%s not allowed in current mode\n",
"%s: Cannot open %s\n"
***************
*** 161,167 ****
char *columns[MAX_FIELD];
while (fgets(line, BUFSIZ, fd)) {
! length = strlen(line) - 1;
*(line + length) = 0;
switch (mode) {
case DUMP_STDIN:
--- 161,168 ----
char *columns[MAX_FIELD];
while (fgets(line, BUFSIZ, fd)) {
! maxcol=0;
! length = strlen(line) - 1;
*(line + length) = 0;
switch (mode) {
case DUMP_STDIN:
***************
*** 182,192 ****
--- 183,197 ----
for (i = 0; i < num_args; i++) {
for (j = args[i * 2]; j <= args[i * 2 + 1]; j++)
if (j <= maxcol) {
+
printf("%s", columns[j - 1]);
+
if (i != num_args - 1 || j != args[i * 2 + 1])
putchar(delim);
}
}
+ } else if (option != OPTIONS) {
+ printf("%s",line);
}
break;
case OPTIONC:
***************
*** 217,224 ****
"Options:\n"
"\t-b LIST\tOutput only bytes from LIST\n"
"\t-c LIST\tOutput only characters from LIST\n"
! "\t-d DELIM\tUse DELIM instead of tab as the field delimiter\n"
! "\t-f N\tPrint only these fields\n"
"\t-n\tIgnored\n"
#endif
);
--- 222,230 ----
"Options:\n"
"\t-b LIST\tOutput only bytes from LIST\n"
"\t-c LIST\tOutput only characters from LIST\n"
! "\t-d DELIM\tUse DELIM instead of tab as the field delimiter\n"
! "\t -s\tOnly output Lines if the include DELIM\n"
! "\t-f N\tPrint only these fields\n"
"\t-n\tIgnored\n"
#endif
);
***************
*** 229,235 ****
case 'd':
if (mode == OPTIONC || mode == OPTIONB)
warn(DELIMITER_NOT_APPLICABLE, "d");
! delim = argv[i++][0];
break;
case 'f':
sprintf(line, "%s", argv[i++]);
--- 235,244 ----
case 'd':
if (mode == OPTIONC || mode == OPTIONB)
warn(DELIMITER_NOT_APPLICABLE, "d");
! if (argc > i)
! delim = argv[i++][0];
! else
! cuterror(SYNTAX_ERROR);
break;
case 'f':
sprintf(line, "%s", argv[i++]);
***************
*** 249,254 ****
--- 258,267 ----
warn(OVERRIDING_PREVIOUS_MODE, "c");
mode = OPTIONC;
break;
+ case 's':
+ option = OPTIONS;
+
+ break;
case '\0': /* - means: read from stdin */
numberFilenames++;
break;
***************
*** 268,273 ****
--- 281,289 ----
/* Here follow the checks, if the selected options are reasonable. */
if (mode == OPTIONB) /* since in Minix char := byte */
mode = OPTIONC;
+
+ if (mode != OPTIONF && option == OPTIONS)
+ warn(DELIMITER_NOT_APPLICABLE,"s");
get_args();
if (numberFilenames != 0) {
i = 1;
More information about the busybox
mailing list