{"id":4125,"date":"2020-05-16T00:01:12","date_gmt":"2020-05-16T07:01:12","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4125"},"modified":"2020-05-16T08:14:12","modified_gmt":"2020-05-16T15:14:12","slug":"creating-a-file-in-the-raw-with-permissions","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4125","title":{"rendered":"Creating a File &#8220;in the Raw&#8221; &#8211; with Permissions"},"content":{"rendered":"<p>Way back <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4052\">in April<\/a>, I concluded my series on the &#8220;raw&#8221; file functions with a program that created a new file &mdash; but one that had no permissions. Thanks to input from readers and research into file-permission functions, I have a solution to the puzzle.<br \/>\n<!--more--><br \/>\nIn <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4036\">an earlier post<\/a>, I wrote about the <em>open()<\/em> function, documenting it like this:<\/p>\n<p><code>int open(const char *path, int oflag, ...);<\/code><\/p>\n<p>This format is exactly how <em>open()<\/em> is presented on its <em>man<\/em> page. I didn&#8217;t explain the <code>...<\/code> argument because its <em>man<\/em> page description is cryptic:<\/p>\n<blockquote><p>The <em><span style=\"text-decoration:underline\">oflag<\/span><\/em> argument may indicate that the file is to be created if it does not exist (by specifying the O_CREAT flag).  In this case, <strong>open<\/strong>() and <strong>openat<\/strong>() require an additional argument <em><span style=\"text-decoration:underline\">mode_t<\/span><\/em> <em><span style=\"text-decoration:underline\">mode<\/span><\/em>; the file is created with mode <em><span style=\"text-decoration:underline\">mode<\/span><\/em> as described in chmod(2) and modified by the process&#8217; umask value (see umask(2)).<\/p><\/blockquote>\n<p>I researched into <em>umask()<\/em> and <em>chmod()<\/em>, but never made the connection that the third argument consists of permission flags required when a new file is created.<\/p>\n<p>Worse: A problem presented itself only in OS X where the newly-created file had no permissions whatsoever. Other platforms didn&#8217;t show this issue.<\/p>\n<p>Regardless, now I know and can share that the mystery third argument in the <em>open()<\/em> function represents file permissions. The code below updates my original file-creation program, adding the proper permission bits to the freshly-minted file.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_05_16-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_05_16-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;fcntl.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main()\r\n{\r\n    char filename[] = \"file_output.txt\";\r\n    char text[] = \"What a lovely little file\\n\";\r\n    int fdes,r;\r\n\r\n    <span class=\"comments\">\/* open the file for writing and creating *\/<\/span>\r\n    fdes = open(filename,O_WRONLY|O_CREAT,S_IRUSR+S_IWUSR+S_IRGRP+S_IROTH);\r\n    if( fdes==-1 )\r\n    {\r\n        fprintf(stderr,\"Unable to create %s\\n\",filename);\r\n        return(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* write the buffer *\/<\/span>\r\n    r = write( fdes, text, sizeof(text)-1 );\r\n    if( r != sizeof(text)-1 )\r\n    {\r\n        fprintf(stderr,\"Some kind of file writing error\\n\");\r\n    }\r\n    <span class=\"comments\">\/* confirm success *\/<\/span>\r\n    printf(\"Data written to %s\\n\",filename);\r\n\r\n    <span class=\"comments\">\/* close 'er up *\/<\/span>\r\n    close(fdes);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The sole modification takes place at Line 12. Here&#8217;s how it looks in <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_04_04-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">the original code<\/a>:<\/p>\n<p><code>fdes = open(filename,O_WRONLY|O_CREAT);<\/code><\/p>\n<p>And the updated version above, the only change to the code:<\/p>\n<p><code>fdes = open(filename,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);<\/code><\/p>\n<p>The four constants are:<\/p>\n<p><code>S_IRUSR<\/code> owner read permission<br \/>\n<code>S_IWUSR<\/code> owner write permission<br \/>\n<code>S_IRGRP<\/code> group (wheel) read permission<br \/>\n<code>S_IROTH<\/code> other read permission<\/p>\n<p>Each is logically OR&#8217;d with each other to cumulatively generate the proper permissions.<\/p>\n<p>And the code works! Unlike the original attempt, the file is created, data output, and the file is blessed with the proper permissions to make it accessible.<\/p>\n<p>Even if the third argument to the <em>open()<\/em> function remained a mystery to you, it&#8217;s possible to use the <em>chmod()<\/em> function to alter the file&#8217;s permissions, as was covered in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4117\">last week&#8217;s Lesson<\/a>. Thankfully, this redundant step can be omitted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time to fix a problem program from an earlier blog post. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4125\">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-4125","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\/4125","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=4125"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4125\/revisions"}],"predecessor-version":[{"id":4148,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4125\/revisions\/4148"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}