1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/types.h>
33#include <sys/uio.h>
34
35#include <assert.h>
36#include <capsicum_helpers.h>
37#include <err.h>
38#include <errno.h>
39#include <limits.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43
44int
45main(int argc, char *argv[])
46{
47	int nflag;	/* if not set, output a trailing newline. */
48	int veclen;	/* number of writev arguments. */
49	struct iovec *iov, *vp; /* Elements to write, current element. */
50	char space[] = " ";
51	char newline[] = "\n";
52
53	if (caph_limit_stdio() < 0 || caph_enter() < 0)
54		err(1, "capsicum");
55
56	/* This utility may NOT do getopt(3) option parsing. */
57	if (*++argv && !strcmp(*argv, "-n")) {
58		++argv;
59		--argc;
60		nflag = 1;
61	} else
62		nflag = 0;
63
64	veclen = (argc >= 2) ? (argc - 2) * 2 + 1 : 0;
65
66	if ((vp = iov = malloc((veclen + 1) * sizeof(struct iovec))) == NULL)
67		err(1, "malloc");
68
69	while (argv[0] != NULL) {
70		size_t len;
71
72		len = strlen(argv[0]);
73
74		/*
75		 * If the next argument is NULL then this is the last argument,
76		 * therefore we need to check for a trailing \c.
77		 */
78		if (argv[1] == NULL) {
79			/* is there room for a '\c' and is there one? */
80			if (len >= 2 &&
81			    argv[0][len - 2] == '\\' &&
82			    argv[0][len - 1] == 'c') {
83				/* chop it and set the no-newline flag. */
84				len -= 2;
85				nflag = 1;
86			}
87		}
88		vp->iov_base = *argv;
89		vp++->iov_len = len;
90		if (*++argv) {
91			vp->iov_base = space;
92			vp++->iov_len = 1;
93		}
94	}
95	if (!nflag) {
96		veclen++;
97		vp->iov_base = newline;
98		vp++->iov_len = 1;
99	}
100	/* assert(veclen == (vp - iov)); */
101	while (veclen) {
102		int nwrite;
103
104		nwrite = (veclen > IOV_MAX) ? IOV_MAX : veclen;
105		if (writev(STDOUT_FILENO, iov, nwrite) == -1)
106			err(1, "write");
107		iov += nwrite;
108		veclen -= nwrite;
109	}
110	return 0;
111}
112