exception Not_enough_lines of int

let find_lines buffer size nb =
  let rec find n index =
    if index < 0 then raise (Not_enough_lines n)
    else if buffer.[index] = '\nthen
      if n <= 1 then index + 1
      else find (n - 1) (index - 1)
    else find n (index-1) in
  find nb (size - 1);;