The command teeis a regular Unix program, like shor sortor cat.
-, < existingInputFile > newOutputFile2, tee ( fork, , tee ). , existingInputFile, newOutputFile2. , tee, argv[0] ( tee) argv[1] ( newOutputFile) , .
, existingInputFile; tee, , tee. , newOutputFile2; ( ) tee, , tee - . , tee, , -.
tee , , .
, . , execvp, ( tee) ( newOutputFile2). , -. dup2 ?
, . tee newOutputFile < existingInputFile > newOutputFile2 tee, newOutputFile. (tee ) , , , , . , tee, , , , :
some_command arg1 arg2 | tee some_command.log | another_command its_arg1 its_arg2 > output.file
dup2() , , :
char *i_filename = "existingInputFile";
int fd = open(i_filename, O_RDONLY);
if (fd < 0)
{
fprintf(stderr, "unable to open file %s for reading (%d: %s)\n",
i_filename, errno, strerror(errno));
exit(1);
}
dup2(fd, STDIN_FILENO);
close(fd);
, fd . , . .
:
// Redirect standard input from existingInputFile
close(0);
char *i_filename = "existingInputFile";
int fd = open(i_filename, O_RDONLY);
if (fd < 0)
{
fprintf(stderr, "unable to open file %s for reading (%d: %s)\n",
i_filename, errno, strerror(errno));
exit(1);
}
assert(fd == 0);
// Redirect standard output to NewOutputFile2
close(1);
char * o_filename = "newOutputFile2";
fd = open(o_filename, O_WRONLY|O_CREAT|O_TRUNC, 0644); // Classically 0666
if (fd < 0)
{
fprintf(stderr, "unable to open file %s for writing (%d: %s)\n",
o_filename, errno, strerror(errno));
exit(1);
}
assert(fd == 1);
, open() , , 0, , open() 0 -1 ( 0 ). , , , 1 open() 1 -1 ( 1 ). , -, 2>/dev/null 2>&1 - .
, 0644 :
O_IRUSR|O_IWUSR|O_IRGRP|O_IROTH
( |O_IWGRP|O_IWOTH, (0666), umask ). , , , O_Ixyyy.