bug-coreutils
[Top][All Lists]
Advanced

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

PATCH: adds seq ordinal/hexidecimal integer output


From: Kuwanger
Subject: PATCH: adds seq ordinal/hexidecimal integer output
Date: Wed, 8 Jun 2005 09:27:10 -0500
User-agent: KMail/1.7.2

seq offers -f to output floating point output.  Includes is a patch to add 
ordinal and hexidecimal output.  Output is cast to int first.  Also, the 
manual page still needs updated, I believe.  I'd like to change this to output 
64-bit numbers, not just 32-bit, though I'm not sure how--coreutils' printf 
only supports 32-bit ordinal/hexidecimal, so perhaps this is okay.

diff -ur coreutils-5.2.1-old/src/seq.c coreutils-5.2.1/src/seq.c
--- coreutils-5.2.1-old/src/seq.c       2005-06-07 16:50:16.016210726 -0500
+++ coreutils-5.2.1/src/seq.c   2005-06-07 17:10:44.853692544 -0500
@@ -102,6 +102,7 @@
 INCREMENT is usually negative if FIRST is greater than LAST.\n\
 When given, the FORMAT argument must contain exactly one of\n\
 the printf-style, floating point output formats %e, %f, %g\n\
+or one of the printf-style, integer output formats %o, %x\n\
 "), stdout);
       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     }
@@ -126,11 +127,14 @@
 }
 
 /* Check whether the format string is valid for a single `double'
-   argument.  Return 0 if not, 1 if correct. */
+   argument.  Return 0 if not, 1 if correct, 2 if a conversion
+   to int is necessary. */
 
 static int
 valid_format (const char *fmt)
 {
+  int end_return = 1;
+  
   while (*fmt != '\0')
     {
       if (*fmt == '%')
@@ -158,7 +162,11 @@
     }
 
   if (!(*fmt == 'e' || *fmt == 'f' || *fmt == 'g'))
-    return 0;
+   {
+     end_return = 2;
+     if (!(*fmt == 'o' || *fmt == 'x'))
+       return 0;
+   }
 
   fmt++;
   while (*fmt != '\0')
@@ -173,13 +181,13 @@
       fmt++;
     }
 
-  return 1;
+  return end_return;
 }
 
 /* Actually print the sequence of numbers in the specified range, with the
    given or default stepping and format.  */
 static void
-print_numbers (const char *fmt)
+print_numbers (const char *fmt, int integerize)
 {
   double i;
 
@@ -190,7 +198,10 @@
        break;
       if (i)
        fputs (separator, stdout);
-      printf (fmt, x);
+      if (integerize)
+        printf (fmt, (int) x);
+      else
+        printf (fmt, x);
     }
 
   if (i)
@@ -298,6 +309,7 @@
 main (int argc, char **argv)
 {
   int optc;
+  int valid_return;
   int step_is_set;
 
   /* The printf(3) format used for output.  */
@@ -383,7 +395,7 @@
       usage (EXIT_FAILURE);
     }
 
-  if (format_str && !valid_format (format_str))
+  if (format_str && !(valid_return = valid_format (format_str)))
     {
       error (0, 0, _("invalid format string: `%s'"), format_str);
       usage (EXIT_FAILURE);
@@ -426,7 +438,7 @@
        format_str = "%g";
     }
 
-  print_numbers (format_str);
+  print_numbers (format_str, valid_return - 1);
 
   exit (EXIT_SUCCESS);
 }




reply via email to

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