diff options
| author | Tristan Riehs <tristan.riehs@bordeaux-inp.fr> | 2023-10-20 08:11:34 +0200 | 
|---|---|---|
| committer | Tristan Riehs <tristan.riehs@bordeaux-inp.fr> | 2023-10-20 08:11:34 +0200 | 
| commit | 75c555bada280559253975349c192f956ab35674 (patch) | |
| tree | d15ba2a4c5535362f7a2caba150ceb08fca6fe09 | |
| parent | fb811629ea6bf1837c2ae4de2ffa3f3207b28c75 (diff) | |
piping supported
| -rw-r--r-- | rpt.c | 22 | 
1 files changed, 12 insertions, 10 deletions
| @@ -14,8 +14,12 @@  /* Size of the buffer used to clone stdin. */  #define BUFSIZE 8000 +/* Clone of stdin.  Used to perform lseek(2) calls between each child +   process. */ +static FILE *stdin_clone; +  /* File descriptor associated to the clone of stdin. */ -int clone_fd; +static int clone_fd;  void  print_usage(FILE *output) @@ -63,7 +67,7 @@ exec_child(char **argv)  {  	int status; -	status = lseek(STDIN_FILENO, 0, L_SET); +	status = lseek(clone_fd, 0, L_SET);  	if (status < 0)  	{ @@ -71,7 +75,7 @@ exec_child(char **argv)  		exit(4);  	} -	status = dup2(dup(clone_fd), STDIN_FILENO); +	status = dup2(clone_fd, STDIN_FILENO);  	if (status < 0)  	{ @@ -131,17 +135,16 @@ copy_stdin()  {  	ssize_t nbytes;  	char buf[BUFSIZE]; -	FILE *tmp_stream; -	tmp_stream = tmpfile(); +	stdin_clone = tmpfile(); /* closed at the end of the main */ -	if (!tmp_stream) +	if (!stdin_clone)  	{  		perror("rpt");  		exit(1);  	} -	clone_fd = fileno(tmp_stream); +	clone_fd = fileno(stdin_clone);  	errno = 0;  	while ((nbytes = read(STDIN_FILENO, buf, BUFSIZE)) > 0) @@ -150,10 +153,9 @@ copy_stdin()  			break;  	} -	fclose(tmp_stream); -  	if (errno)  	{ +		fclose(stdin_clone);  		perror("rpt");  		exit(1);  	} @@ -226,6 +228,6 @@ main(int argc, char* argv[])  	copy_stdin();  	repeat_cmd(argv + optind, count, handle_error_f); -	close(clone_fd); +	fclose(stdin_clone);  	return 0;  } | 
