From debbugs-submit-bounces@debbugs.gnu.org Mon May 06 09:49:25 2024 Received: (at submit) by debbugs.gnu.org; 6 May 2024 13:49:25 +0000 Received: from localhost ([127.0.0.1]:38201 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s3yiX-0004hS-Hh for submit@debbugs.gnu.org; Mon, 06 May 2024 09:49:25 -0400 Received: from lists.gnu.org ([2001:470:142::17]:58694) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s3swg-0005xq-KF for submit@debbugs.gnu.org; Mon, 06 May 2024 03:39:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s3swC-0000Gu-KP for bug-coreutils@gnu.org; Mon, 06 May 2024 03:39:04 -0400 Received: from sender-op-o11.zoho.eu ([136.143.169.11]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s3sw8-0003lk-SJ for bug-coreutils@gnu.org; Mon, 06 May 2024 03:39:04 -0400 ARC-Seal: i=1; a=rsa-sha256; t=1714981135; cv=none; d=zohomail.eu; s=zohoarc; b=h2+sbr6t/QYf0KM7gKf50XEHkxgTa2ua3UmqSmeue9dFPwG7CPyVH00MED7cYnoW/8zfWuuwgkxB1SIBLbLZcGm1LUr43TF5oFEcCWWtfgqqoT/k4L4JmUtsNPCXUj9kMNvjy7QThl9rb60XvqhgnrWvKVzFrDTR7BpoDlwudTk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.eu; s=zohoarc; t=1714981135; h=Content-Type:Date:Date:From:From:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=3bCt8SPgvwAiPbp+xGe+oWl/+2LpK5yUUjD8I7ZKZrI=; b=eCye+54ElHq8Cls/BZvkyQo/74yr3YusyjpvOht8eVPFfNlbdfYRbRV3nLhGKUzmJgOTyJspLLBVMWFG5oMS27GEihUkVgg5Zz73AmzE3ff0Zrnvx1OpKRn6wA4d58W7onA7gACx++52N0hHsoXBJ6SwmflkF91x75qIAtoH8WY= ARC-Authentication-Results: i=1; mx.zohomail.eu; dkim=pass header.i=burette.net; spf=pass smtp.mailfrom=bernard@burette.net; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1714981135; s=zoho; d=burette.net; i=bernard@burette.net; h=Date:Date:From:From:To:To:Message-Id:Message-Id:In-Reply-To:Subject:Subject:MIME-Version:Content-Type:Reply-To:Cc; bh=3bCt8SPgvwAiPbp+xGe+oWl/+2LpK5yUUjD8I7ZKZrI=; b=RA7dEKu195nDul8lm9BlDBx/WIvLtYhHlx1khnORX625ngiYaR7CYWV+5s7Zp4cd Y+uadGJ2jHwANsE5g6k9jDyZQYcES7tGE5Zf0W6sG2/nmDx13iC5LyUCU8SGkJjFCgz IwqGtUMetY6uVG2fpey85DQZkFTi5GsW4VS+BRzs= Received: from mail.zoho.eu by mx.zoho.eu with SMTP id 1714981133753115.53960311822095; Mon, 6 May 2024 09:38:53 +0200 (CEST) Date: Mon, 06 May 2024 09:38:53 +0200 From: Bernard Burette To: "bug-coreutils" Message-Id: <18f4cd75d6b.c507ab56319121.2269758220018460647@burette.net> In-Reply-To: Subject: Weird behaviour when standard input is closed MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_927427_1600188443.1714981133675" Importance: Medium User-Agent: Zoho Mail X-Mailer: Zoho Mail Received-SPF: pass client-ip=136.143.169.11; envelope-from=bernard@burette.net; helo=sender-op-o11.zoho.eu X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: 1.0 (+) X-Debbugs-Envelope-To: submit X-Mailman-Approved-At: Mon, 06 May 2024 09:49:20 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.0 (/) ------=_Part_927427_1600188443.1714981133675 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi, If I try: $ cat <&- cat: -: Bad file descriptor cat: closing standard input: Bad file descriptor $ The error on stdin beign closed is displayed twice plus "-" is for a FILE a= rgument to replace standard input, It would make more sense to me to have s= ometing like: $ cat <&- cat: standard input: Bad file descriptor $ This is the "git diff" of my proposed change: diff --git a/src/cat.c b/src/cat.c index b33faeb35..4be189d85 100644 --- a/src/cat.c +++ b/src/cat.c @@ -50,6 +50,14 @@ /* Name of input file.=C2=A0 May be "-".=C2=A0 */ static char const *infile; +/* Pretty name of input file */ +static char const * +quotef_infile (void) +{ +=C2=A0 if (STREQ (infile, "-")) return _("standard input"); +=C2=A0 return quotef (infile); +} + /* Descriptor on which input file is open.=C2=A0 */ static int input_desc; @@ -164,7 +172,7 @@ simple_cat (char *buf, idx_t bufsize) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 size_t n_read =3D safe_read (input_des= c, buf, bufsize); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (n_read =3D=3D SAFE_READ_ERROR) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 { -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 error (0, errno, "%= s", quotef (infile)); +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 error (0, errno, "%= s", quotef_infile()); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return false; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } @@ -313,7 +321,7 @@ cat (char *inbuf, idx_t insize, char *outbuf, idx_t out= size, =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 size_t n_read =3D safe_read (input_desc, inbuf, insize); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 if (n_read =3D=3D SAFE_READ_ERROR) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 { -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 error (0, errno, "%s", quotef (infile)); +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 error (0, errno, "%s", quotef_infile()); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 write_pending (outbuf, &bpout); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 newlines2 =3D newlines; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return false; @@ -526,7 +534,7 @@ copy_cat (void) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ||= errno =3D=3D EBADF || errno =3D=3D EXDEV || errno =3D=3D ETXTBSY =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ||= errno =3D=3D EPERM) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return 0; -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 error (0, errno, "%s", quotef (= infile)); +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 error (0, errno, "%s", quotef_i= nfile()); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return -1; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } } @@ -684,7 +692,7 @@ main (int argc, char **argv) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 input_desc =3D= open (infile, file_open_mode); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (input_desc= < 0) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 { -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 error (0, errno, "%s", quotef (infile)); +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 error (0, errno, "%s", quotef_infile()); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 ok =3D false; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 continue; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } @@ -692,7 +700,7 @@ main (int argc, char **argv) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (fstat (input_desc, &stat_buf) < 0) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 { -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 error (0, errno, "%= s", quotef (infile)); +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 error (0, errno, "%= s", quotef_infile()); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ok =3D false; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 goto contin; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } @@ -719,7 +727,7 @@ main (int argc, char **argv) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (exhausting= ) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 { -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 error (0, 0, _("%s: input file is output file"), quotef (infile)); +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 error (0, 0, _("%s: input file is output file"), quotef_infile()); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 ok =3D false; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 goto contin; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } @@ -794,7 +802,7 @@ main (int argc, char **argv) =C2=A0=C2=A0=C2=A0=C2=A0 contin: =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (!reading_stdin && close (input_des= c) < 0) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 { -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 error (0, errno, "%= s", quotef (infile)); +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 error (0, errno, "%= s", quotef_infile()); =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ok =3D false; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } =C2=A0=C2=A0=C2=A0=C2=A0 } @@ -807,7 +815,8 @@ main (int argc, char **argv) =C2=A0=C2=A0=C2=A0=C2=A0 } =C2=A0=C2=A0 if (have_read_stdin && close (STDIN_FILENO) < 0) -=C2=A0=C2=A0=C2=A0 error (EXIT_FAILURE, errno, _("closing standard input")= ); +=C2=A0=C2=A0=C2=A0 if (ok) +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 error (EXIT_FAILURE, errno, _("closing stan= dard input")); =C2=A0=C2=A0 return ok ? EXIT_SUCCESS : EXIT_FAILURE; } There are a lot more of coreutil utilities that (could) behave weird on a c= losed standard input, here is my first list: b2sum base32 base64 cat cksum comm csplit cut expand factor fmt fold head j= oin md5sum nl nohup numfmt od paste pr ptx sha1sum sha224sum sha256sum sha3= 84sum sha512sum shred shuf sort split stdbuf stty sum tac tail tee tr tsort= unexpand uniq wc I would like to work on those as well but it would help to have: 1) some kind of consensus on "this is a better way of displaying the error"= , for example other tools also go wild like: =C2=A0=C2=A0=C2=A0=C2=A0 grep: (standard input): Bad file descriptor =C2=A0=C2=A0=C2=A0=C2=A0 - why parentheses? =C2=A0=C2=A0=C2=A0=C2=A0 sed: read error on stdin: Bad file descriptor =C2=A0=C2=A0=C2=A0=C2=A0 - stdin is the C name for standard input, document= ed? 2) a better way of offering changes than sending a diff patch in a e-mail. Thanks, Regards, ------=_Part_927427_1600188443.1714981133675 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable =
Hi,

If I try:
$ cat <&-
cat: -: Bad file descriptor
=
cat: closing standard input: Bad file descriptor
$

The error on stdin beign closed is displayed twice = plus "-" is for a FILE argument to replace standard input, It would make mo= re sense to me to have someting like:
$ cat <&-
cat: standard input: Bad file descriptor
$
<= div>
This is the "git diff" of my proposed change:
<= div>diff --git a/src/cat.c b/src/cat.c
index b33faeb35..4be18= 9d85 100644
--- a/src/cat.c
+++ b/src/cat.c
=
@@ -50,6 +50,14 @@
/* Name of input file.  Ma= y be "-".  */
static char const *infile;
<= br>
+/* Pretty name of input file */
+static char c= onst *
+quotef_infile (void)
+{
+=   if (STREQ (infile, "-")) return _("standard input");
+=   return quotef (infile);
+}
+
/* Descriptor on which input file is open.  */
static= int input_desc;

@@ -164,7 +172,7 @@ simple_ca= t (char *buf, idx_t bufsize)
     &n= bsp; size_t n_read =3D safe_read (input_desc, buf, bufsize);
=        if (n_read =3D=3D SAFE_READ_ERROR)
=
         {
-          error (0, errno, "= %s", quotef (infile));
+      &= nbsp;   error (0, errno, "%s", quotef_infile());
&n= bsp;          return false;
         }

@@ -313,7 +321,7 @@ cat (char *inbuf, idx_t insize, char *= outbuf, idx_t outsize,
      &n= bsp;        size_t n_read =3D safe_read = (input_desc, inbuf, insize);
     &n= bsp;         if (n_read =3D=3D SAFE= _READ_ERROR)
        =          {
- &nb= sp;            =     error (0, errno, "%s", quotef (infile));
+=             &nb= sp;     error (0, errno, "%s", quotef_infile());
           &= nbsp;       write_pending (outbuf, &bpout= );
         &nbs= p;         newlines2 =3D newlines;<= br>
          &= nbsp;        return false;
@@ -526,7 +534,7 @@ copy_cat (void)
    =          || errno =3D=3D EBADF || e= rrno =3D=3D EXDEV || errno =3D=3D ETXTBSY
   &= nbsp;         || errno =3D=3D EPERM= )
          = ; return 0;
-        error= (0, errno, "%s", quotef (infile));
+    =     error (0, errno, "%s", quotef_infile());
&= nbsp;        return -1;
&n= bsp;      }
}
@@ -684,= 7 +692,7 @@ main (int argc, char **argv)
   &n= bsp;       input_desc =3D open (infile, file_= open_mode);
        &= nbsp;  if (input_desc < 0)
    &n= bsp;        {
-  = ;            error (= 0, errno, "%s", quotef (infile));
+    &n= bsp;         error (0, errno, "%s",= quotef_infile());
       =         ok =3D false;
&nbs= p;            &= nbsp; continue;
       &nb= sp;     }
@@ -692,7 +700,7 @@ main (int a= rgc, char **argv)

    &nbs= p;  if (fstat (input_desc, &stat_buf) < 0)
 =         {
-  &nb= sp;       error (0, errno, "%s", quotef (infi= le));
+         = error (0, errno, "%s", quotef_infile());
   &= nbsp;       ok =3D false;
 = ;          goto contin;
         }
@@= -719,7 +727,7 @@ main (int argc, char **argv)
  &n= bsp;          }
