caesar(6) to accept negative argument

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

caesar(6) to accept negative argument

Tristan Le Guern-3
Hi,

This little patch makes caesar(6) useful at both encrypting and
decrypting texts by allowing a negative rotation.

Example:

$ echo Ceci est un test | caesar 10
Moms ocd ex docd
$ echo Ceci est un test | caesar 10 | caesar -10
Ceci est un test

A similar patch was proposed by Dieter Rauschenberger in 2008 with
little response so perhaps explaining why this is useful will help.
Without spoiling too much: one of the puzzle in the Infocom game Leather
Goddesses of Phobos requires to decipher such a text.

I did not touch the man page as it was unclear about rotation boundaries
anyway.

Regards.

Index: caesar.c
===================================================================
RCS file: /cvs/src/games/caesar/caesar.c,v
retrieving revision 1.20
diff -u -p -u -p -r1.20 caesar.c
--- caesar.c 10 Aug 2017 17:24:30 -0000 1.20
+++ caesar.c 15 May 2019 14:34:49 -0000
@@ -80,7 +80,7 @@ main(int argc, char *argv[])
  printit(13);
 
  if (argc > 1) {
- i = strtonum(argv[1], 0, 25, &errstr);
+ i = strtonum(argv[1], -25, 25, &errstr);
  if (errstr)
  errx(1, "rotation is %s: %s", errstr, argv[1]);
  else
@@ -134,7 +134,9 @@ void
 printit(int rot)
 {
  int ch;
-
+
+ if (rot < 0)
+ rot = rot + 26;
  while ((ch = getchar()) != EOF)
  putchar(ROTATE(ch, rot));
  exit(0);

Reply | Threaded
Open this post in threaded view
|

Re: caesar(6) to accept negative argument

Otto Moerbeek
On Wed, May 15, 2019 at 03:36:57PM +0100, tleguern wrote:

> Hi,
>
> This little patch makes caesar(6) useful at both encrypting and
> decrypting texts by allowing a negative rotation.
>
> Example:
>
> $ echo Ceci est un test | caesar 10
> Moms ocd ex docd
> $ echo Ceci est un test | caesar 10 | caesar -10
> Ceci est un test
>
> A similar patch was proposed by Dieter Rauschenberger in 2008 with
> little response so perhaps explaining why this is useful will help.
> Without spoiling too much: one of the puzzle in the Infocom game Leather
> Goddesses of Phobos requires to decipher such a text.
>
> I did not touch the man page as it was unclear about rotation boundaries
> anyway.

$ echo Ceci est un test | caesar 10 | caesar 16
Ceci est un test

We're computing modulo 26 here. Negative numbers have a positive
equivalent. So you diff adds code for no benefit.

        -Otto

>
> Regards.
>
> Index: caesar.c
> ===================================================================
> RCS file: /cvs/src/games/caesar/caesar.c,v
> retrieving revision 1.20
> diff -u -p -u -p -r1.20 caesar.c
> --- caesar.c 10 Aug 2017 17:24:30 -0000 1.20
> +++ caesar.c 15 May 2019 14:34:49 -0000
> @@ -80,7 +80,7 @@ main(int argc, char *argv[])
>   printit(13);
>  
>   if (argc > 1) {
> - i = strtonum(argv[1], 0, 25, &errstr);
> + i = strtonum(argv[1], -25, 25, &errstr);
>   if (errstr)
>   errx(1, "rotation is %s: %s", errstr, argv[1]);
>   else
> @@ -134,7 +134,9 @@ void
>  printit(int rot)
>  {
>   int ch;
> -
> +
> + if (rot < 0)
> + rot = rot + 26;
>   while ((ch = getchar()) != EOF)
>   putchar(ROTATE(ch, rot));
>   exit(0);
>

Reply | Threaded
Open this post in threaded view
|

Re: caesar(6) to accept negative argument

Ingo Schwarze
In reply to this post by Tristan Le Guern-3
Hi,

tleguern wrote on Wed, May 15, 2019 at 03:36:57PM +0100:

> This little patch makes caesar(6) useful at both encrypting and
> decrypting texts by allowing a negative rotation.

Committed, thanks.

> A similar patch was proposed by Dieter Rauschenberger in 2008 with
> little response

Well, it's a game, so people sometimes forget how critical it is
to publish errata for it as fastly as possible.  ;-)

Yours,
  Ingo


CVSROOT: /cvs
Module name: src
Changes by: [hidden email] 2019/05/15 09:59:24

Modified files:
        games/caesar   : caesar.c

Log message:
patch from <tleguern at bouledef dot eu> to support backward rotation,
hoping to save somebody's life from the Leather Goddesses of Phobos

Reply | Threaded
Open this post in threaded view
|

Re: caesar(6) to accept negative argument

Ted Unangst-6
In reply to this post by Otto Moerbeek
Otto Moerbeek wrote:
> We're computing modulo 26 here. Negative numbers have a positive
> equivalent. So you diff adds code for no benefit.

I think the amount of code added is an acceptable cost for improved user
experience. We could use this argument to remove subtraction from bc, but that
would be silly.

Reply | Threaded
Open this post in threaded view
|

Re: caesar(6) to accept negative argument

Otto Moerbeek
On Wed, May 15, 2019 at 02:33:45PM -0400, Ted Unangst wrote:

> Otto Moerbeek wrote:
> > We're computing modulo 26 here. Negative numbers have a positive
> > equivalent. So you diff adds code for no benefit.
>
> I think the amount of code added is an acceptable cost for improved user
> experience. We could use this argument to remove subtraction from bc, but that
> would be silly.
>

Ok, agreed.

        -Otto