{"id":1221,"date":"2015-02-28T00:01:32","date_gmt":"2015-02-28T08:01:32","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1221"},"modified":"2015-03-07T08:03:34","modified_gmt":"2015-03-07T16:03:34","slug":"fetching-the-current-path","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1221","title":{"rendered":"Fetching the Current Path"},"content":{"rendered":"<p>The C library hosts many file and directory management functions. They&#8217;re all pretty standard, no matter which operating system you use. What isn&#8217;t standard, however, is the size of a pathname. That value plays an important role if you&#8217;re to properly allocate memory to store directory information.<br \/>\n<!--more--><br \/>\nIn my book, <em>Beginning Programming with C For Dummies<\/em>, I did not properly allocate memory for a path. Nope, I just guessed. In Exercises <a href=\"http:\/\/www.c-for-dummies.com\/begc4d\/exercises\/ex2305.php\">23-5<\/a> and <a href=\"http:\/\/www.c-for-dummies.com\/begc4d\/exercises\/ex2306.php\">23-6<\/a> I use the immediate value 255 for a buffer to hold the current directory.<\/p>\n<p>An option better than just guessing is to use the <code>PATH_MAX<\/code> constant. It&#8217;s defined in the <code>limits.h<\/code> header file.<\/p>\n<blockquote><p>One of the reasons I didn&#8217;t use <code>PATH_MAX<\/code> in my book is that it&#8217;s not available on all systems, which would require further explanation and a solution similar to what&#8217;s presented in this Lesson.<\/p><\/blockquote>\n<p>In the following code, <code>PATH_MAX<\/code> is used to create storage for the current working directory. The code uses preprocessor directives (covered in <a href=\" http:\/\/c-for-dummies.com\/blog\/?p=1216\">last week&#8217;s Lesson<\/a>) to determine whether <code>PATH_MAX<\/code> exists. If so, its value is used, otherwise the pulled-it-out-of-thin-air value of 255 is set.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;limits.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\n#ifndef PATH_MAX\r\n#define PATH_MAX 255\r\n#endif <span class=\"comments\">\/* PATH_MAX *\/<\/span>\r\n\r\nint main()\r\n{\r\n    char *path;\r\n\r\n<span class=\"comments\">\/* Allocate memory for the current path *\/<\/span>\r\n    path = (char *)malloc(PATH_MAX);\r\n    if( path == NULL)\r\n    {\r\n        perror(\"Unable to allocate memory\");\r\n        exit(1);\r\n    }\r\n\r\n<span class=\"comments\">\/* Fetch and display directory pathname *\/<\/span>\r\n    getcwd(path,PATH_MAX);\r\n    printf(\"Current directory is '%s'\\n\",path);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code uses <em>malloc()<\/em> to allocate storage for the current working directory&#8217;s path. That way an overflow won&#8217;t occur and I&#8217;m not wasting more space than is necessary.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>Current directory is '\/Users\/dang\/prog\/c\/blog'<\/code><\/pre>\n<p>Seeing how ancient the C language is getting, it&#8217;s easy to understand that <code>PATH_MAX<\/code> isn&#8217;t the only constant available to check how large the current path buffer can be. Another constant, <code>MAXPATHLEN<\/code>, is available. It&#8217;s defined in the <code>sys\/param.h<\/code> header file.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1227\">next week&#8217;s Lesson<\/a>, I&#8217;ll compare the <code>PATH_MAX<\/code> and <code>MAXPATHLEN<\/code> constants to see whether they differ. I&#8217;ll also present an alternative to this Lesson&#8217;s code that uses only one statement to perform the same task.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Which directory is the program using? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1221\">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-1221","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\/1221","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=1221"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1221\/revisions"}],"predecessor-version":[{"id":1261,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1221\/revisions\/1261"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}