-1

so I've almost finished my assignment where I basically recreated the UNIX ls -l command, but the only thing missing is the 'total #' that is shown at the beginning of the ls -l command as shown below.

total 68

From my understanding this is the total number of disk blocks taken by the directory. Is there a way to find this value from the stat function call itself? Additionally, I apologize if this may be a repeated question, but I couldn't find any resources that provided clear information. Thanks in advance.

noel880
  • 35
  • 6

1 Answers1

1

You have to compute this total (from individual file sizes). Use stat(2) combined with directory reads, e.g. opendir(3), readdir(3), closedir etc.

You could be interested by nftw(3).

Is there a way to find this value from the stat function call itself?

No, because you need to cumulate the sizes.

BTW, consider also du(1), at least for inspiration. On Linux, it is -like ls(1) is- free software, part of coreutils. So you could study its source code (and/or use strace(1) to understand the system calls it is doing).

Look also into this answer to a related question. What I wrote there is useful to know for you.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547