{"id":4101,"date":"2020-05-02T00:01:54","date_gmt":"2020-05-02T07:01:54","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4101"},"modified":"2020-05-09T08:17:03","modified_gmt":"2020-05-09T15:17:03","slug":"examining-file-type-and-permissions","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4101","title":{"rendered":"Examining File Type and Permissions"},"content":{"rendered":"<p>The value returned as a file&#8217;s inode mode is difficult to interpret, as covered in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4080\">last week&#8217;s Lesson<\/a>. That is, unless you use the macros and defined constants available in the <code>sys\/stat.h<\/code> header file.<br \/>\n<!--more--><br \/>\nSome of these macros are mentioned in my books, specifically when examining a directory listing to determine a file&#8217;s type. For example, the following <em>st_mode<\/em> member macros come in handy:<\/p>\n<p><code>S_ISDIR<\/code>, true when the file is a directory<br \/>\n<code>S_ISLNK<\/code>, true when the file is a symbolic link<br \/>\n<code>S_ISREG<\/code>, true when the file is a plain ol&#8217; &#8220;regular&#8221; file<\/p>\n<p>Other macros are available to test for other file types. To view the full list, examine the contents of the <code>sys\/stat.h<\/code> header file. Use the macros in your code to make sense of the <em>stat<\/em> structure&#8217;s <em>st_mode<\/em> member value. For example:<\/p>\n<p><code>if( S_ISREG(fs.st_mode) )<\/code><\/p>\n<p>The <code>S_ISREG<\/code> macro is used in an evaluation and returns TRUE when the file is a regular file. The following code performs this test:<\/p>\n<h3>2020_05_02-Lesson-a.c<\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;sys\/stat.h&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    const char *filename;\r\n    struct stat fs;\r\n    int r;\r\n\r\n    if( argc&lt;2 )\r\n    {\r\n        puts(\"Filename required\");\r\n        exit(1);\r\n    }\r\n\r\n    filename = argv[1];\r\n    printf(\"Obtaining permission mode for '%s':\\n\",filename);\r\n    r = stat(filename,&amp;fs);\r\n    if( r==-1 )\r\n    {\r\n        fprintf(stderr,\"File error\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* file permissions are kept in the st_mode member *\/<\/span>\r\n    <span class=\"comments\">\/* The S_ISREG() macro tests for regular files *\/<\/span>\r\n    if( S_ISREG(fs.st_mode) )\r\n        puts(\"Regular file\");\r\n    else\r\n        puts(\"Not a regular file\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s a sample run for the file <a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/09\/gettysburg.txt\">gettysburg.txt<\/a>:<\/p>\n<p><code>Obtaining permission mode for 'gettysburg.txt':<br \/>\nRegular file<\/code><\/p>\n<p>Further tests can be done to gather info on the file&#8217;s permissions. Logical AND comparisons are made with the value stored in the <em>st_mode<\/em> member to determine the owner, group, and other permissions for read, write, and execute. Figure 1 shows the matrix for these nine defined constants.<\/p>\n<div id=\"attachment_4103\" style=\"width: 360px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4103\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/05\/0502-figure1.png\" alt=\"Permissions options\" width=\"350\" height=\"292\" class=\"size-full wp-image-4103\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/05\/0502-figure1.png 350w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/05\/0502-figure1-300x250.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><p id=\"caption-attachment-4103\" class=\"wp-caption-text\">Figure 1. Permissions defined constants naming method.<\/p><\/div>\n<p>For example, if you want to test read permission for the &#8220;other&#8221; group, you use the <code>S_IROTH<\/code> defined constant: R for &#8220;read&#8221; and &#8220;OTH&#8221; for the &#8220;other&#8221; group. Here&#8217;s one way to examine this attribute:<\/p>\n<p><code>if( fs.st_mode & S_IROTH )<\/code><\/p>\n<p>When the evaluation above is TRUE, the file in question has read permission for the &#8220;other&#8221; group, meaning anyone on the system can read the file.<\/p>\n<p>The following code adds to the earlier example, processing the permissions bits stored in the <em>stat<\/em> structure&#8217;s <em>st_mode<\/em> member:<\/p>\n<h3>2020_05_02-Lesson-b.c<\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;sys\/stat.h&gt;\r\n\r\nint main()\r\n{\r\n    const char filename[] = \"gettysburg.txt\";\r\n    struct stat fs;\r\n    int r;\r\n\r\n    printf(\"Obtaining permission mode for '%s':\\n\",filename);\r\n    r = stat(filename,&amp;fs);\r\n    if( r==-1 )\r\n    {\r\n        fprintf(stderr,\"File error\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* file permissions are kept in the st_mode member *\/<\/span>\r\n    <span class=\"comments\">\/* The S_ISREG() macro tests for regular files *\/<\/span>\r\n    if( S_ISREG(fs.st_mode) )\r\n        puts(\"Regular file\");\r\n    else\r\n        puts(\"Not a regular file\");\r\n\r\n    printf(\"Owner permissions: \");\r\n    if( fs.st_mode &amp; S_IRUSR )\r\n        printf(\"read \");\r\n    if( fs.st_mode &amp; S_IWUSR )\r\n        printf(\"write \");\r\n    if( fs.st_mode &amp; S_IXUSR )\r\n        printf(\"execute\");\r\n    putchar('\\n');\r\n\r\n    printf(\"Group permissions: \");\r\n    if( fs.st_mode &amp; S_IRGRP )\r\n        printf(\"read \");\r\n    if( fs.st_mode &amp; S_IWGRP )\r\n        printf(\"write \");\r\n    if( fs.st_mode &amp; S_IXGRP )\r\n        printf(\"execute\");\r\n    putchar('\\n');\r\n\r\n    printf(\"Others permissions: \");\r\n    if( fs.st_mode &amp; S_IROTH )\r\n        printf(\"read \");\r\n    if( fs.st_mode &amp; S_IWOTH )\r\n        printf(\"write \");\r\n    if( fs.st_mode &amp; S_IXOTH )\r\n        printf(\"execute\");\r\n    putchar('\\n');\r\n  \r\n    return(0);\r\n}<\/pre>\n<p>(These source code files are available from my Github account: <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/github.com\/dangookin\/C-For-Dummies-Blog<\/a>.)<\/p>\n<p>Here&#8217;s a sample run on the <em>gettysburg.txt<\/em> file:<\/p>\n<p><code>Obtaining permission mode for 'gettysburg.txt':<br \/>\nRegular file<br \/>\nOwner permissions: read write<br \/>\nGroup permissions: read<br \/>\nOthers permissions: read<\/code><\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4117\">next week&#8217;s Lesson<\/a>, I cover how to set permissions for a file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thanks to macros and defined constants, you can determine a file&#8217;s type and evaluate its permissions. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4101\">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-4101","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\/4101","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=4101"}],"version-history":[{"count":10,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4101\/revisions"}],"predecessor-version":[{"id":4136,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4101\/revisions\/4136"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}