Re: Missing crucial information in splice(2) manpage

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Absee,

On Tue, Aug 27, 2024 at 06:09:33AM GMT, Absee Seeab wrote:
> I'm not sure that's a good way to construct documentation examples
> personally, but here is an example program demonstrating that my claim
> is correct, as I agree that's useful for you to have.
> Feel free to edit it as you see fit.

Thanks!

I've rewritten it to:


#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <err.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(void)
{
	int         fd;
	int         pfd[2];
	off_t       off;
	const char  s[12] = "Hello, world";

	fd = open("out", O_WRONLY | O_CREAT | O_EXCL, 0666);
	if (fd == -1)
		err(EXIT_FAILURE, "open");

	if (pipe(pfd) == -1)
		err(EXIT_FAILURE, "pipe");

	if (write(pfd[1], s, sizeof(s)) != sizeof(s))
		err(EXIT_FAILURE, "write");
	if (close(pfd[1]) == -1)
		err(EXIT_FAILURE, "close");

	off = 10;
	if (splice(pfd[0], NULL, fd, &off, sizeof(s), 0) != sizeof(s))
		err(EXIT_FAILURE, "splice");
	if (close(pfd[0]) == -1)
		err(EXIT_FAILURE, "close");

	printf("New offset is %jd\n", (intmax_t) off);

	if (close(fd) == -1)
		err(EXIT_FAILURE, "close");
	exit(EXIT_SUCCESS);
}

And used it as

	$ gcc -Wall -Wextra splice.c 
	$ ./a.out 
	New offset is 22
	$ echo $?
	0
	$ hd out 
	00000000  00 00 00 00 00 00 00 00  00 00 48 65 6c 6c 6f 2c  |..........Hello,|
	00000010  20 77 6f 72 6c 64                                 | world|
	00000016


I think this is interesting information to readers of the page.  (And we
didn't have an example, so this is better than nothing.)

I'll put it all together in a patch.

Have a lovely day!
Alex


-- 
<https://www.alejandro-colomar.es/>

Attachment: signature.asc
Description: PGP signature


[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux