Re: bug#54785: for floating point, printf should use double like in C instead of long double

2022-04-09 Thread Paul Eggert

Vincent Lefevre wrote in :


$ zsh -fc '/usr/bin/printf "%a\n" $((43./2**22))'
0xa.c00025cp-20

instead of

0xa.cp-20


To summarize, this test case is:

printf '%a\n' 1.0251998901367188e-05

and the problem is that converting 1.0251998901367188e-05 to long double 
prints the too-precise "0xa.c00025cp-20", whereas you want it to 
convert to double (which matches what most other programs do) and to 
print "0xa.cp-20" or equivalent.



(Note that ksh uses long double internally, but does not ensure the
round trip back to long double


Yes, ksh messes up here. However, it's more important for Coreutils 
printf to be compatible with the GNU shell, and Bash uses long double:


$ echo $BASH_VERSION
5.1.8(1)-release
$ /usr/bin/printf --version | head -n1
printf (GNU coreutils) 8.32
$ printf '%a\n' 1.0251998901367188e-05
0xa.c00025cp-20
$ /usr/bin/printf '%a\n' 1.0251998901367188e-05
0xa.c00025cp-20



I suggest to parse the argument as a "long double" only if the "L"
length modifier is provided, like in C.

Thanks, good idea.

I checked, and this also appears to be a POSIX conformance issue. POSIX 
 says that floating point operands "shall be evaluated as if by the 
strtod() function". This means double, not long double.


Whatever decision we make here, we should be consistent with Bash so 
I'll cc this email to bug-bash.


I propose that we change both coreutils and Bash to use 'double' rather 
than 'long double' here, unless the user specifies the L modifier (e.g., 
"printf '%La\n' ...". I've written up a patch (attached) to Bash 5.2 
alpha to do that. Assuming the Bash maintainer likes this proposal, I 
plan to implement something similar for Coreutils printf.diff '-x*~' -pru bash-5.2-alpha/builtins/printf.def bash-5.2-alpha-double/builtins/printf.def
--- bash-5.2-alpha/builtins/printf.def	2021-12-29 13:09:20.0 -0800
+++ bash-5.2-alpha-double/builtins/printf.def	2022-04-09 12:02:35.330476097 -0700
@@ -215,13 +215,14 @@ static uintmax_t getuintmax PARAMS((void
 
 #if defined (HAVE_LONG_DOUBLE) && HAVE_DECL_STRTOLD && !defined(STRTOLD_BROKEN)
 typedef long double floatmax_t;
-#  define FLOATMAX_CONV	"L"
+#  define USE_LONG_DOUBLE 1
 #  define strtofltmax	strtold
 #else
 typedef double floatmax_t;
-#  define FLOATMAX_CONV	""
+#  define USE_LONG_DOUBLE 0
 #  define strtofltmax	strtod
 #endif
+static double getdouble PARAMS((void));
 static floatmax_t getfloatmax PARAMS((void));
 
 static intmax_t asciicode PARAMS((void));
@@ -247,7 +248,7 @@ printf_builtin (list)
  WORD_LIST *list;
 {
   int ch, fieldwidth, precision;
-  int have_fieldwidth, have_precision;
+  int have_fieldwidth, have_precision, use_Lmod;
   char convch, thisch, nextch, *format, *modstart, *precstart, *fmt, *start;
 #if defined (HANDLE_MULTIBYTE)
   char mbch[25];		/* 25 > MB_LEN_MAX, plus can handle 4-byte UTF-8 and large Unicode characters*/
@@ -422,8 +423,12 @@ printf_builtin (list)
 
 	  /* skip possible format modifiers */
 	  modstart = fmt;
+	  use_Lmod = 0;
 	  while (*fmt && strchr (LENMODS, *fmt))
-	fmt++;
+	{
+	  use_Lmod |= USE_LONG_DOUBLE && *fmt == 'L';
+	  fmt++;
+	}
 	
 	  if (*fmt == 0)
 	{
@@ -694,11 +699,24 @@ printf_builtin (list)
 #endif
 	  {
 		char *f;
-		floatmax_t p;
 
-		p = getfloatmax ();
-		f = mklong (start, FLOATMAX_CONV, sizeof(FLOATMAX_CONV) - 1);
-		PF (f, p);
+		if (use_Lmod)
+		  {
+		floatmax_t p;
+
+		p = getfloatmax ();
+		f = mklong (start, "L", 1);
+		PF (f, p);
+		  }
+		else
+		  {
+		double p;
+
+		p = getdouble ();
+		f = mklong (start, "", 0);
+		PF (f, p);
+		  }
+
 		break;
 	  }
 
@@ -1248,35 +1266,40 @@ getuintmax ()
   return (ret);
 }
 
+#define getfloat(ret, convert) \
+  char *ep; \
+  if (garglist == 0) \
+return 0; \
+  if (garglist->word->word[0] == '\'' || garglist->word->word[0] == '"') \
+return asciicode (); \
+  errno = 0; \
+  (ret) = (convert) (garglist->word->word, &ep); \
+  if (*ep) \
+{ \
+  sh_invalidnum (garglist->word->word); \
+  /* Same thing about POSIX.2 conversion error requirements. */ \
+  if (0) \
+(ret) = 0; \
+  conversion_error = 1; \
+} \
+  else if (errno == ERANGE) \
+printf_erange (garglist->word->word); \
+  garglist = garglist->next
+
+static double
+getdouble ()
+{
+  double ret;
+  getfloat (ret, strtod);
+  return ret;
+}
+
 static floatmax_t
 getfloatmax ()
 {
   floatmax_t ret;
-  char *ep;
-
-  if (garglist == 0)
-return (0);
-
-  if (garglist->word->word[0] == '\'' || garglist->word->word[0] == '"')
-return asciicode ();
-
-  errno = 0;
-  ret = strtofltmax (garglist->word->word, &ep);
-
-  if (*ep)
-{
-  sh_invalidnum (garglist->word->word);
-#if 0
-  /* Same thing about POSIX.2 conversion error requirements. */
-  ret = 0;
-#endif
-  conversion_error = 1;
-}
-  else if (errno == ERANGE)
-printf_erange (garglist->word->word);
-
-  ga

Login to a remote machine.

2022-04-09 Thread Ayoub Misherghi
   Hi,
 Below I show two ways I ssh logged into a machine.
In the first method I referred to the remote machine
as 192.168.0.212 while in the second method I referred to the
remote machine as [1]testuser5@192.168.0.212 specifying the user.
In the first method it logged me in as user ayoub when the
.ssh directory of ayoub does not have the file for the key
specified with the -i parameter option. With the second method
   it logged me in as the testuser5 user; as expected.
   Some information about the bash version and the Linux version
   is right at the bottom.
   Thanks,
 Ayoub
   ===
   ==
   login one:
   ayoub@VBox:~$ ssh -i id_ed25519_testuser5 192.168.0.212
   Welcome to Ubuntu 21.10 (GNU/Linux 5.13.0-39-generic x86_64)
* Documentation:  [2]https://help.ubuntu.com
* Management: [3]https://landscape.canonical.com
* Support:[4]https://ubuntu.com/advantage
 System information as of Sat Apr  9 11:08:36 PM UTC 2022
 System load:  0.08   Temperature:  67.0 C
 Usage of /:   1.1% of 686.60GB   Processes:139
 Memory usage: 3% Users logged in:  1
 Swap usage:   0% IPv4 address for enp14s0:
   192.168.0.212
* Super-optimized for small spaces - read how we shrank the memory
  footprint of MicroK8s to make it the smallest full K8s around.
  [5]https://ubuntu.com/blog/microk8s-memory-optimisation
   0 updates can be applied immediately.
   Last login: Sat Apr  9 22:05:23 2022 from 192.168.0.177
   ayoub@sony-srvr:~$ exit
   logout
   Connection to 192.168.0.212 closed.
   ===
   ==
   login two:
   ayoub@VBox:~$ ssh -i id_ed25519_testuser5 [6]testuser5@192.168.0.212
   Welcome to Ubuntu 21.10 (GNU/Linux 5.13.0-39-generic x86_64)
* Documentation:  [7]https://help.ubuntu.com
* Management: [8]https://landscape.canonical.com
* Support:[9]https://ubuntu.com/advantage
 System information as of Sat Apr  9 11:10:19 PM UTC 2022
 System load:  0.14   Temperature:  67.0 C
 Usage of /:   1.1% of 686.60GB   Processes:144
 Memory usage: 3% Users logged in:  1
 Swap usage:   0% IPv4 address for enp14s0:
   192.168.0.212
* Super-optimized for small spaces - read how we shrank the memory
  footprint of MicroK8s to make it the smallest full K8s around.
  [10]https://ubuntu.com/blog/microk8s-memory-optimisation
   0 updates can be applied immediately.
   The programs included with the Ubuntu system are free software;
   the exact distribution terms for each program are described in the
   individual files in /usr/share/doc/*/copyright.
   Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
   applicable law.
   testuser5@sony-srvr:~$ exit
   logout
   Connection to 192.168.0.212 closed.
   ayoub@VBox:~$
   ===
   ==
   ayoub@VBox:~$ uname -a
   Linux VBox 5.13.0-39-generic #44-Ubuntu SMP Thu Mar 24 15:35:05 UTC
   2022 x86_64 x86_64 x86_64 GNU/Linux
   ===
   ==
   ayoub@VBox:~$ bash --version
   GNU bash, version 5.1.8(1)-release (x86_64-pc-linux-gnu)
   Copyright (C) 2020 Free Software Foundation, Inc.
   License GPLv3+: GNU GPL version 3 or later
   [11]
   This is free software; you are free to change and redistribute it.
   There is NO WARRANTY, to the extent permitted by law.
   ayoub@VBox:~$

   ===
   ==

   ayoub@VBox:~$ ssh -V
   OpenSSH_8.4p1 Ubuntu-6ubuntu2.1, OpenSSL 1.1.1l  24 Aug 2021
   ayoub@VBox:~$

   ===
   ==
   The remote server had the same versions of bash, ssh and Ubuntu Linux.
   Locally, I was running

   Linux as a virtual machine in VirtualBox on a Windows10 host.

References

   1. mailto:testuser5@192.168.0.212
   2. https://help.ubuntu.com/
   3. https://landscape.canonical.com/
   4. https://ubuntu.com/advantage
   5. https://ubuntu.com/blog/microk8s-memory-optimisation
   6. mailto:testuser5@192.168.0.212
   7. https://help.ubuntu.com/
   8. https://landscape.canonical.com/
   9. https://ubuntu.com/advantage
  10. https://ubuntu.com/blog/microk8s-memory-optimisation
  11. http://gnu.org/licenses/gpl.html


Re: Login to a remote machine.

2022-04-09 Thread Lawrence Velázquez
On Sat, Apr 9, 2022, at 8:14 PM, Ayoub Misherghi wrote:
>  Below I show two ways I ssh logged into a machine.
> In the first method I referred to the remote machine
> as 192.168.0.212 while in the second method I referred to the
> remote machine as [1]testuser5@192.168.0.212 specifying the user.
> In the first method it logged me in as user ayoub when the
> .ssh directory of ayoub does not have the file for the key
> specified with the -i parameter option. With the second method
>it logged me in as the testuser5 user; as expected.
>Some information about the bash version and the Linux version
>is right at the bottom.

What makes you think bash has anything to do with any of this?  Your
shell is not involved with authentication.

-- 
vq



Re: Login to a remote machine.

2022-04-09 Thread Robert Elz
I cannot imagine what you believe any of that has to do
with bash.  It is clearly entirely related to ssh/sshd
and the way you are using it.

I suspect the "problem" relates to a misunderstanding
of what the -i ootion to ssh actually does, but you
should talk to ssh people, not bash people, to confirm
that.

kre