cut: disallow -f '' and -f '-'
function old new delta cut_main 1391 1410 +19 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
b25ea3f156
commit
e8622f0d85
1 changed files with 19 additions and 15 deletions
|
@ -85,8 +85,8 @@
|
||||||
#define opt_REGEX (option_mask32 & OPT_REGEX)
|
#define opt_REGEX (option_mask32 & OPT_REGEX)
|
||||||
|
|
||||||
struct cut_list {
|
struct cut_list {
|
||||||
int startpos;
|
unsigned startpos;
|
||||||
int endpos;
|
unsigned endpos;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int cmpfunc(const void *a, const void *b)
|
static int cmpfunc(const void *a, const void *b)
|
||||||
|
@ -116,7 +116,7 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
|
||||||
|
|
||||||
/* print the chars specified in each cut list */
|
/* print the chars specified in each cut list */
|
||||||
for (; cl_pos < nlists; cl_pos++) {
|
for (; cl_pos < nlists; cl_pos++) {
|
||||||
int spos;
|
unsigned spos;
|
||||||
for (spos = cut_list[cl_pos].startpos; spos < linelen;) {
|
for (spos = cut_list[cl_pos].startpos; spos < linelen;) {
|
||||||
if (!printed[spos]) {
|
if (!printed[spos]) {
|
||||||
printed[spos] = 'X';
|
printed[spos] = 'X';
|
||||||
|
@ -171,7 +171,7 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
|
||||||
/* Cut by fields */
|
/* Cut by fields */
|
||||||
} else {
|
} else {
|
||||||
unsigned next = 0, start = 0, end = 0;
|
unsigned next = 0, start = 0, end = 0;
|
||||||
int dcount = 0; /* we saw Nth delimiter (0 - didn't see any yet) */
|
unsigned dcount = 0; /* we saw Nth delimiter (0 - didn't see any yet) */
|
||||||
|
|
||||||
/* Blank line? Check -s (later check for -s does not catch empty lines) */
|
/* Blank line? Check -s (later check for -s does not catch empty lines) */
|
||||||
if (linelen == 0) {
|
if (linelen == 0) {
|
||||||
|
@ -340,10 +340,10 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* parse list and put values into startpos and endpos.
|
* parse list and put values into startpos and endpos.
|
||||||
* valid list formats: N, N-, N-M, -M
|
* valid range formats: N, N-, N-M, -M
|
||||||
* more than one list can be separated by commas
|
* more than one range can be separated by commas
|
||||||
*/
|
*/
|
||||||
/* take apart the lists, one by one (they are separated with commas) */
|
/* take apart the ranges, one by one (separated with commas) */
|
||||||
while ((ltok = strsep(&sopt, ",")) != NULL) {
|
while ((ltok = strsep(&sopt, ",")) != NULL) {
|
||||||
char *ntok;
|
char *ntok;
|
||||||
int s, e;
|
int s, e;
|
||||||
|
@ -356,22 +356,26 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
|
||||||
/* get the start pos */
|
/* get the start pos */
|
||||||
ntok = strsep(<ok, "-");
|
ntok = strsep(<ok, "-");
|
||||||
if (!ntok[0]) {
|
if (!ntok[0]) {
|
||||||
s = 0;
|
if (!ltok) /* testcase: -f '' */
|
||||||
|
bb_show_usage();
|
||||||
|
if (!ltok[0]) /* testcase: -f - */
|
||||||
|
bb_show_usage();
|
||||||
|
s = 0; /* "-M" means "1-M" */
|
||||||
} else {
|
} else {
|
||||||
/* account for the fact that arrays are zero based, while
|
/* "N" or "N-[M]" */
|
||||||
* the user expects the first char on the line to be char #1 */
|
/* arrays are zero based, while the user expects
|
||||||
|
* the first field/char on the line to be char #1 */
|
||||||
s = xatoi_positive(ntok) - 1;
|
s = xatoi_positive(ntok) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* get the end pos */
|
/* get the end pos */
|
||||||
if (ltok == NULL) {
|
if (!ltok) {
|
||||||
e = s;
|
e = s; /* "N" means "N-N" */
|
||||||
} else if (!ltok[0]) {
|
} else if (!ltok[0]) {
|
||||||
/* if the user specified no end position,
|
/* "N-" means "until the end of the line" */
|
||||||
* that means "til the end of the line" */
|
|
||||||
e = INT_MAX;
|
e = INT_MAX;
|
||||||
} else {
|
} else {
|
||||||
/* again, arrays are zero based, lines are 1 based */
|
/* again, arrays are zero based, fields are 1 based */
|
||||||
e = xatoi_positive(ltok) - 1;
|
e = xatoi_positive(ltok) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue