Home | History | Annotate | Download | only in sys
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
     28 
     29 #pragma	weak _link = link
     30 
     31 #include "lint.h"
     32 #include <stdlib.h>
     33 #include <errno.h>
     34 #include <unistd.h>
     35 #include <limits.h>
     36 
     37 extern	int __xpg4; /* defined in port/gen/xpg4.c; 0 if not xpg4/xpg4v2 */
     38 
     39 extern int __link(const char *existing, const char *new);
     40 
     41 int
     42 link(const char *existing, const char *new)
     43 {
     44 	int 	sz;
     45 	char 	linkbuf[PATH_MAX + 1];
     46 
     47 	/*
     48 	 * XPG4v2 link() requires that the link count of a symbolic
     49 	 * link target be updated rather than the link itself.  This
     50 	 * matches SunOS 4.x and other BSD based implementations.
     51 	 * However, the SVR4 merge apparently introduced the change
     52 	 * that allowed link(src, dest) when "src" was a symbolic link,
     53 	 * to create "dest" as a hard link to "src".  Hence, the link
     54 	 * count of the symbolic link is updated rather than the target
     55 	 * of the symbolic link. This latter behavior remains for
     56 	 * non-XPG4 based environments. For a more detailed discussion,
     57 	 * see bug 1256170.
     58 	 */
     59 	if (__xpg4 != 0) {
     60 		if ((sz = resolvepath(existing, linkbuf, PATH_MAX)) == -1)
     61 			return (-1);
     62 		linkbuf[sz] = '\0';
     63 		existing = linkbuf;
     64 	}
     65 	return (__link(existing, new));
     66 }
     67