echo.c revision 256281
1143206Sdas/*-
2143206Sdas * Copyright (c) 1989, 1993
3143206Sdas *	The Regents of the University of California.  All rights reserved.
4143206Sdas *
5143206Sdas * Redistribution and use in source and binary forms, with or without
6143206Sdas * modification, are permitted provided that the following conditions
7143206Sdas * are met:
8143206Sdas * 1. Redistributions of source code must retain the above copyright
9143206Sdas *    notice, this list of conditions and the following disclaimer.
10143206Sdas * 2. Redistributions in binary form must reproduce the above copyright
11143206Sdas *    notice, this list of conditions and the following disclaimer in the
12143206Sdas *    documentation and/or other materials provided with the distribution.
13143206Sdas * 4. Neither the name of the University nor the names of its contributors
14143206Sdas *    may be used to endorse or promote products derived from this software
15143206Sdas *    without specific prior written permission.
16143206Sdas *
17143206Sdas * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18143206Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19143206Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20143206Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21143206Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22143206Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23143206Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24143206Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25143206Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26143206Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27143206Sdas * SUCH DAMAGE.
28143206Sdas */
29143206Sdas
30143206Sdas#if 0
31143206Sdas#ifndef lint
32143206Sdasstatic char const copyright[] =
33143206Sdas"@(#) Copyright (c) 1989, 1993\n\
34143206Sdas	The Regents of the University of California.  All rights reserved.\n";
35143206Sdas#endif /* not lint */
36143206Sdas
37143206Sdas#ifndef lint
38143206Sdasstatic char sccsid[] = "@(#)echo.c	8.1 (Berkeley) 5/31/93";
39143206Sdas#endif /* not lint */
40143206Sdas#endif
41143206Sdas#include <sys/cdefs.h>
42143206Sdas__FBSDID("$FreeBSD: stable/10/bin/echo/echo.c 181269 2008-08-04 01:25:48Z cperciva $");
43143206Sdas
44143206Sdas#include <sys/types.h>
45143206Sdas#include <sys/uio.h>
46143206Sdas
47143206Sdas#include <assert.h>
48143206Sdas#include <errno.h>
49143206Sdas#include <limits.h>
50143206Sdas#include <stdlib.h>
51143206Sdas#include <string.h>
52143206Sdas#include <unistd.h>
53143206Sdas
54143206Sdas/*
55143206Sdas * Report an error and exit.
56143206Sdas * Use it instead of err(3) to avoid linking-in stdio.
57143206Sdas */
58143206Sdasstatic __dead2 void
59143206Sdaserrexit(const char *prog, const char *reason)
60143206Sdas{
61143206Sdas	char *errstr = strerror(errno);
62143206Sdas	write(STDERR_FILENO, prog, strlen(prog));
63143206Sdas	write(STDERR_FILENO, ": ", 2);
64143206Sdas	write(STDERR_FILENO, reason, strlen(reason));
65143206Sdas	write(STDERR_FILENO, ": ", 2);
66143206Sdas	write(STDERR_FILENO, errstr, strlen(errstr));
67143206Sdas	write(STDERR_FILENO, "\n", 1);
68143206Sdas	exit(1);
69143206Sdas}
70143206Sdas
71143206Sdasint
72main(int argc, char *argv[])
73{
74	int nflag;	/* if not set, output a trailing newline. */
75	int veclen;	/* number of writev arguments. */
76	struct iovec *iov, *vp; /* Elements to write, current element. */
77	char space[] = " ";
78	char newline[] = "\n";
79	char *progname = argv[0];
80
81	/* This utility may NOT do getopt(3) option parsing. */
82	if (*++argv && !strcmp(*argv, "-n")) {
83		++argv;
84		--argc;
85		nflag = 1;
86	} else
87		nflag = 0;
88
89	veclen = (argc >= 2) ? (argc - 2) * 2 + 1 : 0;
90
91	if ((vp = iov = malloc((veclen + 1) * sizeof(struct iovec))) == NULL)
92		errexit(progname, "malloc");
93
94	while (argv[0] != NULL) {
95		size_t len;
96
97		len = strlen(argv[0]);
98
99		/*
100		 * If the next argument is NULL then this is this
101		 * the last argument, therefore we need to check
102		 * for a trailing \c.
103		 */
104		if (argv[1] == NULL) {
105			/* is there room for a '\c' and is there one? */
106			if (len >= 2 &&
107			    argv[0][len - 2] == '\\' &&
108			    argv[0][len - 1] == 'c') {
109				/* chop it and set the no-newline flag. */
110				len -= 2;
111				nflag = 1;
112			}
113		}
114		vp->iov_base = *argv;
115		vp++->iov_len = len;
116		if (*++argv) {
117			vp->iov_base = space;
118			vp++->iov_len = 1;
119		}
120	}
121	if (!nflag) {
122		veclen++;
123		vp->iov_base = newline;
124		vp++->iov_len = 1;
125	}
126	/* assert(veclen == (vp - iov)); */
127	while (veclen) {
128		int nwrite;
129
130		nwrite = (veclen > IOV_MAX) ? IOV_MAX : veclen;
131		if (writev(STDOUT_FILENO, iov, nwrite) == -1)
132			errexit(progname, "write");
133		iov += nwrite;
134		veclen -= nwrite;
135	}
136	return 0;
137}
138