Index: head/.arclint =================================================================== --- head/.arclint (revision 326553) +++ head/.arclint (revision 326554) @@ -1,25 +1,26 @@ { "exclude": "(contrib|crypto)", "linters": { "python": { "type": "pep8", "include": "(\\.py$)" }, "spelling": { "type": "spelling" }, "chmod": { - "type": "chmod" + "type": "chmod", + "exclude": "(/tests/)" }, "merge-conflict": { "type": "merge-conflict" }, "filename": { "type": "filename" }, "json": { "type": "json", "include": "(\\.arclint|\\.json$)" } } } Index: head/usr.bin/sponge/Makefile =================================================================== --- head/usr.bin/sponge/Makefile (nonexistent) +++ head/usr.bin/sponge/Makefile (revision 326554) @@ -0,0 +1,8 @@ +# $FreeBSD$ + +PROG= sponge + +HAS_TESTS= +SUBDIR.${MK_TESTS}+= tests + +.include Property changes on: head/usr.bin/sponge/Makefile ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/usr.bin/sponge/sponge.1 =================================================================== --- head/usr.bin/sponge/sponge.1 (nonexistent) +++ head/usr.bin/sponge/sponge.1 (revision 326554) @@ -0,0 +1,75 @@ +.\" Eitan Adler. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd November 1, 2017 +.Dt SPONGE 1 +.Os +.Sh NAME +.Nm sponge +.Nd buffer stdin and write to stdout +.Sh SYNOPSIS +.Nm +.Op Fl a +.Ar filename +.Sh DESCRIPTION +The +.Nm +utility reads standard in until complete, then opens +the output file and writes to it. +This makes it useful in pipelines that read a file and then write to it. +These options are available: +.Bl -tag -width indent +.It Fl a +Open +.Ar filename +in append mode. +.El +.Pp +If an attempt to allocate memory fails, +.Nm +fails without output. +The file is written even if earlier components +of the pipeline failed. +.Sh SEE ALSO +.Xr builtin 1 , +.Xr csh 1 , +.Xr getrusage 2 , +.Xr tee 1 , +.Xr wait 2 +.Sh EXIT STATUS +.Ex -std +.Sh EXAMPLES +A +.Pa file +can be be sorted "in place" by executing +.Cm sort file | sponge file +.Sh HISTORY +The +.Nm +utility was written by +.An Eitan Adler Aq Mt eadler@FreeBSD.org +and first appeared +in +.Fx 12.0 . Property changes on: head/usr.bin/sponge/sponge.1 ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/usr.bin/sponge/sponge.c =================================================================== --- head/usr.bin/sponge/sponge.c (nonexistent) +++ head/usr.bin/sponge/sponge.c (revision 326554) @@ -0,0 +1,189 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2017 Eitan Adler + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEFAULT_BUF_SIZE 16384 +#define DEFAULT_BUF_CNT 12 + +static int flag_append = 0; + +static void usage(void); +static void *safe_malloc(size_t size); +static void *safe_calloc(size_t count, size_t size); +static void *safe_reallocf(void *ptr, size_t size); + +static void * +safe_malloc(size_t size) +{ + void *ret; + + ret = malloc(size); + if (ret == NULL) { + err(1, "malloc failed"); + } + return (ret); +} + +static void * +safe_calloc(size_t count, size_t size) +{ + void *ret; + + ret = calloc(count, size); + if (ret == NULL) { + err(1, "calloc failed"); + } + return (ret); +} + +static void * +safe_reallocf(void *ptr, size_t size) +{ + void *ret; + + ret = reallocf(ptr, size); + if (ret == NULL) { + err(1, "reallocf failed"); + } + return (ret); +} + +static void +usage(void) +{ + fprintf(stderr, "usage: sponge [-a] filename\n"); +} + +int +main(int argc, char* argv[]) +{ + struct iovec *iov; + char *buf; + char *outfile; + ssize_t i; + size_t bufcnt; + size_t whichbuf; + size_t bufremain; + long maxiovec; + int fd; + int openflags = O_WRONLY; + int opt; + + while ((opt = getopt(argc, argv, "ah")) != -1) { + switch (opt) { + case 'a': + flag_append = 1; + break; + case 'h': + usage(); + exit(0); + case '?': + default: + usage(); + exit(1); + } + } + + if (optind < argc) { + outfile = argv[optind]; + } + + + bufcnt = DEFAULT_BUF_CNT; + whichbuf = 0; + iov = safe_calloc(bufcnt, sizeof(*iov)); + + for (;;) { + buf = safe_malloc(DEFAULT_BUF_SIZE); + i = read(STDIN_FILENO, buf, DEFAULT_BUF_SIZE); + if (whichbuf == bufcnt) { + bufcnt *= 2; + iov = safe_reallocf(iov, bufcnt * sizeof(*iov)); + } + if (i < 0) { + err(1, "read failed"); + } + if (i == 0) { + free(buf); + break; + } + iov[whichbuf].iov_base = buf; + iov[whichbuf].iov_len = i; + whichbuf++; + } + + if (outfile) { + if (flag_append) { + openflags |= O_APPEND; + } else { + openflags |= O_TRUNC; + } + fd = open(outfile, openflags); + } + else { + fd = STDOUT_FILENO; + } + + if (fd < 0) { + err(1, "failed to open"); + } + + maxiovec = sysconf(_SC_IOV_MAX); + if (maxiovec == -1) { + maxiovec = _XOPEN_IOV_MAX; + } + bufcnt = whichbuf; + bufremain = bufcnt; + + while (bufremain > 0) { + whichbuf = (bufremain < maxiovec) ? bufremain : maxiovec; + bufremain -= whichbuf; + + i = writev(fd, iov, whichbuf); + if (i < 0) { + err(1, "failed to write"); + } + } + + if (outfile) { + i = close(fd); + if (i < 0) { + err(1, "failed to close"); + } + } +} Property changes on: head/usr.bin/sponge/sponge.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/usr.bin/sponge/tests/Makefile =================================================================== --- head/usr.bin/sponge/tests/Makefile (nonexistent) +++ head/usr.bin/sponge/tests/Makefile (revision 326554) @@ -0,0 +1,9 @@ +# $FreeBSD$ + +PACKAGE= ${TESTBASE}/usr.bin/sponge + +TESTSDIR= ${TESTSBASE}/usr.bin/sponge/tests + +ATF_TESTS_SH= cp_test + +.include Property changes on: head/usr.bin/sponge/tests/Makefile ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/usr.bin/sponge/tests/Makefile.depend =================================================================== --- head/usr.bin/sponge/tests/Makefile.depend (nonexistent) +++ head/usr.bin/sponge/tests/Makefile.depend (revision 326554) @@ -0,0 +1,18 @@ +# $FreeBSD$ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + gnu/lib/csu \ + include \ + include/xlocale \ + lib/${CSU_DIR} \ + lib/atf/libatf-c \ + lib/libc \ + lib/libcompiler_rt \ + + +.include + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif Property changes on: head/usr.bin/sponge/tests/Makefile.depend ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/usr.bin/sponge/tests/sponge_test.sh =================================================================== --- head/usr.bin/sponge/tests/sponge_test.sh (nonexistent) +++ head/usr.bin/sponge/tests/sponge_test.sh (revision 326554) @@ -0,0 +1,48 @@ +# $FreeBSD$ +# +# SPDX-License-Identifier: BSD-2-Clause +# +# Copyright 2017 Eitan Adler +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +atf_test_case simple +simple_body() { + printf 'd\ne\na\n' >| x + printf 'a\nd\ne\n' >| y + sort x | sponge x + + atf_check -s exit:0 -o empty -e empty 'sort x | sponge x' + + if ! cmp -s x y; then + echo "x and y differ, but they should be equal" + diff -u x y + atf_fail "Original and copy do not match" + fi + +} + +atf_init_test_cases() { + atf_add_test_case simple +} Property changes on: head/usr.bin/sponge/tests/sponge_test.sh ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: head/usr.bin/tee/tee.1 =================================================================== --- head/usr.bin/tee/tee.1 (revision 326553) +++ head/usr.bin/tee/tee.1 (revision 326554) @@ -1,82 +1,84 @@ .\" Copyright (c) 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" the Institute of Electrical and Electronics Engineers, Inc. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)tee.1 8.1 (Berkeley) 6/6/93 .\" $FreeBSD$ .\" .Dd November 13, 2007 .Dt TEE 1 .Os .Sh NAME .Nm tee .Nd duplicate standard input .Sh SYNOPSIS .Nm .Op Fl ai .Op Ar .Sh DESCRIPTION The .Nm utility copies standard input to standard output, making a copy in zero or more files. The output is unbuffered. .Pp The following options are available: .Bl -tag -width indent .It Fl a Append the output to the files rather than overwriting them. .It Fl i Ignore the .Dv SIGINT signal. .El .Pp The following operands are available: .Bl -tag -width indent .It Ar file A pathname of an output .Ar file . .El .Pp The .Nm utility takes the default action for all signals, except in the event of the .Fl i option. +.Sh SEE ALSO +.Xr sponge 1 .Sh EXIT STATUS .Ex -std .Sh STANDARDS The .Nm utility is expected to be .St -p1003.2 compatible.