Skip to main content

checkclobberparam

Function checkclobberparam 

Source
pub fn checkclobberparam(f: &redir) -> i32
Expand description

Port of static int checkclobberparam(struct redir *f) from Src/exec.c:2178.

C body:

struct value vbuf; Value v;
char *s = f->varid; int fd;
if (!s) return 1;
if (!(v = getvalue(&vbuf, &s, 0))) return 1;
if (v->pm->node.flags & PM_READONLY) {
    zwarn("can't allocate file descriptor to readonly parameter %s",
          f->varid);
    errno = 0;
    return 0;
}
/* We can't clobber the value in the parameter if it's
 * already an opened file descriptor */
if (!isset(CLOBBER) && (s = getstrvalue(v)) &&
    (fd = (int)zstrtol(s, &s, 10)) >= 0 && !*s &&
    fd <= max_zsh_fd && fdtable[fd] == FDT_EXTERNAL) {
    zwarn("can't clobber parameter %s containing file descriptor %d",
         f->varid, fd);
    errno = 0;
    return 0;
}
return 1;

Validate that f->varid (the {var}>file brace-FD form’s var name) is writable and (under NOCLOBBER) doesn’t currently hold an FDT_EXTERNAL fd number. Returns 1 on OK, 0 on refusal (zwarn already emitted).

NOCLOBBER + FDT_EXTERNAL clause now ported (c:2199-2213). When NOCLOBBER is set and the param’s value is the fd-number of an FDT_EXTERNAL-marked fd in the fdtable, refuse with a warning so the existing fd doesn’t get clobbered by the upcoming open(2).