Skip to main content

hashcmd

Function hashcmd 

Source
pub fn hashcmd(arg0: &str, pp: &[String]) -> Option<cmdnam>
Expand description

Port of Cmdnam hashcmd(char *arg0, char **pp) from Src/exec.c:1010.

C body:

Cmdnam cn;
char *s, buf[PATH_MAX+1];
char **pq;
if (*arg0 == '/') return NULL;
for (; *pp; pp++)
    if (**pp == '/') {
        s = buf;
        struncpy(&s, *pp, PATH_MAX);
        *s++ = '/';
        if ((s - buf) + strlen(arg0) >= PATH_MAX) continue;
        strcpy(s, arg0);
        if (iscom(buf)) break;
    }
if (!*pp) return NULL;
cn = (Cmdnam) zshcalloc(sizeof *cn);
cn->node.flags = 0;
cn->u.name = pp;
cmdnamtab->addnode(cmdnamtab, ztrdup(arg0), cn);
if (isset(HASHDIRS)) {
    for (pq = pathchecked; pq <= pp; pq++) hashdir(pq);
    pathchecked = pp + 1;
}
return cn;

Walk pp[] (a $path slice starting from pathchecked) for the first absolute-PATH entry where <entry>/<arg0> is an executable regular file. Inserts the unhashed-cmdnam entry into cmdnamtab and (under HASHDIRS) bulk-hashes every PATH dir we walked through so subsequent commands hit the cache.

Returns the just-inserted cmdnam (now in cmdnamtab) on success, None if arg0 is absolute or no PATH entry contains it.