Directories and files using dirent.h

This is a discussion on Directories and files using dirent.h within the C Programming forums, part of the General Programming Boards category; I've the following code written in C and it displays files (name and size) and directories (<dir> if it encounters ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    19

    Directories and files using dirent.h

    I've the following code written in C and it displays files (name and size) and directories (<dir> if it encounters directory which is not hidden or not link) but it's not able to pass testing arguments test. I haven't done this test myself (it's just given to me to test my codes), that's why I don't know the reason for failing this test.
    Code:
    int main(int argc, char* argv[])
    {
    DIR *pdir;
    struct dirent *pent;
    struct stat statbuf;
    if(argc>1)
    {
        pdir=opendir(argv[1]);
    } else {
        pdir=opendir(".");
    }
    
    //chdir(pdir);
    
    if (!pdir){
        printf ("opendir() failure; terminating\n");
        exit(1);
    }
    
    while ((pent=readdir(pdir))!=NULL)
    {
        stat(pent->d_name,&statbuf);
        if(strcmp(".", pent->d_name) == 0 || strcmp("..", pent->d_name) == 0)
            continue;
        if(S_ISDIR(statbuf.st_mode))
            printf("%s <dir>\n", pent->d_name);
        if(!(S_ISDIR(statbuf.st_mode)))
            printf("%s %d\n", pent->d_name, statbuf.st_size);
    }
    closedir(pdir);
    return 0;
    }
    That's the test:
    Code:
    #!/bin/bash
    
    LS_PY=./myls.py
    LS_C=./myls
    
    TMP1=~/lsout1
    TMP2=~/lsout2
    TMPERR=~/lserr
    
    echo Testing files
    $LS_PY > $TMP1
    $LS_C > $TMP2
    diff $TMP1 $TMP2 && echo OK || echo ERROR
    
    echo Testing dirs
    mkdir testdir
    $LS_PY > $TMP1
    $LS_C > $TMP2
    diff $TMP1 $TMP2 && echo OK || echo ERROR
    rmdir testdir
    
    echo Testing arguments
    $LS_PY / > $TMP1
    $LS_C / > $TMP2
    diff $TMP1 $TMP2 && echo OK || echo ERROR
    
    echo Testing error-arguments - should see error below
    $LS_C nonexistentdirectory > /dev/null && echo ERROR - RETURN ZERO || echo RETURN value OK
    
    echo Testing error-arguments - should see error below
    $LS_C tests.sh > /dev/null && echo ERROR - RETURN ZERO || echo RETURN value OK

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Presumably the output of this program doesn't match the output of the python version. More than that I cannot say.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One candidate in the first test is that $TMP1 and $TMP2 are created by the respective calls to myls, which means that the second call to myls will see a different set of files than the first.

    Other than that, diff will see differences if the file is not identical - even a space may be enough to make it think the content is different.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    19
    Numbers are the reason for error. I cannot locate the exact place where this error stems from. All I can say is that I have to change something in C code. Although at the moment this code seems to me fine and very logical. But it induces some random numbers instead of string "<dir>" when comparing names.
    That's the error:
    Code:
    Testing arguments
    1,15c1,15
    < tmp <dir>
    < usr <dir>
    < sys <dir>
    < opt <dir>
    < nonexistent <dir>
    < boot <dir>
    < srv <dir>
    < sbin <dir>
    < bin <dir>
    < lib <dir>
    < etc <dir>
    < proc <dir>
    < media <dir>
    < initrd <dir>
    < mnt <dir>
    ---
    > tmp -1078670360
    > usr -1078670360
    > sys -1078670360
    > opt -1078670360
    > nonexistent -1078670360
    > boot -1078670360
    > srv -1078670360
    > sbin -1078670360
    > bin -1078670360
    > lib -1078670360
    > etc -1078670360
    > proc -1078670360
    > media -1078670360
    > initrd -1078670360
    > mnt -1078670360
    ERROR

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seems like your S_ISDIR isn't working right, as it appears that you are printing the size of a directory rather than the <dir> marker.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    stat() returns a result.
    Perhaps test for success before looking at st_mode for results.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    stat() returns a result.
    Perhaps test for success before looking at st_mode for results.
    Good point. The value -1078670360 is a stack-addres in Linux, so I would guess that stat is actually returning an error and your code isn't getting anything sensible back from stat().

    One possible reason is that you are doing stat on the wrong file - I didn't look at it carefully, but if you are listing files in /, you may want to form a filename of /tmp for stat to give you valid values. Otherwise, it will be looking in the current directory for something called tmp - which I'd guess isn't there. Some work with strcat() would help here.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and deleting directories and deleting files
    By fguy817817 in forum C Programming
    Replies: 1
    Last Post: 04-08-2009, 08:26 AM
  2. problem while opening files from multiple directories
    By V.G in forum Windows Programming
    Replies: 2
    Last Post: 11-08-2004, 03:29 PM
  3. listing files in directories portably
    By ab384 in forum C Programming
    Replies: 2
    Last Post: 11-03-2004, 10:50 AM
  4. Replies: 4
    Last Post: 06-11-2004, 07:18 PM
  5. retrieving drive directories and files
    By PURE_POWER in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-01-2003, 03:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21