xref: /openbsd-src/regress/usr.bin/xargs/xargs-copy.sh (revision c5eacb84f932f8ee2f4590cefa5d7cc99f2ea83e)
1#!/bin/sh
2#
3# $OpenBSD: xargs-copy.sh,v 1.1 2017/10/16 13:52:50 anton Exp $
4#
5# Copyright (c) 2017 Anton Lindqvist <anton@openbsd.org>
6#
7# Permission to use, copy, modify, and distribute this software for any
8# purpose with or without fee is hereby granted, provided that the above
9# copyright notice and this permission notice appear in all copies.
10#
11# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
19# Regression caused by usage of memcpy() instead of memmove().
20# Including the second argument causes the arguments to exceed the size
21# constraints.
22# It's therefore copied and later used as the first argument in the second
23# invocation of the default utility.
24
25arg_max() {
26	printf '#include <limits.h>\nARG_MAX\n' | cc -E - | tail -1 | bc
27}
28
29XARGS=${1:-/usr/bin/xargs}
30
31# ARG_MAX - 4K - strlen("/bin/echo") - strlen("x\n")
32n=$(($(arg_max) - 4*1024 - 9 - 2))
33(echo x; jot -b x -s '' $n) | env -i $XARGS >/dev/null
34