diff options
Diffstat (limited to 'bin/echo')
| -rw-r--r-- | bin/echo/Makefile | 9 | ||||
| -rw-r--r-- | bin/echo/Makefile.depend | 17 | ||||
| -rw-r--r-- | bin/echo/echo.1 | 136 | ||||
| -rw-r--r-- | bin/echo/echo.c | 111 | ||||
| -rw-r--r-- | bin/echo/tests/Makefile | 3 | ||||
| -rw-r--r-- | bin/echo/tests/Makefile.depend | 10 | ||||
| -rw-r--r-- | bin/echo/tests/echo_test.sh | 58 |
7 files changed, 344 insertions, 0 deletions
diff --git a/bin/echo/Makefile b/bin/echo/Makefile new file mode 100644 index 000000000000..e730e0a44a63 --- /dev/null +++ b/bin/echo/Makefile @@ -0,0 +1,9 @@ +.include <src.opts.mk> + +PACKAGE=runtime +PROG= echo + +HAS_TESTS= +SUBDIR.${MK_TESTS}+= tests + +.include <bsd.prog.mk> diff --git a/bin/echo/Makefile.depend b/bin/echo/Makefile.depend new file mode 100644 index 000000000000..38d979787478 --- /dev/null +++ b/bin/echo/Makefile.depend @@ -0,0 +1,17 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + include \ + include/xlocale \ + lib/${CSU_DIR} \ + lib/libc \ + lib/libcapsicum \ + lib/libcasper/libcasper \ + lib/libcompiler_rt \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/bin/echo/echo.1 b/bin/echo/echo.1 new file mode 100644 index 000000000000..cb60bdcc88dd --- /dev/null +++ b/bin/echo/echo.1 @@ -0,0 +1,136 @@ +.\"- +.\" Copyright (c) 1990, 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. +.\" +.Dd October 5, 2016 +.Dt ECHO 1 +.Os +.Sh NAME +.Nm echo +.Nd write arguments to the standard output +.Sh SYNOPSIS +.Nm +.Op Fl n +.Op Ar string ... +.Sh DESCRIPTION +The +.Nm +utility writes any specified operands, separated by single blank +.Pq Ql "\ " +characters and followed by a newline +.Pq Ql \en +character, to the standard +output. +.Pp +The following option is available: +.Bl -tag -width flag +.It Fl n +Do not print the trailing newline character. +.El +.Pp +The end-of-options marker +.Fl Fl +is not recognized and written literally. +.Pp +The newline may also be suppressed by appending +.Ql \ec +to the end of the string, as is done +by iBCS2 compatible systems. +Note that the +.Fl n +option as well as the effect of +.Ql \ec +are implementation-defined in +.St -p1003.1-2001 +as amended by Cor.\& 1-2002. +For portability, +.Nm +should only be used if the first argument does not start with a hyphen +.Pq Ql "-" +and does not contain any backslashes +.Pq Ql "\e" . +If this is not sufficient, +.Xr printf 1 +should be used. +.Pp +Most shells provide a builtin +.Nm +command which tends to differ from this utility +in the treatment of options and backslashes. +Consult the +.Xr builtin 1 +manual page. +.Sh EXIT STATUS +.Ex -std +.Sh EXAMPLES +Special treatment of options and backslashes: +.Bd -literal -offset indent +$ /bin/echo "-hello\\tworld" +-hello\tworld +.Ed +.Pp +Avoid new line character: +.Bd -literal -offset indent +$ /bin/echo -n hello;/bin/echo world +helloworld +.Ed +.Pp +Or to achieve the same result: +.Bd -literal -offset indent +$ /bin/echo "hello\\c";/bin/echo world +helloworld +.Ed +.Sh SEE ALSO +.Xr builtin 1 , +.Xr csh 1 , +.Xr printf 1 , +.Xr sh 1 +.Sh STANDARDS +The +.Nm +utility conforms to +.St -p1003.1-2001 +as amended by Cor.\& 1-2002. +.Sh HISTORY +The +.Nm +command appeared in +.At v2 . +.Sh CAVEATS +The +.Nm +command behaves differently with regards to the built-in +.Nm +shell command in a number of ways including escaped characters handling. +It also differs in behavior between different systems hence complicating writing +portable scripts. +It is advised to use the +.Xr printf 1 +command to avoid these shortcomings. diff --git a/bin/echo/echo.c b/bin/echo/echo.c new file mode 100644 index 000000000000..b8f7507c416e --- /dev/null +++ b/bin/echo/echo.c @@ -0,0 +1,111 @@ +/*- + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 1989, 1993 + * The Regents of the University of California. 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. + * 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. + */ + +#include <sys/types.h> +#include <sys/uio.h> + +#include <assert.h> +#include <capsicum_helpers.h> +#include <err.h> +#include <errno.h> +#include <limits.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +int +main(int argc, char *argv[]) +{ + int nflag; /* if not set, output a trailing newline. */ + int veclen; /* number of writev arguments. */ + struct iovec *iov, *vp; /* Elements to write, current element. */ + char space[] = " "; + char newline[] = "\n"; + + if (caph_limit_stdio() < 0 || caph_enter() < 0) + err(1, "capsicum"); + + /* This utility may NOT do getopt(3) option parsing. */ + if (*++argv && !strcmp(*argv, "-n")) { + ++argv; + --argc; + nflag = 1; + } else + nflag = 0; + + veclen = (argc >= 2) ? (argc - 2) * 2 + 1 : 0; + + if ((vp = iov = malloc((veclen + 1) * sizeof(struct iovec))) == NULL) + err(1, "malloc"); + + while (argv[0] != NULL) { + size_t len; + + len = strlen(argv[0]); + + /* + * If the next argument is NULL then this is the last argument, + * therefore we need to check for a trailing \c. + */ + if (argv[1] == NULL) { + /* is there room for a '\c' and is there one? */ + if (len >= 2 && + argv[0][len - 2] == '\\' && + argv[0][len - 1] == 'c') { + /* chop it and set the no-newline flag. */ + len -= 2; + nflag = 1; + } + } + vp->iov_base = *argv; + vp++->iov_len = len; + if (*++argv) { + vp->iov_base = space; + vp++->iov_len = 1; + } + } + if (!nflag) { + veclen++; + vp->iov_base = newline; + vp++->iov_len = 1; + } + /* assert(veclen == (vp - iov)); */ + while (veclen) { + int nwrite; + + nwrite = (veclen > IOV_MAX) ? IOV_MAX : veclen; + if (writev(STDOUT_FILENO, iov, nwrite) == -1) + err(1, "write"); + iov += nwrite; + veclen -= nwrite; + } + return 0; +} diff --git a/bin/echo/tests/Makefile b/bin/echo/tests/Makefile new file mode 100644 index 000000000000..18fa46816770 --- /dev/null +++ b/bin/echo/tests/Makefile @@ -0,0 +1,3 @@ +ATF_TESTS_SH+= echo_test + +.include <bsd.test.mk> diff --git a/bin/echo/tests/Makefile.depend b/bin/echo/tests/Makefile.depend new file mode 100644 index 000000000000..11aba52f82cf --- /dev/null +++ b/bin/echo/tests/Makefile.depend @@ -0,0 +1,10 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/bin/echo/tests/echo_test.sh b/bin/echo/tests/echo_test.sh new file mode 100644 index 000000000000..ab18f11d685e --- /dev/null +++ b/bin/echo/tests/echo_test.sh @@ -0,0 +1,58 @@ +# +# Copyright 2017 Shivansh Rai +# 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 AUTHOR 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 AUTHOR 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 n_output +n_output_head() +{ + atf_set "descr" "Verify that echo(1) does not print the trailing " \ + "newline character with option '-n'" +} + +n_output_body() +{ + atf_check -s ignore -o inline:"Hello world" \ + /bin/echo -n "Hello world" +} + +atf_test_case append_c_output +append_c_output_head() +{ + atf_set "descr" "Verify that echo(1) does not print the trailing newline " \ + "character when '\c' is appended to the end of the string" +} + +append_c_output_body() +{ + atf_check -s ignore -o inline:"Hello world" \ + /bin/echo "Hello world\c" +} + +atf_init_test_cases() +{ + atf_add_test_case n_output + atf_add_test_case append_c_output +} |
