0

I am doing an assignment that requires me to create a function similar to ls. My code works fine but when it comes to implementing the behaviour of

ls -l child //where child is a folder

there is a weird behaviour.

Let's say i am in the folder 'parent' and in it i have a subfolder, 'child' which contains some text files. When i run my program from the parent folder, it finds the folder and prints out the attributes of the text files in it. However, it will only print the files in the child folder only if the same files itself exists in the parent folder.

Here is a snippet of the code that i am using,

    char CurrDir[100];
    DIR *pDir = NULL;
    struct dirent *pFileNames = NULL;

    getcwd(CurrDir, sizeof(CurrDir))

    strncat(CurrDir, "/", strlen(CurrDir));

    unsigned int CurrDirLen = strlen(CurrDir);
    unsigned int CombSize = CurrDirLen + strlen(argv[1]);

    char SuperCharArr[CombSize];

    for(int i = 0; i < CombSize; ++i)
    {
        if( i < strlen(CurrDir) )
            SuperCharArr[i] = CurrDir[i];
        else
            SuperCharArr[i] = argv[1][i%CurrDirLen];
    }//for

    //insert null character at the end of the character
    SuperCharArr[CombSize] = '\0';

    pDir = opendir( SuperCharArr );
    printf("%s\n", SuperCharArr);

    if( pDir != NULL )
    {
        //Directory detected as pDir is a DirectoryStream
        printf("%s\n", "pDir not null");
        PrintHeader();

        while( (pFileNames = readdir(pDir)) != NULL )
        {
            PrintFileDeails(pFileNames);
        }
    }//if
winhung
  • 553
  • 1
  • 5
  • 19
  • 1
    I've not used `readdir()` but I imagine it returns only the file __name__, not the full __path__. In other words you need to qualify `pFileNames->d_name` with the `SuperCharArr` pathname before you can use it. – AAT Oct 20 '14 at 10:32
  • @AAT yes but at every loop it returns the filename and it's attributes of each file in the directory that you are reading from which in this case is represented by the pDir pointer. However, thank you for your suggestion. – winhung Oct 22 '14 at 06:12
  • Wait, your comment made me check a few things and i think you are right. It is checking in the wrong path. At least, in my function, PrintFileDeails(), it is. Thank you, i will post the right answer soon. – winhung Oct 22 '14 at 06:39
  • OK glad to have been able to help! – AAT Oct 22 '14 at 08:02

1 Answers1

1

In my original code posted here, there was a function called, PrintFileDeails(pFileNames), which takes in a parameter of type direct.

Within PrintFileDeails(), there is a function that checks on the status of the file and the code is as follows,

struct stat FileStat;

if( stat(pFileNames->d_name, &FileStat) == -1 )
{
    perror("stat");
    exit(EXIT_FAILURE);
}//if

This line of code would print out an error where they couldn't find the file and with AAT's comment made me go thru my code again as i suspect that it was not reading the correct folder. Hence, after i passed the full path of where it is supposed to read the file from and it worked fine. Hence, the code was changed to this instead.

if( stat(pFullPath, &FileStat) == -1 )
{
    perror("stat");
    exit(EXIT_FAILURE);
}//if

where pFullPath was passed the variable of SuperCharArr which contained the full path of where the file to be searched was.

The man page for stat() helped too and it can be found here

winhung
  • 553
  • 1
  • 5
  • 19
  • You could also use dirfd() + fstatat() instead of stat()'ing the full path. Better for a variety of reasons, but somewhat newish so less portable. – janneb Oct 22 '14 at 08:21