{"id":4395,"date":"2020-10-10T01:00:04","date_gmt":"2020-10-10T08:00:04","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4395"},"modified":"2020-10-10T07:48:46","modified_gmt":"2020-10-10T14:48:46","slug":"is-this-unix","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4395","title":{"rendered":"Is This Unix?"},"content":{"rendered":"<p>In the beginning, Ken Thompson, Dennis Ritchie, and others created Unix. And it was good. Then came the varieties and variants: BSD, System V, and eventually Linux. These are all similar operating systems, not identical, but with a common root and familiar features. You can&#8217;t call them all &#8220;Unix,&#8221; so over time various standards and terms have been developed.<\/p>\n<p>A term that frequently pops up when ruminating over the various Unixes and Linux distros, is POSIX.<br \/>\n<!--more--><br \/>\nNot an operating system, the Portable Operating System Interface (POSIX) family of standards helps define what makes a Unix-like OS. POSIX provides consistency. This effect extends to the C language, where a POSIX test can be made in your code to determine whether the program is running in a Unix\/Linux environment or something more perverse, such as Windows.<\/p>\n<p>In the <code>unistd.h<\/code> header file, the defined constant <code>_POSIX_VERSION<\/code> is declared. For a Unix, Linux, or other POSIX-obedient operating system, this value is assigned an integer value representing the POSIX version. In Windows, however, which also has the <code>unistd.h<\/code> header file available for its C compilers, the <code>_POSIX_VERSION<\/code> constant is undefined. Therefore, a test can be made in the code to determine whether the system has POSIX functions and features available.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_10_10-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_10_10-Lesson.c<\/a><\/h3>\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#ifndef _POSIX_VERSION\r\n    puts(\"This system is most likely not POSIX (Unix\/Linux)\");\r\n#else\r\n    printf(\"This system is POSIX standard %ld\\n\",_POSIX_VERSION);\r\n#endif\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code uses the <code>#ifndef<\/code> preprocessor directive to determine whether <code>_POSIX_VERSION<\/code> is defined. If not, Line 7 is executed, informing the user that the system isn&#8217;t POSIX-compatible, e.g., most likely Windows. Otherwise, the <code>#else<\/code> condition takes over, and the output shows the current POSIX version, as shown here:<\/p>\n<p><code>This system is POSIX standard 200112<\/code><\/p>\n<p>And for Windows:<\/p>\n<p><code>This system is most likely not POSIX (Unix\/Linux)<\/code><\/p>\n<p>Such a decision tree is useful in a code, such as <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4385\">last week&#8217;s Lesson<\/a>, where a POSIX function <em>ttyname()<\/em> is used. On a Windows computer, the code could be compiled, but the function would be part of an <code>#ifndef<\/code>\/<code>#else<\/code>\/<code>#endif<\/code> decision so that it compiles and presents a proper answer for the user.<\/p>\n<p>Way, way back in time, when I would code for the original IBM PC under DOS, I wrote decisions like this in my code after determining whether the system could generate color output. Back then, color monitors were expensive and rare. So a test was made and the program changed its output routines based on whether color was available. This is yet another practical example of using defined constants to alter the course of your program, and ensure that it&#8217;s compatible across a broad platform of systems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With so many C language functions specific to Unix, it helps to know whether your code is running on that platform. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4395\">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-4395","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\/4395","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=4395"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4395\/revisions"}],"predecessor-version":[{"id":4409,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4395\/revisions\/4409"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}