summaryrefslogtreecommitdiffstats
path: root/bin/sh/redir.c
diff options
context:
space:
mode:
authortjr <tjr@FreeBSD.org>2002-05-19 06:03:05 +0000
committertjr <tjr@FreeBSD.org>2002-05-19 06:03:05 +0000
commitada2f900a38d47a260d94dd3f996a87e81130173 (patch)
tree39da0880e61a1db8ed946867d12f3bb04e4a192f /bin/sh/redir.c
parent619a9e0c61c8a42e5e7f593ec5dcebc0660b783e (diff)
downloadFreeBSD-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.
Diffstat (limited to 'bin/sh/redir.c')
-rw-r--r--bin/sh/redir.c11
1 files changed, 11 insertions, 0 deletions
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
OpenPOWER on IntegriCloud