From 954af9179665457b40453a0417ddf5b3949a0449 Mon Sep 17 00:00:00 2001 From: Ian C Date: Sun, 4 Mar 2007 18:35:36 +0000 Subject: This commit was generated by cvs2svn to compensate for changes in r2, which included commits to RCS files with non-trunk default branches. --- XTERM | 2 + asc.c | 73 +++++++++++++ bin2db.c | 60 +++++++++++ cfile.c | 55 ++++++++++ cfile8.c | 55 ++++++++++ ctime.c | 17 +++ dotrc/_alias | 92 ++++++++++++++++ dotrc/_bashrc | 6 ++ dotrc/_gvimrc | 22 ++++ dotrc/_vimrc | 82 ++++++++++++++ err.c | 36 +++++++ hex.c | 62 +++++++++++ int2bin.c | 88 +++++++++++++++ mb.c | 66 ++++++++++++ num.c | 128 ++++++++++++++++++++++ sc2001.c | 340 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ seekp.c | 110 +++++++++++++++++++ serv.c | 105 ++++++++++++++++++ sig.c | 24 +++++ sock.c | 130 ++++++++++++++++++++++ total.c | 16 +++ 21 files changed, 1569 insertions(+) create mode 100755 XTERM create mode 100644 asc.c create mode 100644 bin2db.c create mode 100644 cfile.c create mode 100644 cfile8.c create mode 100644 ctime.c create mode 100644 dotrc/_alias create mode 100644 dotrc/_bashrc create mode 100644 dotrc/_gvimrc create mode 100644 dotrc/_vimrc create mode 100644 err.c create mode 100644 hex.c create mode 100644 int2bin.c create mode 100644 mb.c create mode 100644 num.c create mode 100644 sc2001.c create mode 100644 seekp.c create mode 100644 serv.c create mode 100644 sig.c create mode 100644 sock.c create mode 100644 total.c diff --git a/XTERM b/XTERM new file mode 100755 index 0000000..6ad7989 --- /dev/null +++ b/XTERM @@ -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 & diff --git a/asc.c b/asc.c new file mode 100644 index 0000000..46f37b2 --- /dev/null +++ b/asc.c @@ -0,0 +1,73 @@ +#include + +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 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;f31) ? (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 +#include +#include + +#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); +} diff --git a/cfile.c b/cfile.c new file mode 100644 index 0000000..91b52e0 --- /dev/null +++ b/cfile.c @@ -0,0 +1,55 @@ +/* + Convert a binary file to a C u_long array +*/ +#include +#include + +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 +#include + +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); +} diff --git a/ctime.c b/ctime.c new file mode 100644 index 0000000..5ee8451 --- /dev/null +++ b/ctime.c @@ -0,0 +1,17 @@ +#include +#include +#include + +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 :bnext +map! :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` +": autocmd BufNewFile,BufReadPost *.htm,*.html :vmenu HTML.Italic `>a` +": autocmd BufNewFile,BufReadPost *.htm,*.html :vmenu HTML.Header.1 `>a` +": autocmd BufNewFile,BufReadPost *.htm,*.html :vmenu HTML.Header.2 `>a` +": autocmd BufNewFile,BufReadPost *.htm,*.html :vmenu HTML.Header.3 `>a` +": 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 diff --git a/err.c b/err.c new file mode 100644 index 0000000..2ae6008 --- /dev/null +++ b/err.c @@ -0,0 +1,36 @@ +/* Report Unix errors */ +#include +#include + +#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) + fprintf(stderr,"%s:errcode %d invalid!\n",argv[0],n); + else + printf("%3d : %s\n",n,sys_errlist[n]); + } +} diff --git a/hex.c b/hex.c new file mode 100644 index 0000000..1323756 --- /dev/null +++ b/hex.c @@ -0,0 +1,62 @@ +#include +#include +#include + +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 +#include +#include +#include + +#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 +#include + +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 + +#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>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=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 +#include +#include +#include +#include + +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; t2unused[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 +#include +#include +#include +#include + +#include +#include + +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); +} diff --git a/serv.c b/serv.c new file mode 100644 index 0000000..276404c --- /dev/null +++ b/serv.c @@ -0,0 +1,105 @@ +/* A simple server to attach to +*/ +#include + +#include +#include + +#include +#include +#include +#include + +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); + } +} diff --git a/sig.c b/sig.c new file mode 100644 index 0000000..1ab0bbb --- /dev/null +++ b/sig.c @@ -0,0 +1,24 @@ +#include +#include +#include + +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; + } + +} diff --git a/sock.c b/sock.c new file mode 100644 index 0000000..03a7c39 --- /dev/null +++ b/sock.c @@ -0,0 +1,130 @@ +/* + Reader/writer to TCP/IP socket +*/ +#include +#include +#include +#include +#include + +#include +#include + +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); + } +} diff --git a/total.c b/total.c new file mode 100644 index 0000000..bfdd7d0 --- /dev/null +++ b/total.c @@ -0,0 +1,16 @@ +#include + +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); +} -- cgit v1.2.3