Thread: realpath() resolves path, but errno is set to 2

  1. #1
    Registered User Andy_PBC's Avatar
    Join Date
    Jan 2011
    Location
    MN
    Posts
    7

    realpath() resolves path, but errno is set to 2

    cur_file is an argument from the command line. I've used the absolute path for testing, though it works the same way if I gave it the basename (a) for an argument - the path still resolves to the absolute path and errno is set to 2.

    Code:
      char real_path[PATH_MAX];
      printf ("%d\n", errno);
      
      if (realpath (cur_file, real_path) == NULL)
      {
        printf ("%d\n", errno);
        perror ("realpath");
        printf ("%s\n", cur_file);
        printf ("%s\n", real_path);
      }
    Output:

    0
    2
    realpath: No such file or directory
    /home/andy/temp/a
    /home/andy/temp/a
    Can anyone tell me why realpath() is saying there's no such file, even though it can resolve the path successfully?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Presumably one of the symbolic links in your input path is broken. What is the result of passing the input path to the file command in the terminal?

  3. #3
    Registered User Andy_PBC's Avatar
    Join Date
    Jan 2011
    Location
    MN
    Posts
    7
    @algorism: file says "/home/andy/temp/a: empty".

    It's a zero-byte file created with touch.

    This is my current solution:
    Code:
      char real_path[PATH_MAX];
      real_path[0] = '\0';
    
      realpath (cur_file, real_path);
    
      if (!strlen (real_path))
        return 1;

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Sorry, I didn't read your question very well. I see what you're doing now. In that case, it's a very strange result. It's the result you would expect if /home/andy/temp existed but /home/andy/temp/a didn't exist.

    I don't see how your current solution could solve anything, since your original post shows real_path receiving a string.

    All I can suggest is that you try it again with the original code and ensure that everything is set up properly.

  5. #5
    Registered User Andy_PBC's Avatar
    Join Date
    Jan 2011
    Location
    MN
    Posts
    7
    Quote Originally Posted by algorism View Post
    Sorry, I didn't read your question very well. I see what you're doing now. In that case, it's a very strange result. It's the result you would expect if /home/andy/temp existed but /home/andy/temp/a didn't exist.
    Yes, agree, very strange. It's the first time I ever checked for an error after using realpath(), and it was purely by accident; I was adding code to check for fopen() and fclose() for errors, then saw that errno was getting changed. I tracked it down to my realpath() line.

    I don't see how your current solution could solve anything, since your original post shows real_path receiving a string.
    Thought I'd use it for an error check, since, as you noted, real_path is receiving a string. So I figured that "current solution" would be a good way to make sure, while ignoring the actual return value of realpath() which is apparently NULL.

    I'm not really sure how necessary it is to check for an error though, but I guess it can help make the program more robust.

    All I can suggest is that you try it again with the original code and ensure that everything is set up properly.
    Thanks. I've studied the code but haven't figured out why realpath() is messing with me (~2000 lines of code split across several source files). If I think of anything else or find out the cause, I'll follow up here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 10-06-2010, 02:28 PM
  2. About errno
    By beachcoder in forum C Programming
    Replies: 3
    Last Post: 07-24-2009, 08:31 AM
  3. errno
    By galindro in forum C Programming
    Replies: 23
    Last Post: 04-03-2008, 02:53 PM
  4. where from extern int errno came ?
    By jabka in forum C Programming
    Replies: 2
    Last Post: 10-14-2007, 05:25 PM
  5. errno, rmdir
    By PunkAE in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2001, 01:26 PM

Tags for this Thread