⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getcwd.html

📁 posix标准英文,html格式
💻 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>getcwd</title></head><body bgcolor="white"><basefont size="3"> <a name="getcwd"></a> <a name="tag_03_215"></a><!-- getcwd --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2004 Edition<br>Copyright &copy; 2001-2004 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_215_01"></a>NAME</h4><blockquote>getcwd - get the pathname of the current working directory</blockquote><h4><a name="tag_03_215_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include &lt;<a href="../basedefs/unistd.h.html">unistd.h</a>&gt;<br><br> char *getcwd(char *</tt><i>buf</i><tt>, size_t</tt> <i>size</i><tt>);<br></tt></code></p></blockquote><h4><a name="tag_03_215_03"></a>DESCRIPTION</h4><blockquote><p>The <i>getcwd</i>() function shall place an absolute pathname of the current working directory in the array pointed to by<i>buf</i>, and return <i>buf</i>. The pathname copied to the array shall contain no components that are symbolic links. The<i>size</i> argument is the size in bytes of the character array pointed to by the <i>buf</i> argument. If <i>buf</i> is a nullpointer, the behavior of <i>getcwd</i>() is unspecified.</p></blockquote><h4><a name="tag_03_215_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, <i>getcwd</i>() shall return the <i>buf</i> argument. Otherwise, <i>getcwd</i>() shall return a nullpointer and set <i>errno</i> to indicate the error. The contents of the array pointed to by <i>buf</i> are then undefined.</p></blockquote><h4><a name="tag_03_215_05"></a>ERRORS</h4><blockquote><p>The <i>getcwd</i>() function shall fail if:</p><dl compact><dt>[EINVAL]</dt><dd>The <i>size</i> argument is 0.</dd><dt>[ERANGE]</dt><dd>The <i>size</i> argument is greater than 0, but is smaller than the length of the pathname +1.</dd></dl><p>The <i>getcwd</i>() function may fail if:</p><dl compact><dt>[EACCES]</dt><dd>Read or search permission was denied for a component of the pathname.</dd><dt>[ENOMEM]</dt><dd>Insufficient storage space is available.</dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_215_06"></a>EXAMPLES</h4><blockquote><h5><a name="tag_03_215_06_01"></a>Determining the Absolute Pathname of the Current Working Directory</h5><p>The following example returns a pointer to an array that holds the absolute pathname of the current working directory. Thepointer is returned in the <i>ptr</i> variable, which points to the <i>buf</i> array where the pathname is stored.</p><pre><tt>#include &lt;stdlib.h&gt;#include &lt;unistd.h&gt;...long size;char *buf;char *ptr;<br>size = pathconf(".", _PC_PATH_MAX);<br>if ((buf = (char *)malloc((size_t)size)) != NULL)    ptr = getcwd(buf, (size_t)size);...</tt></pre></blockquote><h4><a name="tag_03_215_07"></a>APPLICATION USAGE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_215_08"></a>RATIONALE</h4><blockquote><p>Since the maximum pathname length is arbitrary unless {PATH_MAX} is defined, an application generally cannot supply a <i>buf</i>with <i>size</i> {{PATH_MAX}+1}.</p><p>Having <i>getcwd</i>() take no arguments and instead use the <a href="../functions/malloc.html"><i>malloc</i>()</a> function toproduce space for the returned argument was considered. The advantage is that <i>getcwd</i>() knows how big the working directorypathname is and can allocate an appropriate amount of space. But the programmer would have to use the <a href="../functions/free.html"><i>free</i>()</a> function to free the resulting object, or each use of <i>getcwd</i>() would furtherreduce the available memory. Also, <a href="../functions/malloc.html"><i>malloc</i>()</a> and <a href="../functions/free.html"><i>free</i>()</a> are used nowhere else in this volume of IEEE&nbsp;Std&nbsp;1003.1-2001. Finally,<i>getcwd</i>() is taken from the SVID where it has the two arguments used in this volume of IEEE&nbsp;Std&nbsp;1003.1-2001.</p><p>The older function <a href="../functions/getwd.html"><i>getwd</i>()</a> was rejected for use in this context because it had onlya buffer argument and no <i>size</i> argument, and thus had no way to prevent overwriting the buffer, except to depend on theprogrammer to provide a large enough buffer.</p><p>On some implementations, if <i>buf</i> is a null pointer, <i>getcwd</i>() may obtain <i>size</i> bytes of memory using <a href="../functions/malloc.html"><i>malloc</i>()</a>. In this case, the pointer returned by <i>getcwd</i>() may be used as the argumentin a subsequent call to <a href="../functions/free.html"><i>free</i>()</a>. Invoking <i>getcwd</i>() with <i>buf</i> as a nullpointer is not recommended in conforming applications.</p><p>If a program is operating in a directory where some (grand)parent directory does not permit reading, <i>getcwd</i>() may fail,as in most implementations it must read the directory to determine the name of the file. This can occur if search, but not read,permission is granted in an intermediate directory, or if the program is placed in that directory by some more privileged process(for example, login). Including the [EACCES] error condition makes the reporting of the error consistent and warns the applicationwriter that <i>getcwd</i>() can fail for reasons beyond the control of the application writer or user. Some implementations canavoid this occurrence (for example, by implementing <i>getcwd</i>() using <a href="../utilities/pwd.html"><i>pwd</i></a>, where <ahref="../utilities/pwd.html"><i>pwd</i></a> is a set-user-root process), thus the error was made optional. Since this volume ofIEEE&nbsp;Std&nbsp;1003.1-2001 permits the addition of other errors, this would be a common addition and yet one that applicationscould not be expected to deal with without this addition.</p></blockquote><h4><a name="tag_03_215_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_215_10"></a>SEE ALSO</h4><blockquote><p><a href="malloc.html"><i>malloc</i>()</a>, the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/unistd.h.html"><i>&lt;unistd.h&gt;</i></a></p></blockquote><h4><a name="tag_03_215_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_215_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 [ENOMEM] 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 &reg; is a registered Trademark of The Open Group.<br>POSIX &reg; 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 + -