summaryrefslogtreecommitdiff
path: root/bin/echo/echo.c
blob: b5ceee1c7983c967350cafd64e4c6bcd62244284 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <string.h>  // for strlen
#include <sys/uio.h> // for iovec, writev
#include <unistd.h>  // for STDOUT_FILENO

int main(int argc, char **argv)
{
	if (argc <= 1)
		return 0;

	struct iovec iov[2 * argc - 2];
	int iovcnt = 0;

	for (int i = 1; i < argc; i++) {
		iov[iovcnt].iov_base = argv[i];
		iov[iovcnt].iov_len = strlen(argv[i]);
		iovcnt++;

		if (i < argc - 1) {
			iov[iovcnt].iov_base = " ";
			iov[iovcnt].iov_len = 1;
			iovcnt++;
		}
	}

	iov[iovcnt].iov_base = "\n";
	iov[iovcnt].iov_len = 1;
	iovcnt++;

	return writev(STDOUT_FILENO, iov, iovcnt) < 0 ? 1 : 0;
}