aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-07-18 18:32:25 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-07-18 18:32:25 +0000
commitaf1bd0962527ae7c3b75fcc028186e5def6141d5 (patch)
tree07eb5a4c38d6d6df104f0aaa52cbcbd722f54e58
parentb78c782c85705494634a936f6a9b0c6fcaf0860a (diff)
downloadbusybox-w32-af1bd0962527ae7c3b75fcc028186e5def6141d5.tar.gz
busybox-w32-af1bd0962527ae7c3b75fcc028186e5def6141d5.tar.bz2
busybox-w32-af1bd0962527ae7c3b75fcc028186e5def6141d5.zip
awk: fix -F 'regex' bug (miscounted fields if last field is empty)
-rw-r--r--editors/awk.c42
1 files changed, 25 insertions, 17 deletions
diff --git a/editors/awk.c b/editors/awk.c
index c087a5d4e..752c73e7e 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -1522,9 +1522,12 @@ static int awk_split(const char *s, node *spl, char **slist)
1522 if (*getvar_s(intvar[RS]) == '\0') 1522 if (*getvar_s(intvar[RS]) == '\0')
1523 c[2] = '\n'; 1523 c[2] = '\n';
1524 1524
1525 if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */ 1525 if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */
1526 while (*s) { 1526 if (!*s)
1527 l = strcspn(s, c+2); 1527 return n; /* "": zero fields */
1528 n++; /* at least one field will be there */
1529 do {
1530 l = strcspn(s, c+2); /* len till next NUL or \n */
1528 if (regexec(icase ? spl->r.ire : spl->l.re, s, 1, pmatch, 0) == 0 1531 if (regexec(icase ? spl->r.ire : spl->l.re, s, 1, pmatch, 0) == 0
1529 && pmatch[0].rm_so <= l 1532 && pmatch[0].rm_so <= l
1530 ) { 1533 ) {
@@ -1533,24 +1536,27 @@ static int awk_split(const char *s, node *spl, char **slist)
1533 l++; 1536 l++;
1534 pmatch[0].rm_eo++; 1537 pmatch[0].rm_eo++;
1535 } 1538 }
1539 n++; /* we saw yet another delimiter */
1536 } else { 1540 } else {
1537 pmatch[0].rm_eo = l; 1541 pmatch[0].rm_eo = l;
1538 if (s[l]) pmatch[0].rm_eo++; 1542 if (s[l]) pmatch[0].rm_eo++;
1539 } 1543 }
1540
1541 memcpy(s1, s, l); 1544 memcpy(s1, s, l);
1542 s1[l] = '\0'; 1545 s1[l] = '\0';
1543 nextword(&s1); 1546 nextword(&s1);
1544 s += pmatch[0].rm_eo; 1547 s += pmatch[0].rm_eo;
1545 n++; 1548 } while (*s);
1546 } 1549 return n;
1547 } else if (c[0] == '\0') { /* null split */ 1550 }
1551 if (c[0] == '\0') { /* null split */
1548 while (*s) { 1552 while (*s) {
1549 *s1++ = *s++; 1553 *s1++ = *s++;
1550 *s1++ = '\0'; 1554 *s1++ = '\0';
1551 n++; 1555 n++;
1552 } 1556 }
1553 } else if (c[0] != ' ') { /* single-character split */ 1557 return n;
1558 }
1559 if (c[0] != ' ') { /* single-character split */
1554 if (icase) { 1560 if (icase) {
1555 c[0] = toupper(c[0]); 1561 c[0] = toupper(c[0]);
1556 c[1] = tolower(c[1]); 1562 c[1] = tolower(c[1]);
@@ -1560,21 +1566,23 @@ static int awk_split(const char *s, node *spl, char **slist)
1560 *s1++ = '\0'; 1566 *s1++ = '\0';
1561 n++; 1567 n++;
1562 } 1568 }
1563 } else { /* space split */ 1569 return n;
1564 while (*s) { 1570 }
1565 s = skip_whitespace(s); 1571 /* space split */
1566 if (!*s) break; 1572 while (*s) {
1567 n++; 1573 s = skip_whitespace(s);
1568 while (*s && !isspace(*s)) 1574 if (!*s) break;
1569 *s1++ = *s++; 1575 n++;
1570 *s1++ = '\0'; 1576 while (*s && !isspace(*s))
1571 } 1577 *s1++ = *s++;
1578 *s1++ = '\0';
1572 } 1579 }
1573 return n; 1580 return n;
1574} 1581}
1575 1582
1576static void split_f0(void) 1583static void split_f0(void)
1577{ 1584{
1585/* static char *fstrings; */
1578#define fstrings (G.split_f0__fstrings) 1586#define fstrings (G.split_f0__fstrings)
1579 1587
1580 int i, n; 1588 int i, n;