File: dir_test.ml

package info (click to toggle)
approx 4.5-1%2Bsqueeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 428 kB
  • ctags: 683
  • sloc: ml: 1,957; sh: 45; makefile: 36
file content (33 lines) | stat: -rw-r--r-- 902 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(* approx: proxy server for Debian archive files
   Copyright (C) 2010  Eric C. Cooper <ecc@cmu.edu>
   Released under the GNU General Public License *)

open Printf
open Unix
open Util

let non_dirs, path =
  match Sys.argv with
  | [| _ |] -> false, "."
  | [| _; "-n" |] -> true, "."
  | [| _; dir |] -> false, dir
  | [| _; "-n"; dir |] -> true, dir
  | _ ->
      eprintf "Usage: %s [-n] [path]\n" Sys.argv.(0);
      exit 1

let () =
  if non_dirs then
    let bigger (path, n as orig) path' =
      let n' = file_size path' in
      if n >= n' then orig else (path', n')
    in
    let biggest, n = fold_non_dirs bigger ("", 0L) path in
    printf "%Ld\t%s\n" n biggest
  else
    let bigger (path, n as orig) path' =
      let n' = (stat path').st_nlink in
      if n >= n' then orig else (path', n')
    in
    let biggest, n = fold_dirs bigger ("", 0) path in
    printf "%d\t%s\n" n biggest