{"id":6237,"date":"2024-02-10T00:01:25","date_gmt":"2024-02-10T08:01:25","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6237"},"modified":"2024-02-17T09:03:22","modified_gmt":"2024-02-17T17:03:22","slug":"peering-into-the-environment","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6237","title":{"rendered":"Peering into the Environment"},"content":{"rendered":"<p>The operating system&#8217;s environment provides temporary storage for variables, settings, and options. These values are easily accessible from any C program. One of the <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=17\">first posts<\/a> I made on this blog was about accessing the environment. Time to revisit.<br \/>\n<!--more--><br \/>\nYou can view the environment from a terminal window. In Linux, type the <em>env<\/em> command to see a huge dump of environment variables. For Windows\/DOS, type the <em>set<\/em> command.<\/p>\n<p>The output shows a list of environment variables. It&#8217;s often long and detailed. The format for each variable is the same. For example:<\/p>\n<p><code>HOME=\/home\/dang<\/code><\/p>\n<p>The <code>HOME<\/code> variable in Linux indicates the user&#8217;s home directory. It&#8217;s followed by an equal sign, then the home directory path, which is <code>\/home\/dang<\/code> for my account on this system.<\/p>\n<p>Internally, the environment exists as a pointer array. It&#8217;s a list of addresses, with each address referencing a string. This array exists as a global variable available to any C program. Include the <code>stdlib.h<\/code> header file to ensure that your system has access to this variable and the various environment function prototypes.<\/p>\n<p><code>extern char **environ;<\/code><\/p>\n<p>To view the environment, you cycle through the pointers stored at <code>**environ<\/code> until the NULL pointer is encountered:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_02_10-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2024_02_10-Lesson-a.c<\/a><\/h3>\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    extern char **environ;\r\n\r\n    while( *environ )\r\n    {\r\n        printf(\"%s\\n\",*environ);\r\n        environ++;\r\n    }\r\n    return 0;\r\n}<\/pre>\n<p>The <code>**environ<\/code> variable is declared externally. A <em>while<\/em> loop processes each address stored at <code>**environ<\/code> until the NULL is ecountered at the end of the list. A <em>printf()<\/em> statement outputs each string.<\/p>\n<p>Because <code>environ<\/code> is a pointer-pointer, its value is the size of an address. Incrementing the <code>environ<\/code> variable references the next address (string) in the list: <code>environ++<\/code><\/p>\n<p>The program&#8217;s output is the same as the <em>env<\/em> or <em>set<\/em> command&#8217;s dump at the command prompt.<\/p>\n<p>You might think that to fetch an item from the list, you use something like this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_02_10-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2024_02_10-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    extern char **environ;\r\n\r\n    puts(\"Search path:\");\r\n    while( *environ )\r\n    {\r\n        if( strncmp(*environ,\"PATH\",4)==0 )\r\n        {\r\n            printf(\"%s\\n\",*environ);\r\n            break;\r\n        }\r\n        environ++;\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This code adds the <em>strncmp()<\/em> function to the original example. The function scans the first four characters of the <code>*environ<\/code> string to match <code>PATH<\/code>. Once found, the search path is output. This output can be quite lengthy, so I&#8217;m not duplicating it here.<\/p>\n<p>The environ <em>man<\/em> page, however, recommends that you not search the environment manually. Instead, use the <em>getenv()<\/em> function to fetch a specific entry:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_02_10-Lesson-c.c\" rel=\"noopener\" target=\"_blank\">2024_02_10-Lesson-c.c<\/a><\/h3>\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    puts(\"Search path:\");\r\n    printf(\"%s\\n\",getenv(\"PATH\"));\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>getenv()<\/em> function is prototyped in the <code>stdlib.h<\/code> header file. Its argument is a string matching a variable to find in the environment table. The return value is the variable&#8217;s value or NULL when no match is found.<\/p>\n<p>Just as a user can set an environment variable, so can a program. I cover the required tools in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6245\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The environment lists system variables and values your program can access. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6237\">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-6237","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\/6237","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=6237"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6237\/revisions"}],"predecessor-version":[{"id":6260,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6237\/revisions\/6260"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}