home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / c / csh4.zip / Y.C < prev   
C/C++ Source or Header  |  1985-09-04  |  2KB  |  118 lines

  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <setjmp.h>
  4.  
  5. /* y and t
  6.  * y reads the standard input to standard output, then invokes cat
  7.  * to put one or more files to standard output
  8.  * tee copies standard input to output and puts a copy
  9.  * into a file specified on the command line
  10.  */
  11.  
  12. void (*signal())();
  13. void (*teesig)();
  14. jmp_buf y_env;
  15. static FILE *in,*out;
  16.  
  17. FILE *fopen(),*fdopen();
  18.  
  19. void y_intr()
  20. {
  21.     signal(SIGINT,teesig);
  22.     longjmp(y_env,-1);
  23. }
  24.  
  25. y(argc,argv)
  26.     int argc;
  27.     char *argv[];
  28. {
  29.     register int c;
  30.  
  31.     /* handle interrupts */
  32.     if (-1 == setjmp(y_env))
  33.     {
  34.         static char *intmsg = "Interrupted\r\n";
  35.         write(2,intmsg,strlen(intmsg));
  36.         fclose(in);
  37.         fclose(out);
  38.         return -1;
  39.     }
  40.  
  41.     /* set signal catcher */
  42.     teesig = signal(SIGINT,y_intr);
  43.  
  44.     if (NULL == (in = fdopen(0,"r")))
  45.     {
  46.         fprintf(stderr,"can't open stdin\n");
  47.     };
  48.     if (NULL == (out = fdopen(1,"w")))
  49.     {
  50.         fprintf(stderr,"can't open stdout\n");
  51.     };
  52.  
  53.     while(EOF != (c = agetc(in)))
  54.         aputc(c,out);
  55.     if (argc > 1)
  56.         cat(argc,argv);
  57.     fclose(in);
  58.     fclose(out);
  59.     signal(SIGINT,teesig);
  60.     return 0;
  61. }
  62.  
  63. jmp_buf t_env;
  64.  
  65. void t_intr()
  66. {
  67.     signal(SIGINT,teesig);
  68.     longjmp(t_env,-1);
  69. }
  70.  
  71. t(argc,argv)
  72.     int argc;
  73.     char *argv[];
  74. {
  75.     register int c;
  76.     register FILE *tfile;
  77.     FILE *fopen();
  78.  
  79.     /* handle interrupts */
  80.     if (-1 == setjmp(t_env))
  81.     {
  82.         static char *intmsg = "Interrupted\r\n";
  83.         write(2,intmsg,strlen(intmsg));
  84.         fclose (tfile);
  85.         fclose(in);
  86.         fclose(out);
  87.         return -1;
  88.     }
  89.  
  90.     /* set signal catcher */
  91.     teesig = signal(SIGINT,t_intr);
  92.  
  93.     if (NULL == (tfile = fopen(*(++argv),"w")))
  94.     {
  95.         fprintf(stderr,"can't open %s\n",*argv);
  96.     };
  97.     if (NULL == (in = fdopen(0,"r")))
  98.     {
  99.         fprintf(stderr,"can't open stdin\n");
  100.     };
  101.     if (NULL == (out = fdopen(1,"w")))
  102.     {
  103.         fprintf(stderr,"can't open stdout\n");
  104.     };
  105.  
  106.     while(EOF != (c = agetc(in)))
  107.     {
  108.         aputc(c,out);
  109.         if (tfile)
  110.             aputc(c,tfile);
  111.     }
  112.     fclose(in);
  113.     fclose(out);
  114.     fclose(tfile);
  115.     signal(SIGINT,teesig);
  116.     return 0;
  117. }
  118.