Implementation of ls -l. How to calculate the number after total?

  • Thread starter Tsvetelina Borisova
  • Start date
T

Tsvetelina Borisova

Hello, I am writing a program in C that implements the UNIX command ls -l. I understood that the number after total is the blocks that are given to the files? I am completely sure because I couldn't find information about it.Can someone help me and tell me what it is that number and how I can calculated to show it in my implementation. Thanks in advance :)
 
J

Jens Thoms Toerring

Tsvetelina Borisova said:
Hello, I am writing a program in C that implements the UNIX command ls -l. I
understood that the number after total is the blocks that are given to the
files? I am completely sure because I couldn't find information about it.
Can someone help me and tell me what it is that number and how I can
calculated to show it in my implementation. Thanks in advance :)

This isn't a C question but about something UNIX specific, so it
would be better to ask in e.g. comp.unix.programmer (follow-up
set). The best reference for such questions is the POSIX stan-
dard, nowadays called SUS (single Unix Specifications). You'll
find it at

http://pubs.opengroup.org/onlinepubs/9699919799/

(but this might not be the very newest version). There click
on "Shell & Utilities" in the upper left hand panel, then on
"Utilities" in the lower left hand panel, and then select
"ls" in what now appears in the the same panel. The text
describes how 'ls' is supposed to work, the exact output
formats etc. If what you call 'total' is the length of the
file in bytes the following field is the date and time,
at least according to that standard. But keep in mind that
not all implementations of 'ls' may follow this standard.

If you instead want to outpt the number of blocks used by
the file you should ask in a group that deals with the
specifics of the operating system you're writing this
for - it's completely system-dependent, so there can't be
any general "C answer".
Regards, Jens
 
P

Paul

Tsvetelina said:
Hello, I am writing a program in C that implements the UNIX command ls -l.
I understood that the number after total is the blocks that are given to
the files? I am completely sure because I couldn't find information about it.
Can someone help me and tell me what it is that number and how I can calculated
to show it in my implementation. Thanks in advance :)

Click the Coreutils "sources" link, to get a 9,371,720 byte download.
The source for "ls" is in there, and likely in a million other places.
This would be a GNU version of "ls", and you can use these packages
on a Windows system.

http://gnuwin32.sourceforge.net/packages/coreutils.htm

"Sources Zip 9371720 21 April 2005 MD5sum = 5cbd86c56e6eb29b6af2810849d08c8c

( http://iweb.dl.sourceforge.net/project/gnuwin32/coreutils/5.3.0/coreutils-5.3.0-src.zip )

If you download the documentation link, it has a PDF document in it

"Documentation Zip 4540924 21 April 2005 MD5sum = ee0b456daf011d6e348cc64adafe968a

The coreutils.pdf document (1,315,345 bytes) has the documentation. This is
what it lists for the -l option on PDF page 63.

*******
‘-l’
‘--format=long’
‘--format=verbose’

In addition to the name of each file, print the file type, permissions, number of hard
links, owner name, group name, size, and timestamp (see Section 10.1.6 [Formatting
file timestamps], page 59), normally the modification time.
Normally the size is printed as a byte count without punctuation, but this can
be overridden (see Section 2.3 [Block size], page 3). For example, ‘-h’ prints an
abbreviated, human-readable count, and ‘--block-size="’1"’ prints a byte count
with the thousands separator of the current locale.
For each directory that is listed, preface the files with a line ‘total blocks’, where
blocks is the total disk allocation for all files in that directory. The block size currently
defaults to 1024 bytes, but this can be overridden (see Section 2.3 [Block size],
page 3). The blocks computed counts each hard link separately; this is arguably a
deficiency.
*******

With so many systems around, running a copy of "ls", you can modify your
code until it matches the output of the system provided copy of "ls".
For example, on my Windows computer here, I run virtual machine software
(you can get VirtualBox for free), and you can run a Linux distribution
in there, to observe the "ls" command in action. Or, the Coreutils
package above, will allow you to run a port of "ls" in Windows.

"The block size currently defaults to 1024 bytes" tells you how the
block size in a directory is derived.

HTH,
Paul
 
K

Kaz Kylheku

Hello, I am writing a program in C that implements the UNIX command ls -l. I
understood that the number after total is the blocks that are given to the
files?

man 2 stat

look for st_blocks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,829
Messages
2,569,737
Members
45,521
Latest member
maryb

Latest Threads

Top