📄 link.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2004 IEEE and The Open Group, All Rights Reserved --><title>link</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="link"></a> <a name="tag_03_333"></a><!-- link --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2004 Edition<br>Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_333_01"></a>NAME</h4><blockquote>link - link to a file</blockquote><h4><a name="tag_03_333_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include <<a href="../basedefs/unistd.h.html">unistd.h</a>><br><br> int link(const char *</tt><i>path1</i><tt>, const char *</tt><i>path2</i><tt>);<br></tt></code></p></blockquote><h4><a name="tag_03_333_03"></a>DESCRIPTION</h4><blockquote><p>The <i>link</i>() function shall create a new link (directory entry) for the existing file, <i>path1</i>.</p><p>The <i>path1</i> argument points to a pathname naming an existing file. The <i>path2</i> argument points to a pathname namingthe new directory entry to be created. The <i>link</i>() function shall atomically create a new link for the existing file and thelink count of the file shall be incremented by one.</p><p>If <i>path1</i> names a directory, <i>link</i>() shall fail unless the process has appropriate privileges and the implementationsupports using <i>link</i>() on directories.</p><p>Upon successful completion, <i>link</i>() shall mark for update the <i>st_ctime</i> field of the file. Also, the <i>st_ctime</i>and <i>st_mtime</i> fields of the directory that contains the new entry shall be marked for update.</p><p>If <i>link</i>() fails, no link shall be created and the link count of the file shall remain unchanged.</p><p>The implementation may require that the calling process has permission to access the existing file.</p></blockquote><h4><a name="tag_03_333_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, 0 shall be returned. Otherwise, -1 shall be returned and <i>errno</i> set to indicate the error.</p></blockquote><h4><a name="tag_03_333_05"></a>ERRORS</h4><blockquote><p>The <i>link</i>() function shall fail if:</p><dl compact><dt>[EACCES]</dt><dd>A component of either path prefix denies search permission, or the requested link requires writing in a directory that denieswrite permission, or the calling process does not have permission to access the existing file and this is required by theimplementation.</dd><dt>[EEXIST]</dt><dd>The <i>path2</i> argument resolves to an existing file or refers to a symbolic link.</dd><dt>[ELOOP]</dt><dd>A loop exists in symbolic links encountered during resolution of the <i>path1</i> or <i>path2</i> argument.</dd><dt>[EMLINK]</dt><dd>The number of links to the file named by <i>path1</i> would exceed {LINK_MAX}.</dd><dt>[ENAMETOOLONG]</dt><dd>The length of the <i>path1</i> or <i>path2</i> argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}.</dd><dt>[ENOENT]</dt><dd>A component of either path prefix does not exist; the file named by <i>path1</i> does not exist; or <i>path1</i> or<i>path2</i> points to an empty string.</dd><dt>[ENOSPC]</dt><dd>The directory to contain the link cannot be extended.</dd><dt>[ENOTDIR]</dt><dd>A component of either path prefix is not a directory.</dd><dt>[EPERM]</dt><dd>The file named by <i>path1</i> is a directory and either the calling process does not have appropriate privileges or theimplementation prohibits using <i>link</i>() on directories.</dd><dt>[EROFS]</dt><dd>The requested link requires writing in a directory on a read-only file system.</dd><dt>[EXDEV]</dt><dd>The link named by <i>path2</i> and the file named by <i>path1</i> are on different file systems and the implementation does notsupport links between file systems.</dd><dt>[EXDEV]</dt><dd><sup>[<a href="javascript:open_code('XSR')">XSR</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"><i>path1</i> refers to a named STREAM. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd></dl><p>The <i>link</i>() function may fail if:</p><dl compact><dt>[ELOOP]</dt><dd>More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the <i>path1</i> or <i>path2</i> argument.</dd><dt>[ENAMETOOLONG]</dt><dd>As a result of encountering a symbolic link in resolution of the <i>path1</i> or <i>path2</i> argument, the length of thesubstituted pathname string exceeded {PATH_MAX}.</dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_333_06"></a>EXAMPLES</h4><blockquote><h5><a name="tag_03_333_06_01"></a>Creating a Link to a File</h5><p>The following example shows how to create a link to a file named <b>/home/cnd/mod1</b> by creating a new directory entry named<b>/modules/pass1</b>.</p><pre><tt>#include <unistd.h><br>char *path1 = "/home/cnd/mod1";char *path2 = "/modules/pass1";int status;...status = link (path1, path2);</tt></pre><h5><a name="tag_03_333_06_02"></a>Creating a Link to a File Within a Program</h5><p>In the following program example, the <i>link</i>() function links the <b>/etc/passwd</b> file (defined as <b>PASSWDFILE</b>) toa file named <b>/etc/opasswd</b> (defined as <b>SAVEFILE</b>), which is used to save the current password file. Then, afterremoving the current password file (defined as <b>PASSWDFILE</b>), the new password file is saved as the current password fileusing the <i>link</i>() function again.</p><pre><tt>#include <unistd.h><br>#define LOCKFILE "/etc/ptmp"#define PASSWDFILE "/etc/passwd"#define SAVEFILE "/etc/opasswd".../* Save current password file */link (PASSWDFILE, SAVEFILE);<br>/* Remove current password file. */unlink (PASSWDFILE);<br>/* Save new password file as current password file. */link (LOCKFILE,PASSWDFILE);</tt></pre></blockquote><h4><a name="tag_03_333_07"></a>APPLICATION USAGE</h4><blockquote><p>Some implementations do allow links between file systems.</p></blockquote><h4><a name="tag_03_333_08"></a>RATIONALE</h4><blockquote><p>Linking to a directory is restricted to the superuser in most historical implementations because this capability may produceloops in the file hierarchy or otherwise corrupt the file system. This volume of IEEE Std 1003.1-2001 continues thatphilosophy by prohibiting <i>link</i>() and <a href="../functions/unlink.html"><i>unlink</i>()</a> from doing this. Other functionscould do it if the implementor designed such an extension.</p><p>Some historical implementations allow linking of files on different file systems. Wording was added to explicitly allow thisoptional behavior.</p><p>The exception for cross-file system links is intended to apply only to links that are programmatically indistinguishable from"hard" links.</p></blockquote><h4><a name="tag_03_333_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_333_10"></a>SEE ALSO</h4><blockquote><p><a href="symlink.html"><i>symlink</i>()</a>, <a href="unlink.html"><i>unlink</i>()</a>, the Base Definitions volume ofIEEE Std 1003.1-2001, <a href="../basedefs/unistd.h.html"><i><unistd.h></i></a></p></blockquote><h4><a name="tag_03_333_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 1. Derived from Issue 1 of the SVID.</p></blockquote><h4><a name="tag_03_333_12"></a>Issue 6</h4><blockquote><p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p><ul><li><p>The [ELOOP] mandatory error condition is added.</p></li><li><p>A second [ENAMETOOLONG] is added as an optional error condition.</p></li></ul><p>The following changes were made to align with the IEEE P1003.1a draft standard:</p><ul><li><p>An explanation is added of the action when <i>path2</i> refers to a symbolic link.</p></li><li><p>The [ELOOP] optional error condition is added.</p></li></ul></blockquote><div class="box"><em>End of informative text.</em></div><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -