Thread: Problems with stat() <sys/stat.h>

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    22

    Problems with stat() <sys/stat.h>

    Hi guys, I have a project where we have to implement the ls command and some options.

    One of those options is -l, and so I am trying to get the file information in hand.

    I am using this for the code:
    Code:
    getcwd(fileName, (MAX_FNAME + 1));
    strcat(fileName, "/");
    
    if (stat(fileName, status) == -1) {
      puts("Status Failed!");
      return 0;
    }
    No matter what I pass in as the fileName, I get -1 back =\
    I have checked what fileName is, and it looks ok to me...

    Any ideas?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    And what is errno set to?

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I might be wrong about this, but maybe you have to put the current working directory path before the filename?

    Also reading the manpage entry it says that you need permissions set to traverse though all dierctories along the path, but I doubt thats the problem.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    22
    I did a perror instead of printf (I don't know how to get errno..)

    and I got "Bad address"...

    So I printed out fileName, and I get this: .../home/project5/.

    That period is messing me up? Lemme try to fix it

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    22
    Hm... I have this in between the strcat and the if (stat...

    Code:
    if ((strcmp(dirent[i]->d_name, ".") != 0) ||
    	(strcmp(dirent[i]->d_name, "..") != 0)){
        puts("1");
        strcat(fileName, dirent[i]->d_name);
    }
    And somehow when the d_name is ".", it stills gets to that line =|

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Follow the (broken) logic. You're saying if it's not equal to either one, it's ok to be added. That means it will always add it.

    Hint: strcmp() returns 0 on a match.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    22
    Quote Originally Posted by MacGyver View Post
    Follow the (broken) logic. You're saying if it's not equal to either one, it's ok to be added. That means it will always add it.

    Hint: strcmp() returns 0 on a match.
    Hee hee xD
    I wanted a "&&" there. ;D

    Now the fileName, which is the path, is correct.. The only thing is that I still get the same error when trying to use stat() =\

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    22
    Quote Originally Posted by robwhit View Post
    And what is errno set to?
    errno is 14

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    22
    I figured it out! Teehee!

    status wasn't allocated anywhere, that was the problem.. hihi xD

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by headbr View Post
    errno is 14
    That should have a corresponding #define for it. Check your man page for stat to see what the values for errno are that it sets, and test them against it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-06-2007, 04:17 AM
  2. Random Number Range Problem.
    By xamlit in forum C Programming
    Replies: 11
    Last Post: 01-26-2006, 12:55 PM
  3. Capturing file stat information
    By Sue Paterniti in forum C Programming
    Replies: 3
    Last Post: 04-15-2002, 05:47 AM
  4. Shortening main
    By pdstatha in forum C Programming
    Replies: 1
    Last Post: 04-03-2002, 04:56 PM
  5. that damn stat()
    By @licomb in forum Linux Programming
    Replies: 2
    Last Post: 08-22-2001, 04:24 PM