Why&how do "sh" and "ksh" differ in behavior e.g. PS1 presentation, while their binaries are equal?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Why&how do "sh" and "ksh" differ in behavior e.g. PS1 presentation, while their binaries are equal?

Joseph Mayer
Hi misc@,

I noticed that sh and ksh have different behaviors from doing
"export PS1='\[\033[01;35m\]COLOR\$ '", here "sh" shows
"\[\033[01;35m\]COLOR#" in gray, while "ksh" shows "COLOR#" in purple.

sh and ksh's binaries are equivalent.

I do see that sh's and ksh's man pages differ, e.g.
http://man.openbsd.org/ksh#PS1 and http://man.openbsd.org/sh#PS1
differ.

Until now however I used to think that while they have different
documentation (different interfaces), they would actually behave the
same.

On a quick sourcecode check I didn't see any code paths e.g.
"if (argv[0] matches "ksh") { something } else { something else }"
however I presume I missed something.

What's going on, what's the point?

Joseph

Reply | Threaded
Open this post in threaded view
|

Re: Why&how do "sh" and "ksh" differ in behavior e.g. PS1 presentation, while their binaries are equal?

Sebastien Marie-3
On Tue, Oct 30, 2018 at 08:11:24AM +0000, Joseph Mayer wrote:
>
> On a quick sourcecode check I didn't see any code paths e.g.
> "if (argv[0] matches "ksh") { something } else { something else }"
> however I presume I missed something.
>

yes, the check was too quick, it seems :)

src/bin/ksh/main.c
   134  int
   135  main(int argc, char *argv[])
   136  {
   137          int i;
   138          int argi;
   139          Source *s;
   140          struct block *l;
   141          int restricted, errexit;
   142          char **wp;
   143          struct env env;
   144          pid_t ppid;
   145
   146          kshname = argv[0];
   147
        ...
   236          /* Check to see if we're /bin/sh. */
   237          if (!strcmp(kshname, "sh") || !strcmp(kshname, "-sh") ||
   238              (strlen(kshname) >= 3 &&
   239              !strcmp(&kshname[strlen(kshname) - 3], "/sh"))) {
   240                  Flag(FSH) = 1;
   241                  version_param = "SH_VERSION";
   242          }
   243

--
Sebastien Marie

Reply | Threaded
Open this post in threaded view
|

Re: Why&how do "sh" and "ksh" differ in behavior e.g. PS1 presentation, while their binaries are equal?

Joseph Mayer
On Tuesday, October 30, 2018 4:33 PM, Sebastien Marie <[hidden email]> wrote:

> On Tue, Oct 30, 2018 at 08:11:24AM +0000, Joseph Mayer wrote:
>
> > On a quick sourcecode check I didn't see any code paths e.g.
> > "if (argv[0] matches "ksh") { something } else { something else }"
> > however I presume I missed something.
>
> yes, the check was too quick, it seems :)
>
> src/bin/ksh/main.c
> 134 int
> 135 main(int argc, char *argv[])
> 136 {
> 137 int i;
> 138 int argi;
> 139 Source *s;
> 140 struct block *l;
> 141 int restricted, errexit;
> 142 char *wp;
> 143 struct env env;
> 144 pid_t ppid;
> 145
> 146 kshname = argv[0];
> 147
> ...
> 236 / Check to see if we're /bin/sh. */237 if (!strcmp(kshname, "sh") || !strcmp(kshname, "-sh") ||
> 238 (strlen(kshname) >= 3 &&
>
> 239 !strcmp(&kshname[strlen(kshname) - 3], "/sh"))) {
> 240 Flag(FSH) = 1;
> 241 version_param = "SH_VERSION";
> 242 }
> 243
>
> ---------------------------------------------------------------------------------------------------------------------
>
> Sebastien Marie

Hi Sebastien,

Thanks a lot for clarifying.

sh's man page (http://man.openbsd.org/sh#DESCRIPTION) says:

"This version of sh is actually ksh in disguise. As such, it also
supports the features described in ksh(1). This manual page describes
only the parts relevant to a POSIX compliant sh."

When I read that originally, I perceived it as that "sh" and "ksh"
normally would have equivalent behavior - which also seems logical
given that their binaries are byte-equivalent.

The meaning I gather from the sentence is that sh and ksh are
equivalent and that instead the man pages will describe different
functionalities that are actually available in both.

Can that phrase in sh's man page be tweaked so that my misunderstanding
no longer is possible?

Do you find my misunderstanding a reasonable reading?

Joseph

Reply | Threaded
Open this post in threaded view
|

Re: Why&how do "sh" and "ksh" differ in behavior e.g. PS1 presentation, while their binaries are equal?

Joseph Mayer
On Wednesday, October 31, 2018 10:19 PM, Joseph Mayer <[hidden email]> wrote:

> On Tuesday, October 30, 2018 4:33 PM, Sebastien Marie [hidden email] wrote:
>
> > On Tue, Oct 30, 2018 at 08:11:24AM +0000, Joseph Mayer wrote:
> >
> > > On a quick sourcecode check I didn't see any code paths e.g.
> > > "if (argv[0] matches "ksh") { something } else { something else }"
> > > however I presume I missed something.
> >
> > yes, the check was too quick, it seems :)
> > src/bin/ksh/main.c
> > 134 int
> > 135 main(int argc, char *argv[])
> > 136 {
> > 137 int i;
> > 138 int argi;
> > 139 Source *s;
> > 140 struct block *l;
> > 141 int restricted, errexit;
> > 142 char *wp;
> > 143 struct env env;
> > 144 pid_t ppid;
> > 145
> > 146 kshname = argv[0];
> > 147
> > ...
> > 236 / Check to see if we're /bin/sh. */237 if (!strcmp(kshname, "sh") || !strcmp(kshname, "-sh") ||
> > 238 (strlen(kshname) >= 3 &&
> > 239 !strcmp(&kshname[strlen(kshname) - 3], "/sh"))) {
> > 240 Flag(FSH) = 1;
> > 241 version_param = "SH_VERSION";
> > 242 }
> > 243
> >
> > Sebastien Marie
>
> Hi Sebastien,
>
> Thanks a lot for clarifying.
>
> sh's man page (http://man.openbsd.org/sh#DESCRIPTION) says:
>
> "This version of sh is actually ksh in disguise. As such, it also
> supports the features described in ksh(1). This manual page describes
> only the parts relevant to a POSIX compliant sh."
>
> When I read that originally, I perceived it as that "sh" and "ksh"
> normally would have equivalent behavior - which also seems logical
> given that their binaries are byte-equivalent.
>
> The meaning I gather from the sentence is that sh and ksh are
> equivalent and that instead the man pages will describe different
> functionalities that are actually available in both.
>
> Can that phrase in sh's man page be tweaked so that my misunderstanding
> no longer is possible?
>
> Do you find my misunderstanding a reasonable reading?
>
> Joseph

Moving this to bugs@ , sorry for spam.