home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff284.lzh / ARPTools / src / Tee.c < prev    next >
C/C++ Source or Header  |  1989-11-27  |  2KB  |  97 lines

  1. /*
  2.           Tee - Create a T junction in a pipe.
  3.  
  4.           Original effort by Fabio Rossetti. Inspired by the tee program
  5.           by Gary Brant found on <>< 179.
  6.  
  7.       (c) 1989 by Fabio Rossetti
  8.  
  9.       To compile under Lattice C v5.0x use:
  10.  
  11.         lc -O -v -cus tee
  12.         blink lib:cres.o tee.o to tee lib lib:a.lib lib:lc.lib sd nd
  13.  
  14. */
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <exec/libraries.h>
  19.  
  20. #include <libraries/dos.h>
  21. #include <libraries/dosextens.h>
  22. #include <libraries/arpbase.h>
  23.  
  24. #include <arpfunctions.h>
  25.  
  26. #include <proto/exec.h>
  27. #include <proto/dos.h>
  28.  
  29. #define BFSIZE 256
  30.  
  31. struct ArpBase *ArpBase;
  32. BPTR Stdout,Stdin,fh;
  33. struct Process *Pr;
  34. TEXT *Buf;        
  35.  
  36. /* Trick to keep code down to size */
  37. VOID MemCleanup()
  38. {
  39. }
  40.  
  41. /* as above */
  42. VOID _main(Line)
  43. STRPTR Line;
  44.  
  45. {
  46.     
  47.     STRPTR argv;
  48.     REGISTER ULONG count;
  49.  
  50.     Pr = (struct Process*)FindTask(NULL);
  51.     
  52.     if(!(ArpBase = (struct ArpBase*)OpenLibrary(ArpName,ArpVersion))){
  53.             Pr->pr_Result2=ERROR_INVALID_RESIDENT_LIBRARY;
  54.             exit(RETURN_FAIL);
  55.             }
  56.     
  57.     /* parse command line */
  58.  
  59.         argv =  NULL;
  60.  
  61.     while(*Line > ' ')
  62.         ++Line;
  63.  
  64.     /* don't need argc */
  65.     if (GADS(++Line,
  66.         strlen(Line),
  67.         "Usage: Tee <Stdin >Stdout Filename",
  68.         &argv,
  69.         "FILE/A") <= 0) {
  70.             Puts(argv);
  71.             CloseLibrary((struct Library *)ArpBase);
  72.             exit(RETURN_WARN);
  73.             }
  74.  
  75.     Stdout = Output();
  76.     Stdin = Input();
  77.  
  78.     if (!(Buf=(ArpAlloc(BFSIZE)))) {;
  79.             Puts ("Error: No memory");
  80.             Pr->pr_Result2 = ERROR_NO_FREE_STORE;
  81.             exit(RETURN_FAIL);
  82.             }
  83.     if (!(fh  = ArpOpen(argv,MODE_NEWFILE))) {
  84.             Puts ("Error: Can't open file.");
  85.             Pr->pr_Result2 = ERROR_OBJECT_NOT_FOUND;
  86.             exit(RETURN_ERROR);
  87.             }
  88.     /* Now copy streams */    
  89.     while (count = Read(Stdin,Buf,BFSIZE)){
  90.     Write(Stdout,Buf,count);
  91.     Write(fh,Buf,count);
  92.     }
  93.  
  94.     CloseLibrary((struct Library *)ArpBase);
  95.  
  96. }
  97.