xref: /freebsd/bin/echo/echo.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
19ddb49cbSWarner Losh /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1989, 1993
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
84b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
94b88c807SRodney W. Grimes  * are met:
104b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
114b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
124b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
134b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
144b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
164b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
174b88c807SRodney W. Grimes  *    without specific prior written permission.
184b88c807SRodney W. Grimes  *
194b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
204b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
214b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
224b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
234b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
244b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
254b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
264b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
274b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
284b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294b88c807SRodney W. Grimes  * SUCH DAMAGE.
304b88c807SRodney W. Grimes  */
314b88c807SRodney W. Grimes 
3291b7d6dcSDiomidis Spinellis #include <sys/types.h>
3391b7d6dcSDiomidis Spinellis #include <sys/uio.h>
344b88c807SRodney W. Grimes 
3591b7d6dcSDiomidis Spinellis #include <assert.h>
36cd1693d3SConrad Meyer #include <capsicum_helpers.h>
37cd1693d3SConrad Meyer #include <err.h>
3891b7d6dcSDiomidis Spinellis #include <errno.h>
3991b7d6dcSDiomidis Spinellis #include <limits.h>
4091b7d6dcSDiomidis Spinellis #include <stdlib.h>
4191b7d6dcSDiomidis Spinellis #include <string.h>
4291b7d6dcSDiomidis Spinellis #include <unistd.h>
4391b7d6dcSDiomidis Spinellis 
444b88c807SRodney W. Grimes int
main(int argc,char * argv[])4591b7d6dcSDiomidis Spinellis main(int argc, char *argv[])
464b88c807SRodney W. Grimes {
476188858aSAlfred Perlstein 	int nflag;	/* if not set, output a trailing newline. */
4891b7d6dcSDiomidis Spinellis 	int veclen;	/* number of writev arguments. */
4991b7d6dcSDiomidis Spinellis 	struct iovec *iov, *vp; /* Elements to write, current element. */
5091b7d6dcSDiomidis Spinellis 	char space[] = " ";
5191b7d6dcSDiomidis Spinellis 	char newline[] = "\n";
524b88c807SRodney W. Grimes 
537672a014SMariusz Zaborski 	if (caph_limit_stdio() < 0 || caph_enter() < 0)
54cd1693d3SConrad Meyer 		err(1, "capsicum");
55cd1693d3SConrad Meyer 
564b88c807SRodney W. Grimes 	/* This utility may NOT do getopt(3) option parsing. */
574b88c807SRodney W. Grimes 	if (*++argv && !strcmp(*argv, "-n")) {
584b88c807SRodney W. Grimes 		++argv;
5991b7d6dcSDiomidis Spinellis 		--argc;
604b88c807SRodney W. Grimes 		nflag = 1;
6191b7d6dcSDiomidis Spinellis 	} else
624b88c807SRodney W. Grimes 		nflag = 0;
634b88c807SRodney W. Grimes 
6491b7d6dcSDiomidis Spinellis 	veclen = (argc >= 2) ? (argc - 2) * 2 + 1 : 0;
6591b7d6dcSDiomidis Spinellis 
6691b7d6dcSDiomidis Spinellis 	if ((vp = iov = malloc((veclen + 1) * sizeof(struct iovec))) == NULL)
67*4c537df5SHans Petter Selasky 		err(1, "malloc");
6891b7d6dcSDiomidis Spinellis 
696188858aSAlfred Perlstein 	while (argv[0] != NULL) {
70ef3e71daSNate Lawson 		size_t len;
71ef3e71daSNate Lawson 
72ef3e71daSNate Lawson 		len = strlen(argv[0]);
736cef43a7SJordan K. Hubbard 
746188858aSAlfred Perlstein 		/*
75a249b285SLi-Wen Hsu 		 * If the next argument is NULL then this is the last argument,
76a249b285SLi-Wen Hsu 		 * therefore we need to check for a trailing \c.
776188858aSAlfred Perlstein 		 */
786188858aSAlfred Perlstein 		if (argv[1] == NULL) {
796188858aSAlfred Perlstein 			/* is there room for a '\c' and is there one? */
806188858aSAlfred Perlstein 			if (len >= 2 &&
816188858aSAlfred Perlstein 			    argv[0][len - 2] == '\\' &&
826188858aSAlfred Perlstein 			    argv[0][len - 1] == 'c') {
836188858aSAlfred Perlstein 				/* chop it and set the no-newline flag. */
84ef3e71daSNate Lawson 				len -= 2;
856cef43a7SJordan K. Hubbard 				nflag = 1;
866cef43a7SJordan K. Hubbard 			}
876188858aSAlfred Perlstein 		}
8891b7d6dcSDiomidis Spinellis 		vp->iov_base = *argv;
8991b7d6dcSDiomidis Spinellis 		vp++->iov_len = len;
9091b7d6dcSDiomidis Spinellis 		if (*++argv) {
9191b7d6dcSDiomidis Spinellis 			vp->iov_base = space;
9291b7d6dcSDiomidis Spinellis 			vp++->iov_len = 1;
934b88c807SRodney W. Grimes 		}
9491b7d6dcSDiomidis Spinellis 	}
9591b7d6dcSDiomidis Spinellis 	if (!nflag) {
9691b7d6dcSDiomidis Spinellis 		veclen++;
9791b7d6dcSDiomidis Spinellis 		vp->iov_base = newline;
9891b7d6dcSDiomidis Spinellis 		vp++->iov_len = 1;
9991b7d6dcSDiomidis Spinellis 	}
10091b7d6dcSDiomidis Spinellis 	/* assert(veclen == (vp - iov)); */
10191b7d6dcSDiomidis Spinellis 	while (veclen) {
10291b7d6dcSDiomidis Spinellis 		int nwrite;
10391b7d6dcSDiomidis Spinellis 
10491b7d6dcSDiomidis Spinellis 		nwrite = (veclen > IOV_MAX) ? IOV_MAX : veclen;
10591b7d6dcSDiomidis Spinellis 		if (writev(STDOUT_FILENO, iov, nwrite) == -1)
106*4c537df5SHans Petter Selasky 			err(1, "write");
10791b7d6dcSDiomidis Spinellis 		iov += nwrite;
10891b7d6dcSDiomidis Spinellis 		veclen -= nwrite;
10991b7d6dcSDiomidis Spinellis 	}
1109b2010a2SMark Murray 	return 0;
1114b88c807SRodney W. Grimes }
112