[cs631apue] Midterm Question

Jan Schaumann jschauma at stevens.edu
Sat Sep 22 23:04:37 EDT 2012


tparisi <tparisi at stevens.edu> wrote:

> I have a couple of questions.  My first is that when we have to to
> write the number of blocks used by the files in the directory when the
> "-l" option is used, shouldn't this be the sum of all of the st_blocks
> (in stat) because when I use the real ls -l option I do not get that
> number.

Be careful: there are a few different options relating to "blocks", and
some specify 512 byte blocks, others whatever blocksize is in use or was
specified.

By default, when you run 'ls -l', you do not get any number of blocks
for the files -- you get for the directory you're listing the total
number of 512 byte blocks used by the files.

$ cd /tmp
$ ls -l
total 232
drwx------ 2 kjordan1 student    4096 Sep 22 17:08 CRX_75DAF8CB7768
-rw------- 1 jschauma professor   250 Sep 17 18:22 a.c
-rwx------ 1 jschauma professor  8752 Sep 17 18:22 a.out
srwxr-xr-x 1 rdale    student       0 Sep 16 12:08 codelite_indexer.4199.sock
[...]

The line "total 232" is what is referred to here.

'ls -s' lists the number of 512-byte blocks used by the given files
(rounded up), as well as the total:

$ ls -s
total 232
 4 CRX_75DAF8CB7768	        4 ksocket-kjordan1     4 pulse-aq57FvYS2AI1
 4 a.c			        4 ksocket-lgrompon     4 pulse-xxw02lh8pFlQ
[...]

Again, "total 232" is the same as before, but here the number next to
each file shows the number of blocks for each.

Note that the number of units can be changed via the BLOCKSIZE
environment variable:

$ export BLOCKSIZE
$ ls -s
total 116
 2 CRX_75DAF8CB7768	        2 ksocket-kjordan1     2 pulse-aq57FvYS2AI1
 2 a.c			        2 ksocket-lgrompon     2 pulse-xxw02lh8pFlQ
[...]


This is the same as using 'ls -sk', by the way.

> Also in the "-l" option by pathname, do you mean name of the file
> because in the real "ls" it only has the name of the file not the full
> path.

That depends on what is specified:

$ ls -l /etc/passwd
-rw-r--r-- 1 root root 2062 Jul 13 22:18 /etc/passwd
$ cd /etc
$ ls -l passwd
-rw-r--r-- 1 root root 2062 Jul 13 22:18 passwd
$

> Lastly, I was wondering if we could use code from other places to do
> the sorting.

You can use library calls for any functions that are defined in the
common C libraries (take a look at the alphasort(3) and qsort(3)
functions).  You may not copy code from anybody / anywhere else.

-Jan


More information about the cs631apue mailing list