Implement the <> redirection operator.

This commit is contained in:
Brian Somers 2000-10-03 23:13:14 +00:00
parent b6cd2dbdc5
commit 4682f420f2
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=66612
7 changed files with 34 additions and 3 deletions

View File

@ -442,6 +442,7 @@ expredir(n)
switch (redir->type) {
case NFROM:
case NTO:
case NFROMTO:
case NAPPEND:
expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
redir->nfile.expfname = fn.list->text;

View File

@ -1085,6 +1085,8 @@ cmdtxt(n)
p = ">&"; i = 1; goto redir;
case NFROM:
p = "<"; i = 0; goto redir;
case NFROMTO:
p = "<>"; i = 0; goto redir;
case NFROMFD:
p = "<&"; i = 0; goto redir;
redir:

View File

@ -118,6 +118,7 @@ NARG narg # represents a word
NTO nfile # fd> fname
NFROM nfile # fd< fname
NFROMTO nfile # fd<> fname
NAPPEND nfile # fd>> fname
type int
next nodeptr # next redirection in list

View File

@ -1138,6 +1138,8 @@ parseredir: {
}
} else if (c == '&')
np->type = NFROMFD;
else if (c == '>')
np->type = NFROMTO;
else {
np->type = NFROM;
pungetc();

View File

@ -190,6 +190,27 @@ openredirect(redir, memory)
close(f);
}
break;
case NFROMTO:
fname = redir->nfile.expfname;
#ifdef O_CREAT
if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
#else
if ((f = open(fname, O_RDWR, 0666)) < 0) {
if (errno != ENOENT)
error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
else if ((f = creat(fname, 0666)) < 0)
error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
else {
close(f);
if ((f = open(fname, O_RDWR)) < 0) {
error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
remove(fname);
}
}
}
#endif
goto movefd;
case NTO:
fname = redir->nfile.expfname;
#ifdef O_CREAT

View File

@ -331,13 +331,14 @@ The following is a list of valid operators:
.Xc Xc Xc Xc
.El
.It Redirection operators:
.Bl -column "XXX" "XXX" "XXX" "XXX" -offset center -compact
.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
.It Xo
.Li < Ta Xo
.Li > Ta Xo
.Li << Ta Xo
.Li >>
.Xc Xc Xc Xc
.Li >> Ta Xo
.Li <>
.Xc Xc Xc Xc Xc
.It Xo
.Li <& Ta Xo
.Li >& Ta Xo
@ -497,6 +498,8 @@ option
append stdout (or file descriptor n) to file
.It Li [n]< file
redirect stdin (or file descriptor n) from file
.It Li [n]<> file
redirect stdin (or file descriptor n) to and from file
.It Li [n1]<&n2
duplicate stdin (or file descriptor n1) from file descriptor n2
.It Li [n]<&-

View File

@ -157,6 +157,7 @@ shcmd(cmd, fp)
case NAPPEND: s = ">>"; dftfd = 1; break;
case NTOFD: s = ">&"; dftfd = 1; break;
case NFROM: s = "<"; dftfd = 0; break;
case NFROMTO: s = "<>"; dftfd = 0; break;
case NFROMFD: s = "<&"; dftfd = 0; break;
default: s = "*error*"; dftfd = 0; break;
}