tee.c

来自「At can be given its arguments in a file.」· C语言 代码 · 共 82 行

C
82
字号
/*
* TEE.C - Copy data from within a pipe into a file.
*
* PROGRAMMER:	    Martti Ylikoski
* CREATED:	    8.12.1990
*/
static char *VERSION = "Version 1.1. Copyright (c) Martti Ylikoski, 1990, 1991. " ;
/*
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <param.h>
#include <paramstd.h>

static char *progname, *fname	;

extern unsigned long pflags ;

ParamEntry pentry[12] = {
     "P", &ParamSetPause, 0,
     "F", &ParamSetFold, 0,
     "V", &ParamSetVerbose, 0,
     "R", &ParamSetReport, 0,
     "S", &ParamSetSubDirs, 0,
     "?", &ParamSetHelp, 1,
     "H", &ParamSetHelp, 1,
     "NOD", &ParamSetNoDefault, 0,
     "TEST", &ParamSetTest, 0,
     "Y", &ParamSetYes, 0,
     "N", &ParamSetTest, 0,
     "\0", NULL, 0
} ;

ParamBlock params = {
    "/-",   IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
    pentry
} ;

int main(int argc, char *argv[])
{
FILE *fptr ;
int c ;

   progname = argv[0] ;

   ParamHandle(&params, &argc, argv) ;

   if (pflags & PA_HELP)
   {
      fprintf(stderr, "%s - copy data from within a pipe into a file.\n", progname) ;
      fputs(VERSION, stderr) ;
      fputs("\nUsage: tee file [/H | /?] \n", stderr) ;
      return( 0 ) ;
   }

   if (argc == 2)
   {
      fname = argv[1] ;

      /* open file */
      if ((fptr = fopen(fname, "w")) == NULL)
      {
	 printf("%s: unable to open file %s for writing...\n", progname, fname) ;
	 printf("Exiting...\n") ;
	 return( 1 ) ;
      }
   }

   while (( c = getc(stdin)) != EOF)
   {
      putc(c, stdout) ;
      if (argc == 2)
	 putc(c, fptr) ;
   }

   /* close file */
   if ( argc == 2)
      fclose ( fptr ) ;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?