Hi,

today I stumbled upon a script (testssl.sh) which utilizes the \c escape
sequence for printf(1). As we are missing that escape sequence and - if
I am not mistaken - it is defined by POSIX (IEEE Std 1003.1) I thought I
give it a shot.

Please bare with me as I am not an experienced coder or POSIX reader but
I welcome feedback.

Firstly, here's a comparison of printf(1) in base and the patched printf:

$ /usr/bin/printf "%s\n\cbar\n" "foo"
foo
printf: unknown escape sequence `\c'
cbar
$

$ /usr/obj/usr.bin/printf/printf "%s\n\cbar\n" "foo"
foo
$ 

Secondly, the diff against a freshly checked out -current, I also
changed the order of \e in the man page so it fits into the otherwise
alphabetical order of the escape sequences.

Index: usr.bin/printf/printf.c
===================================================================
RCS file: /cvs/src/usr.bin/printf/printf.c,v
retrieving revision 1.22
diff -u -r1.22 printf.c
--- usr.bin/printf/printf.c     25 May 2014 07:36:36 -0000      1.22
+++ usr.bin/printf/printf.c     21 Oct 2014 21:27:47 -0000
@@ -214,7 +214,13 @@
                                break;
 
                        case '\\':
-                               fmt += print_escape(fmt);
+                               nextch = *(fmt + 1);
+                               switch (nextch) {
+                               case 'c':
+                                       return (0);
+                               default:
+                                       fmt += print_escape(fmt);
+                               }
                                break;
 
                        default:
Index: usr.bin/printf/printf.1
===================================================================
RCS file: /cvs/src/usr.bin/printf/printf.1,v
retrieving revision 1.27
diff -u -r1.27 printf.1
--- usr.bin/printf/printf.1     25 May 2014 07:36:36 -0000      1.27
+++ usr.bin/printf/printf.1     21 Oct 2014 21:27:47 -0000
@@ -80,12 +80,14 @@
 The characters and their meanings are as follows:
 .Pp
 .Bl -tag -width Ds -offset indent -compact
-.It Cm \ee
-Write an <escape> character.
 .It Cm \ea
 Write a <bell> character.
 .It Cm \eb
 Write a <backspace> character.
+.It Cm \ec
+Ignore remaining characters in this string.
+.It Cm \ee
+Write an <escape> character.
 .It Cm \ef
 Write a <form-feed> character.
 .It Cm \en

Frank.

Reply via email to