[Toybox] [PATCH 1/5] lib: Add void *cb_param param to dirtree_read().

Georgi Chorbadzhiyski gf at unixsol.org
Tue Mar 20 04:12:42 PDT 2012


Around 03/16/2012 03:15 PM, Rob Landley scribbled:
> On 03/07/2012 02:15 AM, Georgi Chorbadzhiyski wrote:
>> This parameter is passed to callback() function.
> 
> Each command has a block of global variables.  Passing down an
> unmodified variable eats more stack space and makes the callsites larger.
> 
> As for patch 2 in the series... The dirtree stuff is already making a
> call for each file, I'm not sure how the new wrappers differ?

With standard dirtree_read() I would need to duplicate the following code
code in chmod, chgrp, chown and sysctl. Variant of dirtree_node() is
also contained in toys/cp.c

static int dirtree_node(char *path, struct dirtree *node, void *cb_param)
{
    char *s = path + strlen(path);
    struct dirtree *n = node;
    int (*callback)(char *path) = cb_param;

    for ( ; ; n = n->parent) {
        while (s!=path) {
            if (*(--s) == '/') break;
        }
        if (!n) break;
    }
    if (s != path) s++;

    callback(s);

    return 0;
}

void dirtree_for_each(char *path, int (*callback)(char *path))
{
    struct stat sb;
    if (stat(path, &sb) == -1) {
        perror_msg("%s", path);
        return;
    }
    callback(path);
    if (S_ISDIR(sb.st_mode)) {
        strncpy(toybuf, path, sizeof(toybuf) - 1);
        toybuf[sizeof(toybuf) - 1] = 0;
        dirtree_read(toybuf, NULL, callback, dirtree_node);
    }
}

-- 
Georgi Chorbadzhiyski
http://georgi.unixsol.org/

 1332241962.0


More information about the Toybox mailing list