title: system call "open" cannot create file with O_CREAT flag
body: <p>I've got to complete an exercise using linux's system calls:</p>
  
  <ol>
  <li>open the file "testinput.txt" (already present in the working directory, not empty)</li>
  <li>open the file "testoutput.txt" (not present in the working directory)</li>
  <li>copy the content of testinput in testoutput</li>
  <li>close both files</li>
  </ol>
  
  <p>I found that I can open the file with system call <code>open</code> can both open the input file (flag <code>O_RDONLY</code>) and the output file (flag <code>O_CREAT | O_WRONLY</code>).</p>
  
  <p>The input file works correctly. The output file doesn't.</p>
  
  <p>Errno says code 2 = "No such file or direcory" (obviously, I asked to create the file). What am I doing wrong?</p>
  
  <p>P.S.: To highlight I'm using syscalls, I'm not calling function <code>open(...)</code> but <code>syscall(SYS_OPEN, ...)</code></p>
  
  <pre><code>outFileDesc = syscall(SYS_OPEN, "testoutput.txt", O_WRONLY | O_TRUNC | O_CREAT, 438);
  if(outFileDesc == -1) {
      int code = errno;
  
      // gdb used here -&gt; print code -&gt; 2
  
      char str[] = "Unable to open \"testoutput.txt\"\n";
      syscall(SYS_WRITE, FILE_DESC_STDOUT, str, sizeof(str)/sizeof(char));
      syscall(SYS_CLOSE, inFileDesc);
      syscall(SYS_EXIT, 1);
      return 1;   
  }
  </code></pre>
  
  <p>Whole code:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;sys/syscall.h&gt;
  #include &lt;errno.h&gt;
  
  /* file flags fcntl.h */
  #define O_RDONLY    0x0000      /* open for reading only */
  #define O_WRONLY    0x0001      /* open for writing only */
  #define O_CREAT     0x0200      /* create if nonexistant */
  #define O_TRUNC     0x0400      /* truncate to zero length */
  
  #define FILE_DESC_STDOUT 1
  
  #define SYS_EXIT 1
  #define SYS_READ 3
  #define SYS_WRITE 4
  #define SYS_OPEN 5
  #define SYS_CLOSE 6
  
  #define IN_MESSAGE_LEN 9
  #define OUT_MESSAGE_LEN 9
  
  char inBuffer[IN_MESSAGE_LEN];
  char outBuffer[OUT_MESSAGE_LEN];
  
  int main (int argc, char *argv[])
  {
      int inFileDesc, outFileDesc;
  
      // Apertura fine di input
      inFileDesc = syscall(SYS_OPEN, "testinput.txt", O_RDONLY, 438);
      if(inFileDesc == -1) {
          char str[] = "Unable to open \"testinput.txt\"\n";
          syscall(SYS_WRITE, FILE_DESC_STDOUT, str, sizeof(str)/sizeof(char));
          syscall(SYS_EXIT, 1);
          return 1;   
      }
  
      // Apertura fine di output
      outFileDesc = syscall(SYS_OPEN, "testoutput.txt", O_WRONLY | O_TRUNC | O_CREAT, 438);
      if(outFileDesc == -1) {
          int code = errno;
  
          char str[] = "Unable to open \"testoutput.txt\"\n";
          syscall(SYS_WRITE, FILE_DESC_STDOUT, str, sizeof(str)/sizeof(char));
          syscall(SYS_CLOSE, inFileDesc);
          syscall(SYS_EXIT, 1);
          return 1;   
      }
  
      // Travaso contenuto file di input in file di output
      int read, i;
      while((read = syscall(SYS_READ, inFileDesc, inBuffer, IN_MESSAGE_LEN)) != 0) {
  
          for(i = 0; i &lt; IN_MESSAGE_LEN; i++) {
              outBuffer[i] = inBuffer[i];
          }
  
          syscall(SYS_WRITE, outFileDesc, outBuffer, OUT_MESSAGE_LEN);
      }
  
      syscall(SYS_CLOSE, inFileDesc);
      syscall(SYS_CLOSE, outFileDesc);
  
      syscall(SYS_EXIT, 0);
      return 0;
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/zePnH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zePnH.png" alt="Screenshot of the execution"></a></p>