43 lines · c
1// RUN: %clangxx -O0 %s -D_FILE_OFFSET_BITS=64 -o %t2 3// REQUIRES: glibc4 5#ifndef _GNU_SOURCE6# define _GNU_SOURCE7#endif8#include <assert.h>9#include <fcntl.h>10#include <stdlib.h>11#include <sys/syscall.h>12#include <unistd.h>13 14#if !defined(__GLIBC_PREREQ)15# define __GLIBC_PREREQ(a, b) 016#endif17 18#if !__GLIBC_PREREQ(2, 27)19# define copy_file_range(a, b, c, d, e, f) \20 (ssize_t) syscall(__NR_copy_file_range, a, b, c, d, e, f)21#endif22 23int main(void) {24 int fdin = open("/proc/self/maps", O_RDONLY);25 assert(fdin > 0);26 char tmp[] = "/tmp/map.XXXXXX";27 int fdout = mkstemp(tmp);28 assert(fdout > 0);29 off_t offin = -1, offout = 0;30 ssize_t cpy = copy_file_range(fdin, &offin, fdout, &offout, 8, 0);31 assert(cpy < 0);32 offin = 0;33 offout = 16;34 cpy = copy_file_range(fdin, &offin, fdout, &offout, 8, 0);35 assert(cpy < 0);36 offout = 0;37 cpy = copy_file_range(fdin, &offin, fdout, &offout, 8, 0);38 assert(cpy == 8);39 close(fdout);40 close(fdin);41 return 0;42}43