allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (05/15/89)
Posting-number: Volume 6, Issue 100 Submitted-by: jeff@quark.wv.tek.com (Jeff Beadles) Archive-name: count This is something that I wrote a while back, and decided to clean-up and give to the world. It's useful when you need to count in a shell script, when your system does not support it. It can be called like: for num in `count 1 100` # Counts from 1 to 100 #--------------------------------CUT HERE------------------------------------- #! /bin/sh # # This is a shell archive. Save this into a file, edit it # and delete all lines above this comment. Then give this # file to sh by executing the command "sh file". The files # will be extracted into the current directory owned by # you with default permissions. # # The files contained herein are: # # -rw------- 1 jeff 447 May 9 10:39 README # -rw------- 1 jeff 446 May 9 10:12 Makefile # -rw------- 1 jeff 533 May 9 09:58 count.1 # -rw------- 1 jeff 2177 May 9 10:24 count.c # echo 'x - README' if test -f README; then echo 'shar: not overwriting README'; else sed 's/^X//' << '________This_Is_The_END________' > README X count - By: Jeff Beadles jeff@quark.WV.TEK.COM X X X This program will count from the starting number to the stop X number, using the character 'fs' as the field seperator. X X Note, that fs may be in several forms: X -A will use the letter 'A' X -- will use a '-' as fs, and X -\011 will use a tab (Octal 011) as the fs. (sh does the expansion.) X X Bugs may be sent to me if desired. X Please keep your flames to yourself. What do you expect for free? ________This_Is_The_END________ if test `wc -l < README` -ne 13; then echo 'shar: README was damaged during transit (should have been 13 lines)' fi fi ; : end of overwriting check echo 'x - Makefile' if test -f Makefile; then echo 'shar: not overwriting Makefile'; else sed 's/^X//' << '________This_Is_The_END________' > Makefile X# X# Makefile for count. This is a little overkill, but what the heck. X# (This is public domain too!) X# Written by: Jeff Beadles X# jeff@quark.WV.TEK.COM ...tektronix!quark.wv!jeff X# X XCC = cc XCFLAGS = X X#For the executable file XBINDIR=/usr/bin X Xcount: count.c Makefile X $(CC) $(CFLAGS) count.c -o count X Xinstall: count X -strip count X cp count ${BINDIR}/count X chmod 755 ${BINDIR}/count X Xclean: X rm -f *.o core a.out X Xclobber: clean X rm -f count X ________This_Is_The_END________ if test `wc -l < Makefile` -ne 27; then echo 'shar: Makefile was damaged during transit (should have been 27 lines)' fi fi ; : end of overwriting check echo 'x - count.1' if test -f count.1; then echo 'shar: not overwriting count.1'; else sed 's/^X//' << '________This_Is_The_END________' > count.1 X.\" X.\" @(#)count 1.0 05/09/89 X.\" X.TH COUNT 1 "09 MAY 1989" X.UC 4 X.SH NAME Xcount \- count numbers from a start to a stop point. X.SH SYNOPSIS X.B count [-c] start stop X.SH DESCRIPTION X.I Count Xwill count thru an integer sequence of numbers from X.I Start Xto X.I Stop Xwith a newline after each number. X XOptionally, X.I -c Xmay be on the command line. This may be in one of two forms. X.I -$ Xwill put a X.I $ Xbetween each number. X.I -040 Xwill put a space (Octal X.I 040 X) between each number. X X.SH AUTHOR XJeff Beadles jeff@quark.WV.TEK.COM ________This_Is_The_END________ if test `wc -l < count.1` -ne 31; then echo 'shar: count.1 was damaged during transit (should have been 31 lines)' fi fi ; : end of overwriting check echo 'x - count.c' if test -f count.c; then echo 'shar: not overwriting count.c'; else sed 's/^X//' << '________This_Is_The_END________' > count.c X/* Count.c Released into the public domain on 05/09/89 X * Written by: Jeff Beadles jeff@quark.WV.TEK.COM X * or ...!tektronix!quark.WV!jeff X * X * NOTE: This program is not supported by Tektronix, Inc. X * X * This program will count from the starting number to the stop X * number, using the character 'fs' as the field seperator. X * Note, that fs may be in several forms: X * -A will use the letter 'A' X * -- will use a '-' as fs, and X * -\011 will use a tab (Octal 011) as the fs. (sh does the expansion.) X * X * Bugs may be sent to me if desired. X * Please keep your flames to yourself. What do you expect for free? X * X */ X X X#include <stdio.h> X#include <ctype.h> X X/* X * Default field seperator X */ X X#ifndef FS X#define FS '\n' X#endif X Xint Xmain(argc,argv) Xint argc; Xchar **argv; X X{ X void usage(); X int oatc(); X int start = 0; /* Start count */ X int stop = 0; /* Stop count */ X int pos = 1; /* Position in command line for parsing */ X char fs = FS; /* Field Separator */ X X if ( argc < 2) X usage(argv[0]); /* Does not return */ X X if ( argv[1][0] == '-' ) { X if ( (isdigit(argv[1][1])) && (strlen(argv[1]) == 4) ) X fs=oatc(argv[1] + 1); X else X fs = argv[1][1]; X pos++; /* On to the next arg... */ X } X start = atoi(argv[pos++]); /* Start here, and... */ X X if ( argc <= pos) X usage(argv[0]); /* Does not return */ X X stop = atoi(argv[pos]); /* Stop here. */ X if ( start >= stop) /* Are they brain damaged? */ X { X fprintf(stderr,"Error: START must be less than STOP\n"); X exit(-2); X } X X/* X Yes, this is it. It even prints a '\n' when done, if the fs != '\n' (Wow) X */ X while ( start <= stop ) X printf("%d%c",start++,( (start != stop) ? fs : '\n' ) ); X} X X/* X Can you figure out this function with no comments? Sure, you can. X*/ Xvoid usage(program) Xchar *program; X X{ X fprintf(stderr,"Usage: %s [ -c] start stop\n",program); X exit(-1); X} X X/* X * octal ascii to char X */ X Xint oatc(str) Xchar *str; X { X int retval=0; X int pos=0; X int tmp=0; X int loop; X static int table[] = { 1, 8, 64 }; /* Powers of 8, to avoid POW */ X X X for(loop=strlen(str) - 1; loop >= 0; loop--) X retval += ( (str[loop] - '0') * table[pos++] ); X X return((char)retval); X} X ________This_Is_The_END________ if test `wc -l < count.c` -ne 103; then echo 'shar: count.c was damaged during transit (should have been 103 lines)' fi fi ; : end of overwriting check exit 0