Encrypting Ed(1)

November 5, 2019

Here’s my program, installed in /usr/local/bin/ed, which comes before /usr/bin/ed in my path:

#! /bin/sh

# ed -- add encryption to ed

CMD="/usr/bin/ed"; ENCRYPT="F"

while [ "$(expr substr $1X 1 1)X" = "-X" ]
do case $1 in
   -x) ENCRYPT="T"; shift;;
   -p) CMD="$CMD $1 $2"; shift; shift;;
   *)  CMD="$CMD $1"; shift;;

if [ "$ENCRYPT" = "F" ]
then $CMD "$*"
else stty -echo
     echo -n "Enter password: "
     read PASS1
     echo ""
     echo -n "Repeat password: "
     read PASS2
     echo ""
     stty echo
     if [ "$PASS1" != "$PASS2" -o "X$PASS1" = "X" ]
     then echo "Password error" >&2; exit 1
     echo $PASS1 | ccdecrypt -k - $1
     $CMD $1
     echo $PASS2 | ccencrypt -k - $1
     mv $1.cpt $(basename $1 .cpt)

I did my own getopts processing because, well why not? There is a subtle problem here: If ed properly took an -x flag, it would be possible to create a new file in the editor. But since there is no way to pass the name of a newly-created file to the encryption wrapper, in my program an encrypted file must already exist before it can be editted; it can’t be created in the editor. Still, it allows me to write an edit script for an encrypted file, which is what I set out to do.

You can see the program at https://ideone.com/Tb9QsN, but there’s no way to run it.

By the way, I still use sh for shell scripts. Has the rest of the world moved on to bash?


Pages: 1 2

One Response to “Encrypting Ed(1)”

  1. […] task is to enhance ed by adding the -x encryption flag. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: