diff options
-rwxr-xr-x | XTERM | 2 | ||||
-rw-r--r-- | asc.c | 73 | ||||
-rw-r--r-- | bin2db.c | 60 | ||||
-rw-r--r-- | cfile.c | 55 | ||||
-rw-r--r-- | cfile8.c | 55 | ||||
-rw-r--r-- | ctime.c | 17 | ||||
-rw-r--r-- | dotrc/_alias | 92 | ||||
-rw-r--r-- | dotrc/_bashrc | 6 | ||||
-rw-r--r-- | dotrc/_gvimrc | 22 | ||||
-rw-r--r-- | dotrc/_vimrc | 82 | ||||
-rw-r--r-- | err.c | 36 | ||||
-rw-r--r-- | hex.c | 62 | ||||
-rw-r--r-- | int2bin.c | 88 | ||||
-rw-r--r-- | mb.c | 66 | ||||
-rw-r--r-- | num.c | 128 | ||||
-rw-r--r-- | sc2001.c | 340 | ||||
-rw-r--r-- | seekp.c | 110 | ||||
-rw-r--r-- | serv.c | 105 | ||||
-rw-r--r-- | sig.c | 24 | ||||
-rw-r--r-- | sock.c | 130 | ||||
-rw-r--r-- | total.c | 16 |
21 files changed, 1569 insertions, 0 deletions
@@ -0,0 +1,2 @@ +xterm -vb -bg black -fg white -cr red -sb -sl 5000 -sk -si -geom 80x60 \ + -fn 9x15 -n xterm -title xterm & @@ -0,0 +1,73 @@ +#include <stdio.h> + +main(argc,argv) +int argc; +char *argv[]; + +{ + int f,l,r; + unsigned int c; + + char *binary(); + + if(argc==1) + { + printf("%s: usage %s string1 [..stringn] | -c num1 [.. num n]\n", + argv[0],argv[0]); + exit(-1); + } + + if(strcmp(argv[1],"-c")) + { + for(r=1;r<argc;r++) + { + l=strlen(argv[r]); + + for(f=0;f<l;f++) + { + c=(unsigned int)*(argv[r]+f); + printf("%c (%3d - 0x%2.2X %%%s)\n", + ((*(argv[r]+f) > 31) ? (*(argv[r]+f)) : '?') + ,c,c,binary(c,8)); + } + } + } + else + { + if(argc==2) + { + printf("%s:usage %s -c num1 [.. num n]\n",argv[0],argv[0]); + exit(-1); + } + for(f=2;f<argc;f++) + { + c=(char)strtol(argv[f],NULL,0); + + printf("%c (%3d - 0x%2.2X %%%s)\n", + (c>31) ? (char)c : '?', + c,c,binary(c,8)); + } + } + +} + + +char *binary(i,w) +unsigned int i,w; + +{ + static char buff[1024],*p; + unsigned int c=w,b=1<<(w-1); + + p=buff; + + for(;c--;b=b>>1) + if(i&b) + *p++='1'; + else + *p++='0'; + + *p='\0'; + + return(buff); +} diff --git a/bin2db.c b/bin2db.c new file mode 100644 index 0000000..2f7cb6b --- /dev/null +++ b/bin2db.c @@ -0,0 +1,60 @@ +/* + Convert a binary file to a set of assembly DB instructions +*/ +#include <stdlib.h> +#include <stdio.h> +#include <errno.h> + +#define PERLINE 8 + +int main(int argc, char *argv[]) +{ + char *name="bfile"; + FILE *in,*out; + unsigned char num; + int col; + + in=stdin; + out=stdout; + + if (argc>1) + { + if (!(in=fopen(argv[1],"rb"))) + { + perror(argv[1]); + exit(EXIT_FAILURE); + } + } + + if (argc>2) + { + if (!(out=fopen(argv[2],"w"))) + { + perror(argv[0]); + exit(EXIT_FAILURE); + } + } + + if (argc>1) + fprintf(out,"; Auto-generated binary of %s\n\n",argv[1]); + else + fprintf(out,"; Auto-generated binary\n\n"); + + col=0; + num=0; + + while(!feof(in)) + { + fread(&num,sizeof num,1,in); + + if (col==0) + fprintf(out,"\n\tDB\t"); + + fprintf(out,"%s0x%2.2X",col==0?"":",",num); + + col=(col+1)%PERLINE; + } + + fclose(in); + fclose(out); +} @@ -0,0 +1,55 @@ +/* + Convert a binary file to a C u_long array +*/ +#include <stdio.h> +#include <errno.h> + +int main(int argc, char *argv[]) +{ + char *name="bfile"; + FILE *in,*out; + unsigned long num; + int col; + + in=stdin; + out=stdout; + + if (argc>1) + if (!(in=fopen(argv[1],"rb"))) + perror(argv[0]); + + if (argc>2) + if (!(out=fopen(argv[2],"w"))) + perror(argv[0]); + + if (argc>3) + name=argv[3]; + + if (argc>1) + fprintf(out,"/* Auto-generated binary of %s */\n\n",argv[1]); + else + fprintf(out,"/* Auto-generated binary */\n\n"); + + fprintf(out,"unsigned long %s[]=\n\t\t\t{",name); + + col=0; + num=0; + + while(!feof(in)) + { + fread(&num,sizeof(unsigned long),1,in); + + if (col==0) + fprintf(out,"\n\t\t\t"); + + fprintf(out,"0x%.8x,",num); + + col=(col+1)%4; + num=0; + } + + fprintf(out,"\n\t\t\t};\n"); + + fclose(in); + fclose(out); +} diff --git a/cfile8.c b/cfile8.c new file mode 100644 index 0000000..702da6f --- /dev/null +++ b/cfile8.c @@ -0,0 +1,55 @@ +/* + Convert a binary file to a C unsigned char array +*/ +#include <stdio.h> +#include <errno.h> + +int main(int argc, char *argv[]) +{ + char *name="bfile"; + FILE *in,*out; + unsigned char num; + int col; + + in=stdin; + out=stdout; + + if (argc>1) + if (!(in=fopen(argv[1],"rb"))) + perror(argv[0]); + + if (argc>2) + if (!(out=fopen(argv[2],"w"))) + perror(argv[0]); + + if (argc>3) + name=argv[3]; + + if (argc>1) + fprintf(out,"/* Auto-generated binary of %s */\n\n",argv[1]); + else + fprintf(out,"/* Auto-generated binary */\n\n"); + + fprintf(out,"unsigned long %s[]=\n{",name); + + col=0; + num=0; + + while(!feof(in)) + { + fread(&num,sizeof num,1,in); + + if (col==0) + fprintf(out,"\n"); + + fprintf(out,"0x%.2x,",num); + + col=(col+1)%16; + num=0; + } + + fprintf(out,"\n};\n"); + + fclose(in); + fclose(out); +} @@ -0,0 +1,17 @@ +#include <stdlib.h> +#include <stdio.h> +#include <time.h> + +int main(int argc, char *argv[]) +{ + time_t t; + + if (argc>1) + t=(time_t)strtol(argv[1],NULL,0); + else + t=time(NULL); + + printf("%s",ctime(&t)); + + return 0; +} diff --git a/dotrc/_alias b/dotrc/_alias new file mode 100644 index 0000000..2b72874 --- /dev/null +++ b/dotrc/_alias @@ -0,0 +1,92 @@ +alias setcore="ulimit -c unlimited" + +alias mem=free + +#alias XTERM="xterm -vb -bg black -fg grey90 -cr red -sb -sl 5000 -sk -si -ls -geom 80x40 &" + +alias diffsrc='rcsdiff *.[ch] *.html makefile >& diff.out' + +alias ctags=exctags + +alias vi=vim + +alias gvim="gvim -geom 80x83" + +alias V="vi" + +alias ls="ls -Fx" +alias lsc="ls --color" +alias lrt="ls -lrt" +alias la="ls -a" +alias lsd="ls -1 | egrep /" + +alias h="history|more" +alias hi="history 20" +alias p="pwd" + +alias vc="vi ~/.alias" +alias sc="source ~/.alias" + +alias wenv="env | egrep " +alias wal="alias | egrep " +alias wset="set | egrep " + +alias pslink="/usr/sbin/ncpd -s /dev/ttyS1 -b 115200" +alias winamp="wine /home/wine/DriveC/Program\ Files/Winamp/winamp.exe" + +alias bug="gvd" + +alias status="cvs status | egrep Status:" + +. ~/.b_dalias + +# Vars +# +FIGNORE=.o + + +# Functions +# +function setenv () +{ + export $1=$2 +} + +function vgrep() +{ + local vgargs + local vgflist + local vgexp + + if [ $1 == "-i" ] ; then + vgargs="-i" + shift + else + vgargs="" + fi + + vgexp="$1" + shift + + vgflist=`egrep -l $vgargs "$vgexp" $*` + + if [ "$vgflist" == "" ] ; then + echo "No matches found" + else + vi -R -M -c "/$vgexp" $vgflist + fi +} + + +# Directory setting aliases +# +alias dsrc="source ~/.b_dalias" + +function dset () +{ + echo "export $1=\"$PWD\" ; alias $1='cd \"$PWD\"'" >> ~/.b_dalias + dsrc +} + +alias dwot='alias | egrep "=.cd" | egrep -v dwot' +alias vd="vi ~/.b_dalias" diff --git a/dotrc/_bashrc b/dotrc/_bashrc new file mode 100644 index 0000000..dbd3b3d --- /dev/null +++ b/dotrc/_bashrc @@ -0,0 +1,6 @@ + +if [ "$PS1" != "" ] ; then + PS1='[\w] % ' +fi + +test -s ~/.alias && . ~/.alias diff --git a/dotrc/_gvimrc b/dotrc/_gvimrc new file mode 100644 index 0000000..dace2c8 --- /dev/null +++ b/dotrc/_gvimrc @@ -0,0 +1,22 @@ +set ch=2 + +set guioptions-=T +set hlsearch +set mousehide + +set mouse= + +highlight Normal guibg=black guifg=white +highlight Search guibg=grey20 guifg=white +highlight Cursor guibg=white guifg=NONE +highlight NonText guibg=black +highlight Constant gui=NONE guibg=black +highlight Special gui=NONE guibg=black +set background=dark + +"set guifont=-adobe-courier-medium-r-normal-*-*-120-*-*-m-*-iso8859-9 +"set guifont=-adobe-courier-medium-r-normal-*-*-140-*-*-m-*-iso10646-1 +"set guifont="-windows-proggysquare-medium-r-normal--11-80-96-96-c-70-iso8859-1" +set guifont=fixed + +syntax on diff --git a/dotrc/_vimrc b/dotrc/_vimrc new file mode 100644 index 0000000..fcda293 --- /dev/null +++ b/dotrc/_vimrc @@ -0,0 +1,82 @@ +version 4.0 +set compatible +set cpo-=< +set sw=4 +" set expandtab +set ai +set report=2 +set more +set history=50 +set visualbell +set noshowmatch +set modeline + +:if &term =~ "linux" +: syntax on +:endif + +set background=dark +syntax on + +set wildchar=^I +set wildmode=list,longest +set wildignore=*.o,core,*~,*.bak,*.a,*.obj +set wildmenu + +" set fileformats=unix,dos,mac + +" For quickfix +" set mp=make\ \\\|&\ egrep\ ^cc:\ \\\|&\ egrep\ error +set mp=make\ \\\|&\ egrep\ ^cc: + +" +" This should work, but doesn't... +" set errorformat=cc:\ \"%f\"\,\ line\ %l:\ error\ %n:\ %m\. + +" Map error commands +map _a :cn
+map _s :cp
+map __ :cc
+ +" Map buffer cycling +map <C-Tab> :bnext
+map! <C-Tab> :bnext
+ +" Try and set color for xterm +" +" :if &term =~ "xterm" +" : if has("terminfo") +" : set t_Co=16 +" : set t_AB=[%?%p1%{8}%<%t%p1%{40}%+%e%p1%{92}%+%;%dm +" : set t_AF=[%?%p1%{8}%<%t%p1%{30}%+%e%p1%{82}%+%;%dm +" : else +" : set t_Co=16 +" : set t_Sf=[3%dm +" : set t_Sb=[4%dm +" : endif +" :endif + + +" Autocommands for skeletons and the such +" +:if !exists("autocommands_loaded") +: let autocommands_loaded = 1 +": autocmd BufNewFile *.c 0r /devel/proformas/proforma.c +": autocmd BufNewFile *.h 0r /devel/proformas/proforma.h +": autocmd BufNewFile *.htm,*.html 0r $HOME/.skeleton/skeleton.html +": autocmd BufNewFile,BufReadPost *.htm,*.html :vmenu HTML.Bold `>a</b>`<i<b> +": autocmd BufNewFile,BufReadPost *.htm,*.html :vmenu HTML.Italic `>a</i>`<i<i> +": autocmd BufNewFile,BufReadPost *.htm,*.html :vmenu HTML.Header.1 `>a</h1>`<i<h1> +": autocmd BufNewFile,BufReadPost *.htm,*.html :vmenu HTML.Header.2 `>a</h2>`<i<h2> +": autocmd BufNewFile,BufReadPost *.htm,*.html :vmenu HTML.Header.3 `>a</h3>`<i<h3> +": autocmd BufNewFile,BufReadPost *.htm,*.html syntax on +": autocmd BufNewFile,BufReadPost *.css syntax on +": autocmd BufNewFile,BufReadPost *.h syntax on +": autocmd BufNewFile,BufReadPost *.c syntax on +": autocmd BufNewFile,BufReadPost *.cpp syntax on +": autocmd BufNewFile,BufReadPost *.sql syntax on +": autocmd BufNewFile,BufReadPost *.pc syntax on +": autocmd BufNewFile,BufReadPost *.pc set syntax=cpp +": autocmd BufNewFile,BufReadPost *.pkg syntax on +": autocmd BufNewFile,BufReadPost *.pkg set syntax=plsql +:endif @@ -0,0 +1,36 @@ +/* Report Unix errors */ +#include <stdio.h> +#include <errno.h> + +#if 0 +extern int sys_nerr; +extern char *sys_errlist[]; +#endif + +main(argc,argv) +int argc; +char *argv[]; + +{ + int f,n; + + if (argc==1) + { + fprintf(stderr,"%s:usage %s -t|err1 [.. errn]\n",argv[0],argv[0]); + exit(1); + } + + if (!strcmp(argv[1],"-t")) + for(f=0;f<sys_nerr;f++) + printf("%3d : %s\n",f,sys_errlist[f]); + else + for(f=1;f<argc;f++) + { + n=strtol(argv[f],(char **)NULL,0); + + if(n>=sys_nerr) + fprintf(stderr,"%s:errcode %d invalid!\n",argv[0],n); + else + printf("%3d : %s\n",n,sys_errlist[n]); + } +} @@ -0,0 +1,62 @@ +#include <stdlib.h> +#include <stdio.h> +#include <ctype.h> + +int main(int argc,char *argv[]) + +{ + void Dump(char *fn,FILE *fp); + FILE *fp; + int f; + + if (argc==1) + Dump("stdin",stdin); + else + for(f=1;f<argc;f++) + if ((fp=fopen(argv[f],"r"))) + { + Dump(argv[f],fp); + fclose(fp); + } + else + fprintf(stderr,"Couldn't open %s\n",argv[f]); +} + + +void Dump(char *fn,FILE *fp) +{ + char s[17]; + int p; + int f; + int b; + + printf("File:%s\n",fn); + + b=!EOF; + p=0; + + while(b!=EOF) + { + strcpy(s," "); + + printf("%6.6x: ",p); + p+=16; + + for(f=0;f<16;f++) + { + if((b!=EOF)&&((b=getc(fp))!=EOF)) + { + printf("%2.2x ",(unsigned char)b); + + if (isprint(b)) + s[f]=b; + else + s[f]='.'; + } + else + printf("** "); + } + + printf(" %s\n",s); + } +} diff --git a/int2bin.c b/int2bin.c new file mode 100644 index 0000000..78125e3 --- /dev/null +++ b/int2bin.c @@ -0,0 +1,88 @@ +#include <stdlib.h> +#include <stdio.h> +#include <ctype.h> +#include <errno.h> + +#define TRUE 1 +#define FALSE 0 + +static const int ToHex(char c) +{ + c=toupper(c); + + if (c>='0' && c<='9') + return c-'0'; + + if (c>='A' && c<='F') + return c-'A'+10; + + return 0; +} + + +int main (int argc, char *argv[]) +{ + FILE *in, *out; + char buff[1024]; + int done; + int tot; + + if (argc!=3) + { + fprintf(stderr,"%s: usage %s source dest\n",argv[0],argv[0]); + exit(EXIT_FAILURE); + } + + if (!(in=fopen(argv[1],"r"))) + { + fprintf(stderr,"Couldn't open '%s'\n",argv[1]); + exit(EXIT_FAILURE); + } + + if (!(out=fopen(argv[2],"wb"))) + { + fprintf(stderr,"Couldn't create '%s'\n",argv[2]); + exit(EXIT_FAILURE); + } + + done=0; + tot=0; + + while(!done) + { + if (!fgets(buff,sizeof buff,in)) + { + printf("Missing EOF record\n"); + done=TRUE; + } + + if (!done && buff[0]!=':') + { + printf("Invalid Intel HEX file\n"); + done=TRUE; + } + + if (!done && buff[8]=='1') + { + done=TRUE; + } + + if (!done) + { + int len; + int f; + + len=ToHex(buff[1])<<4|ToHex(buff[2]); + + for(f=0;f<len;f++) + { + int b; + + b=ToHex(buff[f*2+9])<<4|ToHex(buff[f*2+10]); + putc(b,out); + } + } + } + + return EXIT_SUCCESS; +} @@ -0,0 +1,66 @@ +/* Mangle a berkley mbox +*/ +#include <stdlib.h> +#include <stdio.h> + +void Process(FILE *fp, const char *p); + +int main(int argc, char *argv[]) +{ + int f; + + if (argc==1) + Process(stdin,"stdin"); + else + for(f=1;f<argc;f++) + Process(fopen(argv[f],"r"),argv[f]); + + return EXIT_SUCCESS; +} + + +void Quit(const char *p) +{ + perror(p); + exit(EXIT_FAILURE); +} + + +void Process(FILE *fp, const char *p) +{ + FILE *out=NULL; + char buff[1024]; + char fn[80]; + int n=1; + + printf("Processing : %s\n",p); + + if (!fp) + Quit("fopen"); + + sprintf(fn,"%s.dir",p); + + if (mkdir(fn,0777)==-1) + Quit("mkdir"); + + while(fgets(buff,sizeof buff,fp)) + { + if (strncmp(buff,"From ",5)==0) + { + if (out) + fclose(out); + + sprintf(fn,"%s.dir/%d.txt",p,n++); + + printf("Creating : %s\n",fn); + + if (!(out=fopen(fn,"w"))) + Quit("fopen"); + } + + fprintf(out,"%s",buff); + } + + fclose(out); + fclose(fp); +} @@ -0,0 +1,128 @@ +#include <stdio.h> + +#define LONG 0 +#define WORD 1 +#define BYTE 2 + +main(argc,argv) +int argc; +char *argv[]; + +{ + signed long snum; + unsigned long num; + unsigned short word; + unsigned char byte; + double dbl; + int mode=LONG; + int arg=0; + int f; + char *binary(); + char *ascii(); + + if(argc==1) + { + printf("%s: usage %s N1 [..Nn]\n",argv[0],argv[0]); + exit(-1); + } + + for(f=1;f<argc;f++) + { + num=(unsigned long)strtoul(argv[f],(char **)NULL,0); + snum=(signed long)num; + + if (num<0x100) + mode=BYTE; + else if (num<0x10000) + mode=WORD; + else + mode=LONG; + + switch(mode) + { + case LONG: + memcpy(&dbl,&num,sizeof(unsigned long)); + printf("Original : %s\n",argv[f]); + printf("Octal : %o\n",num); + printf("Uns. Dec : %u\n",num); + printf("Sgn. Dec : %ld\n",snum); + printf("Hex : %8.8X\n",num); + printf("Binary : %s\n",binary(num,32)); + printf("Ascii : %s\n",ascii(num,4)); + printf("Double : %f\n\n",dbl); + break; + case WORD: + word=(unsigned short)num; + printf("Original : %s\n",argv[f]); + printf("Octal : %o\n",word); + printf("Uns. Dec : %u\n",word); + printf("Sgn. Dec : %ld\n",(signed short)snum); + printf("Hex : %4.4X\n",word); + printf("Binary : %s\n",binary(word,16)); + printf("Ascii : %s\n\n",ascii((unsigned long)word,2)); + break; + case BYTE: + byte=(unsigned char)num; + printf("Original : %s\n",argv[f]); + printf("Octal : %o\n",byte); + printf("Uns. Dec : %u\n",byte); + printf("Sgn. Dec : %ld\n",(signed char)snum); + printf("Hex : %2.2X\n",byte); + printf("Binary : %s\n",binary(byte,8)); + printf("Ascii : %s\n\n",ascii((unsigned long)byte,1)); + break; + } + } +} + + +char *binary(i,w) +unsigned long i,w; + +{ + static char buff[1024],*p; + unsigned long c=w,b=1<<(w-1); + int first=1; + + p=buff; + + for(;c--;b=b>>1) + { + if (!((c+1)%4)&&!first) + *p++=' '; + + if(i&b) + *p++='1'; + else + *p++='0'; + + first=0; + } + + *p='\0'; + + return(buff); +} + + +char *ascii(num,l) +unsigned long num; +{ + static char s[5]; + int f; + unsigned char c; + + s[l]=0; + + for(f=0;f<l;f++) + { + c=num&0xff; + if ((c>=32)&&(c<127)) + s[(l-1)-f]=c; + else + s[(l-1)-f]='.'; + num=num>>8; + } + + return(s); +} diff --git a/sc2001.c b/sc2001.c new file mode 100644 index 0000000..a2781b1 --- /dev/null +++ b/sc2001.c @@ -0,0 +1,340 @@ +/* Sim Carol 2001 - By Bill Godfrey. +billg@bacchae.co.uk */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <limits.h> +#include <assert.h> + +typedef unsigned long term_t; /* Single term */ + +typedef enum +{ + OP_NULL, /* Terminator sentinel. */ + OP_ADD, /* max + min */ + OP_MUL, /* max * min */ + OP_SUB, /* max - min where (max!=min) */ + OP_DIV /* max / min where (max%min==0) */ +} op_t; + +#define MAX_TERMS 10 /* Yuck. */ + +typedef struct +{ + term_t t1; + term_t t2; + op_t op; +} step_t; + +typedef struct +{ + term_t unused[MAX_TERMS+1]; /* zero term array. */ + step_t steps[MAX_TERMS]; /* .op=OP_NULL term array. */ + term_t target; /* Desired target. */ + term_t result; /* Obtained target, or ULONG_MAX */ +} sol_t; + +term_t distance(term_t targ, term_t result) +{ + return (targ > result)?(targ-result):(result-targ); +} + +size_t count_unused(sol_t *s) +{ + term_t *t=s->unused; + size_t r=0; + + while (*t) + { + ++r;++t; + } + return r; +} + +size_t count_steps(sol_t *s) +{ + step_t *t=s->steps; + size_t r=0; + + while (t->op != OP_NULL) + { + ++r;++t; + } + return r; +} + +term_t do_step(step_t *s) +{ + term_t r; + + assert(s->t1 >= s->t2); + + switch (s->op) + { + case OP_ADD: + r=s->t1+s->t2; break; + case OP_MUL: + r=s->t1*s->t2; break; + case OP_SUB: + r=s->t1-s->t2; break; + case OP_DIV: + r=s->t1/s->t2; break; + default: + assert(!!"Shouldn't be here."); + } + return r; +} + +const char ops[]=" +x-/"; /* char array for operator symbols. */ + +#ifdef USE_ASCII +#define LETNUM(a) ('A'+(a)) +#else +const char letters[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; +#define LETNUM(a) (letters[a]) +#endif + +void disp_sol(sol_t *sol) +{ + step_t *step=sol->steps; + size_t n=0; + + while (step->op != OP_NULL) + { + printf("[%c] %5lu %c %5lu = %5lu\n",LETNUM(n++),step->t1,ops[step->op],step->t2,do_step(step)); + ++step; + } + printf("-- \n"); +} + +void disp_unused(char*str1,term_t *unu,char*str2) +{ + size_t n; + + printf("%s[ ",str1); + for(n=0; unu[n]; ++n) + { + printf("%lu ",unu[n]); + } + printf("]%s",str2); +} + +void evaluate_sol(sol_t *sol, sol_t *anear) +{ + term_t newdist,olddist; + + olddist=distance(anear->target,anear->result); + newdist=distance(sol->target,sol->result); + + if ((olddist==0) && (newdist==0)) /* We have already had a winner */ + { + if (count_steps(sol) < count_steps(anear)) /* Less steps? */ + { + *anear=*sol; + printf("Improved match.\n"); + disp_sol(anear); + } + } + else if (newdist==0) /* First winner. */ + { + *anear=*sol; + printf("-- \nFirst match.\n"); + disp_sol(anear); + } + else if (newdist < olddist) /* A better nearest. */ + { + *anear=*sol; + } +} + +void update_unused(term_t *unused, size_t num_unused, term_t new, size_t t1, size_t t2) +{ + size_t lastt=num_unused-1; + + unused[t1]=new; /* Replace earlier term with new result. */ + unused[t2]=unused[lastt]; /* Move last in list over t2 */ + unused[lastt]=0; +} + +void go_calc(sol_t *start, sol_t *nearest) +{ + size_t t1,t2,maxt,mint; + term_t max,min; + op_t op; + size_t num_unused; + sol_t newsol; + step_t *newstep; + + num_unused=count_unused(start); + +#if 0 + disp_unused("Consider ",start->unused,"\n"); +#endif + + for (t1=0; t1<(num_unused-1); ++t1) + for (t2=t1+1; t2<num_unused; ++t2) + for (op=OP_ADD; op<=OP_DIV; ++op) + { + if(start->unused[t1] > start->unused[t2]) + { + maxt=t1; mint=t2; + } + else + { + maxt=t2; mint=t1; + } + + max=start->unused[maxt]; + min=start->unused[mint]; + + if ((op == OP_SUB) && (max==min)) + { + break; /* a-b produces a zero. Try next operator */ + } + if ((op == OP_DIV) && (max%min)) + { + break; /* a/b leaves a remainder. */ + } + + if ((op == OP_MUL) && (min==1)) + { + break; /* (a*1)=a wasted step. */ + } + + if ((op == OP_DIV) && (min==1)) + { + break; /* (a/1)=a wasted step. */ + } + + /* Now apply max op min to a copy of start */ + + newsol=*start; + newstep=newsol.steps + count_steps(&newsol); + assert(newstep->op == OP_NULL); + + newstep->t1=max; + newstep->t2=min; + newstep->op=op; /* Write new step in */ + + newstep[1].op=OP_NULL; /* Add sentinel */ + + newsol.result=do_step(newstep); /* Complete newstep */ + +#if 0 + disp_unused("From ",start->unused," "); + printf("(%lu%c%lu)=%lu ", + max,ops[op],min,newsol.result); +#endif + + update_unused(newsol.unused,num_unused,newsol.result,t1,t2); + +#if 0 + disp_unused("to ",newsol.unused,"\n"); +#endif + + evaluate_sol(&newsol,nearest); +#if 0 + printf("IN\n"); +#endif + go_calc(&newsol,nearest); +#if 0 + printf("OUT\n"); +#endif + } + +} + + +int main(int argc, char *argv[]) +{ + int a; + sol_t start={0}; + sol_t nearest={0}; + size_t num_unused=0; /* Number of actual terms. */ + int retval=EXIT_FAILURE; + int targetarg=0; /* Which argv[] points to the one after -t? */ + int state=0; /* 1=seen a -t */ + int toomanywarn=0; /* Has "too many" warning been delivered? */ + + fprintf(stderr,"Sim Carol 2001, By Bill Godfrey.\n"); + + for (a=1; a<argc; ++a) + { + if ((state==0) && (strcmp(argv[a],"-t")==0)) + { + state=1; /* Getting target. */ + } + else if ((state==0) && (num_unused < MAX_TERMS)) + { + term_t newterm=atol(argv[a]); + + if (newterm == 0) + { + fprintf(stderr,"Warning. \"%s\" is not a valid starting number. Ignoring.\n",argv[a]); + } + else + { + start.unused[num_unused++]=atol(argv[a]); + } + } + else if (state==1) + { + if (targetarg) + { + fprintf(stderr,"Warning. Already have a target. Ignoring \"-t\" \"%s\".\n",argv[a]); + } + else + { + start.target=atol(argv[a]); + targetarg=a; + } + state=0; + } + else if ((state==0) && (num_unused == MAX_TERMS) && (!toomanywarn)) + { + fprintf(stderr,"Warning. Too many starting numbers. Ignoring all after %s.\n",argv[a]); + toomanywarn=1; + } + } + + if (targetarg == 0) + { + fprintf(stderr,"Error. No target specified.\n"); + goto tidyup; + } + + if (start.target == 0) + { + fprintf(stderr,"Error. \"%s\" is not a valid target.\n",argv[targetarg]); + goto tidyup; + } + + if (num_unused <2) + { + fprintf(stderr,"Error. Not enough starting numbers.\n"); + goto tidyup; + } + + start.unused[num_unused]=0; /* terminate the arrays */ + start.steps[0].op=OP_NULL; + + nearest.result=ULONG_MAX; /* Our first attempt has a long way to go. */ + + go_calc(&start,&nearest); + + { + term_t dist=distance(nearest.target,nearest.result); + + if (dist) + { + printf("Nearest answer %lu from target.\n",dist); + disp_sol(&nearest); + } + } + + retval=EXIT_SUCCESS; + tidyup: + + return retval; +} @@ -0,0 +1,110 @@ +/* + Query remote host for TCP/IP socket +*/ +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netinet/tcp.h> +#include <netdb.h> + +#include <stdio.h> +#include <errno.h> + +char *name; + +int Connect(); + +main(argc,argv) +int argc; +char *argv[]; + +{ + int f; + + name=argv[0]; + + if (argc<2) + { + fprintf(stderr,"%s: usage %s host\n",name,name); + exit(1); + } + + setbuf(stdout,NULL); + + for(f=0;f<0x10000;f++) + { + if (argc>2) + printf("Trying %s:%d...\n",argv[1],f); + + Connect(argv[1],f); + + if ((argc==2)&&(f)&&((f%10000)==0)) + fprintf(stderr,"Tried up to %s:%d\n",argv[1],f); + } + + return(0); +} + + +char *GetLine() + +{ + static char buff[1024]; + int l; + + if (feof(stdin)) + return(NULL); + + printf("> "); + + if (!gets(buff)) + return(NULL); + + l=strlen(buff); + + if (buff[l-1]=='\n') + buff[l-1]=0; + + if (strlen(buff)) + return(buff); + else + return(GetLine()); +} + + +int Connect(n,p) +char *n; +int p; + +{ + static int init=0; + static struct hostent *remote; + static struct sockaddr_in addr; + int sock; + + if (!init) + { + if (!(remote=gethostbyname(n))) + { + fprintf(stderr,"%s: unknown host %s\n",name,n); + exit(1); + } + + bcopy(remote->h_addr,&addr.sin_addr,remote->h_length); + init=1; + } + + if ((sock=socket(AF_INET,SOCK_STREAM,0))==-1) + { + perror(name); + exit(1); + } + + addr.sin_family=AF_INET; + addr.sin_port=htons(p); + + if (connect(sock,&addr,sizeof(addr))!=-1) + printf("%s:%d\n",n,p); + + close(sock); +} @@ -0,0 +1,105 @@ +/* A simple server to attach to +*/ +#include <sys/types.h> + +#include <stdio.h> +#include <errno.h> + +#include <sys/socket.h> +#include <netinet/in.h> +#include <netinet/tcp.h> +#include <netdb.h> + +char *name; +int sock_fd; +int connect_fd; + +void Socket(short); + +main(argc,argv) +int argc; +char *argv[]; + +{ + struct sockaddr_in addr; + char buff[1024]; + int len; + int addrlen; + + name=argv[0]; + + if (argc!=2) + { + fprintf(stderr,"%s: usage %s port\n",name,name); + exit(1); + } + + Socket((short)atoi(argv[1])); + + printf("%s: socket bound\n",name); + + while(1) + { + printf("%s: accepting\n",name); + if ((connect_fd=accept(sock_fd,NULL,0))==-1) + { + perror(name); + exit(1); + } + + addrlen=sizeof(addr); + if (getsockname(connect_fd,&addr,&addrlen)!=0) + perror(name); + + printf("%s: connection ON port %d\n",name,ntohs(addr.sin_port)); + + addrlen=sizeof(addr); + if (getpeername(connect_fd,&addr,&addrlen)!=0) + perror(name); + + printf("%s: connection FROM port %d\n",name,ntohs(addr.sin_port)); + + while((len=read(connect_fd,buff,1024))>0) + { + buff[len]=0; + printf("%s: recieved '%s'\n",name,buff); + write(connect_fd,buff,len); + } + + perror(name); + + close(connect_fd); + } + + return(0); +} + + +void Socket(short p) +{ + struct sockaddr_in addr; + + if ((sock_fd=socket(AF_INET,SOCK_STREAM,0))==-1) + { + perror(name); + exit(1); + } + + /* Bind port to address + */ + addr.sin_family=AF_INET; + addr.sin_addr.s_addr=INADDR_ANY; + addr.sin_port=htons(p); + + if (bind(sock_fd,&addr,sizeof(addr))==-1) + { + perror(name); + exit(1); + } + + if (listen(sock_fd,5)==-1) + { + perror(name); + exit(1); + } +} @@ -0,0 +1,24 @@ +#include <stdlib.h> +#include <unistd.h> +#include <stdio.h> + +int main() +{ + setbuf(stdout,NULL); + printf("\nIan C\n\n-- \n"); + /*system("fortune /home/ianc/Documents/sigfortune");*/ + + switch (fork()) + { + case 0: + execlp("fortune","fortune","/home/ianc/Documents/sigfortune",(const char *)NULL); + + case -1: + break; + + default: + wait(NULL); + break; + } + +} @@ -0,0 +1,130 @@ +/* + Reader/writer to TCP/IP socket +*/ +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netinet/tcp.h> +#include <netdb.h> + +#include <stdio.h> +#include <errno.h> + +char *name; +int sock; + +char *GetLine(); +int Connect(); + +main(argc,argv) +int argc; +char *argv[]; + +{ + struct sockaddr_in addr; + int addrlen; + char *p; + char buff[1024]; + int len; + + name=argv[0]; + + if (argc<3) + { + fprintf(stderr,"%s: usage %s host port [nowrite]\n",name,name); + exit(1); + } + + Connect(argv[1],atoi(argv[2])); + + /* Test to see how to get the connected local port number + */ + addrlen=sizeof(addr); + if (getsockname(sock,&addr,&addrlen)!=0) + perror(name); + + printf("%s: bound through port %d\n",name,ntohs(addr.sin_port)); + + while((argc==4)||(p=GetLine())) + { + if ((argc!=4)&&(write(sock,p,strlen(p))==-1)) + { + perror(name); + close(sock); + exit(1); + } + + if ((len=read(sock,buff,1024))<=0) + { + perror(name); + close(sock); + exit(1); + } + buff[len]=0; + printf("%s\n",buff); + } + + close(sock); + printf("\n"); + + return(0); +} + + +char *GetLine() + +{ + static char buff[1024]; + int l; + + if (feof(stdin)) + return(NULL); + + printf("> "); + + if (!gets(buff)) + return(NULL); + + l=strlen(buff); + + if (buff[l-1]=='\n') + buff[l-1]=0; + + if (strlen(buff)) + return(buff); + else + return(GetLine()); +} + + +int Connect(n,p) +char *n; +int p; + +{ + struct hostent *remote; + struct sockaddr_in addr; + + if (!(remote=gethostbyname(n))) + { + fprintf(stderr,"%s: unknown host %s\n",name,n); + exit(1); + } + + bcopy(remote->h_addr,&addr.sin_addr,remote->h_length); + + if ((sock=socket(AF_INET,SOCK_STREAM,0))==-1) + { + perror(name); + exit(1); + } + + addr.sin_family=AF_INET; + addr.sin_port=htons(p); + + if (connect(sock,&addr,sizeof(addr))==-1) + { + perror(name); + exit(1); + } +} @@ -0,0 +1,16 @@ +#include <stdio.h> + +int main(int argc, char*argv[]) +{ + int tot=0; + char s[128]; + + gets(s); + while(!feof(stdin)) + { + tot+=atoi(s); + gets(s); + } + + printf("%d\n",tot); +} |