modernc.org/99c@v1.0.1-0.20181109153923-a9e8197063d9/examples/strace/main.c (about)

     1  #include <stdlib.h>
     2  #include <fcntl.h>
     3  #include <unistd.h>
     4  
     5  #define BUFSIZE 1<<16
     6  
     7  int main(int argc, char **argv)
     8  {
     9  	char *buf = malloc(BUFSIZE);
    10  	if (!buf) {
    11  		return 1;
    12  	}
    13  
    14  	for (int i = 1; i < argc; i++) {
    15  		int fd = open(argv[i], O_RDWR);
    16  		if (fd < 0) {
    17  			return 1;
    18  		}
    19  
    20  		ssize_t n;
    21  		while ((n = read(fd, buf, BUFSIZE)) > 0) {
    22  			write(0, buf, n);
    23  		}
    24  	}
    25  	free(buf);
    26  }