home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / lambda / soundpot / p / ut-c.lbr / TEE.CZ / TEE.C
Encoding:
C/C++ Source or Header  |  1993-10-25  |  2.6 KB  |  105 lines

  1. /* tee.c -- UTOOL.  Copy STDIN to STDOUT and named file.
  2.  
  3.      author: David H. Wolen
  4.      last change: 1/16/83
  5.  
  6.      usage:  tee [-a] outfile
  7.  
  8.      options:  -a   append to outfile instead of copying to it
  9.  
  10.      input:  STDIN
  11.      output: STDOUT and file
  12.  
  13.      notes:    (a)  if -a and outfile doesn't exist, the
  14.                     option is ignored.
  15.                (b)  if -a, it creates and releases a temp
  16.                     file (temptee.$$$) on the logged drive.
  17.  
  18.      linkage:  a:clink tee -f dio -ca (uses deff3.crl)
  19. */
  20.  
  21. #include "a:bdscio.h"
  22. #include "dio.h"
  23.  
  24. #define  STDIN  0
  25. #define  STDOUT 1
  26.  
  27. main(argc,argv)
  28. int  argc;
  29. char *argv[];
  30. {
  31.      int  append;
  32.      char *s, ibuf[BUFSIZ], obuf[BUFSIZ];
  33.  
  34.      dioinit(&argc,argv);
  35.      append=FALSE;
  36.  
  37.      /* process options */
  38.  
  39.      while(--argc > 0 && (*++argv)[0] == '-')
  40.           for(s=argv[0]+1; *s != '\0'; s++)
  41.                switch(*s)
  42.                     {case 'A':
  43.                          append=TRUE;
  44.                          break;
  45.                     default:
  46.                          error("usage: tee [-a] outfile");
  47.                     }
  48.  
  49.      if(!append)
  50.           {if(fcreat(*argv,obuf) == ERROR)
  51.                error("tee: can't create output file");
  52.           if(tfcopy(STDIN,obuf,TRUE) == ERROR)
  53.                error("tee: file output error");
  54.           fefc(obuf);
  55.           }
  56.      else
  57.           {if(fcreat("temptee.$$$",obuf) == ERROR)
  58.                error("tee: can't create temp file");
  59.           if(fopen(*argv,ibuf) != ERROR)
  60.                if(tfcopy(ibuf,obuf,FALSE) == ERROR)
  61.                     terror("tee: file output error copying prior to append");
  62.           if(tfcopy(STDIN,obuf,TRUE) == ERROR)
  63.                terror("tee: file output error during append");
  64.           fefc(obuf);
  65.           unlink(*argv);
  66.           if(rename("temptee.$$$",*argv) == ERROR)
  67.                error("tee: output is on temptee.$$$ ; can't rename");
  68.           }
  69.  
  70.      dioflush();
  71. }
  72.  
  73.  
  74.  
  75.  
  76. /* tfcopy -- copy file fin to fout, line by line.  Returns OK or
  77.      ERROR.  If echo, output goes to both fout and STDOUT.
  78. */
  79. tfcopy(fin,fout,echo)
  80. char *fin, *fout;
  81. int  echo;
  82. {
  83.      int  c;
  84.      char line[MAXLINE];
  85.  
  86.      while(fgets(line,fin))
  87.           {if(fputs(line,fout) == ERROR)
  88.                return(ERROR);
  89.           if(echo)
  90.                fputs(line,STDOUT);
  91.           }
  92.  
  93.      return(OK);
  94. }
  95.  
  96.  
  97.  
  98. /* terror -- release temp file, then normal error */
  99. terror(msg)
  100. char *msg;
  101. {
  102.      unlink("temptee.$$$");
  103.      error(msg);
  104. }
  105.