[Toybox] lsof cleanup

Rob Landley rob at landley.net
Sun Mar 18 13:52:41 PDT 2018


On 03/18/2018 02:40 PM, haroon maqsood wrote:
> HI,
> 
> in lsof.c 
> 
>   // TODO: llist_traverse to measure the columns first.
> 
> a bit more info on this please.

Remember how in the two examples I posted the host one (ubuntu 14.04) fit in 80
columns but the toybox one didn't? The toybox version had more horizontal space
characters than it needed for what it was displaying.

The actual issue is this:

$ ./toybox ls -l
total 96
-rw-rw-r-- 1 landley landley  5382 2018-03-18 15:41 Config.in
-rw-rw-r-- 1 landley landley   820 2018-03-18 15:41 LICENSE
-rw-rw-r-- 1 landley landley  1697 2018-03-18 15:41 Makefile
-rw-rw-r-- 1 landley landley  6624 2018-03-18 15:41 README
-rwxrwxr-x 1 landley landley  1205 2018-03-18 15:41 configure
drwxrwxr-x 4 landley landley  4096 2018-03-18 15:41 generated
drwxrwxr-x 3 landley landley  4096 2018-03-18 15:41 kconfig
drwxrwxr-x 2 landley landley  4096 2018-03-18 15:41 lib
-r-xr-xr-x 1 landley landley 27248 2018-03-18 15:41 ls
-rw-rw-r-- 1 landley landley  7218 2018-03-18 15:41 main.c
drwxrwxr-x 2 landley landley  4096 2018-03-18 15:41 scripts
drwxrwxr-x 3 landley landley  4096 2018-03-18 15:41 tests
drwxrwxr-x 9 landley landley  4096 2018-03-18 15:41 toys
-rw-rw-r-- 1 landley landley  3400 2018-03-18 15:41 toys.h
drwxrwxr-x 2 landley landley  4096 2018-03-18 15:41 www

Notice how it's using exactly as many spaces as it needs to keep the columns
apart, even though the length of the names might vary, the number of symlinks
might go to double digits, and the size _does_ vary. This means you need to do a
pass over all the fields you're going to display to measure them, and then do a
second pass to actually display them with the appropriate column sizes.

There's code that sort of does this in ls.c and vmstat.c and an overflow version
of it in ps.c, and it's getting to the point where I'm thinking there should be
some sort of generic infrastructure in lib that I should be able to plug stuff
into and just have it do it. Alas, I don't know what that infrastructure should
look like. (It's a pending design todo item.)

The problem is, I've got a bunch of fields I want to display, and some are
strings and some are numeric and some are dates and some are other things.

I _do_ have this plumbing sort of genericized in "ps", but that's fairly
heavyweight infrastructure (because ps -o can change what you're displaying in
arbitrary ways) and I'm not sure I want vmstat.c to look like ps.c. For ls, the
fields output by -l are fixed and having C do it rather than parsing a table
seemed better at the time I wrote it.

This is why working out what generic infrastructure to display padded columns
would look like (or whether it's worth trying to collapse them together) is hard
and needs some serious thought. I think what I need to do is try to untangle
ps.c and factor out half of it into lib/ and split top.c and pgrep.c out into
their own files, and then see if the resulting infrastructure can apply to
vmstat.c and ls.c and so on. Or whether I should just have lsof.c do what
vmstat.c is currently doing, measuring it by hand in two passes inside the
individual command.

Rob



More information about the Toybox mailing list