summaryrefslogtreecommitdiff
authorDenys Vlasenko <vda.linux@googlemail.com>2013-11-21 14:09:55 (GMT)
committer Denys Vlasenko <vda.linux@googlemail.com>2013-11-21 14:09:55 (GMT)
commitbd0e221620eb725043b4e748248046ece996a393 (patch)
tree54d12beebe19b7c50c6b14b15ad18c302fa0eff9
parent2635369a92db338321b2ba38e73539992967357c (diff)
downloadbusybox-bd0e221620eb725043b4e748248046ece996a393.zip
busybox-bd0e221620eb725043b4e748248046ece996a393.tar.gz
busybox-bd0e221620eb725043b4e748248046ece996a393.tar.bz2
awk: fix a bug in argc counting in recent change
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat
-rw-r--r--editors/awk.c16
-rwxr-xr-xtestsuite/awk.tests14
2 files changed, 21 insertions, 9 deletions
diff --git a/editors/awk.c b/editors/awk.c
index 29fb2e7..d0e3781 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -3204,15 +3204,17 @@ int awk_main(int argc, char **argv)
opt = getopt32(argv, OPTSTR_AWK, &opt_F, &list_v, &list_f, IF_FEATURE_AWK_GNU_EXTENSIONS(&list_e,) NULL);
argv += optind;
argc -= optind;
- if (opt & OPT_F) { /* -F */
+ if (opt & OPT_W)
+ bb_error_msg("warning: option -W is ignored");
+ if (opt & OPT_F) {
unescape_string_in_place(opt_F);
setvar_s(intvar[FS], opt_F);
}
- while (list_v) { /* -v */
+ while (list_v) {
if (!is_assignment(llist_pop(&list_v)))
bb_show_usage();
}
- while (list_f) { /* -f */
+ while (list_f) {
char *s = NULL;
FILE *from_file;
@@ -3230,7 +3232,7 @@ int awk_main(int argc, char **argv)
}
g_progname = "cmd. line";
#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
- while (list_e) { /* -e */
+ while (list_e) {
parse_program(llist_pop(&list_e));
}
#endif
@@ -3238,13 +3240,11 @@ int awk_main(int argc, char **argv)
if (!*argv)
bb_show_usage();
parse_program(*argv++);
- argc++;
+ argc--;
}
- if (opt & OPT_W) // -W
- bb_error_msg("warning: option -W is ignored");
/* fill in ARGV array */
- setvar_i(intvar[ARGC], argc);
+ setvar_i(intvar[ARGC], argc + 1);
setari_u(intvar[ARGV], 0, "awk");
i = 0;
while (*argv)
diff --git a/testsuite/awk.tests b/testsuite/awk.tests
index a023024..50b2a83 100755
--- a/testsuite/awk.tests
+++ b/testsuite/awk.tests
@@ -275,10 +275,22 @@ testing "awk large integer" \
"" ""
testing "awk length(array)" \
- "awk 'BEGIN{ A[1]=2; A["qwe"]="asd"; print length(A)}'" \
+ "awk 'BEGIN{ A[1]=2; A[\"qwe\"]=\"asd\"; print length(A)}'" \
"2\n" \
"" ""
+testing "awk -f and ARGC" \
+ "awk -f - input" \
+ "re\n2\n" \
+ "do re mi\n" \
+ '{print $2; print ARGC;}' \
+
+testing "awk -e and ARGC" \
+ "awk -e '{print \$2; print ARGC;}' input" \
+ "re\n2\n" \
+ "do re mi\n" \
+ "" \
+
# testing "description" "command" "result" "infile" "stdin"
exit $FAILCOUNT