title: How to create a file inside the `/etc` folder in Linux with C?
body: <p>I'm writing a program in C that will have to check a <strong>configuration file</strong> every time it starts to set some variables.</p>
  
  <p>At the first start of the program I suppose there won't be any configuration file, so <strong>I need to create it</strong> (with default settings).</p>
  
  <p>I've been said configurations files of program belongs to the folder <code>/etc</code>, more specifically to a particular folder created on purpose for the program itself (i.e. <code>/etc/myprog</code>). Here comes the first question I should have asked: is it true? Why <code>/etc</code>?</p>
  
  <p>In any case I tried to create that file using this:</p>
  
  <pre><code>open("/etc/myprog/myprog.conf", O_WRONLY | O_CREAT, 0644);
  </code></pre>
  
  <p>the <code>open</code> returns <code>-1</code> and sets <code>errno</code> global variable to <code>2</code> (i.e. <strong>folder does not exist</strong>). 
  If I try to create the file straight inside <code>/etc</code> (therefore <code>"/etc/myprog.conf"</code> as first argument of the <code>open</code>) I get instead an <code>errno</code> set to <code>13</code> (i.e. <strong>permission denied</strong>).</p>
  
  <p>Is there a way to grant my program permissions to write in <code>/etc</code>?</p>
  
  <p><strong>EDIT:</strong> I see most users are suggesting to use <code>sudo</code>. If possible I would have preferred to avoid this option as this file has to be created just once (at the first start). Maybe I should make 2 different executables? (e.g. <code>myprog_bootstrap</code> and <code>myprog</code>, having to run only the first one with <code>sudo</code>)</p>