{"id":4080,"date":"2020-04-25T00:01:20","date_gmt":"2020-04-25T07:01:20","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4080"},"modified":"2020-05-02T07:45:15","modified_gmt":"2020-05-02T14:45:15","slug":"finding-file-permissions","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4080","title":{"rendered":"Finding File Permissions"},"content":{"rendered":"<p>All files carry with them something called an <em>inode<\/em>, which contains file data beyond the file&#8217;s name and its contents. The inode references the file&#8217;s size, its timestamps, and the file type or mode. It&#8217;s this file mode that determines how the file is used, which users can access the file, and what can be done with the file when accessed.<br \/>\n<!--more--><br \/>\nFile permissions come into play because they grant users read, write, and execute access to a file. If you own the file, you have full access. If you aren&#8217;t the owner, you may only be able to read the file &mdash; or not access it at all. The execute mode is granted to programs and directories (folders).<\/p>\n<p>In Unix-like operating systems, the file permissions are visible when displaying a long directory listing, such as:<\/p>\n<pre class=\"screen\">\r\n$ ls -l gettysburg.txt \r\n-rw-r--r-- 1 dang  staff  177 Sep  5  2015 gettysburg.txt<\/pre>\n<p>The inode mode, or file permissions, is the first bit of gobbledygook listed: <code>-rw-r--r--<\/code> This cryptograph isn&#8217;t difficult to read once you know its presentation. Figure 1 illustrates the details.<\/p>\n<div id=\"attachment_4084\" style=\"width: 522px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4084\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/04\/0425-figure1.png\" alt=\"figure 1 file permissions\" width=\"512\" height=\"351\" class=\"size-full wp-image-4084\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/04\/0425-figure1.png 512w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/04\/0425-figure1-300x206.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/04\/0425-figure1-438x300.png 438w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><p id=\"caption-attachment-4084\" class=\"wp-caption-text\">Figure 1. File permissions (mode) breakdown. File type followed by three triplets of access types: &#8220;r&#8221; read, &#8220;w&#8221; write, and &#8220;x&#8221; execute, one set for each user type: owner, group, other.<\/p><\/div>\n<p>For file type, you often see <code>l<\/code> (little L) for a symbolic link, <code>d<\/code> for a directory, or &#8220;-&#8221; for a regular file. The other three items are clustered into triplets of <code>rwx<\/code> for read, write, and execute permissions: the first set is for the owner, the file creator; the second set is for a user group; the final set is for users not in the specific group (others).<\/p>\n<p>The shell command <em>chmod<\/em> is used by the file&#8217;s owner to change permissions, a topic I need not bore you with. For C, however, examining a file&#8217;s mode and its permissions falls under control of the <em>stat()<\/em> function, which returns file details, specifically information stored in the inode. This function is prototyped in the <code>sys\/stat.h<\/code> header file:<\/p>\n<p><code>int stat(const char *restrict path, struct stat *restrict buf);<\/code><\/p>\n<p>The first argument, <code>path<\/code>, is a string representing a pathname or filename. The second argument, <code>buf<\/code>, is the address (pointer) of a <em>stat<\/em> structure variable.<\/p>\n<p>In my books, you find the <em>stat()<\/em> function used to gather a file&#8217;s size and timestamp, and for examining a directory. Within the <em>stat<\/em> structure, however, you can access the <em>st_mode<\/em> member to divine a file&#8217;s mode, including its permissions:<\/p>\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    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    printf(\"Permission bits: %X\\n\",fs.st_mode);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>At Line 19, the <em>stat()<\/em> function examines the file provided at the command prompt. The <code>fs<\/code> <em>stat<\/em> structure&#8217;s address is passed to the function as its second argument.<\/p>\n<p>At Line 27, then the <code>fs<\/code> structure&#8217;s <em>st_mode<\/em> element is output. Here is the result when the program is run on 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 \/>\nPermission bits: 81A4<\/code><\/p>\n<p>The hexadecimal value <code>81A4<\/code> is obviously less useful than the <code>-rw-r--r--<\/code> thing shown earlier, though both are related. To make the <em>st_mode<\/em> member relevant, you can employ macros and defined constants, which assist in sifting through the binary data. I cover this topic in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4101\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the details all files carry is which of the machine&#8217;s users can access the file and in what ways the access is made. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4080\">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-4080","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\/4080","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=4080"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4080\/revisions"}],"predecessor-version":[{"id":4122,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4080\/revisions\/4122"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}