=            if (exhausting= )
          = ;   {
-       &n= bsp;      error (0, 0, _("%s: input file is output= file"), quotef (infile));
+     &nb= sp;        error (0, 0, _("%s: input fil= e is output file"), quotef_infile());
    = ;           ok =3D false;=
          =      goto contin;
    = ;         }
@@ -794,7= +802,7 @@ main (int argc, char **argv)
   &nb= sp; contin:
       if (!reading= _stdin && close (input_desc) < 0)
  &nbs= p;      {
-    &= nbsp;     error (0, errno, "%s", quotef (infile));
<= /div>
+          error (0,= errno, "%s", quotef_infile());
     = ;      ok =3D false;
  &nb= sp;      }
     = }
@@ -807,7 +815,8 @@ main (int argc, char **argv)
<= div>     }

   if= (have_read_stdin && close (STDIN_FILENO) < 0)
-&n= bsp;   error (EXIT_FAILURE, errno, _("closing standard input"));<= br>
+    if (ok)
+   =    error (EXIT_FAILURE, errno, _("closing standard input"));
<= /div>

   return ok ? EXIT_SUCCESS : EXIT_FAILU= RE;
}

There are a lot more of co= reutil utilities that (could) behave weird on a closed standard input, here= is my first list:
b2sum base32 base64 cat cksum comm csplit cut expand factor fmt fold head = join md5sum nl nohup numfmt od paste pr ptx sha1sum sha224sum sha256sum sha= 384sum sha512sum shred shuf sort split stdbuf stty sum tac tail tee tr tsor= t unexpand uniq wc

