home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume06 / count < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  6.5 KB

  1. From decwrl!ucbvax!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!allbery Mon May 15 07:39:42 PDT 1989
  2. Article 869 of comp.sources.misc:
  3. Path: decwrl!ucbvax!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!allbery
  4. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5. Newsgroups: comp.sources.misc
  6. Subject: v06i100: Count program
  7. Message-ID: <54804@uunet.UU.NET>
  8. Date: 15 May 89 03:30:00 GMT
  9. Sender: allbery@uunet.UU.NET
  10. Reply-To: Jeff Beadles <jeff@quark.wv.tek.com>
  11. Organization: Tektronix, Inc., Wilsonville, OR
  12. Lines: 235
  13. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  14.  
  15. Posting-number: Volume 6, Issue 100
  16. Submitted-by: jeff@quark.wv.tek.com (Jeff Beadles)
  17. Archive-name: count
  18.  
  19.  
  20. This is something that I wrote a while back, and decided to clean-up and
  21. give to the world.  It's useful when you need to count in a shell script,
  22. when your system does not support it.  It can be called like:
  23.  
  24.     for num in `count 1 100`        # Counts from 1 to 100
  25.  
  26.  
  27. #--------------------------------CUT HERE-------------------------------------
  28. #! /bin/sh
  29. #
  30. # This is a shell archive.  Save this into a file, edit it
  31. # and delete all lines above this comment.  Then give this
  32. # file to sh by executing the command "sh file".  The files
  33. # will be extracted into the current directory owned by
  34. # you with default permissions.
  35. #
  36. # The files contained herein are:
  37. #
  38. # -rw-------  1 jeff          447 May  9 10:39 README
  39. # -rw-------  1 jeff          446 May  9 10:12 Makefile
  40. # -rw-------  1 jeff          533 May  9 09:58 count.1
  41. # -rw-------  1 jeff         2177 May  9 10:24 count.c
  42. #
  43. echo 'x - README'
  44. if test -f README; then echo 'shar: not overwriting README'; else
  45. sed 's/^X//' << '________This_Is_The_END________' > README
  46. X    count -  By:  Jeff Beadles    jeff@quark.WV.TEK.COM
  47. X
  48. X
  49. X    This program will count from the starting number to the stop
  50. X    number, using the character 'fs' as the field seperator.
  51. X
  52. X    Note, that fs may be in several forms:
  53. X    -A    will use the letter 'A'
  54. X    --    will use a '-' as fs, and
  55. X    -\011    will use a tab (Octal 011) as the fs.  (sh does the expansion.)
  56. X
  57. X    Bugs may be sent to me if desired.
  58. X    Please keep your flames to yourself.  What do you expect for free?
  59. ________This_Is_The_END________
  60. if test `wc -l < README` -ne 13; then
  61.     echo 'shar: README was damaged during transit (should have been 13 lines)'
  62. fi
  63. fi        ; : end of overwriting check
  64. echo 'x - Makefile'
  65. if test -f Makefile; then echo 'shar: not overwriting Makefile'; else
  66. sed 's/^X//' << '________This_Is_The_END________' > Makefile
  67. X#
  68. X# Makefile for count.  This is a little overkill, but what the heck.
  69. X# (This is public domain too!)
  70. X# Written by:  Jeff Beadles
  71. X# jeff@quark.WV.TEK.COM        ...tektronix!quark.wv!jeff
  72. X#
  73. X
  74. XCC = cc
  75. XCFLAGS =
  76. X
  77. X#For the executable file
  78. XBINDIR=/usr/bin
  79. X
  80. Xcount: count.c Makefile
  81. X    $(CC) $(CFLAGS) count.c -o count
  82. X
  83. Xinstall: count
  84. X    -strip count
  85. X    cp count ${BINDIR}/count
  86. X    chmod 755 ${BINDIR}/count
  87. X
  88. Xclean:
  89. X    rm -f *.o core a.out
  90. X
  91. Xclobber: clean
  92. X    rm -f count
  93. X
  94. ________This_Is_The_END________
  95. if test `wc -l < Makefile` -ne 27; then
  96.     echo 'shar: Makefile was damaged during transit (should have been 27 lines)'
  97. fi
  98. fi        ; : end of overwriting check
  99. echo 'x - count.1'
  100. if test -f count.1; then echo 'shar: not overwriting count.1'; else
  101. sed 's/^X//' << '________This_Is_The_END________' > count.1
  102. X.\"
  103. X.\" @(#)count        1.0    05/09/89
  104. X.\"
  105. X.TH COUNT 1 "09 MAY 1989"
  106. X.UC 4
  107. X.SH NAME
  108. Xcount \- count numbers from a start to a stop point.
  109. X.SH SYNOPSIS
  110. X.B count [-c] start stop
  111. X.SH DESCRIPTION
  112. X.I Count
  113. Xwill count thru an integer sequence of numbers from
  114. X.I Start
  115. Xto
  116. X.I Stop
  117. Xwith a newline after each number.
  118. X
  119. XOptionally,
  120. X.I -c
  121. Xmay be on the command line.  This may be in one of two forms.
  122. X.I -$
  123. Xwill put a 
  124. X.I $
  125. Xbetween each number.
  126. X.I -040
  127. Xwill put a space (Octal
  128. X.I 040
  129. X) between each number.
  130. X
  131. X.SH AUTHOR
  132. XJeff Beadles    jeff@quark.WV.TEK.COM
  133. ________This_Is_The_END________
  134. if test `wc -l < count.1` -ne 31; then
  135.     echo 'shar: count.1 was damaged during transit (should have been 31 lines)'
  136. fi
  137. fi        ; : end of overwriting check
  138. echo 'x - count.c'
  139. if test -f count.c; then echo 'shar: not overwriting count.c'; else
  140. sed 's/^X//' << '________This_Is_The_END________' > count.c
  141. X/*    Count.c  Released into the public domain on 05/09/89
  142. X *    Written by:  Jeff Beadles  jeff@quark.WV.TEK.COM
  143. X *      or ...!tektronix!quark.WV!jeff
  144. X *
  145. X *    NOTE:  This program is not supported by Tektronix, Inc.
  146. X *
  147. X *    This program will count from the starting number to the stop
  148. X *    number, using the character 'fs' as the field seperator.
  149. X *    Note, that fs may be in several forms:
  150. X *    -A    will use the letter 'A'
  151. X *    --    will use a '-' as fs, and
  152. X *    -\011    will use a tab (Octal 011) as the fs.  (sh does the expansion.)
  153. X *
  154. X *    Bugs may be sent to me if desired.
  155. X *    Please keep your flames to yourself.  What do you expect for free?
  156. X *
  157. X */
  158. X
  159. X
  160. X#include <stdio.h>
  161. X#include <ctype.h>
  162. X
  163. X/*
  164. X *    Default field seperator
  165. X */
  166. X
  167. X#ifndef FS
  168. X#define FS '\n'
  169. X#endif
  170. X
  171. Xint
  172. Xmain(argc,argv)
  173. Xint argc;
  174. Xchar **argv;
  175. X
  176. X{
  177. X    void usage();
  178. X    int oatc();
  179. X    int    start = 0;    /* Start count                */
  180. X    int    stop  = 0;    /* Stop  count                */
  181. X    int    pos   = 1;    /* Position in command line for parsing    */
  182. X    char    fs    = FS;    /* Field Separator            */
  183. X
  184. X    if ( argc < 2)
  185. X        usage(argv[0]);        /* Does not return */
  186. X
  187. X    if ( argv[1][0] == '-' ) {
  188. X        if ( (isdigit(argv[1][1])) && (strlen(argv[1]) == 4) )
  189. X            fs=oatc(argv[1] + 1);
  190. X            else
  191. X            fs = argv[1][1];
  192. X        pos++;        /* On to the next arg... */
  193. X    }
  194. X    start = atoi(argv[pos++]);    /* Start here, and... */
  195. X
  196. X    if ( argc <= pos)
  197. X        usage(argv[0]);        /* Does not return */
  198. X
  199. X    stop  = atoi(argv[pos]);     /* Stop here. */
  200. X    if ( start >= stop)        /* Are they brain damaged? */
  201. X    {
  202. X        fprintf(stderr,"Error:  START must be less than STOP\n");
  203. X        exit(-2);
  204. X    }
  205. X
  206. X/*
  207. X   Yes, this is it.  It even prints a '\n' when done, if the fs != '\n' (Wow)
  208. X */
  209. X    while ( start <= stop )
  210. X        printf("%d%c",start++,( (start != stop) ? fs : '\n' ) );
  211. X}
  212. X
  213. X/*
  214. X   Can you figure out this function with no comments?  Sure, you can.
  215. X*/
  216. Xvoid usage(program)
  217. Xchar *program;
  218. X
  219. X{
  220. X    fprintf(stderr,"Usage: %s [ -c] start stop\n",program);
  221. X    exit(-1);
  222. X}
  223. X
  224. X/*
  225. X *    octal ascii to char
  226. X */
  227. X
  228. Xint oatc(str)
  229. Xchar *str;
  230. X    {
  231. X    int retval=0;
  232. X    int pos=0;
  233. X    int tmp=0;
  234. X    int loop;
  235. X    static int table[] = { 1, 8, 64 };   /* Powers of 8, to avoid POW */
  236. X
  237. X
  238. X    for(loop=strlen(str) - 1; loop >= 0; loop--)
  239. X        retval += ( (str[loop] - '0') * table[pos++] );
  240. X
  241. X    return((char)retval);
  242. X}
  243. X
  244. ________This_Is_The_END________
  245. if test `wc -l < count.c` -ne 103; then
  246.     echo 'shar: count.c was damaged during transit (should have been 103 lines)'
  247. fi
  248. fi        ; : end of overwriting check
  249. exit 0
  250.  
  251.  
  252.