bash の連番生成プチハック

bash 3.1.17 で

$ echo {01..10}
01 02 03 04 05 06 07 08 09 10

とさせるパッチ。

--- braces.c.orig	Mon Mar 26 01:31:51 2007
+++ braces.c	Mon Mar 26 01:27:29 2007
@@ -61,7 +61,7 @@
 static int brace_gobbler __P((char *, size_t, int *, int));
 static char **expand_amble __P((char *, size_t, int));
 static char **expand_seqterm __P((char *, size_t));
-static char **mkseq __P((int, int, int));
+static char **mkseq __P((int, int, int, int));
 static char **array_concat __P((char **, char **));
 #else
 static int brace_gobbler ();
@@ -260,11 +260,15 @@
 #define ST_CHAR	2
 
 static char **
-mkseq (start, end, type)
-     int start, end, type;
+mkseq (start, end, type, width)
+     int start, end, type, width;
 {
   int n, incr, i;
   char **result, *t;
+  char buf[16];
+
+  if (width > sizeof(buf) - 1)
+	  width = sizeof(buf) - 1;
 
   n = abs (end - start) + 1;
   result = strvec_create (n + 1);
@@ -276,8 +280,10 @@
   n = start;
   do
     {
-      if (type == ST_INT)
-	result[i++] = itos (n);
+      if (type == ST_INT) {
+	  sprintf(buf, "%0*d", width, n);
+	  result[i++] = savestring(buf);
+      }
       else
 	{
 	  t = (char *)xmalloc (2);
@@ -349,7 +355,7 @@
       rhs_v = tr;
     }
 
-  result = mkseq (lhs_v, rhs_v, lhs_t);
+  result = mkseq (lhs_v, rhs_v, lhs_t, i);
 
   free (lhs);
   free (rhs);

このソースを見ていて気づいたのだが、

$ echo {a..g}
a b c d e f g

ということもできるらしい。