WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / splice / splice_read.c
blob46dae6a25cfb9562970421eb05d4adf5461e6767
1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <limits.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
12 int main(int argc, char *argv[])
14 int fd;
15 size_t size;
16 ssize_t spliced;
18 if (argc < 2) {
19 fprintf(stderr, "Usage: %s INPUT [BYTES]\n", argv[0]);
20 return EXIT_FAILURE;
23 fd = open(argv[1], O_RDONLY);
24 if (fd < 0) {
25 perror(argv[1]);
26 return EXIT_FAILURE;
29 if (argc == 3)
30 size = atol(argv[2]);
31 else {
32 struct stat statbuf;
34 if (fstat(fd, &statbuf) < 0) {
35 perror(argv[1]);
36 return EXIT_FAILURE;
39 if (statbuf.st_size > INT_MAX) {
40 fprintf(stderr, "%s: Too big\n", argv[1]);
41 return EXIT_FAILURE;
44 size = statbuf.st_size;
47 /* splice(2) file to stdout. */
48 spliced = splice(fd, NULL, STDOUT_FILENO, NULL,
49 size, SPLICE_F_MOVE);
50 if (spliced < 0) {
51 perror("splice");
52 return EXIT_FAILURE;
55 close(fd);
56 return EXIT_SUCCESS;