diff options
author | tjr <tjr@FreeBSD.org> | 2002-05-19 06:03:05 +0000 |
---|---|---|
committer | tjr <tjr@FreeBSD.org> | 2002-05-19 06:03:05 +0000 |
commit | ada2f900a38d47a260d94dd3f996a87e81130173 (patch) | |
tree | 39da0880e61a1db8ed946867d12f3bb04e4a192f | |
parent | 619a9e0c61c8a42e5e7f593ec5dcebc0660b783e (diff) | |
download | FreeBSD-src-ada2f900a38d47a260d94dd3f996a87e81130173.zip FreeBSD-src-ada2f900a38d47a260d94dd3f996a87e81130173.tar.gz |
Implement the -C (-o noclobber) option, which prevents existing regular
files from being overwritten by shell redirection.
-rw-r--r-- | bin/sh/error.c | 1 | ||||
-rw-r--r-- | bin/sh/eval.c | 1 | ||||
-rw-r--r-- | bin/sh/jobs.c | 2 | ||||
-rw-r--r-- | bin/sh/nodetypes | 1 | ||||
-rw-r--r-- | bin/sh/parser.c | 2 | ||||
-rw-r--r-- | bin/sh/redir.c | 11 | ||||
-rw-r--r-- | bin/sh/sh.1 | 1 | ||||
-rw-r--r-- | bin/sh/show.c | 1 |
8 files changed, 19 insertions, 1 deletions
diff --git a/bin/sh/error.c b/bin/sh/error.c index 0cf20b3..96e6534 100644 --- a/bin/sh/error.c +++ b/bin/sh/error.c @@ -247,6 +247,7 @@ STATIC const struct errname errormsg[] = { #ifdef ELIBACC { ELIBACC, E_EXEC, "shared library missing" }, #endif + { EEXIST, E_CREAT, "file exists" }, { 0, 0, NULL }, }; diff --git a/bin/sh/eval.c b/bin/sh/eval.c index 9f65d90..0c3f85b 100644 --- a/bin/sh/eval.c +++ b/bin/sh/eval.c @@ -448,6 +448,7 @@ expredir(union node *n) case NTO: case NFROMTO: case NAPPEND: + case NCLOBBER: expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); redir->nfile.expfname = fn.list->text; break; diff --git a/bin/sh/jobs.c b/bin/sh/jobs.c index 14f1909..b1cc2de 100644 --- a/bin/sh/jobs.c +++ b/bin/sh/jobs.c @@ -1057,6 +1057,8 @@ until: p = ">>"; i = 1; goto redir; case NTOFD: p = ">&"; i = 1; goto redir; + case NCLOBBER: + p = ">|"; i = 1; goto redir; case NFROM: p = "<"; i = 0; goto redir; case NFROMTO: diff --git a/bin/sh/nodetypes b/bin/sh/nodetypes index 53dd18f..27fbcd1 100644 --- a/bin/sh/nodetypes +++ b/bin/sh/nodetypes @@ -120,6 +120,7 @@ NTO nfile # fd> fname NFROM nfile # fd< fname NFROMTO nfile # fd<> fname NAPPEND nfile # fd>> fname +NCLOBBER nfile # fd>| fname type int next nodeptr # next redirection in list fd int # file descriptor being redirected diff --git a/bin/sh/parser.c b/bin/sh/parser.c index 08cd737..6e0f23b 100644 --- a/bin/sh/parser.c +++ b/bin/sh/parser.c @@ -1145,6 +1145,8 @@ parseredir: { np->type = NAPPEND; else if (c == '&') np->type = NTOFD; + else if (c == '|') + np->type = NCLOBBER; else { np->type = NTO; pungetc(); diff --git a/bin/sh/redir.c b/bin/sh/redir.c index b596463..60a6151 100644 --- a/bin/sh/redir.c +++ b/bin/sh/redir.c @@ -43,6 +43,7 @@ static const char rcsid[] = #endif /* not lint */ #include <sys/types.h> +#include <sys/stat.h> #include <signal.h> #include <string.h> #include <fcntl.h> @@ -62,6 +63,7 @@ static const char rcsid[] = #include "output.h" #include "memalloc.h" #include "error.h" +#include "options.h" #define EMPTY -2 /* marks an unused slot in redirtab */ @@ -161,6 +163,7 @@ again: STATIC void openredirect(union node *redir, char memory[10]) { + struct stat sb; int fd = redir->nfile.fd; char *fname; int f; @@ -207,6 +210,9 @@ movefd: goto movefd; case NTO: fname = redir->nfile.expfname; + if (Cflag && stat(fname, &sb) != -1 && S_ISREG(sb.st_mode)) + error("cannot create %s: %s", fname, + errmsg(EEXIST, E_CREAT)); #ifdef O_CREAT if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); @@ -215,6 +221,11 @@ movefd: error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); #endif goto movefd; + case NCLOBBER: + fname = redir->nfile.expfname; + if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) + error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); + goto movefd; case NAPPEND: fname = redir->nfile.expfname; #ifdef O_APPEND diff --git a/bin/sh/sh.1 b/bin/sh/sh.1 index 9c8cc34..e9f5f08 100644 --- a/bin/sh/sh.1 +++ b/bin/sh/sh.1 @@ -195,7 +195,6 @@ completion. .It Fl C Li noclobber Do not overwrite existing files with .Dq Li > . -(UNIMPLEMENTED) .It Fl E Li emacs Enable the builtin .Xr emacs 1 diff --git a/bin/sh/show.c b/bin/sh/show.c index 3880d38..0e9a181 100644 --- a/bin/sh/show.c +++ b/bin/sh/show.c @@ -142,6 +142,7 @@ shcmd(union node *cmd, FILE *fp) case NTO: s = ">"; dftfd = 1; break; case NAPPEND: s = ">>"; dftfd = 1; break; case NTOFD: s = ">&"; dftfd = 1; break; + case NCLOBBER: s = ">|"; dftfd = 1; break; case NFROM: s = "<"; dftfd = 0; break; case NFROMTO: s = "<>"; dftfd = 0; break; case NFROMFD: s = "<&"; dftfd = 0; break; |