title: UID program resulting in Invalid Characters
body: <p>I'm not sure if this was the right group to post in because this topic could fit under multiple categories, but I hope you guys can help.</p>
  
  <p>So I have a book and one of the programs in the book are as follows:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/stat.h&gt;
  #include "hacking.h"
  
     void usage(char *prog_name, char *filename) {
          printf("Usage:\t %s &lt;data to add to %s&gt;\n", prog_name, filename);
          exit(0);
      }
  
  void fatal(char *);
  void *ec_malloc(unsigned int);
  
  int main(int argc, char *argv[]) {
      int file_descriptor;
      int userid;
      char *buffer, *datafile;
  
      buffer = (char *) ec_malloc(100);
      datafile = (char *) ec_malloc(20);
      strcpy(datafile, "/var/notes");
  
      if(argc &lt; 2) {
      usage(argv[0], datafile);
      }
  
      strcpy(buffer, argv[1]); // Copy message into buffer
  
      printf("[DEBUG] buffer @ %p and holds %s\n", buffer, buffer);
      printf("[DEBUG] datafile @ %p and holds %s\n", datafile, datafile);
  
      // Open File
      file_descriptor = open(datafile, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
      if(file_descriptor == -1) {
          fatal("in main() opening file");
      }
      printf("file_descriptor: %i\n", file_descriptor);
  
      userid = getuid(); // Get real User ID
  
      // Write to File
      if(write(file_descriptor, &amp;userid, 4) == -1) { // Write userid before file
          fatal("in main() writing userid to file");
      }
      write(file_descriptor,  "\n", 1); // Terminate line
  
      if(write(file_descriptor, buffer, strlen(buffer)) == -1) {
          fatal("in main() writing to file");
      }
      write(file_descriptor, "\n", 1);
  
      // Closing File
      if(close(file_descriptor) == -1) {
      fatal("in main() closing file");
      }
  
      printf("Note has been saved\n");
      free(buffer);
      free(datafile);
  }
  </code></pre>
  
  <p>So what that program is supposed to do is take the string entered on the command line and put it into a text file in var named notes. Before doing that, it is supposed to write the UID inside the file. When I run the program on root (I created it with root and ran it first with root), it works no problem and writes the UID to be \00\00\00\00 because it should be zero. However if I log out of my root account and go to my other account (which isnt root), I run it and I get the error that I dont have permission to open the file. That is because /var/notes was created using root. This all makes sense. So what i did was used</p>
  
  <pre><code>sudo chmod 777 /var/notes 
  </code></pre>
  
  <p>to be able to access it on my user account. Then I re-ran the program above and it left the note I left to be copied into it, however the UID came out as unknown characters. Exactly, they are: \E8\00\00
  Now that shouldn't be but how come this is happening? The UID on that account is 1000.</p>