The total charm-quark production cross section per unit of rapidity d σ (c c ¯) / d y , and the fragmentation fractions of charm quarks to different charm-hadron species f (c → h c) , are measured for the first time in p–Pb collisions at s NN = 5.02 Te V at midrapidity ( - 0.96 < y < 0.04 in the centre-of-mass frame) using data collected by ALICE at the CERN LHC. The results are obtained based on all the available measurements of prompt production of ground-state charm-hadron species: D 0 , D , D s , and J / ψ mesons, and Λ c and Ξ c 0 baryons. The resulting cross section is d σ (c c ¯) / d y = 219.6 ± 6.3 (stat.) - 11.8 10.5 (syst.) - 2.9 8.3 (extr.) ± 5.4 (BR) ± 4.6 (lumi.) ± 19.5 (rapidity shape) 15.0 (Ω c 0) mb , which is consistent with a binary scaling of pQCD calculations from pp collisions. The measured fragmentation fractions are compatible with those measured in pp collisions at s = 5.02 and 13 TeV, showing an increase in the relative production rates of charm baryons with respect to charm mesons in pp and p–Pb collisions compared with e e - and e - p collisions. The p T -integrated nuclear modification factor of charm quarks, R pPb (c c ¯) = 0.91 ± 0.04 (stat.) - 0.09 0.08 (syst.) - 0.03 0.05 (extr.) ± 0.03 (lumi.) , is found to be consistent with unity and with theoretical predictions including nuclear modifications of the parton distribution functions.