util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: Sami Kerola <kerolasa@iki.fi>
Subject: [PATCH 3/3] line: use util-linux conventions
Date: Sun,  7 Dec 2014 10:13:04 +0000	[thread overview]
Message-ID: <1417947184-24178-3-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1417947184-24178-1-git-send-email-kerolasa@iki.fi>

This change add --help and --version print outs, checking at exit the
stdout write was successful, and as a minor thing the command is made to
use wide char functions to read and write inputs.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 text-utils/line.c | 64 ++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 49 insertions(+), 15 deletions(-)

diff --git a/text-utils/line.c b/text-utils/line.c
index 636122e..80daa34 100644
--- a/text-utils/line.c
+++ b/text-utils/line.c
@@ -15,31 +15,65 @@
  * See Documentation/deprecated.txt for more information.
  */
 
-#include	<stdio.h>
-#include	<unistd.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <unistd.h>
 
-static int	status;		/* exit status */
+#include "c.h"
+#include "closestream.h"
+#include "nls.h"
+#include "widechar.h"
 
-static void
-doline(int fd)
+static void __attribute__((__noreturn__)) usage(FILE *out)
 {
-	char c;
+	fputs(USAGE_HEADER, out);
+	fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
+	fputs(USAGE_OPTIONS, out);
+	fputs(USAGE_HELP, out);
+	fputs(USAGE_VERSION, out);
+	fprintf(out, USAGE_MAN_TAIL("line(1)"));
+	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
+int main(int argc, char **argv)
+{
+	wint_t c;
+	int opt;
+	int status = EXIT_SUCCESS;
+
+	static const struct option longopts[] = {
+		{"version", no_argument, NULL, 'V'},
+		{"help", no_argument, NULL, 'h'},
+		{NULL, 0, NULL, 0}
+	};
+
+	setlocale(LC_ALL, "");
+	bindtextdomain(PACKAGE, LOCALEDIR);
+	textdomain(PACKAGE);
+	atexit(close_stdout);
+
+	while ((opt = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
+		switch (opt) {
+		case 'V':
+			printf(UTIL_LINUX_VERSION);
+			return EXIT_SUCCESS;
+		case 'h':
+			usage(stdout);
+		default:
+			usage(stderr);
+		}
 
 	for (;;) {
-		if (read(fd, &c, 1) <= 0) {
-			status = 1;
+		c = getwchar();
+		if (c == WEOF) {
+			status = EXIT_FAILURE;
 			break;
 		}
 		if (c == '\n')
 			break;
-		putchar(c);
+		putwchar(c);
 	}
-	putchar('\n');
-}
+	putwchar(L'\n');
 
-int
-main(void)
-{
-	doline(0);
 	return status;
 }
-- 
2.1.3


  parent reply	other threads:[~2014-12-07 10:13 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-07 10:13 [PATCH 1/3] login-utils: use libc error printing facility Sami Kerola
2014-12-07 10:13 ` [PATCH 2/3] textual: improve error messages Sami Kerola
2014-12-07 10:13 ` Sami Kerola [this message]
2014-12-09  8:22 ` [PATCH 1/3] login-utils: use libc error printing facility Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1417947184-24178-3-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).