NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: bin/48201



The following reply was made to PR bin/48201; it has been noted by GNATS.

From: Miwa Susumu <miwarin%gmail.com@localhost>
To: gnats-bugs%NetBSD.org@localhost
Cc: 
Subject: Re: bin/48201
Date: Wed, 15 Oct 2014 22:39:43 +0900

 import from FreeBSD
 https://svnweb.freebsd.org/base/head/bin/sh/redir.c
 
 --- /usr/src/bin/sh/redir.c.orig        2014-10-15 22:18:54.000000000 +0900
 +++ /usr/src/bin/sh/redir.c     2014-10-15 21:10:47.000000000 +0900
 @@ -173,10 +173,11 @@
  STATIC void
  openredirect(union node *redir, char memory[10], int flags)
  {
 +       struct stat sb;
         int fd = redir->nfile.fd;
         char *fname;
         int f;
 -       int oflags = O_WRONLY|O_CREAT|O_TRUNC, eflags;
 +       int eflags;
 
         /*
          * We suppress interrupts so that we won't leave open file
 @@ -203,12 +204,29 @@
                         goto ecreate;
                 break;
         case NTO:
 -               if (Cflag)
 -                       oflags |= O_EXCL;
 +               if (Cflag) {
 +                       fname = redir->nfile.expfname;
 +                       if (stat(fname, &sb) == -1) {
 +                               if ((f = open(fname,
 O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0)
 +                                       goto ecreate;
 +                       } else if (!S_ISREG(sb.st_mode)) {
 +                               if ((f = open(fname, O_WRONLY, 0666)) < 0)
 +                                       goto ecreate;
 +                               if (fstat(f, &sb) != -1 &&
 S_ISREG(sb.st_mode)) {
 +                                       close(f);
 +                                       errno = EEXIST;
 +                                       goto ecreate;
 +                               }
 +                       } else {
 +                               errno = EEXIST;
 +                               goto ecreate;
 +                       }
 +                       break;
 +               }
                 /* FALLTHROUGH */
         case NCLOBBER:
                 fname = redir->nfile.expfname;
 -               if ((f = open(fname, oflags, 0666)) < 0)
 +               if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
                         goto ecreate;
                 break;
         case NAPPEND:
 
 
 Detail
 ================================================
 
 Shell Command Language
  http://pubs.opengroup.org/onlinepubs/009604599/utilities/xcu_chap02.html
 
 <<<
 Output redirection using the '>' format shall fail if the noclobber
 option is set (see the description of set -C) and the file named by
 the expansion of word exists and is a regular file. Otherwise,
 redirection using the '>' or ">|" formats shall cause the file whose
 name results from the expansion of word to be created and opened for
 output on the designated file descriptor, or standard output if none
 is specified. If the file does not exist, it shall be created;
 otherwise, it shall be truncated to be an empty file after being
 opened
 >>>
 
 I think separately.
 --------
 (1) Output redirection using the '>' format shall fail
 if the noclobber option is set (see the description of set -C)
 the file named by the expansion of word exists
 is a regular file.
 
 (2) Otherwise, redirection using the '>' or ">|" formats shall cause
 the file whose name results from the expansion of word to be created
 opened for output on the designated file descriptor, or standard
 output if none is specified.
 
 (3) If the file does not exist,
 it shall be created;
 
 (4) otherwise,
 it shall be truncated to be an empty file after being opened
 --------
 
 
 The patch of the above, I verify the operation.
 --------
 (1) It is an error ... ok
 % touch hoge
 % /bin/sh -c 'set -C; printf "" > hoge'
 sh: cannot create hoge: file exists
 
 (2) can be written to /dev/null ... ok
 % /bin/sh -c 'set -C; printf "" > /dev/null'
 
 (3) hoge is generated ... ok
 % rm hoge
 % /bin/sh -c 'set -C; printf "" > hoge'
 % /bin/ls -l hoge
 -rw-r--r--  1 rin  users  0 Oct 15 22:36 hoge
 
 (4) hoge becomes empty ... ok
 % /bin/sh -c 'printf "hoge\n" > hoge'
 % cat hoge
 hoge
 %  /bin/sh -c 'printf "" > hoge'
 % cat hoge
 
 -- 
 miwarin
 


Home | Main Index | Thread Index | Old Index