* Re: [Cooker] strange problem with "hotplug" package
2003-05-06 13:45 [Cooker] strange problem with "hotplug" package Andrey Borzenkov
@ 2003-05-06 15:54 ` Stepan Kasal
2003-05-06 20:12 ` Jim Meyering
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Stepan Kasal @ 2003-05-06 15:54 UTC (permalink / raw)
To: linux-hotplug
Hello to all people reading this,
On Tue, May 06, 2003 at 05:45:40PM +0400, Andrey Borzenkov wrote:
> bor@cooker% grep -q bor /etc/passwd <&- >&-
> grep: write error: Bad file descriptor
> bor@cooker% echo $?
> 1
the exit code is always correct. The only problem is the bogus
error message.
So, we should not try to close(stdout) if -q was given.
Curiously enough, the problem doesn't appear with
grep -q bor /etc/passwd >&-
because the file /etc/passwd is opened with file descriptor 1,
and it is closed on exit instead of stdout.
(With <&- >&-, /etc/passwd is fd 0 and the bug bites.)
Cause: if a match was found, we exit(0) immediately, without closing
the open file. This optimization can be moved after the input file
is closed, without any loss.
The patch attached to the end of this mail should fix both problems.
(Apply to the 2.5.1 source tree.) It will appear in the next release.
Regards,
Stepan Kasal
Tue May 6 17:49:29 CEST 2003 Stepan Kasal <kasal@math.cas.cz>
* src/grep.c(main): Don't register atexit(close_stdout) if -q
was given---no output will be written; there is also no need
to use close_stdout_set_status().
* src/grep.c(grepbuf): move exit(0) ...
(grepfile): ... here, when the bufdesc is closed; this doesn't
present any performance loss, done_on_match is 1 and ensures
that we get out quickly.
--- grep-2.5.1.orig/src/grep.c Tue Mar 26 16:54:12 2002
+++ grep-2.5.1/src/grep.c Tue May 6 17:47:38 2003
@@ -722,8 +722,6 @@ grepbuf (char const *beg, char const *li
outleft--;
if (!outleft || done_on_match)
{
- if (exit_on_match)
- exit (0);
after_last_match = bufoffset - (buflim - endp);
return nlines;
}
@@ -978,6 +976,9 @@ grepfile (char const *file, struct stats
}
}
+ if (!status && exit_on_match)
+ exit (0);
+
return status;
}
@@ -1348,8 +1349,6 @@ main (int argc, char **argv)
textdomain (PACKAGE);
#endif
- atexit (close_stdout);
-
prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv);
while ((opt = get_nondigit_option (argc, argv, &default_context)) != -1)
@@ -1524,7 +1523,6 @@ main (int argc, char **argv)
case 'q':
exit_on_match = 1;
- close_stdout_set_status(0);
break;
case 'R':
@@ -1630,6 +1628,10 @@ main (int argc, char **argv)
break;
}
+
+ /* don't close stdout for -q, consider ``grep -q pat <&- >&-'' */
+ if (!exit_on_match)
+ atexit(close_stdout);
/* POSIX.2 says that -q overrides -l, which in turn overrides the
other output options. */
-------------------------------------------------------
Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara
The only event dedicated to issues related to Linux enterprise solutions
www.enterpriselinuxforum.com
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [Cooker] strange problem with "hotplug" package
2003-05-06 13:45 [Cooker] strange problem with "hotplug" package Andrey Borzenkov
2003-05-06 15:54 ` Stepan Kasal
@ 2003-05-06 20:12 ` Jim Meyering
2003-05-08 12:24 ` Stepan Kasal
2003-05-08 16:17 ` Jim Meyering
3 siblings, 0 replies; 5+ messages in thread
From: Jim Meyering @ 2003-05-06 20:12 UTC (permalink / raw)
To: linux-hotplug
Hi Stepan!
With your new version of grep, will `grep -q --help > /dev/full' fail?
I was surprised to see that with 2.5.1, it does give a diagnostic,
but doesn't actually fail:
$ grep -q --help > /dev/full && echo ok
grep: write error: No space left on device
ok
While without -q, it works as I'd expect:
$ grep --help > /dev/full && echo ok
grep: write error: No space left on device
[Exit 1]
Jim
P.S. Thanks for taking up the reins of GNU grep.
Stepan Kasal <kasal@math.cas.cz> wrote:
...
> + /* don't close stdout for -q, consider ``grep -q pat <&- >&-'' */
> + if (!exit_on_match)
> + atexit(close_stdout);
-------------------------------------------------------
Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara
The only event dedicated to issues related to Linux enterprise solutions
www.enterpriselinuxforum.com
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Cooker] strange problem with "hotplug" package
2003-05-06 13:45 [Cooker] strange problem with "hotplug" package Andrey Borzenkov
2003-05-06 15:54 ` Stepan Kasal
2003-05-06 20:12 ` Jim Meyering
@ 2003-05-08 12:24 ` Stepan Kasal
2003-05-08 16:17 ` Jim Meyering
3 siblings, 0 replies; 5+ messages in thread
From: Stepan Kasal @ 2003-05-08 12:24 UTC (permalink / raw)
To: linux-hotplug
[-- Attachment #1: Type: text/plain, Size: 745 bytes --]
Hello,
On Tue, May 06, 2003 at 10:12:20PM +0200, Jim Meyering wrote:
> $ grep -q --help > /dev/full && echo ok
> grep: write error: No space left on device
> ok
thank you very much for pointing this out; I haven't thought about this.
> While without -q, it works as I'd expect:
> $ grep --help > /dev/full && echo ok
> grep: write error: No space left on device
> [Exit 1]
No, I'd expect exit code 2 in both cases, not 1.
I have revised my previous patch; see the patch attached to this mail.
(It's relative to plain 2.5.1.) Feedback welcome, of course.
> P.S. Thanks for taking up the reins of GNU grep.
Well I have to admit that I haven't done much yet.
But I hope I'll get to it soon. ;-)
Have a nice day,
Stepan Kasal
[-- Attachment #2: grep-2.5.1-close_stdout2.patch --]
[-- Type: text/plain, Size: 3203 bytes --]
Thu May 8 14:08:57 CEST 2003 Stepan Kasal <kasal@math.cas.cz>
* lib/closeout.c (default_exit_status): set to 2.
* src/grep.c (main): Don't register atexit(close_stdout) if -q
was given---no output will be written; there is also no need
to use close_stdout_set_status(). Move the atexit() call after
all calls to usage(); usage() and --version call close_stdout()
explicitely before exit.
(grepbuf): move exit(0) ...
(grepfile): ... here, when the bufdesc is closed; this doesn't
present any performance loss, done_on_match is 1 and ensures
that we get out of grepbuf() quickly.
diff -urpN grep-2.5.1.orig/lib/closeout.c grep-2.5.1/lib/closeout.c
--- grep-2.5.1.orig/lib/closeout.c Sun Mar 4 06:33:12 2001
+++ grep-2.5.1/lib/closeout.c Thu May 8 13:50:14 2003
@@ -47,7 +47,8 @@ extern int errno;
#include "__fpending.h"
#endif
-static int default_exit_status = EXIT_FAILURE;
+static int default_exit_status = /* EXIT_FAILURE */
+ 2 /* error exit code for grep */;
static const char *file_name;
/* Set the value to be used for the exit status when close_stdout is called.
diff -urpN grep-2.5.1.orig/src/grep.c grep-2.5.1/src/grep.c
--- grep-2.5.1.orig/src/grep.c Tue Mar 26 16:54:12 2002
+++ grep-2.5.1/src/grep.c Thu May 8 14:05:27 2003
@@ -722,8 +722,6 @@ grepbuf (char const *beg, char const *li
outleft--;
if (!outleft || done_on_match)
{
- if (exit_on_match)
- exit (0);
after_last_match = bufoffset - (buflim - endp);
return nlines;
}
@@ -978,6 +976,9 @@ grepfile (char const *file, struct stats
}
}
+ if (!status && exit_on_match)
+ exit (0);
+
return status;
}
@@ -1125,6 +1126,7 @@ two FILEs given, assume -h. Exit status
and 2 if trouble.\n"));
printf (_("\nReport bugs to <bug-gnu-utils@gnu.org>.\n"));
}
+ close_stdout ();
exit (status);
}
@@ -1348,8 +1350,6 @@ main (int argc, char **argv)
textdomain (PACKAGE);
#endif
- atexit (close_stdout);
-
prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv);
while ((opt = get_nondigit_option (argc, argv, &default_context)) != -1)
@@ -1524,7 +1524,6 @@ main (int argc, char **argv)
case 'q':
exit_on_match = 1;
- close_stdout_set_status(0);
break;
case 'R':
@@ -1579,7 +1578,7 @@ main (int argc, char **argv)
!strcasecmp(optarg, "if-tty"))
color_option = 2;
else
- show_help = 1;
+ error (2, 0, _("unrecognized colour option"));
} else
color_option = 2;
if(color_option == 2) {
@@ -1667,6 +1666,7 @@ Copyright 1988, 1992-1999, 2000, 2001 Fr
This is free software; see the source for copying conditions. There is NO\n\
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"));
printf ("\n");
+ close_stdout ();
exit (0);
}
@@ -1693,6 +1693,10 @@ warranty; not even for MERCHANTABILITY o
}
else
usage (2);
+
+ /* don't close stdout for -q, consider ``grep -q pat <&- >&-'' */
+ if (!exit_on_match)
+ atexit (close_stdout);
if (!install_matcher (matcher) && !install_matcher ("default"))
abort ();
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [Cooker] strange problem with "hotplug" package
2003-05-06 13:45 [Cooker] strange problem with "hotplug" package Andrey Borzenkov
` (2 preceding siblings ...)
2003-05-08 12:24 ` Stepan Kasal
@ 2003-05-08 16:17 ` Jim Meyering
3 siblings, 0 replies; 5+ messages in thread
From: Jim Meyering @ 2003-05-08 16:17 UTC (permalink / raw)
To: linux-hotplug
Thanks!
Stepan Kasal <kasal@math.cas.cz> wrote:
> @@ -1579,7 +1578,7 @@ main (int argc, char **argv)
> !strcasecmp(optarg, "if-tty"))
> color_option = 2;
> else
> - show_help = 1;
> + error (2, 0, _("unrecognized colour option"));
A minor nit: it is customary to include the offending string
in such a diagnostic. It's also nice to allow the user to specify
a prefix of the option argument: grep --color=al
To that end, you might want to use XARGMATCH as in e.g., coreutils' ls.c.
It does this for --time={atime, access, use, ctime, status}
case TIME_OPTION:
time_type = XARGMATCH ("--time", optarg, time_args, time_types);
break;
With that and the corresponding declarations,
ls gives diagnostics like this:
$ ls --time=z
ls: invalid argument `z' for `--time'
Valid arguments are:
- `atime', `access', `use'
- `ctime', `status'
Try `ls --help' for more information.
You'd need lib/argmatch.[ch].
-------------------------------------------------------
Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara
The only event dedicated to issues related to Linux enterprise solutions
www.enterpriselinuxforum.com
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
^ permalink raw reply [flat|nested] 5+ messages in thread