As a part of an assignment from one of my classes, I have to write a program in C to duplicate the results of the ls -al command. I have read up on the necessary materials but I
I believe you'll observe that if you ./a.out .
you will get the behaviour you expect.
You have a slightly subtle bug, observable if you examine the return code of your call to stat(2)
.
The fundamental mistake: the dirent
s returned by readdir(2)
(the myfile
in your code) will have a d_name
relative to mydir
. Your code will stat
..
first, succeed, and so mystat
will contain valid data for ..
, then all subsequent calls to stat(2)
will fail, returning -1
, which you do not check for, so mystat
will not be modified, and you will print the st_size
for the old value, i.e. that of ..
.