  The total charm-quark production cross section per unit of rapidity
$\mathrm{d}\sigma({\rm c\overline{c}})/\mathrm{d}y$, and the fragmentation
fractions of charm quarks to different charm-hadron species
$f(\mathrm{c}\rightarrow {\rm h_{c}})$, are measured for the first time in
p$-$Pb collisions at $\sqrt{s_\mathrm{NN}} = 5.02$ TeV 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: $\mathrm{D}^{0}$,
$\mathrm{D}^{+}$, $\mathrm{D}_\mathrm{s}^{+}$, and $\mathrm{J/\psi}$ mesons,
and $\Lambda_\mathrm{c}^{+}$ and $\Xi_{\rm c}^{0}$ baryons. The resulting cross
section is $\mathrm{d}\sigma({\rm c\overline{c}})/\mathrm{d}y =219.6 \pm
6.3\;(\mathrm{stat.}) {\;}_{-11.8}^{+10.5}\;(\mathrm{syst.})
{\;}_{-2.9}^{+7.6}\;(\mathrm{extr.})\pm 5.4\;(\mathrm{BR})\pm
4.6\;(\mathrm{lumi.}) \pm 19.5\;(\text{rapidity shape})+15.0\;(\Omega_{\rm
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 $\sqrt{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 $\mathrm{e^{+}e^{-}}$
and $\mathrm{e^{-}p}$ collisions. The $p_\mathrm{T}$-integrated nuclear
modification factor of charm quarks, $R_\mathrm{pPb}({\rm c\overline{c}})= 0.91
\pm 0.04\;{\rm (stat.)}{}^{+0.08}_{-0.09}\;{\rm
(syst.)}{}^{+0.04}_{-0.03}\;{\rm (extr.)}{}\pm 0.03\;{\rm (lumi.)}$, is found
to be consistent with unity and with theoretical predictions including nuclear
modifications of the parton distribution functions.
