own handwriting Implementation ls-l command tangled for a long time, just finally figured out. Directly on the code important part
Long Ntotalblocks = 0;dir* DP = opendir (dirpathname); struct dirent* dirp;while ((DIRP = Readdir (DP)) = NULL) {Lstat (FULLP Ath, &sbuf); Long nblocks = sbuf.st_size/4096;if (sbuf.st_size% 4096 > 0) nblocks++;if (S_islnk (sbuf.st_mode)) NBlo cks = 0;ntotalblocks + = nblocks;} Closedir (DP), cout << "Total" << ntotalblocks * 4 << Endl;
4096 is the default size of the block, and since Total is in K (1024), it is multiplied by 4.
Total is actually all the files accounted for the sum of the size of the block, because the file size is not necessarily a multiple of the standard 4096, such as a file size of 10000, you need to occupy 10000/4096+1=3 block, the size of the occupied block is 3*4096, K is the unit is 3*4096 /1024=12.
A lot of people on the net say all right, but missing a key part, that is ls-l is not calculated symbolic link, because the symbolic link st_size represents the length of the address of the symbolic links, a closer look, APUEv3 Chinese version of the P97 4.17 symbolic link of the example can be seen.
How does the total of the linux command ls-l be calculated?