Embed Notice
HTML Code
Corresponding Notice
- Embed this notice
翠星石 (suiseiseki@freesoftwareextremist.com)'s status on Friday, 24-Nov-2023 22:24:15 JST翠星石 @kirby Dumping a file to stdout takes 5 lines;
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/sendfile.h>
int main()
{
/* open file */
int fp = open("main.c", O_RDONLY);
/* get file stats */
struct stat s;
fstat(fp, &s);
/* write file directly to stdout, with length from stat */
sendfile(fileno(stdout), fp, NULL, s.st_size);
close(fp);
}