title: using getline and strtok together in a loop
body: <p>I am new to C, and trying to implement whoami, as an exercise to myself. I have following code:</p>
  
  <pre><code>#define _POSIX_SOURCE
  #include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt; // strtok
  
  int str_to_int(const char *str)
  {
      int acc = 0;
      int i;
      for (i = 0; str[i] != '\0'; ++i) {
          acc = (10 * acc) + (str[i] - 48); // 48 -&gt; 0 in ascii
      }
      return acc;
  }
  
  int main()
  {
      FILE *passwd;
      char *line = NULL;
      size_t line_size;
  
      passwd = fopen("/etc/passwd","r");
  
      uid_t uid = getuid();
  
      while (getline(&amp;line, &amp;line_size,passwd) != -1) {
          char *name = strtok(line,":");
          strtok(line,":"); // passwd
          char *user_id = strtok(line,":");
          if (str_to_int(user_id) == uid) {
              printf("%s\n",name);
              break;
          }
      }
  
      fclose(passwd);
      return 0;
  }
  </code></pre>
  
  <p>Do I need to save line pointer inside of the while loop. Because I think strtok modifies it somehow, but I am not sure if I need to copy the line, or starting address of the line before I use it with strtok.</p>