Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
count.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include <stdio.h>
00008 #include <string.h>
00009 #include <stdlib.h>
00010 #include <time.h>
00011
00012 int ranco(int,int) ;
00013
00014 int main( int argc , char *argv[] )
00015 {
00016 int ii , bot = -1 , top = -1 , step = -1 , rando_count = 0, rando_num ;
00017 int narg , ndig = 4 , iout ;
00018 static char root[6664] , fmt[128] , suffix[6664] ;
00019 float sclfac = 0.0 ;
00020
00021
00022
00023 if( argc < 3 || strncmp(argv[1],"-help",2) == 0 ){
00024
00025 printf(
00026 "Usage: count [options] bot top [step]\n"
00027 "\n"
00028 "* Produces many numbered copies of the root and/or suffix,\n"
00029 " counting from 'bot' to 'top' with stride 'step'.\n"
00030 "* If 'bot' > 'top', counts backwards with stride '-step'.\n"
00031 "* If step is of the form 'R#', then '#' random counts are produced\n"
00032 " in the range 'bot..top' (inclusive).\n"
00033 "* 'bot' and 'top' must not be negative; step must be positive.\n"
00034 "\n"
00035 "Options:\n"
00036 " -digits n prints numbers with 'n' digits [default=4]\n"
00037 " -root rrr prints string 'rrr' before the number [default=empty]\n"
00038 " -suffix sss prints string 'sss' after the number [default=empty]\n"
00039 " -scale fff multiplies each number by the factor 'fff';\n"
00040 " if this option is used, -digits is ignored and\n"
00041 " the floating point format '%%g' is used for output.\n"
00042 " ('fff' can be a floating point number.)\n"
00043 "\n"
00044 "The main application of this program is for use in C shell programming:\n"
00045 " foreach fred ( `count 1 20` )\n"
00046 " mv wilma.${fred} barney.${fred}\n"
00047 " end\n"
00048 "The backward quote operator in the foreach statement executes the\n"
00049 "count program, captures its output, and puts it on the command line.\n"
00050 "The loop body renames each file wilma.0001 to wilma.0020 to barney.0001\n"
00051 "to barney.0020. Read the man page for csh to get more information. In\n"
00052 "particular, the csh built-in command '@' can be useful.\n"
00053 ) ;
00054
00055 exit(0) ;
00056 }
00057
00058
00059
00060 narg = 1 ;
00061 root[0] = '\0' ;
00062 suffix[0] = '\0' ;
00063
00064 do {
00065
00066
00067
00068 if( strncmp(argv[narg],"-digits",2) == 0 ){
00069 ndig = strtol( argv[++narg] , NULL , 10 ) ;
00070 if( ndig < 1 ){
00071 fprintf( stderr , "-digits illegal!\n" ) ;
00072 exit(1) ;
00073 }
00074 continue ;
00075 }
00076
00077 if( strncmp(argv[narg],"-root",2) == 0 ){
00078 strcpy(root,argv[++narg]) ;
00079 continue ;
00080 }
00081
00082 if( strncmp(argv[narg],"-suffix",3) == 0 ){
00083 strcpy(suffix,argv[++narg]) ;
00084 continue ;
00085 }
00086
00087 if( strncmp(argv[narg],"-scale",3) == 0 ){
00088 sclfac = strtod(argv[++narg],NULL) ;
00089 continue ;
00090 }
00091
00092 if( strncmp(argv[narg],"-",1) == 0 ){
00093 fprintf( stderr , "unknown switch %s\n" , argv[narg] ) ;
00094 exit(1) ;
00095 }
00096
00097
00098
00099 if( bot < 0 ){
00100 bot = strtol( argv[narg] , NULL , 10 ) ;
00101 if( bot < 0 ){
00102 fprintf( stderr , "illegal value of bot %d\n" , bot ) ;
00103 exit(1) ;
00104 }
00105 continue ;
00106 }
00107
00108 if( top < 0 ){
00109 top = strtol( argv[narg] , NULL , 10 ) ;
00110 if( top < 0 ){
00111 fprintf( stderr , "illegal value of top %d\n" , top ) ;
00112 exit(1) ;
00113 }
00114 continue ;
00115 }
00116
00117 if( step < 0 ){
00118 if( argv[narg][0] == 'R' || argv[narg][0] == 'r' ){
00119 rando_count = 1 ;
00120 rando_num = strtol( argv[narg]+1 , NULL , 10 ) ;
00121 if( rando_num <= 0 ){
00122 fprintf( stderr , "illegal value of random count %d\n" , rando_num ) ;
00123 exit(1) ;
00124 }
00125 continue ;
00126 }
00127 step = strtol( argv[narg] , NULL , 10 ) ;
00128 if( step <= 0 ){
00129 fprintf( stderr , "illegal value of step %d\n" , step ) ;
00130 exit(1) ;
00131 }
00132 continue ;
00133 }
00134
00135 fprintf( stderr , "too many arguments: %s\n" , argv[narg] ) ;
00136 exit(1) ;
00137
00138 } while ( ++narg < argc ) ;
00139
00140
00141
00142 if( step <= 0 ) step = 1 ;
00143
00144 if( sclfac == 0.0 )
00145 sprintf( fmt , " %%s%%0%dd%%s" , ndig ) ;
00146 else
00147 strcpy( fmt , " %s%g%s" ) ;
00148
00149
00150
00151 if( ! rando_count ){
00152 if( bot <= top ){
00153 for( ii=bot ; ii <= top ; ii += step )
00154 if( sclfac == 0.0 )
00155 printf( fmt , root , ii , suffix ) ;
00156 else
00157 printf( fmt , root , sclfac*ii , suffix ) ;
00158 } else {
00159 for( ii=bot ; ii >= top ; ii -= step )
00160 if( sclfac == 0.0 )
00161 printf( fmt , root , ii , suffix ) ;
00162 else
00163 printf( fmt , root , sclfac*ii , suffix ) ;
00164 }
00165 } else {
00166 for( ii=0 ; ii < rando_num ; ii++ ){
00167 iout = ranco( bot , top ) ;
00168 if( sclfac == 0.0 )
00169 printf( fmt , root , iout , suffix ) ;
00170 else
00171 printf( fmt , root , sclfac*iout , suffix ) ;
00172 }
00173 }
00174
00175 printf( "\n" ) ;
00176 exit(0) ;
00177 }
00178
00179 int ranco( int bot , int top )
00180 {
00181 static int first = 1 ;
00182 int ir , ii ;
00183 double dr ;
00184
00185 if( first ){
00186 srand48( time(NULL) ) ;
00187 dr = drand48() ;
00188 ir = (int)(dr*100) ;
00189 for( ii=0 ; ii < ir ; ii++ ) dr = drand48() ;
00190 first = 0 ;
00191 }
00192
00193 ir = bot + (top-bot+0.999999)*drand48() ;
00194 return ir ;
00195 }