summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libc/link.c
blob: 38ce78d87aba56624d3ae93223ab62d53ad92383 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 *  link() - POSIX 1003.1b - 5.3.4 - Create a new link
 *
 *  COPYRIGHT (c) 1989-1998.
 *  On-Line Applications Research Corporation (OAR).
 *  Copyright assigned to U.S. Government, 1994.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.OARcorp.com/rtems/license.html.
 *
 *  $Id$
 */

#include <rtems.h>
#include <rtems/libio.h>
#include <errno.h>

#include "libio_.h"

int link(
  const char *existing,
  const char *new
)
{
  rtems_filesystem_location_info_t    existing_loc;
  rtems_filesystem_location_info_t    parent_loc;
  int                                 i;
  int                                 result;
  const char                         *name_start;

  /*
   * Get the node we are linking to.
   */

  result = rtems_filesystem_evaluate_path( existing, 0, &existing_loc, TRUE );
  if ( result != 0 )
     return -1;

  /*
   * Get the parent of the node we are creating.
   */

  rtems_filesystem_get_start_loc( new, &i, &parent_loc );
  result = (*parent_loc.ops->evalformake)( &new[i], &parent_loc, &name_start );
  if ( result != 0 ) {
    if ( existing_loc.ops->freenod )
      (*existing_loc.ops->freenod)( &parent_loc );
    set_errno_and_return_minus_one( result );
  }

  /*
   *  Check to see if the caller is trying to link across file system
   *  boundaries.
   */

  if ( parent_loc.mt_entry != existing_loc.mt_entry ) {
    if ( existing_loc.ops->freenod )
      (*existing_loc.ops->freenod)( &existing_loc );

    if ( parent_loc.ops->freenod )
      (*parent_loc.ops->freenod)( &parent_loc );

    set_errno_and_return_minus_one( EXDEV );
  }

  if ( !parent_loc.ops->link ) {

    if ( existing_loc.ops->freenod )
      (*existing_loc.ops->freenod)( &existing_loc );

    if ( parent_loc.ops->freenod )
      (*parent_loc.ops->freenod)( &parent_loc );

    set_errno_and_return_minus_one( ENOTSUP );
  }

  result = (*parent_loc.ops->link)( &existing_loc, &parent_loc, name_start );
  
  if ( existing_loc.ops->freenod )
    (*existing_loc.ops->freenod)( &existing_loc );

  if ( parent_loc.ops->freenod )
    (*parent_loc.ops->freenod)( &parent_loc );

  return result;
}

/*
 *  _link_r
 *
 *  This is the Newlib dependent reentrant version of link().
 */

#if defined(RTEMS_NEWLIB)

#include <reent.h>

int _link_r(
  struct _reent *ptr,
  const char    *existing,
  const char    *new
)
{
  return link( existing, new );
}
#endif