]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - text-utils/ul.c
wipefs: add --lock and LOCK_BLOCK_DEVICE
[thirdparty/util-linux.git] / text-utils / ul.c
index 3fd0b6a85d4b8013ea33138451485a37bd8c0b22..39b69a5972350a26b07d1bc53e801e5bd5aff729 100644 (file)
 #include <stdio.h>
 #include <unistd.h>            /* for getopt(), isatty() */
 #include <string.h>            /* for memset(), strcpy() */
-#include <term.h>              /* for setupterm() */
 #include <stdlib.h>            /* for getenv() */
 #include <limits.h>            /* for INT_MAX */
 #include <signal.h>            /* for signal() */
 #include <errno.h>
 #include <getopt.h>
 
+#if defined(HAVE_NCURSESW_TERM_H)
+# include <ncursesw/term.h>
+#elif defined(HAVE_NCURSES_TERM_H)
+# include <ncurses/term.h>
+#elif defined(HAVE_TERM_H)
+# include <term.h>
+#endif
+
 #include "nls.h"
 #include "xalloc.h"
 #include "widechar.h"
@@ -62,15 +69,14 @@ static int put1wc(int c)
 {
        if (putwchar(c) == WEOF)
                return EOF;
-       else
-               return c;
+
+       return c;
 }
 #define putwp(s) tputs(s, STDOUT_FILENO, put1wc)
 #else
 #define putwp(s) putp(s)
 #endif
 
-static void usage(FILE *out);
 static int handle_escape(FILE * f);
 static void filter(FILE *f);
 static void flushln(void);
@@ -101,37 +107,37 @@ static void print_out(char *line);
 #define        UNDERL  010     /* Ul */
 #define        BOLD    020     /* Bold */
 
-int    must_use_uc, must_overstrike;
-char   *CURS_UP,
-       *CURS_RIGHT,
-       *CURS_LEFT,
-       *ENTER_STANDOUT,
-       *EXIT_STANDOUT,
-       *ENTER_UNDERLINE,
-       *EXIT_UNDERLINE,
-       *ENTER_DIM,
-       *ENTER_BOLD,
-       *ENTER_REVERSE,
-       *UNDER_CHAR,
-       *EXIT_ATTRIBUTES;
-
-struct CHAR    {
+static int     must_use_uc, must_overstrike;
+static char    *CURS_UP,
+               *CURS_RIGHT,
+               *CURS_LEFT,
+               *ENTER_STANDOUT,
+               *EXIT_STANDOUT,
+               *ENTER_UNDERLINE,
+               *EXIT_UNDERLINE,
+               *ENTER_DIM,
+               *ENTER_BOLD,
+               *ENTER_REVERSE,
+               *UNDER_CHAR,
+               *EXIT_ATTRIBUTES;
+
+struct CHAR {
        char    c_mode;
        wchar_t c_char;
        int     c_width;
 };
 
-struct CHAR    *obuf;
-int    obuflen;
-int    col, maxcol;
-int    mode;
-int    halfpos;
-int    upln;
-int    iflag;
+static struct  CHAR    *obuf;
+static int     obuflen;
+static int     col, maxcol;
+static int     mode;
+static int     halfpos;
+static int     upln;
+static int     iflag;
 
-static void __attribute__((__noreturn__))
-usage(FILE *out)
+static void __attribute__((__noreturn__)) usage(void)
 {
+       FILE *out = stdout;
        fputs(USAGE_HEADER, out);
        fprintf(out, _(" %s [options] [<file> ...]\n"), program_invocation_short_name);
 
@@ -142,12 +148,11 @@ usage(FILE *out)
        fputs(_(" -t, -T, --terminal TERMINAL  override the TERM environment variable\n"), out);
        fputs(_(" -i, --indicated              underlining is indicated via a separate line\n"), out);
        fputs(USAGE_SEPARATOR, out);
-       fputs(USAGE_HELP, out);
-       fputs(USAGE_VERSION, out);
+       printf(USAGE_HELP_OPTIONS(30));
 
-       fprintf(out, USAGE_MAN_TAIL("ul(1)"));
+       printf(USAGE_MAN_TAIL("ul(1)"));
 
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       exit(EXIT_SUCCESS);
 }
 
 int main(int argc, char **argv)
@@ -157,17 +162,17 @@ int main(int argc, char **argv)
        FILE *f;
 
        static const struct option longopts[] = {
-               { "terminal",   required_argument,      0, 't' },
-               { "indicated",  no_argument,            0, 'i' },
-               { "version",    no_argument,            0, 'V' },
-               { "help",       no_argument,            0, 'h' },
-               { NULL, 0, 0, 0 }
+               { "terminal",   required_argument,      NULL, 't' },
+               { "indicated",  no_argument,            NULL, 'i' },
+               { "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);
+       close_stdout_atexit();
 
        signal(SIGINT, sig_handler);
        signal(SIGTERM, sig_handler);
@@ -186,13 +191,13 @@ int main(int argc, char **argv)
                case 'i':
                        iflag = 1;
                        break;
+
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
+                       print_version(EXIT_SUCCESS);
                case 'h':
-                       usage(stdout);
+                       usage();
                default:
-                       usage(stderr);
+                       errtryhelp(EXIT_FAILURE);
                }
        setupterm(termtype, STDOUT_FILENO, &ret);
        switch (ret) {
@@ -202,7 +207,7 @@ int main(int argc, char **argv)
 
        default:
                warnx(_("trouble reading terminfo"));
-               /* fall through to ... */
+               /* fallthrough */
 
        case 0:
                if (tflag)
@@ -311,7 +316,7 @@ static void filter(FILE *f)
                        }
                        obuf[col].c_char = '_';
                        obuf[col].c_width = 1;
-                       /* fall through */
+                       /* fallthrough */
                case ' ':
                        setcol(col + 1);
                        continue;
@@ -402,7 +407,7 @@ static void flushln(void)
 static void overstrike(void)
 {
        register int i;
-       register wchar_t *lbuf = xmalloc((maxcol + 1) * sizeof(wchar_t));
+       register wchar_t *lbuf = xcalloc(maxcol + 1, sizeof(wchar_t));
        register wchar_t *cp = lbuf;
        int hadbold=0;
 
@@ -441,7 +446,7 @@ static void overstrike(void)
 static void iattr(void)
 {
        register int i;
-       register wchar_t *lbuf = xmalloc((maxcol + 1) * sizeof(wchar_t));
+       register wchar_t *lbuf = xcalloc(maxcol + 1, sizeof(wchar_t));
        register wchar_t *cp = lbuf;
 
        for (i = 0; i < maxcol; i++)