Hast du dir mal den Befehl GETRC angeschaut?
Dein Programm könnte deutlich kompakter aussehen.