title: detecting loops in symbolic links (c programming)
body: <p>I'm looking to detect loops in symbolic links in a C program:</p>
  
  <pre><code>$ ln -s self self
  $ ln -s a b
  $ ln -s b a
  </code></pre>
  
  <p>Here's what I've got so far:</p>
  
  <pre><code>#include &lt;sys/stat.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  
  int 
  main(int argc, char *argv[])
  {
       struct stat buffer;
       int status;
  
       if (argc != 2) {
            fprintf(stderr, "error: file name required\n");
            return 0;
       }
  
       errno = 0;
       status = lstat(argv[1], &amp;buffer);
  
       if (errno == ELOOP) {
            fprintf(stderr, "loop found");
       }
  
       return 1;
  }
  </code></pre>
  
  <p>I'm running my program like this:</p>
  
  <pre><code>$ findloops self
  $ findloops a
  </code></pre>
  
  <p>Any idea what I'm doing wrong?</p>
  
  <p>This is <strong>NOT</strong> homework.</p>
  
  <p><a href="http://publib.boulder.ibm.com/infocenter/zos/v1r10/index.jsp?topic=/com.ibm.zos.r10.bpxbd00/rtlsta.htm" rel="nofollow noreferrer">This</a> is where I got the idea from.</p>