~nabijaczleweli/voreutils

ref: 8252842e7ff74305e5642881658959dc00307d44 voreutils/cmd/echo.cpp -rw-r--r-- 671 bytes
8252842eнаб Add echo a month ago
                                                                                
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
31
32
33
34
35
36
37
38
39
40
41
// SPDX-License-Identifier: 0BSD


#include <cstdio>
#include <cstring>
#include <errno.h>
#include <string_view>
#include <vore-optarg>

using namespace std::literals;


int main(int, const char * const * argv) {
	const char * argv0 = argv[0];
	if(*argv)
		++argv;

	bool newline = true;
	if(*argv && *argv == "-n"sv) {
		++argv;
		newline = false;
	}

	bool first = true;
	for(const char * arg : vore::opt::args{argv}) {
		if(first)
			first = false;
		else
			std::fputc(' ', stdout);

		std::fputs(arg, stdout);
	}

	if(newline)
		std::fputc('\n', stdout);

	if(std::fflush(stdout)) {
		std::fprintf(stderr, "%s: %s\n", argv0, std::strerror(errno));
		return 1;
	}
}