Thread: Distinguish symbolic link from file (stat function)

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    13

    Distinguish symbolic link from file (stat function)

    Hi, I'm new to the forum. Sorry fo my bad English
    I've a problem to distinguish symbolic link from file, I created a link ("mylink") to a file, but when I run the following code,
    1. On Debian (gcc-4.3), I get that it is a regular file
    2. On Windows (Dev-C++), I get this error: "undefined reference to S_ISLNK"

    Code:
    #include <stdio.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    
    struct stat file;
    int status;
        
    main() {
         status = stat("/home/mario/Desktop/mylink", &file);
         if (S_ISLNK(file.st_mode))
    	  printf("Is a link\n");
         else if (S_ISREG(file.st_mode))
    	  printf("Is a file\n");
    }
    How can I fix the problem?
    Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Probably because Windows doesn't support symbolic links? And your English is fine.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    13
    Quote Originally Posted by rags_to_riches View Post
    Probably because Windows doesn't support symbolic links? And your English is fine.
    Why on Debian it is not identified as a link?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Because stat() returns information about the file that the link refers to.
    To get information about the symbolic link itself, use the lstat() call.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    13
    Is there a way to fix the problem on Windows?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by mario++ View Post
    Is there a way to fix the problem on Windows?
    Yes, sure. In windows, remove all the code and make it:
    Code:
    printf("Is a file\n");
    As stated before, Windows doesn't support symbolic links. So files are never symbolic links.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by EVOEx View Post
    Yes, sure. In windows, remove all the code and make it:
    Code:
    printf("Is a file\n");
    As stated before, Windows doesn't support symbolic links. So files are never symbolic links.
    I'm sure he's referring to Windows Shortcuts as 'symbolic links' though.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    13
    Quote Originally Posted by cpjust View Post
    I'm sure he's referring to Windows Shortcuts as 'symbolic links' though.
    Yes, I'm refferring to Windows Shortcut

  9. #9
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by EVOEx View Post
    As stated before, Windows doesn't support symbolic links.
    In the best Botchamania tradition: Huh!? Symbolic Links (Windows)

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    13
    Quote Originally Posted by mario++ View Post
    Yes, I'm refferring to Windows Shortcut
    What I mean: Is it possible to distinguish a Windows Shortcut (.lnk) from a file?

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Quote Originally Posted by mario++ View Post
    What I mean: Is it possible to distinguish a Windows Shortcut (.lnk) from a file?
    The Windows Shortcut is nothing more than a glorified ini file (last time i checked). It has no special meaning whatsoever on the filesystem unlike the *nix symoblic links.

    Since a Windows Shortcut is recognizable by its extension, you answered your own question.
    Last edited by Florian; 05-07-2010 at 04:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM