Listing directories with Limbo

47 views
Skip to first unread message

da...@boddie.org.uk

unread,
Jan 24, 2023, 10:30:53 AM1/24/23
to inferno-os
I've written a simple version of ls with a smaller footprint than the standard
ls but I'm now encountering problems with it. It seems that it doesn't list the
contents of all the files in a union directory. This is a bit strange given
that the core loop with dirread is very similar to that in the readall function
of the readdir module, and that works fine.

I'm probably missing something obvious but I just can't see it. Any ideas?

implement Ls;

include "draw.m";
include "sys.m";
include "workdir.m";

Ls: module
{
    init:    fn(nil: ref Draw->Context, nil: list of string);
};

init(nil: ref Draw->Context, argv: list of string)
{
    sys := load Sys Sys->PATH;
    gwd := load Workdir Workdir->PATH;

    if (len(argv) < 2) {
        wd := gwd->init();
        if (wd == nil)
            return;
        argv = wd::nil;
    } else
        argv = tl argv;

    for (; argv != nil; argv = tl argv) {

        file := hd argv;
    (ok, dir) := sys->stat(file);
        if (ok < 0) continue;

        if (dir.mode & Sys->DMDIR != 0) {
            fd := sys->open(dir.name, Sys->OREAD);
            if (fd != nil) {
                for (;;) {
                    (nr, b) := sys->dirread(fd);
                    if (nr <= 0) break;
                    for (i := 0; i < nr; i++)
                        sys->print("%s\n", b[i].name);
                }
            }
        } else
            sys->print("%s\n", dir.name);
    }
}

da...@boddie.org.uk

unread,
Jan 25, 2023, 10:41:23 AM1/25/23
to inferno-os
Caerwyn provided the solution: I needed to open the file specified as an argument, not the contents of dir.name.
This lets dirread return the contents of all directories that feed into the directory I am listing instead of just one of them.
Reply all
Reply to author
Forward
0 new messages