Path: utzoo!mnetor!tmsoft!mason From: mason@tmsoft.uucp (Dave Mason) Newsgroups: comp.sources.d Subject: v06i100: Count program - leading zero enhancement Message-ID: <1989May16.000313.8145@tmsoft.uucp> Date: 16 May 89 00:03:13 GMT References: <54804@uunet.UU.NET> Reply-To: mason@tmsoft.UUCP (Dave Mason) Followup-To: comp.sources.d Organization: TM Software Associates, Toronto Lines: 78 In article <54804@uunet.UU.NET> Jeff Beadles gives us the classic little hack. It's beautiful. 3 lines changed and it now gives leading zeros useful for filenames. (Note this may not work on really old printf's. I wasn't willing to add the 2 other lines required to make it work everywhere.) ../Dave *** count.1.orig Mon May 15 19:38:00 1989 --- count.1 Mon May 15 19:54:37 1989 *************** *** 27,31 **** --- 27,35 ---- .I 040 ) between each number. + Leading zeros on the + .I Start + value will be reflected in the generated numbers. .SH AUTHOR Jeff Beadles jeff@quark.WV.TEK.COM + Dave Mason mason@tmsoft (leading zeros hack) *** count.c.orig Mon May 15 19:38:02 1989 --- count.c Mon May 15 19:55:28 1989 *************** *** 16,21 **** --- 16,30 ---- * */ + /* Great program! Hacked by Dave Mason 890515 to: + * Add leading zeros (string will always be at least as long as + * the lower limit). E.g.: + * count 05 7 + * will produce: + * 05 + * 06 + * 07 + */ #include #include *************** *** 37,42 **** --- 46,52 ---- void usage(); int oatc(); int start = 0; /* Start count */ + int len; /* Minimum length */ int stop = 0; /* Stop count */ int pos = 1; /* Position in command line for parsing */ char fs = FS; /* Field Separator */ *************** *** 51,56 **** --- 61,67 ---- fs = argv[1][1]; pos++; /* On to the next arg... */ } + len = strlen(argv[pos]); /* Minimum length */ start = atoi(argv[pos++]); /* Start here, and... */ if ( argc <= pos) *************** *** 67,73 **** Yes, this is it. It even prints a '\n' when done, if the fs != '\n' (Wow) */ while ( start <= stop ) ! printf("%d%c",start++,( (start != stop) ? fs : '\n' ) ); } /* --- 78,84 ---- Yes, this is it. It even prints a '\n' when done, if the fs != '\n' (Wow) */ while ( start <= stop ) ! printf("%.*d%c",len,start++,( (start != stop) ? fs : '\n' ) ); } /*