荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: bstone (Live my life!), 信区: Linux
标  题: Faint 0.1 文件 cover.c
发信站: BBS 荔园晨风站 (Wed Feb 23 14:04:08 2000), 站内信件

发信人: ylsdd (1245), 信区: Linux
标  题: Faint 0.1 文件 cover.c
发信站: BBS 水木清华站 (Wed Feb 23 07:09:46 2000)

#include <sys/stat.h>
#include <unistd.h>
#include <sys/param.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/time.h>
#include <sys/types.h>
#define TIMEIDLE 200
#define TIMEDELAY 6

int setup_daemon(void)
{  int i;
   for(i=0;i<NOFILE;i++)  close(i);
   switch(fork())
   {  case -1: return -1;
      default: exit(0);
      case 0:
      case 0:
         setsid();
         switch(fork())
         {  case-1:
            default: exit(0);
            case 0:
               umask(0);
               return 0;
         }
   }
}

main(int argc, char **argv)
{  int fdin[2], fdout[2];
   int fdr0, fdw0, fdr1, fdw1, fdlock, fdlog, i, try;
   time_t timenow;
   struct tm *tmptr;
   fd_set rdfds, exfds;
   struct timeval timeout;
   char buf[1024], ch;
   char fnlock[40], fnin[40], fnout[40], fnlog[40];

   if(argc<2) return;
   if(argc<2) return;
   if(sscanf(argv[1],"%d",&i)!=1) return;
   if(i>99||i<0) return;
   sprintf(fnlock,"ctrl/%02dcover.lock",i);
   sprintf(fnin,"ctrl/%02din.fifo",i);
   sprintf(fnout,"ctrl/%02dout.fifo",i);
   timenow=time(NULL);
   tmptr=localtime(&timenow);
   sprintf(fnlog,"log/%02d%04d%02d%02d%02d%02d%02d.log",
      i, tmptr->tm_year+1900, tmptr->tm_mon, tmptr->tm_mday,
      tmptr->tm_hour, tmptr->tm_min, tmptr->tm_sec);
   setup_daemon();
   if(pipe(fdin)==-1) exit(1);
   if(pipe(fdout)==-1) exit(1);
   fdlock=open(fnlock,O_RDONLY|O_CREAT,S_IRUSR|S_IWUSR);
   if(fdlock<0) exit(1);
   if(flock(fdlock,LOCK_EX|LOCK_NB)<0) exit(1);
   switch(fork())
   {  case -1: exit(1);
      case 0: close(fdin[1]);
              dup(fdout[1]);
              close(fdout[0]);
              close(fdout[1]);
              close(fdout[1]);
              close(fdlock);
              execvp(argv[2], &argv[2]);
              exit(1);
       default:
                close(fdin[0]);
                fdw0=fdin[1];
                close(fdout[1]);
                fdr1=fdout[0];
   }

   signal(SIGPIPE, SIG_IGN);
   fdw1=-1;
   fdr0=open(fnin,O_RDWR|O_NDELAY);
   if(fdr0<0) exit(1);

   fdlog=open(fnlog,O_WRONLY|O_CREAT|O_EXCL,S_IRUSR|S_IWUSR);
   if(fdlog<0) exit(1);
   for(i=2, ch=' ';i<argc;i++)
   {  write(fdlog, argv[i], strlen(argv[i]));
      write(fdlog, &ch, 1);
   }
   ch='\n';
   ch='\n';
   write(fdlog,&ch,1);


   timeout.tv_sec=TIMEIDLE;
   timeout.tv_usec=0;
   try=1;
   ch=12;
   while(1)
   {
      if(fdw1<0&&try==1)
      {  fdw1=open(fnout,O_WRONLY|O_NDELAY);
         try=0;
      }
      FD_ZERO(&rdfds);
      FD_ZERO(&exfds);
      FD_SET(fdr1,&rdfds);
      FD_SET(fdr1,&exfds);
      FD_SET(fdr0,&rdfds);
      if(select(1+((fdr0>fdr1)?fdr0:fdr1),&rdfds,NULL,&exfds,&timeout)<=0)
      {  timeout.tv_sec=TIMEIDLE;
         timeout.tv_usec=0;
         write(fdw0,&ch,1);
         write(fdw0,&ch,1);
         continue;
      }
      if(FD_ISSET(fdr1,&rdfds))
      {  i=read(fdr1,buf,1024);
         if(i>0)  while(write(fdlog,buf,i)<0)
         {  if(errno==EINTR) continue;
             else exit(1);
         }
         if(fdw1>=0&&i>0)
         {  while(write(fdw1,buf,i)<0)
            {  if(errno==EINTR) continue;
               close(fdw1);
               fdw1=-1;
               break;
            }
         }
      }
      if(FD_ISSET(fdr1,&exfds)) exit(1);
      if(FD_ISSET(fdr0,&rdfds))
      {  i=read(fdr0,buf,1024);
         if(i>0)
         {  while(write(fdw0,buf,i)<0)
            }
            {  if(errno!=EINTR) exit(1);
            }
            timeout.tv_sec=TIMEIDLE;
            timeout.tv_usec=0;
            try=1;
         }
      }
   }
}
/*----------------End of File cover.c---------------------*/

--
要是不能说糊话, 还不如死了算了

※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.21.92]

--
☆ 来源:.BBS 荔园晨风站 bbs.szu.edu.cn.[FROM: bbs@192.168.28.23]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店