{"id":1227,"date":"2015-03-07T00:01:08","date_gmt":"2015-03-07T08:01:08","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1227"},"modified":"2015-02-28T08:06:53","modified_gmt":"2015-02-28T16:06:53","slug":"other-ways-to-fetch-the-cwd","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1227","title":{"rendered":"Other Ways to Fetch the CWD"},"content":{"rendered":"<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1221\">last week&#8217;s Lesson<\/a> I mentioned the constant, <code>MAXPATHLEN<\/code>. Like <code>PATH_MAX<\/code>, it returns the size of the maximum number of characters allowed in a pathname. Unlike <code>PATH_MAX<\/code>, however, <code>MAXPATHLEN<\/code> is defined in the <code>sys\/param.h<\/code> header file, not <code>limits.h<\/code>.<br \/>\n<!--more--><br \/>\nWhy the two constants? Who knows!<\/p>\n<p>A better question is to determine whether or not they&#8217;re equal. After all, internally one may be assigned to the other. To confirm whether that&#8217;s the case, you can go spelunking in the header files, or just write some quick code to compare the values:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;limits.h&gt;\r\n#include &lt;sys\/param.h&gt;\r\n\r\nint main()\r\n{\r\n\tprintf(\"The value of PATH_MAX is %d\\n\",PATH_MAX);\r\n\tprintf(\"The value of MAXPATHLEN is %d\\n\",MAXPATHLEN);\r\n\r\n\treturn(0);\r\n}<\/pre>\n<p>Here is the output on my Mac:<\/p>\n<pre><code>The value of PATH_MAX is 1024\r\nThe value of MAXPATHLEN is 1024<\/code><\/pre>\n<p>By exploring the header files, I found the following line in <code>sys.param.h<\/code>:<\/p>\n<pre><code>#define MAXPATHLEN      PATH_MAX<\/code><\/pre>\n<p>There it is! So <code>MAXPATHLEN<\/code> is directly related to <code>PATH_MAX<\/code>, which is frequently the case when two constants represent the same value. One constant (probably <code>MAXPATHLEN<\/code>) is historical and included for backwards compatibility.<\/p>\n<p>While exploring these two constants, I noticed another function for reading the current directory, <em>getwd()<\/em>.<\/p>\n<p>Like <em>getcwd()<\/em>, the <em>getwd()<\/em> function also returns the current working directory. It requires the <code>unistd.h<\/code> header file. It&#8217;s a bit more concise than the <em>getcwd()<\/em> function, and it&#8217;s capable of returning a directory in one punch, as the following code demonstrates:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main()\r\n{\r\n\tprintf(\"Current directory is '%s'\\n\",getwd(NULL));\r\n\r\n\treturn(0);\r\n}<\/pre>\n<p>So why isn&#8217;t everyone using <em>getwd()<\/em> instead of <em>getcwd()<\/em>? How could I make such an oversight in my books? Easy.<\/p>\n<p>While using <em>getwd()<\/em> is pretty nifty, as the one-statement code above demonstrates, it&#8217;s not the best function choice for a variety of reasons.<\/p>\n<p>I stumbled upon <em>getwd()<\/em> as I was exploring the C library man pages. I recommend that you do so as well: Look up an item in the man pages and see the related functions. It&#8217;s a great way to expand your C knowledge.<\/p>\n<p>On the man page for <em>getwd()<\/em> (shared with <em>getcwd()<\/em>), it states the following:<\/p>\n<pre><code>\r\n<strong>BUGS<\/strong>\r\n     The <strong>getwd<\/strong>() function does not do sufficient error checking and is not\r\n     able to return very long, but valid, paths.  It is provided for compati-\r\n     bility.<\/code><\/pre>\n<p>The <em>getwd()<\/em> function apparently had some serious issues, so it was deprecated a while back. The current version is provided for compatibility and it calls <em>getcwd()<\/em> internally. So if you&#8217;re going to learn and use a C library function to fetch the current directory, use <em>getcwd()<\/em>, which is the function covered in my books.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The faster, more compact current working directory function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1227\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1227","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1227"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1227\/revisions"}],"predecessor-version":[{"id":1256,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1227\/revisions\/1256"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}