~ubuntu-branches/ubuntu/karmic/sed/karmic

« back to all changes in this revision

Viewing changes to sed/execute.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Adams
  • Date: 2009-07-22 18:11:39 UTC
  • mfrom: (1.1.5 upstream) (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090722181139-nw6f06lk08ni2b4c
Tags: 4.2.1-1
* New upstream version.
* Imported Upstream version 4.2.1
* Bump to Standards-Version 3.8.2.
* Add build dependency on libselinux-dev.
* Remove postinst and prerm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
extern int errno;
32
32
#endif
33
33
 
 
34
#ifndef BOOTSTRAP
 
35
#include <selinux/selinux.h>
 
36
#include <selinux/context.h>
 
37
#endif
 
38
 
34
39
#ifdef HAVE_UNISTD_H
35
40
# include <unistd.h>
36
41
#endif
133
138
 
134
139
  const char *in_file_name;
135
140
 
 
141
  /* Owner and mode to be set just before closing the file.  */
 
142
  struct stat st;
 
143
 
136
144
  /* if NULL, none of the following are valid */
137
145
  FILE *fp;
138
146
 
708
716
 
709
717
  if (in_place_extension)
710
718
    {
711
 
      int input_fd, output_fd;
 
719
      int input_fd;
712
720
      char *tmpdir, *p;
713
 
      struct stat st;
 
721
#ifndef BOOTSTRAP
 
722
      security_context_t old_fscreatecon;
 
723
      int reset_fscreatecon = 0;
 
724
      memset (&old_fscreatecon, 0, sizeof (old_fscreatecon));
 
725
#endif
714
726
 
715
727
      if (follow_symlinks)
716
728
        input->in_file_name = follow_symlink (name);
720
732
      /* get the base name */
721
733
      tmpdir = ck_strdup(input->in_file_name);
722
734
      if ((p = strrchr(tmpdir, '/')))
723
 
        *(p + 1) = 0;
 
735
        *p = 0;
724
736
      else
725
737
        strcpy(tmpdir, ".");
726
738
 
728
740
        panic(_("couldn't edit %s: is a terminal"), input->in_file_name);
729
741
 
730
742
      input_fd = fileno (input->fp);
731
 
      fstat (input_fd, &st);
732
 
      if (!S_ISREG (st.st_mode))
 
743
      fstat (input_fd, &input->st);
 
744
      if (!S_ISREG (input->st.st_mode))
733
745
        panic(_("couldn't edit %s: not a regular file"), input->in_file_name);
734
746
 
 
747
#ifndef BOOTSTRAP
 
748
      if (is_selinux_enabled ())
 
749
        {
 
750
          security_context_t con;
 
751
          if (getfilecon (input->in_file_name, &con) != -1)
 
752
            {
 
753
              /* Save and restore the old context for the sake of w and W
 
754
                 commands.  */
 
755
              reset_fscreatecon = getfscreatecon (&old_fscreatecon) >= 0;
 
756
              if (setfscreatecon (con) < 0)
 
757
                fprintf (stderr, _("%s: warning: failed to set default file creation context to %s: %s"),
 
758
                         myname, con, strerror (errno));
 
759
              freecon (con);
 
760
            }
 
761
          else
 
762
            {
 
763
              if (errno != ENOSYS)
 
764
                fprintf (stderr, _("%s: warning: failed to get security context of %s: %s"),
 
765
                         myname, input->in_file_name, strerror (errno));
 
766
            }
 
767
        }
 
768
#endif
 
769
 
735
770
      output_file.fp = ck_mkstemp (&input->out_file_name, tmpdir, "sed");
736
771
      output_file.missing_newline = false;
737
772
      free (tmpdir);
738
773
 
 
774
#ifndef BOOTSTRAP
 
775
      if (reset_fscreatecon)
 
776
        {
 
777
          setfscreatecon (old_fscreatecon);
 
778
          freecon (old_fscreatecon);
 
779
        }
 
780
#endif
 
781
 
739
782
      if (!output_file.fp)
740
783
        panic(_("couldn't open temporary file %s: %s"), input->out_file_name, strerror(errno));
741
 
 
742
 
      output_fd = fileno (output_file.fp);
743
 
#ifdef HAVE_FCHOWN
744
 
      if (fchown (output_fd, st.st_uid, st.st_gid) == -1)
745
 
        fchown (output_fd, -1, st.st_gid);
746
 
#endif
747
 
      copy_acl (input->in_file_name, input_fd,
748
 
                input->out_file_name, output_fd,
749
 
                st.st_mode);
750
784
    }
751
785
  else
752
786
    output_file.fp = stdout;
766
800
  if (in_place_extension && output_file.fp != NULL)
767
801
    {
768
802
      const char *target_name;
 
803
      int input_fd, output_fd;
 
804
 
 
805
      target_name = input->in_file_name;
 
806
      input_fd = fileno (input->fp);
 
807
      output_fd = fileno (output_file.fp);
 
808
      copy_acl (input->in_file_name, input_fd,
 
809
                input->out_file_name, output_fd,
 
810
                input->st.st_mode);
 
811
#ifdef HAVE_FCHOWN
 
812
      if (fchown (output_fd, input->st.st_uid, input->st.st_gid) == -1)
 
813
        fchown (output_fd, -1, input->st.st_gid);
 
814
#endif
 
815
 
 
816
      ck_fclose (input->fp);
769
817
      ck_fclose (output_file.fp);
770
 
 
771
 
      target_name = input->in_file_name;
772
818
      if (strcmp(in_place_extension, "*") != 0)
773
819
        {
774
820
          char *backup_file_name = get_backup_file_name(target_name);
779
825
      ck_rename (input->out_file_name, target_name, input->out_file_name);
780
826
      free (input->out_file_name);
781
827
    }
 
828
  else
 
829
    ck_fclose (input->fp);
782
830
 
783
 
  ck_fclose (input->fp);
784
831
  input->fp = NULL;
785
832
}
786
833