]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
text-utils: cleanup strtoxx_or_err()
authorKarel Zak <kzak@redhat.com>
Tue, 15 May 2012 15:46:20 +0000 (17:46 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 15 May 2012 15:46:20 +0000 (17:46 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
text-utils/col.c
text-utils/column.c

index dc9e9c8b2bcc573cd6cc4e9fe988cf60bde6421d..57fa475619d70f43abb2e90bcde352ac85cb1f09 100644 (file)
@@ -106,7 +106,7 @@ CSET last_set;                      /* char_set of last char printed */
 LINE *lines;
 int compress_spaces;           /* if doing space -> tab conversion */
 int fine;                      /* if `fine' resolution (half lines) */
-int max_bufd_lines;            /* max # lines to keep in memory */
+unsigned max_bufd_lines;       /* max # lines to keep in memory */
 int nblank_lines;              /* # blanks after last flushed line */
 int no_backspaces;             /* if not to output any backspaces */
 int pass_unknown_seqs;         /* whether to pass unknown control sequences */
@@ -157,7 +157,6 @@ int main(int argc, char **argv)
        int this_line;                  /* line l points to */
        int nflushd_lines;              /* number of lines that were flushed */
        int adjust, opt, warned;
-       unsigned long tmplong;
        int ret = EXIT_SUCCESS;
 
        static const struct option longopts[] = {
@@ -197,10 +196,7 @@ int main(int argc, char **argv)
                         * Buffered line count, which is a value in half
                         * lines e.g. twice the amount specified.
                         */
-                       tmplong = strtoul_or_err(optarg, _("bad -l argument")) * 2;
-                       if ((INT_MAX) < tmplong)
-                               errx(EXIT_FAILURE, _("argument %lu is too large"), tmplong);
-                       max_bufd_lines = (int) tmplong;
+                       max_bufd_lines = strtou32_or_err(optarg, _("bad -l argument")) * 2;
                        break;
                case 'p':
                        pass_unknown_seqs = 1;
@@ -343,7 +339,8 @@ int main(int argc, char **argv)
                        }
                        this_line = cur_line + adjust;
                        nmove = this_line - nflushd_lines;
-                       if (nmove >= max_bufd_lines + BUFFER_MARGIN) {
+                       if (nmove > 0
+                           && (unsigned) nmove >= max_bufd_lines + BUFFER_MARGIN) {
                                nflushd_lines += nmove - max_bufd_lines;
                                flush_lines(nmove - max_bufd_lines);
                        }
index b210a93811518c681c543f34c2111b6de36ceea2..aab9187ea507f311c9f4391bc0f016090cb6d83e 100644 (file)
 #include <sys/ioctl.h>
 
 #include <ctype.h>
-#include <limits.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <getopt.h>
-#include "nls.h"
 
+#include "nls.h"
 #include "widechar.h"
 #include "c.h"
 #include "xalloc.h"
 #include "strutils.h"
 #include "closestream.h"
+#include "ttyutils.h"
 
 #ifdef HAVE_WIDECHAR
 #define wcs_width(s) wcswidth(s,wcslen(s))
@@ -105,10 +105,9 @@ static void __attribute__((__noreturn__)) usage(int rc)
 
 int main(int argc, char **argv)
 {
-       struct winsize win;
        int ch, tflag = 0, xflag = 0;
        int i;
-       long termwidth = 80;
+       int termwidth = 80;
        int entries = 0;                /* number of records */
        unsigned int eval = 0;          /* exit value */
        int maxlength = 0;              /* longest record */
@@ -134,14 +133,9 @@ int main(int argc, char **argv)
        textdomain(PACKAGE);
        atexit(close_stdout);
 
-       if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
-               char *p;
-
-               if ((p = getenv("COLUMNS")) != NULL)
-                       termwidth = strtol_or_err(p,
-                                       _("terminal environment COLUMNS failed"));
-       } else
-               termwidth = win.ws_col;
+       termwidth = get_terminal_width();
+       if (termwidth <= 0)
+               termwidth = 80;
 
        while ((ch = getopt_long(argc, argv, "hVc:s:tx", longopts, NULL)) != -1)
                switch(ch) {
@@ -153,11 +147,7 @@ int main(int argc, char **argv)
                                 PACKAGE_STRING);
                                 return EXIT_SUCCESS;
                case 'c':
-                       termwidth = strtol_or_err(optarg,
-                                                 _("bad columns width value"));
-                       if (termwidth < 1)
-                               errx(EXIT_FAILURE,
-                                    _("-%c positive integer expected as an argument"), ch);
+                       termwidth = strtou32_or_err(optarg, _("invalid columns argument"));
                        break;
                case 's':
                        separator = mbs_to_wcs(optarg);