bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

printf "%c" - fixed


From: GVK
Subject: printf "%c" - fixed
Date: Fri, 13 Aug 2004 01:36:26 +0530
User-agent: Mozilla Thunderbird 0.7.1 (X11/20040626)

Hello,
This is my first time messing with some source-code. Someone on Savannah complained that printf "%c" [int] outputs the first digit instead of a character, i.e
$ printf "%c" 65
6
instead of expected output 'A'. So, I kinda fixed it :-)
This is also my first patch. Please let me know if it's wrong/incorrect/stupid.

The original message can be found at
https://savannah.gnu.org/support/?func=detailitem&item_id=102985


regards,
GVK
---     printf.c        2004-08-13 00:49:19.207943496 +0530
+++ printf.c    2004-08-13 01:23:29.850198488 +0530
@@ -327,6 +327,7 @@
             int precision, const char *argument)
 {
   char *p;             /* Null-terminated copy of % directive. */
+  int temp_int;
 
   p = xmalloc ((unsigned) (length + 1));
   strncpy (p, start, length);
@@ -394,7 +395,20 @@
       break;
 
     case 'c':
-      printf (p, *argument);
+      /*  Previously, it was printf( p,  *argument ) for "%c". The problem was,
+         printf "%c" 65 outputs '6'. Instead of printing argument, we convert 
it to int
+         and see if it falls in printable range or not, then print it.
+
+         This is the first time I'm actually playing with a real source-code. 
If it's naive/wrong/stupid,
+         please let me know by mailing me at vamsee_k@students.iiit.net
+      */
+      temp_int = atoi(argument);
+      if( !( temp_int >= 0 && temp_int < 256 ) )         /*  oops, can't print 
this one :-)  */
+       {
+         fprintf ( stderr, _("no corresponding printable character for \"%s\": 
%s\n"),argument,program_name );
+         exit( EXIT_FAILURE );
+       }
+      printf( p, (char) temp_int );
       break;
 
     case 's':

reply via email to

[Prev in Thread] Current Thread [Next in Thread]