[轉] Simulate the Linux command tee in C

出處:http://stackoverflow.com/questions/12398947/simulate-the-linux-command-tee-in-c

I have to do the simulation of the command tee in C for Linux. How does tee work internally? It looks like a T-shaped pipe, so should I use a pipe? Is there a special kind of pipe?
share|improve this question
‘man tee’ will tell you all about the command. Tee allows you to split the stdout of a one program into a file and to stdout. – James Sep 13 ’12 at 3:20
@wich Please stop adding the homework tag to questions. See meta.stackoverflow.com/questions/147100/… – meagar Sep 14 ’12 at 18:00

add comment (requires an account with 50 reputation)

tee takes stdin and copies the data stream to stdout as well as a file given as an option, it can be used in many very different situations.
An implementation in C is quite simple, just make a program that copies all data from stdin to stdout, but also use the same output statements for stdout on a file that you opened based on the command line argument.
basically in pseudo code:
file f = open(argv[1])
while (! end of file stdin) {
  buffer = read stdin
  write stdout buffer
  write f buffer
}
close(f)
Note that you don’t really have to do anything with pipes, your shell will sort out the pipes, the program only has to copy data from one stream to two others.
share|improve this answer
Ok, I have this: #include <stdio.h> main(int agrc, char **argv[]){ FILE *fp; FILE *fp1; fp = fopen(*argv[1], “r”); fp1 = fopen(*argv[2], “w”); char buffer[100]; while (feof(fp) == 0){ fscanf(fp, “%s”, buffer); printf(“%s”, buffer); fprintf(fp1, buffer); } fclose(fp); fclose(fp1); } I execute ./a.out hello.txt hello2.txt But… segmentation fault. What is wrong here? By the way, it compiles correctly. Thank you. – Ecle Beckett Sep 13 ’12 at 4:08
It can be one of several reasons. One, you allocate a buffer with limited size, if any line is longer than that size you will access memory outside the buffer space. My suggestion is to not use fscanf and printf but use functions that just read and write a sequence of bytes instead. Two is that you do not do any error checking, if the fopen fails it will return NULL, the other functions will try to access that NULL pointer. – wich Sep 13 ’12 at 5:22

add comment (requires an account with 50 reputation)

I finished the program! Thank you so much 🙂
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

main(int argc, char *argv[]){

FILE *fp, *fp1;
char buffer;

if(argc != 4){
    printf("nError");
    printf("nSintaxis: tee [archivo1] [archivo2]n");
    exit(0);
}

if(strcmp(argv[1], "tee") == 0){
    fp = fopen(argv[2], "r");
    fp1 = fopen(argv[3], "w");

    printf("Content in %s:n", argv[2]);

    while(!feof(fp)){
        buffer = fgetc(fp);
        fputc(buffer, fp1);
        printf("%c", buffer);
    }

    printf("nn%s received %sn", argv[3], argv[2]);   

    fclose(fp);
    fclose(fp1);
    }
    else
        printf("nThe first argument have to be teen");
}
share|improve this answer
add comment (requires an account with 50 reputation)
未經允許不得轉載:GoMCU » [轉] Simulate the Linux command tee in C