I would like to= work on those as well but it would help to have:
1) some kin= d of consensus on "this is a better way of displaying the error", for examp= le other tools also go wild like:
     gr= ep: (standard input): Bad file descriptor
   &= nbsp; - why parentheses?
     sed: read e= rror on stdin: Bad file descriptor
     -= stdin is the C name for standard input, documented?
2) a bet= ter way of offering changes than sending a diff patch in a e-mail.

Thanks,
Regards,

------=_Part_927427_1600188443.1714981133675-- From debbugs-submit-bounces@debbugs.gnu.org Mon May 06 11:02:35 2024 Received: (at 70801) by debbugs.gnu.org; 6 May 2024 15:02:35 +0000 Received: from localhost ([127.0.0.1]:38521 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s3zrP-0005jh-9K for submit@debbugs.gnu.org; Mon, 06 May 2024 11:02:35 -0400 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]:49536) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s3zrL-0005jb-3X for 70801@debbugs.gnu.org; Mon, 06 May 2024 11:02:33 -0400 Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-41b79450f8cso12398565e9.3 for <70801@debbugs.gnu.org>; Mon, 06 May 2024 08:02:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715007720; x=1715612520; darn=debbugs.gnu.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :sender:from:to:cc:subject:date:message-id:reply-to; bh=O53K1phydFrBmOHiLYyzy56v0+ICQ3hkd2KzJr7LozY=; b=juFez87tytJyfUmQVYVN8nGEiH0dFoCw5wnApr4Vsc3QgUfHzNsI+vji2qptfFCOxw Uytw+dnbPjYpQ1tMESFU9hNI65vPuW7d5JRhwTAT2U2+tI13iW2kZq8N+nmUvfIsjk6B h5W54hUOTqoYkJHWHgatl/W97Sws4rV6KOFpHerskOAGmSfCS55FaulHEV551YRNq8zn 0EGI0620G+z/uCWIm+BuZHG8vtjyKEvA2PePkvLpsZ4Zk89nzcnaRANXjKEoisyNq4vt OSCDjvK1JoQY9hKfMaj8am7n3ZgXKJJ83FaUz4gWrlQ8lkE5LKTQ5gLwl/mICk9Hb6np oNCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715007720; x=1715612520; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :sender:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=O53K1phydFrBmOHiLYyzy56v0+ICQ3hkd2KzJr7LozY=; b=nsAdgtdtsSluuXIMu/Pf0kLwixkBpC6hgBaPpoykIQDuH3E+RCuaX++0k4LSoF8Kjo 9gXtVnzHX6bvTidnNpOMemvlRdwDH5rNL6wPH29J4fdNUr6l3HeIWitYIHSeBi7TQV4F KYjfVshPvwrvAc5E21HNgVxGrgssS7Xrcs4gwhab2j0MMM482qrhvb8VcMZosTRYuZ6g hqSzQuiA4JjTvMj4Zs0+T19A7QU/oSrGGqseZs1k7N1BR4aAqGBKoMP5i5BI1IGNIf/M pKtd5CQE4Twpap96pBVBXcS855ccKk2t/CzftmAzrR8coG4TXfxYbMzurm7rDBo2rBcc i8zg== X-Forwarded-Encrypted: i=1; AJvYcCVi6Xa20EtzmtSOCPFwNK0GLFNBNY4/nL+I6ya4FLGB9xq90fjWbt1LKCALeZUTawKFvye8qdllCvMBTdASbPGUtRKQo/g= X-Gm-Message-State: AOJu0YzIbNB09apU/ZzRzESlooEP9OaKZ5RXH7o7KnaXsh11HdzkOL7s DIgEyKFjS7NX8Yv6Pe6axvWeANWwXYMrNoiDZE4wHKVM7u3O/lODRR05Hw== X-Google-Smtp-Source: AGHT+IHIADjzYppA8cMP5PksLXVuBs6z/Qg+2WfB9jMX7/qL5Ia3AQUei+Qot8IqNfLiZib4khm7jg== X-Received: by 2002:a05:600c:4fcd:b0:41f:202e:36f0 with SMTP id o13-20020a05600c4fcd00b0041f202e36f0mr457802wmq.12.1715007720170; Mon, 06 May 2024 08:02:00 -0700 (PDT) Received: from [192.168.1.59] (86-44-211-146-dynamic.agg2.lod.rsl-rtd.eircom.net. [86.44.211.146]) by smtp.googlemail.com with ESMTPSA id m9-20020a05600c4f4900b0041bf7da4200sm16467983wmq.33.2024.05.06.08.01.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 06 May 2024 08:01:59 -0700 (PDT) Message-ID: Date: Mon, 6 May 2024 16:01:57 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Beta Subject: Re: bug#70801: Weird behaviour when standard input is closed To: Bernard Burette , 70801@debbugs.gnu.org References: <18f4cd75d6b.c507ab56319121.2269758220018460647@burette.net> Content-Language: en-US From: =?UTF-8?Q?P=C3=A1draig_Brady?= In-Reply-To: <18f4cd75d6b.c507ab56319121.2269758220018460647@burette.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Score: 0.3 (/) X-Debbugs-Envelope-To: 70801 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.7 (/) On 06/05/2024 08:38, Bernard Burette wrote: > Hi, > > > > If I try: > > $ cat <&- > > cat: -: Bad file descriptor > > cat: closing standard input: Bad file descriptor > > $ > > > > The error on stdin beign closed is displayed twice plus "-" is for a FILE argument to replace standard input, It would make more sense to me to have someting like: > > $ cat <&- > > cat: standard input: Bad file descriptor > > $ > > > > This is the "git diff" of my proposed change: > > diff --git a/src/cat.c b/src/cat.c > > index b33faeb35..4be189d85 100644 > > --- a/src/cat.c > > +++ b/src/cat.c > > @@ -50,6 +50,14 @@ > > /* Name of input file.  May be "-".  */ > > static char const *infile; > > > > +/* Pretty name of input file */ > > +static char const * > > +quotef_infile (void) > > +{ > > +  if (STREQ (infile, "-")) return _("standard input"); > > +  return quotef (infile); > > +} > > + > > /* Descriptor on which input file is open.  */ > > static int input_desc; > > > > @@ -164,7 +172,7 @@ simple_cat (char *buf, idx_t bufsize) > >        size_t n_read = safe_read (input_desc, buf, bufsize); > >        if (n_read == SAFE_READ_ERROR) > >          { > > -          error (0, errno, "%s", quotef (infile)); > > +          error (0, errno, "%s", quotef_infile()); > >            return false; > >          } > > > > @@ -313,7 +321,7 @@ cat (char *inbuf, idx_t insize, char *outbuf, idx_t outsize, > >                size_t n_read = safe_read (input_desc, inbuf, insize); > >                if (n_read == SAFE_READ_ERROR) > >                  { > > -                  error (0, errno, "%s", quotef (infile)); > > +                  error (0, errno, "%s", quotef_infile()); > >                    write_pending (outbuf, &bpout); > >                    newlines2 = newlines; > >                    return false; > > @@ -526,7 +534,7 @@ copy_cat (void) > >              || errno == EBADF || errno == EXDEV || errno == ETXTBSY > >              || errno == EPERM) > >            return 0; > > -        error (0, errno, "%s", quotef (infile)); > > +        error (0, errno, "%s", quotef_infile()); > >          return -1; > >        } > > } > > @@ -684,7 +692,7 @@ main (int argc, char **argv) > >            input_desc = open (infile, file_open_mode); > >            if (input_desc < 0) > >              { > > -              error (0, errno, "%s", quotef (infile)); > > +              error (0, errno, "%s", quotef_infile()); > >                ok = false; > >                continue; > >              } > > @@ -692,7 +700,7 @@ main (int argc, char **argv) > > > >        if (fstat (input_desc, &stat_buf) < 0) > >          { > > -          error (0, errno, "%s", quotef (infile)); > > +          error (0, errno, "%s", quotef_infile()); > >            ok = false; > >            goto contin; > >          } > > @@ -719,7 +727,7 @@ main (int argc, char **argv) > >              } > >            if (exhausting) > >              { > > -              error (0, 0, _("%s: input file is output file"), quotef (infile)); > > +              error (0, 0, _("%s: input file is output file"), quotef_infile()); > >                ok = false; > >                goto contin; > >              } > > @@ -794,7 +802,7 @@ main (int argc, char **argv) > >      contin: > >        if (!reading_stdin && close (input_desc) < 0) > >          { > > -          error (0, errno, "%s", quotef (infile)); > > +          error (0, errno, "%s", quotef_infile()); > >            ok = false; > >          } > >      } > > @@ -807,7 +815,8 @@ main (int argc, char **argv) > >      } > > > >    if (have_read_stdin && close (STDIN_FILENO) < 0) > > -    error (EXIT_FAILURE, errno, _("closing standard input")); > > +    if (ok) > > +      error (EXIT_FAILURE, errno, _("closing standard input")); > > > >    return ok ? EXIT_SUCCESS : EXIT_FAILURE; > > } > > > > There are a lot more of coreutil utilities that (could) behave weird on a closed standard input, here is my first list: > > b2sum base32 base64 cat cksum comm csplit cut expand factor fmt fold head join md5sum nl nohup numfmt od paste pr ptx sha1sum sha224sum sha256sum sha384sum sha512sum shred shuf sort split stdbuf stty sum tac tail tee tr tsort unexpand uniq wc > > > > > > I would like to work on those as well but it would help to have: > > 1) some kind of consensus on "this is a better way of displaying the error", for example other tools also go wild like: > >      grep: (standard input): Bad file descriptor > >      - why parentheses? > >      sed: read error on stdin: Bad file descriptor > >      - stdin is the C name for standard input, documented? > > 2) a better way of offering changes than sending a diff patch in a e-mail. Yes it would be good to fix up at least the duplicated error. We did the same on the write side of things with: https://github.com/coreutils/coreutils/commit/0b2ff7637 Also we generally checked that readers diagnosed errors in: https://github.com/coreutils/coreutils/blob/master/tests/misc/read-errors.sh So something similar could probably be done here. cheers, Pádraig