{"id":3535,"date":"2019-03-16T00:01:16","date_gmt":"2019-03-16T07:01:16","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3535"},"modified":"2019-03-09T10:12:44","modified_gmt":"2019-03-09T18:12:44","slug":"fetching-the-search-path","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3535","title":{"rendered":"Fetching the Search Path"},"content":{"rendered":"<p>The search path is a list of directories in which the operating system scans for programs. Its purpose is to allow quick access to common programs and system utilities without the necessity of typing a full pathname or changing to the right directory to start an application.<br \/>\n<!--more--><br \/>\nBack in the text mode days, the search path was extremely helpful. It remains so today in the terminal window: Directories full of common programs are listed on the path. When you type a command, the operating system searches each directory on the path for a matching program file.<\/p>\n<p>Various methods exist to display the search path, but you can write a quick utility in C that displays each directory on a line by itself. To do so, you must examine the system&#8217;s environment and look for the PATH variable. I demonstrated such a program back in 2013&#8217;s <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=17\">blog post<\/a> on the environment:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    char *searchpath;\r\n\r\n    searchpath = getenv(\"PATH\");\r\n    printf(\"The search path is '%s'\\n\",searchpath);\r\n    return(0);\r\n}<\/pre>\n<p>With just a few modifications, you can dump out the directory list by scanning through the string returned (<code>searchpath<\/code>) and replacing the path separator character with a newline.<\/p>\n<p>In Windows, the path separator character is <code>;<\/code>, the semicolon.<\/p>\n<p>For Unix, the path separator character is <code>:<\/code>, the colon.<\/p>\n<p>Here&#8217;s the code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\n#define SEPARATOR ';'\r\n#define SEPARATOR ':'\r\n\r\nint main()\r\n{\r\n    char *searchpath,*s;\r\n\r\n    searchpath = getenv(\"PATH\");\r\n    s = searchpath;\r\n\r\n    while(*s)\r\n    {\r\n        if( *s == SEPARATOR)\r\n            putchar('\\n');\r\n        else\r\n            putchar(*s);\r\n        s++;\r\n    }\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Comment out Line 4 or 5, depending on the OS.<\/p>\n<p>Character pointer <code>s<\/code> is initialized to the base of the string returned at Line 12.<\/p>\n<p>The <em>while<\/em> loop at Line 14 steps through the string one character at a time. If the character matches the <code>SEPARATOR<\/code>, a newline is output. Otherwise, the character found is output.<\/p>\n<p>Line 20 increments variable <code>s<\/code> to keep moving through the string.<\/p>\n<p>Here&#8217;s sample output from my Mac:<\/p>\n<p><code>\/usr\/local\/bin<br \/>\n\/usr\/bin<br \/>\n\/bin<br \/>\n\/usr\/sbin<br \/>\n\/sbin<br \/>\n\/Users\/dang\/bin<\/code><\/p>\n<p>In Windows, the list is much longer. It&#8217;s been tradition in Windows (since the days of MS-DOS) to stack directories in the path for each program installed. Some applications are getting better at avoiding this noisome tradition, but you still find plenty that insist upon putting their own directories on the path.<\/p>\n<p>In my Mac&#8217;s path, you see the directory <code>\/Users\/dang\/bin<\/code>. Into this directory I place all my command line utilities, most of which are written in C but with a few in Bash shell script and some in Perl. Having this personal <code>bin<\/code> directory allows me to use my utilities in any directory when I&#8217;m working in a terminal window &mdash; which is quite often. I find the C language quite practical for writing such short, useful command line utilities, as did Unix titans Ken Thompson, Brian Kernighan, and Dennis Ritchie. I hope you do as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a handy utility that displays directories specified by the operating system in which to find executable (program) files. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3535\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3535","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\/3535","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=3535"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3535\/revisions"}],"predecessor-version":[{"id":3540,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3535\/revisions\/3540"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}