Úloha du

V této úloze máte za úkol vytvořit zjednodušenou variantu unixového příkazu du, který zjišťuje, kolik místa zabírají soubory v adresářích. [20 bodů]

Naše implementace bude pracovat přes příkazovou řádku a její chování budou určovat následující přepínače příkazové řádky:

Pro zjednodušení si stanovme, že program bude jakožto parametry očekávat nejprve nepovinné přepínače, a pak cesty k adresářům. Vše se odděluje mezerami. Pokud není zadán žádný adresář, použije se adresář aktuální (./). Jestliže bude místo adresáře jako parametr poskytnuta cesta k souboru, vypíše se jeho velikost (podobně jako by byl použit přepínač -a).

Poznámky

Příklady

$ ./du --help
Usage: du [OPTIONS] [FILES]
Summarize disk usage of each file from FILES, recursively for directories.
Available OPTIONS are:
  -a --all             write counts for all files, not just directories
  -c --total           produce a grand total
  -h --human-readable  print sizes in human readable format
     --si              like -h, but use powers of 1000, not 1024
  -s --summarize       display only a total for each argument
     --help            display this help and exit
$ mkdir -p first/second third
$ dd if=/dev/zero of=a bs=1024 count=100 &> /dev/null
$ dd if=/dev/zero of=first/b bs=1024 count=200 &> /dev/null
$ dd if=/dev/zero of=first/c bs=1024 count=300 &> /dev/null
$ dd if=/dev/zero of=first/second/d bs=1024 count=1024 &> /dev/null
$ ./du
1024      ./first/second
1524      ./first
0         ./third
1624      .
$ ./du first third
1024      first/second
1524      first
0         third
$ ./du -c first third
1024      first/second
1524      first
0         third
1524      total
$ ./du first
1024      first/second
1524      first
$ ./du -s first
1524    first
$ ./du --summarize first
1524    first
$ ./du -h first
1.0 MiB   first/second
1.5 MiB   first
$ ./du --si first
1.1 MB    first/second
1.6 MB    first
$ ./du -h -s -c first a
1.5 MiB   first
100 KiB   a
1.6 MiB   total
$ ./du -a -h first
200 KiB   first/b
300 KiB   first/c
1.0 MiB   first/second/d
1.0 MiB   first/second
1.5 MiB   first
$ ./du -a -s first
./du: cannot both summarize and show all entries
$ mkdir fourth && chmod -r fourth
$ ./du fourth first
./du: cannot read directory fourth: permission denied
0         fourth
1024      first/second
1524      first
$ ./du fifth first
./du: cannot access fifth: no such file or directory
1024      first/second
1524      first

Vypracování úlohy

Řešení úlohy uložte do souboru du.hs a nahrajte do třetí odevzdávárny. Nejzazší termín pro vypracování je 10. 4. 2011, 23:59, později již není možné soubor s řešením odevzdat pomocí IS.

← IB016