From ee2310cb394f1b91abb69373a6189e41937dd454 Mon Sep 17 00:00:00 2001 From: ubuntu201711081 <201711081@jbnu.ac.kr> Date: Sun, 6 Dec 2020 05:19:22 +0000 Subject: [PATCH] add client get input from pipe --- client.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/client.c b/client.c index 1c5b842..e223ceb 100644 --- a/client.c +++ b/client.c @@ -133,6 +133,7 @@ int recvFile(int sock, const char * filename,size_t file_size){ DisplayProgressBar100Percent(file_size); END: free(buf); + close(fd); return return_value; } @@ -198,6 +199,9 @@ static inline struct timespec timespec_sub(struct timespec a,struct timespec b){ return ret; } +static char filename_buf[1024]; +static bool stdinisatty; + int main(int argc, const char *argv[]){ struct sockaddr_in addr; const char * filename; @@ -207,15 +211,15 @@ int main(int argc, const char *argv[]){ int sock, err; int retval = 0; init_bench_data(); + stdinisatty = isatty(STDIN_FILENO); - - if (argc < 4){ + if (argc < (stdinisatty ? 4 : 3)){ fprintf(stderr,"USAUE: %s SERVERNAME PORT [Option]... [FILENAME]...\n",argv[0]); return 1; } server_name = argv[1]; server_port = atoi(argv[2]); - for(;;){ + while(arg_filename_start < argc){ if (strcmp("-b",argv[arg_filename_start])==0 ||strcmp("--benchmark",argv[arg_filename_start])==0){ arg_filename_start++; @@ -227,7 +231,6 @@ int main(int argc, const char *argv[]){ } else break; } - if (server_port == 0){ fprintf(stderr,"port invalid\n"); return 1; @@ -251,8 +254,19 @@ int main(int argc, const char *argv[]){ if (bench.benchmode){ clock_gettime(bench.clock_id,&bench.begin); } - while (arg_filename_start < argc){ - filename = argv[arg_filename_start++]; + for (;;){ + if (stdinisatty){ + if (arg_filename_start >= argc) break; + filename = argv[arg_filename_start++]; + } + else{ + //unsafe. + int t = fscanf(stdin,"%s",filename_buf); + if (t != 1) break; + filename = filename_buf; + } + fprintf(stdout,"request %s\n",filename); + sock = socket(AF_INET,SOCK_STREAM,0); if(sock < 0){ perror("sock create fail");