{"id":17,"date":"2013-04-27T00:01:28","date_gmt":"2013-04-27T08:01:28","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=17"},"modified":"2013-05-04T18:48:48","modified_gmt":"2013-05-05T02:48:48","slug":"hello-environment","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=17","title":{"rendered":"Hello, Environment"},"content":{"rendered":"<p>The operating system keeps several variables in memory, variables that hold information necessary to running the computer. For example, the path variable lists directories in which the operating system looks for programs. The prompt variable describes how to display the command prompt. Some programs even create variables, allowing the programs to save configuration or other information.<br \/>\n<!--more--><br \/>\nTo read the environment, your program uses the <code>environ<\/code> pointer array. It must be declared as an external variable, as shown in the following code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nextern char **environ;\r\n\r\nint main()\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 above dumps the environment by plowing through the environ array one string at a time. (The <code>environ<\/code> variable is declared in the <code>stdlib.h<\/code> header file.)<\/p>\n<p>When you know a specific value to fetch, you use the <em>getenv()<\/em> function, as demonstrated in the following code:<\/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>The code above uses the <em>getenv()<\/em> function to fetch the <code>PATH<\/code> environment variable. A pointer to that variable is returned, which the program stores in the local address <code>searchpath<\/code>. Further manipulation of the path would require additional code, for example, to check to see whether a specific directory is on the path or not.<\/p>\n<p>While you could manipulate the environment by checking the <code>environ<\/code> variable, the man pages recommend that you use <em>getenv()<\/em> to fetch items. More importantly, you should use the <em>putenv()<\/em> function to add an item to the environment or the <em>setenv()<\/em> or <em>unsetenv()<\/em> functions to change an item.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The operating system keeps an environment, chock full of informative variables. Use the C language to read and even manipulate that information. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=17\">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-17","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\/17","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=17"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":45,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions\/45"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}