home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 1 / FFMCD01.bin / bbs / libdisks / d700t799 / disk774.lha / ExtraCmds / src / Tee.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-05  |  4.7 KB  |  196 lines

  1. /*
  2.  * Tee - pipe fitting
  3.  *
  4.  * Version 37.2 =TP= 29-Jan-92
  5.  * Compile with SAS/C 5.10 and link without startup code:
  6.  *      lc -cqfist -v -b0 -rr -O -ms Tee
  7.  *      blink Tee.o to Tee sd sc
  8.  *      protect Tee +p
  9.  *
  10.  * Copyright (c) 1989, 1992 Torsten Poulin
  11.  *
  12.  * Torsten Poulin
  13.  * Banebrinken 99, 2, lejlighed 77
  14.  * DK 2400  København NV
  15.  * DENMARK
  16.  */
  17. /* section: Essential Utilities */
  18.  
  19. /****** English:TEE *******************************************************
  20. *
  21. *   FORMAT
  22. *       TEE [[TO] <files>] [APPEND] [IGNORE]
  23. *
  24. *   TEMPLATE
  25. *       TO/M,APPEND/S,IGNORE/S
  26. *
  27. *   PURPOSE
  28. *       Tee is a pipe fitting.
  29. *
  30. *   SPECIFICATION
  31. *       Tee transcribes the default input to the default output and
  32. *       makes copies in the files specified by the TO option.
  33. *
  34. *       The APPEND switch causes the output to be appended to the
  35. *       files rather than overwriting them.
  36. *
  37. *       Ctrl-C's can be ignored by giving the IGNORE switch.
  38. *
  39. ***************************************************************************
  40. *
  41. */
  42. /****** dansk:TEE *********************************************************
  43. *
  44. *   FORMAT
  45. *       TEE [[TO] <filer>] [APPEND] [IGNORE]
  46. *
  47. *   SKABELON
  48. *       TO/M,APPEND/S,IGNORE/S
  49. *
  50. *   FORMÅL
  51. *       Tee er en rørfitting.
  52. *
  53. *   SPECIFIKATION
  54. *       Tee transskiberer sit standardinput til sit standardoutput
  55. *       og laver kopier i de filer der angives med argumentet TO.
  56. *
  57. *       Med kontakten APPEND tilføjes uddata til filerne fremfor
  58. *       at overskrive dem.
  59. *
  60. *       Ctrl-C'er kan ignoreres ved at bruge kontakten IGNORE.
  61. *
  62. ***************************************************************************
  63. *
  64. */
  65.  
  66.  
  67. #include <exec/types.h>
  68. #include <exec/memory.h>
  69. #include <dos/dos.h>
  70. #include <proto/exec.h>
  71. #include <proto/dos.h>
  72. #include <proto/intuition.h>
  73.  
  74. #define OPT_TO      0
  75. #define OPT_APPEND  1
  76. #define OPT_IGNORE  2
  77.  
  78.  
  79. struct fileList {
  80.     struct fileList *next;
  81.     BPTR file;
  82. };
  83.  
  84.  
  85. char const *version = "\0$VER: Tee 37.2 (29.1.92) ©1989,92 Torsten Poulin";
  86.  
  87. int entrypoint(void)
  88. {
  89.     struct RDArgs        *args;
  90.  
  91.     struct DosLibrary    *DOSBase;
  92.     struct Library       *SysBase;
  93.  
  94.     struct fileList *list, *p;
  95.     register UBYTE breakcheck = 0;
  96.     LONG c;    
  97.     LONG arg[3];
  98.     LONG rc = RETURN_OK;
  99.  
  100.  
  101.     SysBase = *(struct Library **) 4L;
  102.     if(!(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37L)))
  103.     {
  104.         rc = RETURN_FAIL;
  105.         goto exit1;
  106.     }
  107.  
  108.     arg[OPT_TO] = arg[OPT_APPEND] = arg[OPT_IGNORE] = 0L;
  109.     
  110.     if(args = ReadArgs("TO/M,APPEND/S,IGNORE/S", arg, NULL))
  111.     {
  112.         list = NULL;
  113.         
  114.         if(arg[OPT_TO])
  115.         {
  116.             UBYTE **name;
  117.             struct fileList *newnode;
  118.             LONG mode;
  119.             BPTR file;
  120.             
  121.             mode = arg[OPT_APPEND] ? MODE_READWRITE : MODE_NEWFILE;
  122.             
  123.             list = NULL;
  124.             name = (UBYTE **) arg[OPT_TO];
  125.             
  126.             /* Open the files */
  127.             for(; *name; name++)
  128.             {
  129.                 if(!(file = Open(*name, mode)))
  130.                 {
  131.                     BPTR console;
  132.                     if(console = Open("CONSOLE:", MODE_OLDFILE))
  133.                     {
  134.                         PutStr("Tee: can't open ");
  135.                         PutStr(*name);
  136.                         FPutC(console, '\n');
  137.                         Close(console);
  138.                     }
  139.                     continue;
  140.                 }
  141.                 if(arg[OPT_APPEND])
  142.                     Seek(file, 0L, OFFSET_END);
  143.  
  144.                 /* Store the filehandle */
  145.                 if(!(newnode=AllocMem(sizeof(struct fileList),MEMF_PUBLIC)))
  146.                 {
  147.                     PrintFault(ERROR_NO_FREE_STORE, "Tee");
  148.                     rc = RETURN_FAIL;
  149.                     goto exit2;
  150.                 }
  151.                 newnode->file = file;
  152.                 newnode->next = list;
  153.                 list = newnode;
  154.             }
  155.     }
  156.  
  157.         /* Do the copying ... */
  158.         while((c = FGetC(Input())) != -1L)
  159.         {
  160.             if(!arg[OPT_IGNORE]
  161.                && !(breakcheck -= 8)
  162.                && SetSignal(0L,0L) & SIGBREAKF_CTRL_C)
  163.             {
  164.                 rc = RETURN_WARN;
  165.                 break;
  166.             }
  167.             FPutC(Output(), c);
  168.             for(p = list; p; p = p->next)
  169.                 FPutC(p->file, c);
  170.         }
  171.  exit2:       
  172.         /* Close the files and free up memory*/
  173.         for(p = list; p; )
  174.         {
  175.             struct fileList *remember;
  176.                 
  177.             remember = p;
  178.             Close(p->file);
  179.             p = p->next;
  180.             FreeMem(remember, sizeof(struct fileList));
  181.         }
  182.     
  183.         FreeArgs(args);
  184.     }
  185.     else
  186.     {
  187.         LONG err = IoErr();
  188.         PrintFault(err, "Tee");
  189.         rc = RETURN_ERROR;
  190.     }
  191.  
  192.     CloseLibrary((struct Library *) DOSBase);
  193.  exit1:
  194.     return rc;
  195. }
  196.