D-meson production in p-Pb collisions at s NN =5.02 TeV and in pp collisions at s =7 TeV