[kwlug-disc] MKS - really the "lc" command

Doug Moen doug at moens.org
Tue Aug 2 08:13:00 EDT 2022


I have the same problem, the lc output is not sensitive to terminal width.

Here's the code that doesn't work:
#ifdef TIOCGWINSZ
    if (isatty(1)) {
        struct winsize win;
        if (ioctl(1, TIOCGWINSZ, &win) != -1) {
            ncols = (win.ws_col == 0 ? 5 : (win.ws_col / COLUMNWIDTH));
            ncols = ncols ? ncols : 1;  /* can't have 0 columns */
        }
    }
#endif /* TIOGCWINSZ */

Why doesn't it work?
Because the #ifdef is failing. The program is not including the necessary header file.

Here's the relevant code from my lc, which works on BSD, Linux and Cygwin.
I can't include the whole program, because of the HyperC dependencies.
Maybe this is motivation to port my code to straight C.

// These are all of the system #includes
#include <sys/stat.h>   /* prereq for dirent.h on bsd */

#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <termios.h>    /* TIOCGWINSZ on cygwin */
#include <unistd.h>

/*
 * Compute terminal width, in columns, defaulting to 80.
 *
 * BSD ls gets termwidth from tty associated with stdout,
 * and defaults to 80 if stdout is a pipe (eg, to 'more').
 * That's not desirable behaviour, so I search first stdout
 * then stdin, looking for a tty.
 */
static int
termwidth(void)
{
    char *p;
    struct winsize win;

    p = getenv("COLUMNS");
    if (p != NULL && *p != '\0') {
    return atoi(p);
    }
    if (isatty(STDOUT_FILENO)) {
    if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != -1 && win.ws_col > 0)
        return win.ws_col;
    else
        return 80;
    }
    if (isatty(STDIN_FILENO)) {
    if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) != -1 && win.ws_col > 0)
        return win.ws_col;
    else
        return 80;
    }
    return 80;
}



On Mon, Aug 1, 2022, at 5:27 PM, Steve Izma wrote:
> On Sat, Jul 30, 2022 at 11:11:57AM -0400, John Sellens wrote:
>> Subject: Re: [kwlug-disc] MKS - really the "lc" command
>> 
>> I can't function without "lc".
>
> ** Neither can I. **
>
>
>> So more than a few years ago, I took my copy of lc, and managed to
>> get it added to the freebsd ports tree:
>>     https://cgit.freebsd.org/ports/tree/misc/lc
>> where it seems to still exist today.
>> 
>> And you can get it yourself here:
>>     https://www.generalconcepts.com/resources/software/
>> 
>> There might possibly be some new situations or file types it doesn't
>> know about, but I use it at least a hundred times a day.  There may
>> be some official UW updates since my copy of course, but I don't
>> think all the UW/MFCF developed software was ever made available.
>
> The source code I have from the 1990s looks like an earlier
> version of yours, John (I guess the earlier U of W version that
> was floating around). Significantly, the earlier version lacks
> TIOCGWINSZ, which I assume gets the terminal size in order to
> increase columns for wide screens. I can't seem to get it to work
> with my Linux terminals: the number of columns is always five.
>
> This is the one thing I've been wanting to improve on over the
> years, but I've never spent the time looking into it. Do I need
> to do something else to get TIOCGWINSZ to work?
>
> 	-- Steve
>
> -- 
> Steve Izma
> -
> Home: 35 Locust St., Kitchener, Ontario, Canada  N2H 1W6
> E-mail: sizma at golden.net  phone: 519-745-1313
> cell (text only; not frequently checked): 519-998-2684
>
> ==
> The most erroneous stories are those we think we know best – and
> therefore never scrutinize or question.
>     -- Stephen Jay Gould, *Full House: The Spread of Excellence
>        from Plato to Darwin*, 1996
>
> _______________________________________________
> kwlug-disc mailing list
> kwlug-disc at kwlug.org
> https://kwlug.org/mailman/listinfo/kwlug-disc_kwlug.org




More information about the kwlug-disc mailing list