#include <sys/types.h>
#include <sys/stat.h>
#include <sys/io.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <time.h>#define CTRLPORT 0x378 //(Data port)
#define STATPORT 0x379 //(Status port)
#define BIT0 0x01
#define BIT1 0x02
#define BIT2 0x04
#define BIT3 0x08
#define BIT4 0x10
//#define PERED 0xF7
int main(void) {
fd_set DSet;
timeval tv;
int i,s0,s1,ret,rport,ret2,buf3,dEtc,port;
char buf[2];
char buf2[5];
char chPort[20];
int makeserver() {
sockaddr_in saddr;
memset(&saddr,0,sizeof(sockaddr_in));
saddr.sin_family=AF_INET;
saddr.sin_port=htons(port);
saddr.sin_addr.s_addr=htonl(INADDR_ANY);
if((s0=socket(AF_INET,SOCK_STREAM,0))==-1) return -1;
if(bind(s0,(sockaddr *) &saddr,sizeof(saddr))<0) return -1;
listen(s0,1);
sockaddr_in CliAddr;
socklen_t AddrLen;
s1=accept(s0,(sockaddr *) &CliAddr, &AddrLen);
if(s1<0) return -1;
shutdown(s0,2);
close(s0);
return 0;
}
pid_t pid, sid;
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE);
}
if ((chdir("/")) < 0) {
exit(EXIT_FAILURE);
}
while (1)
{
dEtc=open("/usr/etc/radio.var",O_RDONLY|O_NDELAY);
if(dEtc==-1) {port=17601; goto l2;}
i=0;
for(;;) {
read(dEtc,&chPort[i],1);
if(chPort[i]<'0') break;
if(chPort[i]>'9') break;
if(chPort[i]==0) break;
i++;
}
chPort[i]='\0';
port=17600+atoi(chPort);
if(port<100) port=17601;
l2:
close(dEtc);
printf("\n%d\n",port);
if(ioperm(CTRLPORT,8,1)) goto l1;
if(ioperm(STATPORT,8,1)) goto l1;
printf("res: %d\n",makeserver());
printf("State: %d\n",inb(STATPORT));
/////////////
buf3=0;
outb(buf3,CTRLPORT);
/////////////
l0:for(;;) {
FD_ZERO(&DSet);
FD_SET(s1,&DSet);
tv.tv_sec=0;
tv.tv_usec=0;
ret=select(s1+1,&DSet,0,0,&tv);
if(ret<0) goto l1;
if(ret>0) {
ret2=read(s1,buf,sizeof(CTRLPORT));
if(ret2<=0) goto l1;
rport=atoi(buf);
printf("\nReceive: %d",rport);
buf3=inb(CTRLPORT);
if(rport==0) buf3=0;
if(rport==11) buf3|=BIT0;
if(rport==21) buf3|=BIT1;
if(rport==31) {buf3|=BIT2;if(buf3&BIT3) buf3-=BIT3;}
if(rport==41) buf3|=BIT4;
if(rport==10) {if(buf3&BIT0) buf3-=BIT0;}
if(rport==20) {if(buf3&BIT1) buf3-=BIT1;}
if(rport==30) {
if(buf3&BIT2) buf3-=BIT2;
buf3|=BIT3;}
if(rport==40) {if(buf3&BIT4) buf3-=BIT4;}
outb(buf3,CTRLPORT);
printf("\nControl: 0x%X ",inb(CTRLPORT));
buf[1]='\0';
}
buf2[0]=inb(STATPORT);
int buf21;
char temp[1];
sleep(0.150);
buf21=inb(STATPORT);
if (buf21==0x87) temp[0]='0';
//Rjim simpleks
if (buf21==0xA7) temp[0]='1'; //simpleks
if ((buf21==0xE7)||(buf21==0xF7)) temp[0]='2'; //+Peredat Knopka najata
if (buf21==0xEF) temp[0]='3'; //+Avariya
if (buf21==0xB7) temp[0]='4'; //Vizov
//Dupleks
if (buf21==0x07) temp[0]='5'; //Dupleks
if (buf21==0x47) temp[0]='6'; //+Peredat
if (buf21==0x4F) temp[0]='7'; //+Avariya
if (buf21==0x17) temp[0]='8'; //Vizpv
if (buf21==0x57) temp[0]='9'; //Vizov pri pered
write(s1,&temp[0],1/*sizeof(STATPORT)/2*/);
}
///Close active connections
l1:
//////////////////////
for (i=0;i<5;i++)
{
if (makeserver())
{
buf3=0;
outb(buf3,CTRLPORT);
goto l0;
break;
}
else
{
buf3=0;
outb(buf3,CTRLPORT);
sleep(1);
}
}
//////////////////////
printf("\nClient closed\n");
ioperm(CTRLPORT,8,0);
ioperm(STATPORT,8,0);
shutdown(s1,2);
shutdown(s0,2);
close(s1);
close(s0);
}
exit(EXIT_SUCCESS);
}
Целиком текст такой.