system_chown Function

public impure elemental function system_chown(dirname, owner, group)

NAME

system_chown(3f) - [M_system:FILE_SYSTEM] change file owner and group
(LICENSE:PD)

SYNOPSIS

elemental impure logical function system_chown(path,owner,group)

character(len=*),intent(in) :: path
integer,intent(in)          :: owner
integer,intent(in)          :: group

DESCRIPTION

    The chown(3f) function changes owner and group of a file

   The path argument points to a pathname naming a file. The
   user ID and group ID of the named file shall be set to the numeric
   values contained in owner and group, respectively.

   Only processes with an effective user ID equal to the user ID of
   the file or with appropriate privileges may change the ownership
   of a file.

OPTIONS

   path   a character string representing a file pathname.
          Trailing spaces are ignored.
   owner  UID of owner that ownership is to be changed to
   group  GID of group that ownership is to be changed to

RETURN VALUE

   The system_chown(3f) function should return zero 0 if successful.
   Otherwise, these functions shall return 1 and set errno to
   indicate the error. If 1 is returned, no changes are made in
   the user ID and group ID of the file.

EXAMPLE

Sample program:

program demo_system_chown
use M_system, only : system_chown
use M_system, only : system_getuid
use M_system, only : system_getgid
use M_system, only : system_perror
implicit none
integer                     :: i
character(len=80),parameter :: names(*)=[&
        & character(len=80) :: &
        & 'myfile1',&
        & '/usr/local']
do i=1,size(names)
   if(.not. system_chown(&
   & trim(names(i)),  &
   & system_getuid(), &
   & system_getgid()) &
      )then
      call system_perror('*demo_system_chown* '//trim(names(i)))
   endif
enddo
end program demo_system_chown

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: dirname
integer, intent(in) :: owner
integer, intent(in) :: group

Return Value logical


Contents

Source Code


Source Code

elemental impure function system_chown(dirname,owner,group)
implicit none

! ident_11="@(#) M_system system_chown(3f) change owner and group of a file relative to directory file descriptor"

character(len=*),intent(in) :: dirname
integer,intent(in)          :: owner
integer,intent(in)          :: group
logical                     :: system_chown
character(kind=c_char,len=1),allocatable :: temp(:)

! int chown(const char *path, uid_t owner, gid_t group);
interface
  function c_chown(c_dirname,c_owner,c_group) bind (C,name="my_chown") result (c_ierr)
  import c_char,c_int
  character(kind=c_char,len=1),intent(in) :: c_dirname(*)
  integer(kind=c_int),intent(in),value    :: c_owner
  integer(kind=c_int),intent(in),value    :: c_group
  integer(kind=c_int)                     :: c_ierr
  end function c_chown
end interface

   temp = str2_carr(trim(dirname)) ! kludge for bug in ifort (IFORT) 2021.3.0 20210609
   if(c_chown(temp,int(owner,kind=c_int),int(group,kind=c_int)).eq.1)then
      system_chown=.true.
   else
      system_chown=.false.
   endif

end function system_chown