system_link Function

public impure elemental function system_link(oldname, newname) result(ierr)

NAME

    system_link(3f) - [M_system:FILE_SYSTEM] link one file to another
                      file relative to two directory file descriptors
    (LICENSE:PD)

SYNOPSIS

elemental impure integer function link(oldpath,newpath);

 character(len=*),intent(in) :: oldpath
 character(len=*),intent(in) :: newpath

DESCRIPTION

    The link() function shall create a new link (directory entry)
    for the existing file, path1.

    The path1 argument points to a pathname naming an existing
    file. The path2 argument points to a pathname naming the
    new directory entry to be created. The link() function shall
    atomically create a new link for the existing file and the link
    count of the file shall be incremented by one.

    If path1 names a directory, link() shall fail unless the process
    has appropriate privileges and the implementation supports using
    link() on directories.

    If path1 names a symbolic link, it is implementation-defined
    whether link() follows the symbolic link, or creates a new link
    to the symbolic link itself.

    Upon successful completion, link() shall mark for update the
    last file status change timestamp of the file. Also, the last
    data modification and last file status change timestamps of the
    directory that contains the new entry shall be marked for update.

    If link() fails, no link shall be created and the link count of
    the file shall remain unchanged.

    The implementation may require that the calling process has
    permission to access the existing file.

    The linkat() function shall be equivalent to the link() function
    except that symbolic links shall be handled as specified by the
    value of flag (see below) and except in the case where either path1
    or path2 or both are relative paths. In this case a relative path
    path1 is interpreted relative to the directory associated with
    the file descriptor fd1 instead of the current working directory
    and similarly for path2 and the file descriptor fd2. If the
    file descriptor was opened without O_SEARCH, the function shall
    check whether directory searches are permitted using the current
    permissions of the directory underlying the file descriptor. If
    the file descriptor was opened with O_SEARCH, the function shall
    not perform the check.

    Values for flag are constructed by a bitwise-inclusive OR of
    flags from the following list, defined in <fcntl.h>:

    AT_SYMLINK_FOLLOW
          If path1 names a symbolic link, a new link for the target
          of the symbolic link is created.

    If linkat() is passed the special value AT_FDCWD in the fd1 or
    fd2 parameter, the current working directory shall be used for the
    respective path argument. If both fd1 and fd2 have value AT_FDCWD,
    the behavior shall be identical to a call to link(), except that
    symbolic links shall be handled as specified by the value of flag.

    Some implementations do allow links between file systems.

    If path1 refers to a symbolic link, application developers should
    use linkat() with appropriate flags to select whether or not the
    symbolic link should be resolved.

    If the AT_SYMLINK_FOLLOW flag is clear in the flag argument and
    the path1 argument names a symbolic link, a new link is created
    for the symbolic link path1 and not its target.

RETURN VALUE

    Upon successful completion, these functions shall return
    0. Otherwise, these functions shall return -1 and set errno to
    indicate the error.

EXAMPLES

Creating a Link to a File

program demo_system_link
use M_system, only : system_link, system_perror
integer :: ierr
ierr = system_link('myfile1','myfile2')
if(ierr.ne.0)then
   call system_perror('*demo_system_link*')
endif
end program demo_system_link

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: oldname
character(len=*), intent(in) :: newname

Return Value integer


Contents

Source Code


Source Code

elemental impure function system_link(oldname,newname) result(ierr)

! ident_12="@(#) M_system system_link(3f) call link(3c) to create a file link"

character(len=*),intent(in) :: oldname
character(len=*),intent(in) :: newname
integer                     :: ierr
integer(kind=c_int)         :: c_ierr
character(kind=c_char,len=1),allocatable :: temp1(:)
character(kind=c_char,len=1),allocatable :: temp2(:)

interface
  function c_link(c_oldname,c_newname) bind (C,name="link") result (c_ierr)
  import c_char,c_int
  character(kind=c_char,len=1),intent(in) :: c_oldname(*)
  character(kind=c_char,len=1),intent(in) :: c_newname(*)
  integer(kind=c_int)                     :: c_ierr
  end function c_link
end interface

   temp1 = str2_carr(trim(oldname)) ! kludge for bug in ifort (IFORT) 2021.3.0 20210609
   temp2 = str2_carr(trim(newname)) ! kludge for bug in ifort (IFORT) 2021.3.0 20210609
   c_ierr=c_link(temp1,temp2)
   ierr=c_ierr

end function system_link