//
// Author: Hongwei Xi (hwxi AT cs DOT bu DOT edu)
// Time: May 20, 2009
//

//
// list all the words with a given prefix; see the command [look]
//

(* ****** ****** *)

#include "BUCASCS320.hats"

(* ****** ****** *)

fun mylook
  (inp: FILEref, prfx: string, nprfx: size_t): void =
  if feof (inp) = 0 then let
    val word = input_line (inp)
    val sgn = strncasecmp (word, prfx, nprfx)
    val () = if sgn = 0 then printf ("%s\n", @(word))
  in
    if strncmp (prfx, word, nprfx) >= 0 then mylook (inp, prfx, nprfx)
  end (* end of [if] *)
// end of [mylook]

(* ****** ****** *)

#define WORDS_FILE "/usr/share/dict/words"

implement main (argc, argv) = let
  val () = assert (argc >= 2)
  val prfx = argv.[1]
  val nprfx = string_length (prfx)
  val inp = open_file (WORDS_FILE, file_mode_r)
  val () = mylook (inp, prfx, nprfx)
  val () = close_file (inp)
in
  // empty
end (* end of [main] *)

(* ****** ****** *)

(* end of [mylook.dats] *)