Implementing the ls -al command in C

前端 未结 5 1800
误落风尘
误落风尘 2021-02-05 09:38

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

5条回答
  •  滥情空心
    2021-02-05 10:10

    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 dirents 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 ...

提交回复
热议问题